OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/linked_ptr.h" | 13 #include "base/linked_ptr.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
18 | 18 |
19 // Class for managing global and per-extension preferences. | 19 // Class for managing global and per-extension preferences. |
20 // This class is instantiated by ExtensionsService, so it should be accessed | 20 // This class is instantiated by ExtensionsService, so it should be accessed |
21 // from there. | 21 // from there. |
| 22 // |
| 23 // This class distinguishes the following kinds of preferences: |
| 24 // - global preferences: |
| 25 // not associated with extensions |
| 26 // - per-extension preferences: |
| 27 // meta-preferences describing properties of the extension like |
| 28 // installation time, whether the extension is enabled, etc. |
| 29 // - extension controlled preferences: |
| 30 // global preferences that an individual extension wants to set; |
| 31 // these are arbitrary preferences that are controlled by other |
| 32 // sub-systems. |
22 class ExtensionPrefs { | 33 class ExtensionPrefs { |
23 public: | 34 public: |
24 // Key name for a preference that keeps track of per-extension settings. This | 35 // Key name for a preference that keeps track of per-extension settings. This |
25 // is a dictionary object read from the Preferences file, keyed off of | 36 // is a dictionary object read from the Preferences file, keyed off of |
26 // extension ids. | 37 // extension ids. |
27 static const char kExtensionsPref[]; | 38 static const char kExtensionsPref[]; |
28 | 39 |
29 typedef std::vector<linked_ptr<ExtensionInfo> > ExtensionsInfo; | 40 typedef std::vector<linked_ptr<ExtensionInfo> > ExtensionsInfo; |
30 | 41 |
31 // This enum is used for the launch type the user wants to use for an | 42 // This enum is used for the launch type the user wants to use for an |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 void OnExtensionInstalled(const Extension* extension, | 77 void OnExtensionInstalled(const Extension* extension, |
67 Extension::State initial_state, | 78 Extension::State initial_state, |
68 bool initial_incognito_enabled); | 79 bool initial_incognito_enabled); |
69 | 80 |
70 // Called when an extension is uninstalled, so that prefs get cleaned up. | 81 // Called when an extension is uninstalled, so that prefs get cleaned up. |
71 void OnExtensionUninstalled(const std::string& extension_id, | 82 void OnExtensionUninstalled(const std::string& extension_id, |
72 const Extension::Location& location, | 83 const Extension::Location& location, |
73 bool external_uninstall); | 84 bool external_uninstall); |
74 | 85 |
75 // Returns the state (enabled/disabled) of the given extension. | 86 // Returns the state (enabled/disabled) of the given extension. |
76 Extension::State GetExtensionState(const std::string& extension_id); | 87 Extension::State GetExtensionState(const std::string& extension_id) const; |
77 | 88 |
78 // Called to change the extension's state when it is enabled/disabled. | 89 // Called to change the extension's state when it is enabled/disabled. |
79 void SetExtensionState(const Extension* extension, Extension::State); | 90 void SetExtensionState(const Extension* extension, Extension::State); |
80 | 91 |
81 // Did the extension ask to escalate its permission during an upgrade? | 92 // Did the extension ask to escalate its permission during an upgrade? |
82 bool DidExtensionEscalatePermissions(const std::string& id); | 93 bool DidExtensionEscalatePermissions(const std::string& id); |
83 | 94 |
84 // If |did_escalate| is true, the preferences for |extension| will be set to | 95 // If |did_escalate| is true, the preferences for |extension| will be set to |
85 // require the install warning when the user tries to enable. | 96 // require the install warning when the user tries to enable. |
86 void SetDidExtensionEscalatePermissions(const Extension* extension, | 97 void SetDidExtensionEscalatePermissions(const Extension* extension, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 // highest current application launch index found. | 198 // highest current application launch index found. |
188 int GetNextAppLaunchIndex(); | 199 int GetNextAppLaunchIndex(); |
189 | 200 |
190 // The extension's update URL data. If not empty, the ExtensionUpdater | 201 // The extension's update URL data. If not empty, the ExtensionUpdater |
191 // will append a ap= parameter to the URL when checking if a new version | 202 // will append a ap= parameter to the URL when checking if a new version |
192 // of the extension is available. | 203 // of the extension is available. |
193 void SetUpdateUrlData(const std::string& extension_id, | 204 void SetUpdateUrlData(const std::string& extension_id, |
194 const std::string& data); | 205 const std::string& data); |
195 std::string GetUpdateUrlData(const std::string& extension_id); | 206 std::string GetUpdateUrlData(const std::string& extension_id); |
196 | 207 |
| 208 // Sets a preference value that is controlled by the extension. In other |
| 209 // words, this is not a pref value *about* the extension but something |
| 210 // global the extension wants to override. |
| 211 void SetExtensionControlledPref(const std::string& extension_id, |
| 212 const std::string& pref_key, |
| 213 Value* value); |
| 214 |
197 static void RegisterUserPrefs(PrefService* prefs); | 215 static void RegisterUserPrefs(PrefService* prefs); |
198 | 216 |
199 // The underlying PrefService. | 217 // The underlying PrefService. |
200 PrefService* pref_service() const { return prefs_; } | 218 PrefService* pref_service() const { return prefs_; } |
201 | 219 |
| 220 protected: |
| 221 // For unit testing. Enables injecting an artificial clock that is used |
| 222 // to query the current time, when an extension is installed. |
| 223 base::Time GetCurrentTime() const; |
| 224 |
202 private: | 225 private: |
203 // Converts absolute paths in the pref to paths relative to the | 226 // Converts absolute paths in the pref to paths relative to the |
204 // install_directory_. | 227 // install_directory_. |
205 void MakePathsRelative(); | 228 void MakePathsRelative(); |
206 | 229 |
207 // Converts internal relative paths to be absolute. Used for export to | 230 // Converts internal relative paths to be absolute. Used for export to |
208 // consumers who expect full paths. | 231 // consumers who expect full paths. |
209 void MakePathsAbsolute(DictionaryValue* dict); | 232 void MakePathsAbsolute(DictionaryValue* dict); |
210 | 233 |
211 // Sets the pref |key| for extension |id| to |value|. | 234 // Sets the pref |key| for extension |id| to |value|. |
(...skipping 21 matching lines...) Expand all Loading... |
233 bool ReadExtensionPrefInteger(const std::string& extension_id, | 256 bool ReadExtensionPrefInteger(const std::string& extension_id, |
234 const std::string& pref_key, | 257 const std::string& pref_key, |
235 int* out_value); | 258 int* out_value); |
236 | 259 |
237 // Ensures and returns a mutable dictionary for extension |id|'s prefs. | 260 // Ensures and returns a mutable dictionary for extension |id|'s prefs. |
238 DictionaryValue* GetOrCreateExtensionPref(const std::string& id); | 261 DictionaryValue* GetOrCreateExtensionPref(const std::string& id); |
239 | 262 |
240 // Same as above, but returns NULL if it doesn't exist. | 263 // Same as above, but returns NULL if it doesn't exist. |
241 DictionaryValue* GetExtensionPref(const std::string& id) const; | 264 DictionaryValue* GetExtensionPref(const std::string& id) const; |
242 | 265 |
| 266 // Returns the dictionary of preferences controlled by the specified extension |
| 267 // or NULL if unknown. All entries in the dictionary contain non-expanded |
| 268 // paths. |
| 269 DictionaryValue* GetExtensionControlledPrefs(const std::string& id) const; |
| 270 |
243 // Serializes the data and schedules a persistent save via the |PrefService|. | 271 // Serializes the data and schedules a persistent save via the |PrefService|. |
244 // Additionally fires a PREF_CHANGED notification with the top-level | 272 // Additionally fires a PREF_CHANGED notification with the top-level |
245 // |kExtensionsPref| path set. | 273 // |kExtensionsPref| path set. |
246 // TODO(andybons): Switch this to EXTENSION_PREF_CHANGED to be more granular. | 274 // TODO(andybons): Switch this to EXTENSION_PREF_CHANGED to be more granular. |
247 // TODO(andybons): Use a ScopedPrefUpdate to update observers on changes to | 275 // TODO(andybons): Use a ScopedPrefUpdate to update observers on changes to |
248 // the mutable extension dictionary. | 276 // the mutable extension dictionary. |
249 void SavePrefsAndNotify(); | 277 void SavePrefsAndNotify(); |
250 | 278 |
251 // Checks if kPrefBlacklist is set to true in the DictionaryValue. | 279 // Checks if kPrefBlacklist is set to true in the DictionaryValue. |
252 // Return false if the value is false or kPrefBlacklist does not exist. | 280 // Return false if the value is false or kPrefBlacklist does not exist. |
253 // This is used to decide if an extension is blacklisted. | 281 // This is used to decide if an extension is blacklisted. |
254 bool IsBlacklistBitSet(DictionaryValue* ext); | 282 bool IsBlacklistBitSet(DictionaryValue* ext); |
255 | 283 |
256 // Helper methods for the public last ping day functions. | 284 // Helper methods for the public last ping day functions. |
257 base::Time LastPingDayImpl(const DictionaryValue* dictionary) const; | 285 base::Time LastPingDayImpl(const DictionaryValue* dictionary) const; |
258 void SetLastPingDayImpl(const base::Time& time, DictionaryValue* dictionary); | 286 void SetLastPingDayImpl(const base::Time& time, DictionaryValue* dictionary); |
259 | 287 |
| 288 // Helper method to acquire the installation time of an extension |
| 289 base::Time GetInstallTime(const DictionaryValue* extension) const; |
| 290 |
| 291 // Installs the persistent extension preferences into |prefs_|'s extension |
| 292 // pref store. |
| 293 void InstallPersistedExtensionControlledPrefs(); |
| 294 |
| 295 // Returns the extension controlled preference value of the extension that was |
| 296 // installed most recently. |
| 297 const Value* WinningExtensionControlledPrefValue(const std::string& key) |
| 298 const; |
| 299 |
| 300 // Find the most recently installed extension that defines a preference |
| 301 // for |pref_key|, then store its value in the PrefValueStore's extension pref |
| 302 // store and send notifications to observers in case the value changed. |
| 303 void UpdateWinningPref(const std::string& pref_key); |
| 304 |
| 305 // Retrieves a list of preference keys that the specified extension |
| 306 // intents to manage. |
| 307 void GetExtensionControlledPrefKeys(const std::string& extension_id, |
| 308 std::vector<std::string> *out) const; |
| 309 |
260 // The pref service specific to this set of extension prefs. | 310 // The pref service specific to this set of extension prefs. |
261 PrefService* prefs_; | 311 PrefService* prefs_; |
262 | 312 |
263 // Base extensions install directory. | 313 // Base extensions install directory. |
264 FilePath install_directory_; | 314 FilePath install_directory_; |
265 | 315 |
266 // The URLs of all of the toolstrips. | 316 // The URLs of all of the toolstrips. |
267 URLList shelf_order_; | 317 URLList shelf_order_; |
268 | 318 |
269 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs); | 319 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs); |
270 }; | 320 }; |
271 | 321 |
272 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ | 322 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ |
OLD | NEW |