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