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 // |
21 // from there. | 21 // This class distinguishes the following kinds of preferences: |
22 // - global preferences: | |
23 // internal state for the extension system in general, not associated | |
24 // with an individual extension, such as lastUpdateTime. | |
25 // - per-extension preferences: | |
26 // meta-preferences describing properties of the extension like | |
27 // installation time, whether the extension is enabled, etc. | |
28 // - extension controlled preferences: | |
29 // global preferences that an individual extension wants to set; | |
30 // these are arbitrary preferences that are controlled by other | |
31 // sub-systems, e.g., proxy settings. | |
32 // | |
33 // As several extensions may want to set extension controlled preferences | |
Aaron Boodman
2010/12/03 20:00:44
What I meant was something like this, for the sect
battre (please use the other)
2010/12/06 10:28:50
Done.
| |
34 // this class governs the precedence: The most recently installed extension | |
35 // takes precedence over previously installed extensions. The effective | |
36 // extension controlled preferences are stored in the extension pref store | |
37 // available from the PrefService. | |
22 class ExtensionPrefs { | 38 class ExtensionPrefs { |
23 public: | 39 public: |
24 // Key name for a preference that keeps track of per-extension settings. This | 40 // 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 | 41 // is a dictionary object read from the Preferences file, keyed off of |
26 // extension ids. | 42 // extension ids. |
27 static const char kExtensionsPref[]; | 43 static const char kExtensionsPref[]; |
28 | 44 |
29 typedef std::vector<linked_ptr<ExtensionInfo> > ExtensionsInfo; | 45 typedef std::vector<linked_ptr<ExtensionInfo> > ExtensionsInfo; |
30 | 46 |
47 // Vector containing identifiers for preferences. | |
48 typedef std::set<std::string> PrefKeySet; | |
49 | |
50 // Vector containing identifiers for extensions. | |
51 typedef std::vector<std::string> ExtensionIdSet; | |
52 | |
31 // This enum is used for the launch type the user wants to use for an | 53 // This enum is used for the launch type the user wants to use for an |
32 // application. | 54 // application. |
33 // Do not remove items or re-order this enum as it is used in preferences | 55 // Do not remove items or re-order this enum as it is used in preferences |
34 // and histograms. | 56 // and histograms. |
35 enum LaunchType { | 57 enum LaunchType { |
36 LAUNCH_PINNED, | 58 LAUNCH_PINNED, |
37 LAUNCH_REGULAR, | 59 LAUNCH_REGULAR, |
38 LAUNCH_FULLSCREEN, | 60 LAUNCH_FULLSCREEN, |
39 LAUNCH_WINDOW | 61 LAUNCH_WINDOW |
40 }; | 62 }; |
41 | 63 |
42 explicit ExtensionPrefs(PrefService* prefs, const FilePath& root_dir_); | 64 explicit ExtensionPrefs(PrefService* prefs, const FilePath& root_dir); |
43 ~ExtensionPrefs(); | 65 ~ExtensionPrefs(); |
44 | 66 |
45 // Returns a copy of the Extensions prefs. | 67 // Returns a copy of the Extensions prefs. |
46 // TODO(erikkay) Remove this so that external consumers don't need to be | 68 // TODO(erikkay) Remove this so that external consumers don't need to be |
47 // aware of the internal structure of the preferences. | 69 // aware of the internal structure of the preferences. |
48 DictionaryValue* CopyCurrentExtensions(); | 70 DictionaryValue* CopyCurrentExtensions(); |
49 | 71 |
50 // Populate |killed_ids| with extension ids that have been killed. | 72 // Populate |killed_ids| with extension ids that have been killed. |
51 void GetKilledExtensionIds(std::set<std::string>* killed_ids); | 73 void GetKilledExtensionIds(std::set<std::string>* killed_ids); |
52 | 74 |
(...skipping 14 matching lines...) Expand all Loading... | |
67 void OnExtensionInstalled(const Extension* extension, | 89 void OnExtensionInstalled(const Extension* extension, |
68 Extension::State initial_state, | 90 Extension::State initial_state, |
69 bool initial_incognito_enabled); | 91 bool initial_incognito_enabled); |
70 | 92 |
71 // Called when an extension is uninstalled, so that prefs get cleaned up. | 93 // Called when an extension is uninstalled, so that prefs get cleaned up. |
72 void OnExtensionUninstalled(const std::string& extension_id, | 94 void OnExtensionUninstalled(const std::string& extension_id, |
73 const Extension::Location& location, | 95 const Extension::Location& location, |
74 bool external_uninstall); | 96 bool external_uninstall); |
75 | 97 |
76 // Returns the state (enabled/disabled) of the given extension. | 98 // Returns the state (enabled/disabled) of the given extension. |
77 Extension::State GetExtensionState(const std::string& extension_id); | 99 Extension::State GetExtensionState(const std::string& extension_id) const; |
78 | 100 |
79 // Called to change the extension's state when it is enabled/disabled. | 101 // Called to change the extension's state when it is enabled/disabled. |
80 void SetExtensionState(const Extension* extension, Extension::State); | 102 void SetExtensionState(const Extension* extension, Extension::State); |
81 | 103 |
104 // Returns all installed and enabled extensions | |
105 void GetEnabledExtensions(ExtensionIdSet* out) const; | |
106 | |
82 // Getter and setter for browser action visibility. | 107 // Getter and setter for browser action visibility. |
83 bool GetBrowserActionVisibility(const Extension* extension); | 108 bool GetBrowserActionVisibility(const Extension* extension); |
84 void SetBrowserActionVisibility(const Extension* extension, bool visible); | 109 void SetBrowserActionVisibility(const Extension* extension, bool visible); |
85 | 110 |
86 // Did the extension ask to escalate its permission during an upgrade? | 111 // Did the extension ask to escalate its permission during an upgrade? |
87 bool DidExtensionEscalatePermissions(const std::string& id); | 112 bool DidExtensionEscalatePermissions(const std::string& id); |
88 | 113 |
89 // If |did_escalate| is true, the preferences for |extension| will be set to | 114 // If |did_escalate| is true, the preferences for |extension| will be set to |
90 // require the install warning when the user tries to enable. | 115 // require the install warning when the user tries to enable. |
91 void SetDidExtensionEscalatePermissions(const Extension* extension, | 116 void SetDidExtensionEscalatePermissions(const Extension* extension, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 // highest current application launch index found. | 250 // highest current application launch index found. |
226 int GetNextAppLaunchIndex(); | 251 int GetNextAppLaunchIndex(); |
227 | 252 |
228 // The extension's update URL data. If not empty, the ExtensionUpdater | 253 // The extension's update URL data. If not empty, the ExtensionUpdater |
229 // will append a ap= parameter to the URL when checking if a new version | 254 // will append a ap= parameter to the URL when checking if a new version |
230 // of the extension is available. | 255 // of the extension is available. |
231 void SetUpdateUrlData(const std::string& extension_id, | 256 void SetUpdateUrlData(const std::string& extension_id, |
232 const std::string& data); | 257 const std::string& data); |
233 std::string GetUpdateUrlData(const std::string& extension_id); | 258 std::string GetUpdateUrlData(const std::string& extension_id); |
234 | 259 |
260 // Sets a preference value that is controlled by the extension. In other | |
261 // words, this is not a pref value *about* the extension but something | |
262 // global the extension wants to override. | |
263 void SetExtensionControlledPref(const std::string& extension_id, | |
264 const std::string& pref_key, | |
265 Value* value); | |
266 | |
235 static void RegisterUserPrefs(PrefService* prefs); | 267 static void RegisterUserPrefs(PrefService* prefs); |
236 | 268 |
237 // The underlying PrefService. | 269 // The underlying PrefService. |
238 PrefService* pref_service() const { return prefs_; } | 270 PrefService* pref_service() const { return prefs_; } |
239 | 271 |
272 protected: | |
273 // For unit testing. Enables injecting an artificial clock that is used | |
274 // to query the current time, when an extension is installed. | |
275 virtual base::Time GetCurrentTime() const; | |
276 | |
240 private: | 277 private: |
241 // Converts absolute paths in the pref to paths relative to the | 278 // Converts absolute paths in the pref to paths relative to the |
242 // install_directory_. | 279 // install_directory_. |
243 void MakePathsRelative(); | 280 void MakePathsRelative(); |
244 | 281 |
245 // Converts internal relative paths to be absolute. Used for export to | 282 // Converts internal relative paths to be absolute. Used for export to |
246 // consumers who expect full paths. | 283 // consumers who expect full paths. |
247 void MakePathsAbsolute(DictionaryValue* dict); | 284 void MakePathsAbsolute(DictionaryValue* dict); |
248 | 285 |
249 // Sets the pref |key| for extension |id| to |value|. | 286 // Sets the pref |key| for extension |id| to |value|. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 void AddToExtensionPrefStringSet(const std::string& extension_id, | 326 void AddToExtensionPrefStringSet(const std::string& extension_id, |
290 const std::string& pref_key, | 327 const std::string& pref_key, |
291 const std::set<std::string>& added_values); | 328 const std::set<std::string>& added_values); |
292 | 329 |
293 // Ensures and returns a mutable dictionary for extension |id|'s prefs. | 330 // Ensures and returns a mutable dictionary for extension |id|'s prefs. |
294 DictionaryValue* GetOrCreateExtensionPref(const std::string& id); | 331 DictionaryValue* GetOrCreateExtensionPref(const std::string& id); |
295 | 332 |
296 // Same as above, but returns NULL if it doesn't exist. | 333 // Same as above, but returns NULL if it doesn't exist. |
297 DictionaryValue* GetExtensionPref(const std::string& id) const; | 334 DictionaryValue* GetExtensionPref(const std::string& id) const; |
298 | 335 |
336 // Returns the dictionary of preferences controlled by the specified extension | |
337 // or NULL if unknown. All entries in the dictionary contain non-expanded | |
338 // paths. | |
339 DictionaryValue* GetExtensionControlledPrefs(const std::string& id) const; | |
340 | |
299 // Serializes the data and schedules a persistent save via the |PrefService|. | 341 // Serializes the data and schedules a persistent save via the |PrefService|. |
300 // Additionally fires a PREF_CHANGED notification with the top-level | 342 // Additionally fires a PREF_CHANGED notification with the top-level |
301 // |kExtensionsPref| path set. | 343 // |kExtensionsPref| path set. |
302 // TODO(andybons): Switch this to EXTENSION_PREF_CHANGED to be more granular. | 344 // TODO(andybons): Switch this to EXTENSION_PREF_CHANGED to be more granular. |
303 // TODO(andybons): Use a ScopedPrefUpdate to update observers on changes to | 345 // TODO(andybons): Use a ScopedPrefUpdate to update observers on changes to |
304 // the mutable extension dictionary. | 346 // the mutable extension dictionary. |
305 void SavePrefsAndNotify(); | 347 void SavePrefsAndNotify(); |
306 | 348 |
307 // Checks if kPrefBlacklist is set to true in the DictionaryValue. | 349 // Checks if kPrefBlacklist is set to true in the DictionaryValue. |
308 // Return false if the value is false or kPrefBlacklist does not exist. | 350 // Return false if the value is false or kPrefBlacklist does not exist. |
309 // This is used to decide if an extension is blacklisted. | 351 // This is used to decide if an extension is blacklisted. |
310 bool IsBlacklistBitSet(DictionaryValue* ext); | 352 bool IsBlacklistBitSet(DictionaryValue* ext); |
311 | 353 |
312 // Helper methods for the public last ping day functions. | 354 // Helper methods for the public last ping day functions. |
313 base::Time LastPingDayImpl(const DictionaryValue* dictionary) const; | 355 base::Time LastPingDayImpl(const DictionaryValue* dictionary) const; |
314 void SetLastPingDayImpl(const base::Time& time, DictionaryValue* dictionary); | 356 void SetLastPingDayImpl(const base::Time& time, DictionaryValue* dictionary); |
315 | 357 |
358 // Helper method to acquire the installation time of an extension. | |
359 base::Time GetInstallTime(const std::string& extension_id) const; | |
360 | |
361 // Fix missing preference entries in the extensions that are were introduced | |
362 // in a later Chrome version. | |
363 void FixMissingPrefs(const ExtensionIdSet& extension_ids); | |
364 | |
365 // Installs the persistent extension preferences into |prefs_|'s extension | |
366 // pref store. | |
367 void InitPrefStore(); | |
368 | |
369 // Returns the extension controlled preference value of the extension that was | |
370 // installed most recently. | |
371 const Value* GetWinningExtensionControlledPrefValue( | |
372 const std::string& key) const; | |
373 | |
374 // Executes UpdatePrefStore for all |pref_keys|. | |
375 void UpdatePrefStore(const PrefKeySet& pref_keys); | |
376 | |
377 // Finds the most recently installed extension that defines a preference | |
378 // for |pref_key|, then stores its value in the PrefValueStore's extension | |
379 // pref store and sends notifications to observers in case the value changed. | |
380 void UpdatePrefStore(const std::string& pref_key); | |
381 | |
382 // Retrieves a list of preference keys that the specified extension | |
383 // intends to manage. Keys are always appended, |out| is not cleared. | |
384 void GetExtensionControlledPrefKeys(const std::string& extension_id, | |
385 PrefKeySet *out) const; | |
386 | |
316 // The pref service specific to this set of extension prefs. | 387 // The pref service specific to this set of extension prefs. |
317 PrefService* prefs_; | 388 PrefService* prefs_; |
318 | 389 |
319 // Base extensions install directory. | 390 // Base extensions install directory. |
320 FilePath install_directory_; | 391 FilePath install_directory_; |
321 | 392 |
322 // The URLs of all of the toolstrips. | 393 // The URLs of all of the toolstrips. |
323 URLList shelf_order_; | 394 URLList shelf_order_; |
324 | 395 |
325 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs); | 396 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs); |
326 }; | 397 }; |
327 | 398 |
328 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ | 399 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ |
OLD | NEW |