| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "chrome/browser/extensions/extension_content_settings_store.h" | 15 #include "chrome/browser/extensions/extension_content_settings_store.h" |
| 16 #include "chrome/browser/extensions/extension_prefs_scope.h" | 16 #include "chrome/browser/extensions/extension_prefs_scope.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/string_ordinal.h" | |
| 20 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 21 | 20 |
| 22 class ExtensionPrefValueMap; | 21 class ExtensionPrefValueMap; |
| 23 class URLPatternSet; | 22 class URLPatternSet; |
| 24 | 23 |
| 25 // Class for managing global and per-extension preferences. | 24 // Class for managing global and per-extension preferences. |
| 26 // | 25 // |
| 27 // This class distinguishes the following kinds of preferences: | 26 // This class distinguishes the following kinds of preferences: |
| 28 // - global preferences: | 27 // - global preferences: |
| 29 // internal state for the extension system in general, not associated | 28 // internal state for the extension system in general, not associated |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 // ExtensionService::IsExtensionEnabled instead. | 92 // ExtensionService::IsExtensionEnabled instead. |
| 94 bool IsExtensionDisabled(const std::string& id) const; | 93 bool IsExtensionDisabled(const std::string& id) const; |
| 95 | 94 |
| 96 // Get the order that the browser actions appear in the toolbar. | 95 // Get the order that the browser actions appear in the toolbar. |
| 97 std::vector<std::string> GetToolbarOrder(); | 96 std::vector<std::string> GetToolbarOrder(); |
| 98 | 97 |
| 99 // Set the order that the browser actions appear in the toolbar. | 98 // Set the order that the browser actions appear in the toolbar. |
| 100 void SetToolbarOrder(const std::vector<std::string>& extension_ids); | 99 void SetToolbarOrder(const std::vector<std::string>& extension_ids); |
| 101 | 100 |
| 102 // Called when an extension is installed, so that prefs get created. | 101 // Called when an extension is installed, so that prefs get created. |
| 103 // If |page_ordinal| is an invalid ordinal, then a page will be found | 102 // If |page_index| is -1, and the then a page will be found for the App. |
| 104 // for the App. | |
| 105 void OnExtensionInstalled(const Extension* extension, | 103 void OnExtensionInstalled(const Extension* extension, |
| 106 Extension::State initial_state, | 104 Extension::State initial_state, |
| 107 bool from_webstore, | 105 bool from_webstore, |
| 108 const StringOrdinal& page_ordinal); | 106 int page_index); |
| 109 | 107 |
| 110 // Called when an extension is uninstalled, so that prefs get cleaned up. | 108 // Called when an extension is uninstalled, so that prefs get cleaned up. |
| 111 void OnExtensionUninstalled(const std::string& extension_id, | 109 void OnExtensionUninstalled(const std::string& extension_id, |
| 112 const Extension::Location& location, | 110 const Extension::Location& location, |
| 113 bool external_uninstall); | 111 bool external_uninstall); |
| 114 | 112 |
| 115 // Updates the app launcher value for the moved extension so that it is now | |
| 116 // located after the given predecessor and before the successor. | |
| 117 // Empty strings are used to indicate no successor or predecessor. | |
| 118 void OnExtensionMoved( | |
| 119 const std::string& moved_extension_id, | |
| 120 const std::string& predecessor_extension_id, | |
| 121 const std::string& successor_extension_id); | |
| 122 | |
| 123 // Called to change the extension's state when it is enabled/disabled. | 113 // Called to change the extension's state when it is enabled/disabled. |
| 124 void SetExtensionState(const std::string& extension_id, Extension::State); | 114 void SetExtensionState(const std::string& extension_id, Extension::State); |
| 125 | 115 |
| 126 // Returns all installed extensions | 116 // Returns all installed extensions |
| 127 void GetExtensions(ExtensionIdSet* out); | 117 void GetExtensions(ExtensionIdSet* out); |
| 128 | 118 |
| 129 // Getter and setter for browser action visibility. | 119 // Getter and setter for browser action visibility. |
| 130 bool GetBrowserActionVisibility(const Extension* extension); | 120 bool GetBrowserActionVisibility(const Extension* extension); |
| 131 void SetBrowserActionVisibility(const Extension* extension, bool visible); | 121 void SetBrowserActionVisibility(const Extension* extension, bool visible); |
| 132 | 122 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 std::set<std::string> GetIdleInstallInfoIds(); | 297 std::set<std::string> GetIdleInstallInfoIds(); |
| 308 | 298 |
| 309 // We allow the web store to set a string containing login information when a | 299 // We allow the web store to set a string containing login information when a |
| 310 // purchase is made, so that when a user logs into sync with a different | 300 // purchase is made, so that when a user logs into sync with a different |
| 311 // account we can recognize the situation. The Get function returns true if | 301 // account we can recognize the situation. The Get function returns true if |
| 312 // there was previously stored data (placing it in |result|), or false | 302 // there was previously stored data (placing it in |result|), or false |
| 313 // otherwise. The Set will overwrite any previous login. | 303 // otherwise. The Set will overwrite any previous login. |
| 314 bool GetWebStoreLogin(std::string* result); | 304 bool GetWebStoreLogin(std::string* result); |
| 315 void SetWebStoreLogin(const std::string& login); | 305 void SetWebStoreLogin(const std::string& login); |
| 316 | 306 |
| 317 // Get the application launch ordinal for an app with |extension_id|. This | 307 // Get the application launch index for an extension with |extension_id|. This |
| 318 // determines the order in which the app appears on the page it's on in the | 308 // determines the order of which the applications appear on the New Tab Page. |
| 319 // New Tab Page (Note that you can compare app launch ordinals only if the | 309 // A value of 0 generally indicates top left. If the extension has no launch |
| 320 // apps are on the same page). A string value close to |a*| generally | 310 // index a -1 value is returned. |
| 321 // indicates top left. If the extension has no launch ordinal, an invalid | 311 int GetAppLaunchIndex(const std::string& extension_id); |
| 322 // StringOrdinal is returned. | |
| 323 StringOrdinal GetAppLaunchOrdinal(const std::string& extension_id) const; | |
| 324 | 312 |
| 325 // Sets a specific launch ordinal for an app with |extension_id|. | 313 // Sets a specific launch index for an extension with |extension_id|. |
| 326 void SetAppLaunchOrdinal(const std::string& extension_id, | 314 void SetAppLaunchIndex(const std::string& extension_id, int index); |
| 327 const StringOrdinal& ordinal); | |
| 328 | 315 |
| 329 // Returns a StringOrdinal that is lower than any app launch ordinal for the | 316 // Gets the next available application launch index. This is 1 higher than the |
| 330 // given page. | 317 // highest current application launch index found for the page |on_page|. |
| 331 StringOrdinal CreateFirstAppLaunchOrdinal(const StringOrdinal& page_ordinal) | 318 int GetNextAppLaunchIndex(int on_page); |
| 332 const; | |
| 333 | 319 |
| 334 // Returns a StringOrdinal that is higher than any app launch ordinal for the | 320 // Gets the page a new app should install to. Starts on page 0, and if there |
| 335 // given page. | 321 // are N or more apps on it, tries to install on the next page. |
| 336 StringOrdinal CreateNextAppLaunchOrdinal(const StringOrdinal& page_ordinal) | 322 int GetNaturalAppPageIndex(); |
| 337 const; | |
| 338 | 323 |
| 339 // Returns a StringOrdinal that is lower than any existing page ordinal. | 324 // Sets the order the apps should be displayed in the app launcher. |
| 340 StringOrdinal CreateFirstAppPageOrdinal() const; | 325 void SetAppLauncherOrder(const std::vector<std::string>& extension_ids); |
| 341 | 326 |
| 342 // Gets the page a new app should install to, which is the earliest non-full | 327 // Get the application page index for an extension with |extension_id|. This |
| 343 // page. The returned ordinal may correspond to a page that doesn't yet exist | 328 // determines which page an app will appear on in page-based NTPs. If |
| 344 // if all pages are full. | 329 // the app has no page specified, -1 is returned. |
| 345 StringOrdinal GetNaturalAppPageOrdinal() const; | 330 int GetPageIndex(const std::string& extension_id); |
| 346 | 331 |
| 347 // Get the page ordinal for an app with |extension_id|. This determines | 332 // Sets a specific page index for an extension with |extension_id|. |
| 348 // which page an app will appear on in page-based NTPs. If the app has no | 333 void SetPageIndex(const std::string& extension_id, int index); |
| 349 // page specified, an invalid StringOrdinal is returned. | |
| 350 StringOrdinal GetPageOrdinal(const std::string& extension_id) const; | |
| 351 | 334 |
| 352 // Sets a specific page ordinal for an app with |extension_id|. | 335 // Removes the page index for an extension. |
| 353 void SetPageOrdinal(const std::string& extension_id, | 336 void ClearPageIndex(const std::string& extension_id); |
| 354 const StringOrdinal& ordinal); | |
| 355 | |
| 356 // Removes the page ordinal for an app. | |
| 357 void ClearPageOrdinal(const std::string& extension_id); | |
| 358 | |
| 359 // Convert the page StringOrdinal value to its integer equivalent. This takes | |
| 360 // O(# of apps) worst-case. | |
| 361 int PageStringOrdinalAsInteger(const StringOrdinal& page_ordinal) const; | |
| 362 | |
| 363 // Converts the page index integer to its StringOrdinal equivalent. This takes | |
| 364 // O(# of apps) worst-case. | |
| 365 StringOrdinal PageIntegerAsStringOrdinal(size_t page_index) const; | |
| 366 | 337 |
| 367 // Returns true if the user repositioned the app on the app launcher via drag | 338 // Returns true if the user repositioned the app on the app launcher via drag |
| 368 // and drop. | 339 // and drop. |
| 369 bool WasAppDraggedByUser(const std::string& extension_id); | 340 bool WasAppDraggedByUser(const std::string& extension_id); |
| 370 | 341 |
| 371 // Sets a flag indicating that the user repositioned the app on the app | 342 // Sets a flag indicating that the user repositioned the app on the app |
| 372 // launcher by drag and dropping it. | 343 // launcher by drag and dropping it. |
| 373 void SetAppDraggedByUser(const std::string& extension_id); | 344 void SetAppDraggedByUser(const std::string& extension_id); |
| 374 | 345 |
| 375 // The extension's update URL data. If not empty, the ExtensionUpdater | 346 // The extension's update URL data. If not empty, the ExtensionUpdater |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 | 402 |
| 432 // The underlying PrefService. | 403 // The underlying PrefService. |
| 433 PrefService* pref_service() const { return prefs_; } | 404 PrefService* pref_service() const { return prefs_; } |
| 434 | 405 |
| 435 protected: | 406 protected: |
| 436 // For unit testing. Enables injecting an artificial clock that is used | 407 // For unit testing. Enables injecting an artificial clock that is used |
| 437 // to query the current time, when an extension is installed. | 408 // to query the current time, when an extension is installed. |
| 438 virtual base::Time GetCurrentTime() const; | 409 virtual base::Time GetCurrentTime() const; |
| 439 | 410 |
| 440 private: | 411 private: |
| 441 friend class ExtensionPrefsGetMinOrMaxAppLaunchOrdinalsOnPage; // Unit test. | |
| 442 friend class ExtensionPrefsMigrateAppIndex; // Unit test. | |
| 443 friend class ExtensionPrefsUninstallExtension; // Unit test. | 412 friend class ExtensionPrefsUninstallExtension; // Unit test. |
| 444 | 413 |
| 445 // ExtensionContentSettingsStore::Observer methods: | 414 // ExtensionContentSettingsStore::Observer methods: |
| 446 virtual void OnContentSettingChanged( | 415 virtual void OnContentSettingChanged( |
| 447 const std::string& extension_id, | 416 const std::string& extension_id, |
| 448 bool incognito) OVERRIDE; | 417 bool incognito) OVERRIDE; |
| 449 | 418 |
| 450 // Converts absolute paths in the pref to paths relative to the | 419 // Converts absolute paths in the pref to paths relative to the |
| 451 // install_directory_. | 420 // install_directory_. |
| 452 void MakePathsRelative(); | 421 void MakePathsRelative(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 481 // Reads an integer pref |pref_key| from extension with id |extension_id|. | 450 // Reads an integer pref |pref_key| from extension with id |extension_id|. |
| 482 bool ReadExtensionPrefInteger(const std::string& extension_id, | 451 bool ReadExtensionPrefInteger(const std::string& extension_id, |
| 483 const std::string& pref_key, | 452 const std::string& pref_key, |
| 484 int* out_value); | 453 int* out_value); |
| 485 | 454 |
| 486 // Reads a list pref |pref_key| from extension with id |extension_id|. | 455 // Reads a list pref |pref_key| from extension with id |extension_id|. |
| 487 bool ReadExtensionPrefList(const std::string& extension_id, | 456 bool ReadExtensionPrefList(const std::string& extension_id, |
| 488 const std::string& pref_key, | 457 const std::string& pref_key, |
| 489 const base::ListValue** out_value); | 458 const base::ListValue** out_value); |
| 490 | 459 |
| 491 // Reads a string pref |pref_key| from extension with id |extension_id|. | |
| 492 bool ReadExtensionPrefString(const std::string& extension_id, | |
| 493 const std::string& pref_key, | |
| 494 std::string* out_value) const; | |
| 495 | |
| 496 // Interprets the list pref, |pref_key| in |extension_id|'s preferences, as a | 460 // Interprets the list pref, |pref_key| in |extension_id|'s preferences, as a |
| 497 // URLPatternSet. The |valid_schemes| specify how to parse the URLPatterns. | 461 // URLPatternSet. The |valid_schemes| specify how to parse the URLPatterns. |
| 498 bool ReadExtensionPrefURLPatternSet(const std::string& extension_id, | 462 bool ReadExtensionPrefURLPatternSet(const std::string& extension_id, |
| 499 const std::string& pref_key, | 463 const std::string& pref_key, |
| 500 URLPatternSet* result, | 464 URLPatternSet* result, |
| 501 int valid_schemes); | 465 int valid_schemes); |
| 502 | 466 |
| 503 // Converts |new_value| to a list of strings and sets the |pref_key| pref | 467 // Converts |new_value| to a list of strings and sets the |pref_key| pref |
| 504 // belonging to |extension_id|. | 468 // belonging to |extension_id|. |
| 505 void SetExtensionPrefURLPatternSet(const std::string& extension_id, | 469 void SetExtensionPrefURLPatternSet(const std::string& extension_id, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 // in a later Chrome version. | 507 // in a later Chrome version. |
| 544 void FixMissingPrefs(const ExtensionIdSet& extension_ids); | 508 void FixMissingPrefs(const ExtensionIdSet& extension_ids); |
| 545 | 509 |
| 546 // Installs the persistent extension preferences into |prefs_|'s extension | 510 // Installs the persistent extension preferences into |prefs_|'s extension |
| 547 // pref store. Does nothing if |extensions_disabled| is true. | 511 // pref store. Does nothing if |extensions_disabled| is true. |
| 548 void InitPrefStore(bool extensions_disabled); | 512 void InitPrefStore(bool extensions_disabled); |
| 549 | 513 |
| 550 // Migrates the permissions data in the pref store. | 514 // Migrates the permissions data in the pref store. |
| 551 void MigratePermissions(const ExtensionIdSet& extension_ids); | 515 void MigratePermissions(const ExtensionIdSet& extension_ids); |
| 552 | 516 |
| 553 // Migrates the app launcher and page index values. | |
| 554 void MigrateAppIndex(const ExtensionIdSet& extension_ids); | |
| 555 | |
| 556 // Checks whether there is a state pref for the extension and if so, whether | 517 // Checks whether there is a state pref for the extension and if so, whether |
| 557 // it matches |check_state|. | 518 // it matches |check_state|. |
| 558 bool DoesExtensionHaveState(const std::string& id, | 519 bool DoesExtensionHaveState(const std::string& id, |
| 559 Extension::State check_state) const; | 520 Extension::State check_state) const; |
| 560 | 521 |
| 561 // An enum used by GetMinOrMaxAppLaunchOrdinalsOnPage to specify which | |
| 562 // value should be returned. | |
| 563 enum AppLaunchOrdinalReturn {MIN_ORDINAL, MAX_ORDINAL}; | |
| 564 | |
| 565 // This function returns the lowest ordinal on |page_ordinal| if | |
| 566 // |return_value| == AppLaunchOrdinalReturn::MIN_ORDINAL, otherwise it returns | |
| 567 // the largest ordinal on |page_ordinal|. If there are no apps on the page | |
| 568 // then an invalid StringOrdinal is returned. It is an error to call this | |
| 569 // function with an invalid |page_ordinal|. | |
| 570 StringOrdinal GetMinOrMaxAppLaunchOrdinalsOnPage( | |
| 571 const StringOrdinal& page_ordinal, | |
| 572 AppLaunchOrdinalReturn return_type) const; | |
| 573 | |
| 574 // Initialize the |page_ordinal_map_| with the page ordinals used by the | |
| 575 // given extensions. | |
| 576 void InitializePageOrdinalMap(const ExtensionIdSet& extension_ids); | |
| 577 | |
| 578 // Called when an application changes the value of its page ordinal so that | |
| 579 // |page_ordinal_map_| is aware that |old_value| page ordinal has been | |
| 580 // replace by the |new_value| page ordinal and adjusts its mapping | |
| 581 // accordingly. This works with valid and invalid StringOrdinals. | |
| 582 void UpdatePageOrdinalMap(const StringOrdinal& old_value, | |
| 583 const StringOrdinal& new_value); | |
| 584 | |
| 585 // The pref service specific to this set of extension prefs. Owned by profile. | 522 // The pref service specific to this set of extension prefs. Owned by profile. |
| 586 PrefService* prefs_; | 523 PrefService* prefs_; |
| 587 | 524 |
| 588 // Base extensions install directory. | 525 // Base extensions install directory. |
| 589 FilePath install_directory_; | 526 FilePath install_directory_; |
| 590 | 527 |
| 591 // A map of all the StringOrdinal page indices mapping to how often they are | |
| 592 // used, this is used for mapping the StringOrdinals to their integer | |
| 593 // equivalent as well as quick lookup of the sorted StringOrdinals. | |
| 594 // TODO(csharp) Convert this to a two-layer map to allow page ordinal lookup | |
| 595 // by id and vice versa. | |
| 596 typedef std::map<StringOrdinal, int, StringOrdinalLessThan> PageOrdinalMap; | |
| 597 PageOrdinalMap page_ordinal_map_; | |
| 598 | |
| 599 // Weak pointer, owned by Profile. | 528 // Weak pointer, owned by Profile. |
| 600 ExtensionPrefValueMap* extension_pref_value_map_; | 529 ExtensionPrefValueMap* extension_pref_value_map_; |
| 601 | 530 |
| 602 scoped_refptr<ExtensionContentSettingsStore> content_settings_store_; | 531 scoped_refptr<ExtensionContentSettingsStore> content_settings_store_; |
| 603 | 532 |
| 604 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs); | 533 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs); |
| 605 }; | 534 }; |
| 606 | 535 |
| 607 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ | 536 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_ |
| OLD | NEW |