1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__ 6 #define CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/macros.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/scoped_observer.h" 14 #include "chrome/browser/extensions/api/content_settings/content_settings_store.h" 15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile_observer.h" 17 #include "components/prefs/pref_change_registrar.h" 18 #include "extensions/browser/browser_context_keyed_api_factory.h" 19 #include "extensions/browser/event_router.h" 20 #include "extensions/browser/extension_function.h" 21 #include "extensions/browser/extension_prefs_scope.h" 22 23 class ExtensionPrefValueMap; 24 class PrefService; 25 26 namespace base { 27 class Value; 28 } 29 30 namespace extensions { 31 class ExtensionPrefs; 32 33 class PreferenceEventRouter : public ProfileObserver { 34 public: 35 explicit PreferenceEventRouter(Profile* profile); 36 ~PreferenceEventRouter() override; 37 38 private: 39 void OnPrefChanged(PrefService* pref_service, 40 const std::string& pref_key); 41 42 // ProfileObserver: 43 void OnOffTheRecordProfileCreated(Profile* off_the_record) override; 44 void OnProfileWillBeDestroyed(Profile* profile) override; 45 46 void ObserveOffTheRecordPrefs(PrefService* prefs); 47 48 PrefChangeRegistrar registrar_; 49 std::unique_ptr<PrefChangeRegistrar> incognito_registrar_; 50 51 // Weak, owns us (transitively via ExtensionService). 52 Profile* profile_; 53 54 ScopedObserver<Profile, ProfileObserver> observed_profiles_{this}; 55 56 DISALLOW_COPY_AND_ASSIGN(PreferenceEventRouter); 57 }; 58 59 // The class containing the implementation for extension-controlled preference 60 // manipulation. This implementation is separate from PreferenceAPI, since 61 // we need to be able to use these methods in testing, where we use 62 // TestExtensionPrefs and don't construct a profile. 63 // 64 // See also PreferenceAPI and TestPreferenceAPI. 65 class PreferenceAPIBase { 66 public: 67 // Functions for manipulating preference values that are controlled by the 68 // extension. In other words, these are not pref values *about* the extension, 69 // but rather about something global the extension wants to override. 70 71 // Set a new extension-controlled preference value. 72 void SetExtensionControlledPref(const std::string& extension_id, 73 const std::string& pref_key, 74 ExtensionPrefsScope scope, 75 base::Value value); 76 77 // Remove an extension-controlled preference value. 78 void RemoveExtensionControlledPref(const std::string& extension_id, 79 const std::string& pref_key, 80 ExtensionPrefsScope scope); 81 82 // Returns true if currently no extension with higher precedence controls the 83 // preference. 84 bool CanExtensionControlPref(const std::string& extension_id, 85 const std::string& pref_key, 86 bool incognito); 87 88 // Returns true if extension |extension_id| currently controls the 89 // preference. If |from_incognito| is not NULL, looks at incognito preferences 90 // first, and |from_incognito| is set to true if the effective pref value is 91 // coming from the incognito preferences, false if it is coming from the 92 // normal ones. 93 bool DoesExtensionControlPref(const std::string& extension_id, 94 const std::string& pref_key, 95 bool* from_incognito); 96 97 protected: 98 // Virtual for testing. 99 virtual ExtensionPrefs* extension_prefs() = 0; 100 virtual ExtensionPrefValueMap* extension_pref_value_map() = 0; 101 virtual scoped_refptr<ContentSettingsStore> content_settings_store() = 0; 102 }; 103 104 class PreferenceAPI : public PreferenceAPIBase, 105 public BrowserContextKeyedAPI, 106 public EventRouter::Observer, 107 public ContentSettingsStore::Observer { 108 public: 109 explicit PreferenceAPI(content::BrowserContext* context); 110 ~PreferenceAPI() override; 111 112 // KeyedService implementation. 113 void Shutdown() override; 114 115 // BrowserContextKeyedAPI implementation. 116 static BrowserContextKeyedAPIFactory<PreferenceAPI>* GetFactoryInstance(); 117 118 // Convenience method to get the PreferenceAPI for a profile. 119 static PreferenceAPI* Get(content::BrowserContext* context); 120 121 // EventRouter::Observer implementation. 122 void OnListenerAdded(const EventListenerInfo& details) override; 123 124 private: 125 friend class BrowserContextKeyedAPIFactory<PreferenceAPI>; 126 127 // ContentSettingsStore::Observer implementation. 128 void OnContentSettingChanged(const std::string& extension_id, 129 bool incognito) override; 130 131 // Clears incognito session-only content settings for all extensions. 132 void ClearIncognitoSessionOnlyContentSettings(); 133 134 // PreferenceAPIBase implementation. 135 ExtensionPrefs* extension_prefs() override; 136 ExtensionPrefValueMap* extension_pref_value_map() override; 137 scoped_refptr<ContentSettingsStore> content_settings_store() override; 138 139 Profile* profile_; 140 141 // BrowserContextKeyedAPI implementation. service_name()142 static const char* service_name() { 143 return "PreferenceAPI"; 144 } 145 static const bool kServiceIsNULLWhileTesting = true; 146 static const bool kServiceRedirectedInIncognito = true; 147 148 // Created lazily upon OnListenerAdded. 149 std::unique_ptr<PreferenceEventRouter> preference_event_router_; 150 151 DISALLOW_COPY_AND_ASSIGN(PreferenceAPI); 152 }; 153 154 class PrefTransformerInterface { 155 public: 156 virtual ~PrefTransformerInterface() = default; 157 158 // Converts the representation of a preference as seen by the extension 159 // into a representation that is used in the pref stores of the browser. 160 // Returns the pref store representation in case of success or sets 161 // |error| and returns NULL otherwise. |bad_message| is passed to simulate 162 // the behavior of EXTENSION_FUNCTION_VALIDATE. It is never NULL. 163 // The ownership of the returned value is passed to the caller. 164 virtual std::unique_ptr<base::Value> ExtensionToBrowserPref( 165 const base::Value* extension_pref, 166 std::string* error, 167 bool* bad_message) = 0; 168 169 // Converts the representation of the preference as stored in the browser 170 // into a representation that is used by the extension. 171 // Returns the extension representation in case of success or NULL otherwise. 172 // The ownership of the returned value is passed to the caller. 173 virtual std::unique_ptr<base::Value> BrowserToExtensionPref( 174 const base::Value* browser_pref, 175 bool is_incognito_profile) = 0; 176 }; 177 178 // A base class to provide functionality common to the other *PreferenceFunction 179 // classes. 180 class PreferenceFunction : public ExtensionFunction { 181 protected: 182 enum PermissionType { PERMISSION_TYPE_READ, PERMISSION_TYPE_WRITE }; 183 184 ~PreferenceFunction() override; 185 }; 186 187 class GetPreferenceFunction : public PreferenceFunction { 188 public: 189 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.get", TYPES_CHROMESETTING_GET) 190 191 protected: 192 ~GetPreferenceFunction() override; 193 194 // ExtensionFunction: 195 ResponseAction Run() override; 196 }; 197 198 class SetPreferenceFunction : public PreferenceFunction { 199 public: 200 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.set", TYPES_CHROMESETTING_SET) 201 202 protected: 203 ~SetPreferenceFunction() override; 204 205 // ExtensionFunction: 206 ResponseAction Run() override; 207 }; 208 209 class ClearPreferenceFunction : public PreferenceFunction { 210 public: 211 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.clear", 212 TYPES_CHROMESETTING_CLEAR) 213 214 protected: 215 ~ClearPreferenceFunction() override; 216 217 // ExtensionFunction: 218 ResponseAction Run() override; 219 }; 220 221 } // namespace extensions 222 223 #endif // CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__ 224