Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / preference / preference_api.h
blob0f95a9be60ce7dd24d4b0366bdf66c439ec14535
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__
8 #include <string>
10 #include "base/memory/ref_counted.h"
11 #include "base/prefs/pref_change_registrar.h"
12 #include "chrome/browser/extensions/api/content_settings/content_settings_store.h"
13 #include "chrome/browser/extensions/chrome_extension_function.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "extensions/browser/browser_context_keyed_api_factory.h"
16 #include "extensions/browser/event_router.h"
17 #include "extensions/browser/extension_prefs_scope.h"
19 class ExtensionPrefValueMap;
20 class PrefService;
22 namespace base {
23 class Value;
26 namespace extensions {
27 class ExtensionPrefs;
29 class PreferenceEventRouter {
30 public:
31 explicit PreferenceEventRouter(Profile* profile);
32 virtual ~PreferenceEventRouter();
34 private:
35 void OnPrefChanged(PrefService* pref_service,
36 const std::string& pref_key);
38 PrefChangeRegistrar registrar_;
39 PrefChangeRegistrar incognito_registrar_;
41 // Weak, owns us (transitively via ExtensionService).
42 Profile* profile_;
44 DISALLOW_COPY_AND_ASSIGN(PreferenceEventRouter);
47 // The class containing the implementation for extension-controlled preference
48 // manipulation. This implementation is separate from PreferenceAPI, since
49 // we need to be able to use these methods in testing, where we use
50 // TestExtensionPrefs and don't construct a profile.
52 // See also PreferenceAPI and TestPreferenceAPI.
53 class PreferenceAPIBase {
54 public:
55 // Functions for manipulating preference values that are controlled by the
56 // extension. In other words, these are not pref values *about* the extension,
57 // but rather about something global the extension wants to override.
59 // Set a new extension-controlled preference value.
60 // Takes ownership of |value|.
61 void SetExtensionControlledPref(const std::string& extension_id,
62 const std::string& pref_key,
63 ExtensionPrefsScope scope,
64 base::Value* value);
66 // Remove an extension-controlled preference value.
67 void RemoveExtensionControlledPref(const std::string& extension_id,
68 const std::string& pref_key,
69 ExtensionPrefsScope scope);
71 // Returns true if currently no extension with higher precedence controls the
72 // preference.
73 bool CanExtensionControlPref(const std::string& extension_id,
74 const std::string& pref_key,
75 bool incognito);
77 // Returns true if extension |extension_id| currently controls the
78 // preference. If |from_incognito| is not NULL, looks at incognito preferences
79 // first, and |from_incognito| is set to true if the effective pref value is
80 // coming from the incognito preferences, false if it is coming from the
81 // normal ones.
82 bool DoesExtensionControlPref(const std::string& extension_id,
83 const std::string& pref_key,
84 bool* from_incognito);
86 protected:
87 // Virtual for testing.
88 virtual ExtensionPrefs* extension_prefs() = 0;
89 virtual ExtensionPrefValueMap* extension_pref_value_map() = 0;
90 virtual scoped_refptr<ContentSettingsStore> content_settings_store() = 0;
93 class PreferenceAPI : public PreferenceAPIBase,
94 public BrowserContextKeyedAPI,
95 public EventRouter::Observer,
96 public ContentSettingsStore::Observer {
97 public:
98 explicit PreferenceAPI(content::BrowserContext* context);
99 ~PreferenceAPI() override;
101 // KeyedService implementation.
102 void Shutdown() override;
104 // BrowserContextKeyedAPI implementation.
105 static BrowserContextKeyedAPIFactory<PreferenceAPI>* GetFactoryInstance();
107 // Convenience method to get the PreferenceAPI for a profile.
108 static PreferenceAPI* Get(content::BrowserContext* context);
110 // EventRouter::Observer implementation.
111 void OnListenerAdded(const EventListenerInfo& details) override;
113 private:
114 friend class BrowserContextKeyedAPIFactory<PreferenceAPI>;
116 // ContentSettingsStore::Observer implementation.
117 void OnContentSettingChanged(const std::string& extension_id,
118 bool incognito) override;
120 // Clears incognito session-only content settings for all extensions.
121 void ClearIncognitoSessionOnlyContentSettings();
123 // PreferenceAPIBase implementation.
124 ExtensionPrefs* extension_prefs() override;
125 ExtensionPrefValueMap* extension_pref_value_map() override;
126 scoped_refptr<ContentSettingsStore> content_settings_store() override;
128 Profile* profile_;
130 // BrowserContextKeyedAPI implementation.
131 static const char* service_name() {
132 return "PreferenceAPI";
134 static const bool kServiceIsNULLWhileTesting = true;
135 static const bool kServiceRedirectedInIncognito = true;
137 // Created lazily upon OnListenerAdded.
138 scoped_ptr<PreferenceEventRouter> preference_event_router_;
140 DISALLOW_COPY_AND_ASSIGN(PreferenceAPI);
143 class PrefTransformerInterface {
144 public:
145 virtual ~PrefTransformerInterface() {}
147 // Converts the representation of a preference as seen by the extension
148 // into a representation that is used in the pref stores of the browser.
149 // Returns the pref store representation in case of success or sets
150 // |error| and returns NULL otherwise. |bad_message| is passed to simulate
151 // the behavior of EXTENSION_FUNCTION_VALIDATE. It is never NULL.
152 // The ownership of the returned value is passed to the caller.
153 virtual base::Value* ExtensionToBrowserPref(
154 const base::Value* extension_pref,
155 std::string* error,
156 bool* bad_message) = 0;
158 // Converts the representation of the preference as stored in the browser
159 // into a representation that is used by the extension.
160 // Returns the extension representation in case of success or NULL otherwise.
161 // The ownership of the returned value is passed to the caller.
162 virtual base::Value* BrowserToExtensionPref(
163 const base::Value* browser_pref) = 0;
166 // A base class to provide functionality common to the other *PreferenceFunction
167 // classes.
168 class PreferenceFunction : public ChromeSyncExtensionFunction {
169 protected:
170 enum PermissionType { PERMISSION_TYPE_READ, PERMISSION_TYPE_WRITE };
172 ~PreferenceFunction() override;
174 // Given an |extension_pref_key|, provides its |browser_pref_key| from the
175 // static map in preference_api.cc. Returns true if the corresponding
176 // browser pref exists and the extension has the API permission needed to
177 // modify that pref. Sets |error_| if the extension doesn't have the needed
178 // permission.
179 bool ValidateBrowserPref(const std::string& extension_pref_key,
180 PermissionType permission_type,
181 std::string* browser_pref_key);
184 class GetPreferenceFunction : public PreferenceFunction {
185 public:
186 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.get", TYPES_CHROMESETTING_GET)
188 protected:
189 ~GetPreferenceFunction() override;
191 // ExtensionFunction:
192 bool RunSync() override;
195 class SetPreferenceFunction : public PreferenceFunction {
196 public:
197 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.set", TYPES_CHROMESETTING_SET)
199 protected:
200 ~SetPreferenceFunction() override;
202 // ExtensionFunction:
203 bool RunSync() override;
206 class ClearPreferenceFunction : public PreferenceFunction {
207 public:
208 DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.clear",
209 TYPES_CHROMESETTING_CLEAR)
211 protected:
212 ~ClearPreferenceFunction() override;
214 // ExtensionFunction:
215 bool RunSync() override;
218 } // namespace extensions
220 #endif // CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__