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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__
10 #include "base/prefs/pref_change_registrar.h"
11 #include "chrome/browser/extensions/api/content_settings/content_settings_store.h"
12 #include "chrome/browser/extensions/chrome_extension_function.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "extensions/browser/browser_context_keyed_api_factory.h"
15 #include "extensions/browser/event_router.h"
16 #include "extensions/browser/extension_prefs_scope.h"
18 class ExtensionPrefValueMap;
25 namespace extensions {
28 class PreferenceEventRouter {
30 explicit PreferenceEventRouter(Profile* profile);
31 virtual ~PreferenceEventRouter();
34 void OnPrefChanged(PrefService* pref_service,
35 const std::string& pref_key);
37 PrefChangeRegistrar registrar_;
38 PrefChangeRegistrar incognito_registrar_;
40 // Weak, owns us (transitively via ExtensionService).
43 DISALLOW_COPY_AND_ASSIGN(PreferenceEventRouter);
46 // The class containing the implementation for extension-controlled preference
47 // manipulation. This implementation is separate from PreferenceAPI, since
48 // we need to be able to use these methods in testing, where we use
49 // TestExtensionPrefs and don't construct a profile.
51 // See also PreferenceAPI and TestPreferenceAPI.
52 class PreferenceAPIBase {
54 // Functions for manipulating preference values that are controlled by the
55 // extension. In other words, these are not pref values *about* the extension,
56 // but rather about something global the extension wants to override.
58 // Set a new extension-controlled preference value.
59 // Takes ownership of |value|.
60 void SetExtensionControlledPref(const std::string& extension_id,
61 const std::string& pref_key,
62 ExtensionPrefsScope scope,
65 // Remove an extension-controlled preference value.
66 void RemoveExtensionControlledPref(const std::string& extension_id,
67 const std::string& pref_key,
68 ExtensionPrefsScope scope);
70 // Returns true if currently no extension with higher precedence controls the
72 bool CanExtensionControlPref(const std::string& extension_id,
73 const std::string& pref_key,
76 // Returns true if extension |extension_id| currently controls the
77 // preference. If |from_incognito| is not NULL, looks at incognito preferences
78 // first, and |from_incognito| is set to true if the effective pref value is
79 // coming from the incognito preferences, false if it is coming from the
81 bool DoesExtensionControlPref(const std::string& extension_id,
82 const std::string& pref_key,
83 bool* from_incognito);
86 // Virtual for testing.
87 virtual ExtensionPrefs* extension_prefs() = 0;
88 virtual ExtensionPrefValueMap* extension_pref_value_map() = 0;
91 class PreferenceAPI : public PreferenceAPIBase,
92 public BrowserContextKeyedAPI,
93 public EventRouter::Observer,
94 public ContentSettingsStore::Observer {
96 explicit PreferenceAPI(content::BrowserContext* context);
97 virtual ~PreferenceAPI();
99 // KeyedService implementation.
100 virtual void Shutdown() OVERRIDE;
102 // BrowserContextKeyedAPI implementation.
103 static BrowserContextKeyedAPIFactory<PreferenceAPI>* GetFactoryInstance();
105 // Convenience method to get the PreferenceAPI for a profile.
106 static PreferenceAPI* Get(content::BrowserContext* context);
108 // EventRouter::Observer implementation.
109 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
112 friend class BrowserContextKeyedAPIFactory<PreferenceAPI>;
114 // ContentSettingsStore::Observer implementation.
115 virtual void OnContentSettingChanged(const std::string& extension_id,
116 bool incognito) OVERRIDE;
118 // Clears incognito session-only content settings for all extensions.
119 void ClearIncognitoSessionOnlyContentSettings();
121 // PreferenceAPIBase implementation.
122 virtual ExtensionPrefs* extension_prefs() OVERRIDE;
123 virtual ExtensionPrefValueMap* extension_pref_value_map() OVERRIDE;
127 // BrowserContextKeyedAPI implementation.
128 static const char* service_name() {
129 return "PreferenceAPI";
131 static const bool kServiceIsNULLWhileTesting = true;
132 static const bool kServiceRedirectedInIncognito = true;
134 // Created lazily upon OnListenerAdded.
135 scoped_ptr<PreferenceEventRouter> preference_event_router_;
137 DISALLOW_COPY_AND_ASSIGN(PreferenceAPI);
140 class PrefTransformerInterface {
142 virtual ~PrefTransformerInterface() {}
144 // Converts the representation of a preference as seen by the extension
145 // into a representation that is used in the pref stores of the browser.
146 // Returns the pref store representation in case of success or sets
147 // |error| and returns NULL otherwise. |bad_message| is passed to simulate
148 // the behavior of EXTENSION_FUNCTION_VALIDATE. It is never NULL.
149 // The ownership of the returned value is passed to the caller.
150 virtual base::Value* ExtensionToBrowserPref(
151 const base::Value* extension_pref,
153 bool* bad_message) = 0;
155 // Converts the representation of the preference as stored in the browser
156 // into a representation that is used by the extension.
157 // Returns the extension representation in case of success or NULL otherwise.
158 // The ownership of the returned value is passed to the caller.
159 virtual base::Value* BrowserToExtensionPref(
160 const base::Value* browser_pref) = 0;
163 // A base class to provide functionality common to the other *PreferenceFunction
165 class PreferenceFunction : public ChromeSyncExtensionFunction {
167 enum PermissionType { PERMISSION_TYPE_READ, PERMISSION_TYPE_WRITE };
169 virtual ~PreferenceFunction();
171 // Given an |extension_pref_key|, provides its |browser_pref_key| from the
172 // static map in preference_api.cc. Returns true if the corresponding
173 // browser pref exists and the extension has the API permission needed to
174 // modify that pref. Sets |error_| if the extension doesn't have the needed
176 bool ValidateBrowserPref(const std::string& extension_pref_key,
177 PermissionType permission_type,
178 std::string* browser_pref_key);
181 class GetPreferenceFunction : public PreferenceFunction {
183 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.get", TYPES_CHROMESETTING_GET)
186 virtual ~GetPreferenceFunction();
188 // ExtensionFunction:
189 virtual bool RunImpl() OVERRIDE;
192 class SetPreferenceFunction : public PreferenceFunction {
194 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.set", TYPES_CHROMESETTING_SET)
197 virtual ~SetPreferenceFunction();
199 // ExtensionFunction:
200 virtual bool RunImpl() OVERRIDE;
203 class ClearPreferenceFunction : public PreferenceFunction {
205 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.clear",
206 TYPES_CHROMESETTING_CLEAR)
209 virtual ~ClearPreferenceFunction();
211 // ExtensionFunction:
212 virtual bool RunImpl() OVERRIDE;
215 } // namespace extensions
217 #endif // CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__