bbb35e45f9db6975332c12ba0ba10921c5c54d2e
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / preference / preference_api.h
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 <string>
9
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"
17
18 class ExtensionPrefValueMap;
19 class PrefService;
20
21 namespace base {
22 class Value;
23 }
24
25 namespace extensions {
26 class ExtensionPrefs;
27
28 class PreferenceEventRouter {
29  public:
30   explicit PreferenceEventRouter(Profile* profile);
31   virtual ~PreferenceEventRouter();
32
33  private:
34   void OnPrefChanged(PrefService* pref_service,
35                      const std::string& pref_key);
36
37   PrefChangeRegistrar registrar_;
38   PrefChangeRegistrar incognito_registrar_;
39
40   // Weak, owns us (transitively via ExtensionService).
41   Profile* profile_;
42
43   DISALLOW_COPY_AND_ASSIGN(PreferenceEventRouter);
44 };
45
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.
50 //
51 // See also PreferenceAPI and TestPreferenceAPI.
52 class PreferenceAPIBase {
53  public:
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.
57
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,
63                                   base::Value* value);
64
65   // Remove an extension-controlled preference value.
66   void RemoveExtensionControlledPref(const std::string& extension_id,
67                                      const std::string& pref_key,
68                                      ExtensionPrefsScope scope);
69
70   // Returns true if currently no extension with higher precedence controls the
71   // preference.
72   bool CanExtensionControlPref(const std::string& extension_id,
73                                const std::string& pref_key,
74                                bool incognito);
75
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
80   // normal ones.
81   bool DoesExtensionControlPref(const std::string& extension_id,
82                                 const std::string& pref_key,
83                                 bool* from_incognito);
84
85  protected:
86   // Virtual for testing.
87   virtual ExtensionPrefs* extension_prefs() = 0;
88   virtual ExtensionPrefValueMap* extension_pref_value_map() = 0;
89 };
90
91 class PreferenceAPI : public PreferenceAPIBase,
92                       public BrowserContextKeyedAPI,
93                       public EventRouter::Observer,
94                       public ContentSettingsStore::Observer {
95  public:
96   explicit PreferenceAPI(content::BrowserContext* context);
97   virtual ~PreferenceAPI();
98
99   // KeyedService implementation.
100   virtual void Shutdown() OVERRIDE;
101
102   // BrowserContextKeyedAPI implementation.
103   static BrowserContextKeyedAPIFactory<PreferenceAPI>* GetFactoryInstance();
104
105   // Convenience method to get the PreferenceAPI for a profile.
106   static PreferenceAPI* Get(content::BrowserContext* context);
107
108   // EventRouter::Observer implementation.
109   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
110
111  private:
112   friend class BrowserContextKeyedAPIFactory<PreferenceAPI>;
113
114   // ContentSettingsStore::Observer implementation.
115   virtual void OnContentSettingChanged(const std::string& extension_id,
116                                        bool incognito) OVERRIDE;
117
118   // Clears incognito session-only content settings for all extensions.
119   void ClearIncognitoSessionOnlyContentSettings();
120
121   // PreferenceAPIBase implementation.
122   virtual ExtensionPrefs* extension_prefs() OVERRIDE;
123   virtual ExtensionPrefValueMap* extension_pref_value_map() OVERRIDE;
124
125   Profile* profile_;
126
127   // BrowserContextKeyedAPI implementation.
128   static const char* service_name() {
129     return "PreferenceAPI";
130   }
131   static const bool kServiceIsNULLWhileTesting = true;
132   static const bool kServiceRedirectedInIncognito = true;
133
134   // Created lazily upon OnListenerAdded.
135   scoped_ptr<PreferenceEventRouter> preference_event_router_;
136
137   DISALLOW_COPY_AND_ASSIGN(PreferenceAPI);
138 };
139
140 class PrefTransformerInterface {
141  public:
142   virtual ~PrefTransformerInterface() {}
143
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,
152       std::string* error,
153       bool* bad_message) = 0;
154
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;
161 };
162
163 // A base class to provide functionality common to the other *PreferenceFunction
164 // classes.
165 class PreferenceFunction : public ChromeSyncExtensionFunction {
166  protected:
167   enum PermissionType { PERMISSION_TYPE_READ, PERMISSION_TYPE_WRITE };
168
169   virtual ~PreferenceFunction();
170
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
175   // permission.
176   bool ValidateBrowserPref(const std::string& extension_pref_key,
177                            PermissionType permission_type,
178                            std::string* browser_pref_key);
179 };
180
181 class GetPreferenceFunction : public PreferenceFunction {
182  public:
183   DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.get", TYPES_CHROMESETTING_GET)
184
185  protected:
186   virtual ~GetPreferenceFunction();
187
188   // ExtensionFunction:
189   virtual bool RunImpl() OVERRIDE;
190 };
191
192 class SetPreferenceFunction : public PreferenceFunction {
193  public:
194   DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.set", TYPES_CHROMESETTING_SET)
195
196  protected:
197   virtual ~SetPreferenceFunction();
198
199   // ExtensionFunction:
200   virtual bool RunImpl() OVERRIDE;
201 };
202
203 class ClearPreferenceFunction : public PreferenceFunction {
204  public:
205   DECLARE_EXTENSION_FUNCTION("types.ChromeSetting.clear",
206                              TYPES_CHROMESETTING_CLEAR)
207
208  protected:
209   virtual ~ClearPreferenceFunction();
210
211   // ExtensionFunction:
212   virtual bool RunImpl() OVERRIDE;
213 };
214
215 }  // namespace extensions
216
217 #endif  // CHROME_BROWSER_EXTENSIONS_API_PREFERENCE_PREFERENCE_API_H__