| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
| 6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback_list.h" |
| 14 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 17 #include "base/prefs/pref_change_registrar.h" | 18 #include "base/prefs/pref_change_registrar.h" |
| 18 #include "chrome/browser/search_engines/template_url_id.h" | 19 #include "chrome/browser/search_engines/template_url_id.h" |
| 19 #include "chrome/browser/webdata/web_data_service.h" | 20 #include "chrome/browser/webdata/web_data_service.h" |
| 20 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 21 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 21 #include "content/public/browser/notification_observer.h" | 22 #include "content/public/browser/notification_observer.h" |
| 22 #include "content/public/browser/notification_registrar.h" | 23 #include "content/public/browser/notification_registrar.h" |
| 23 #include "sync/api/sync_change.h" | 24 #include "sync/api/sync_change.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 46 // persisted to the database maintained by WebDataService. *ALL* mutations | 47 // persisted to the database maintained by WebDataService. *ALL* mutations |
| 47 // to the TemplateURLs must funnel through TemplateURLService. This allows | 48 // to the TemplateURLs must funnel through TemplateURLService. This allows |
| 48 // TemplateURLService to notify listeners of changes as well as keep the | 49 // TemplateURLService to notify listeners of changes as well as keep the |
| 49 // database in sync. | 50 // database in sync. |
| 50 // | 51 // |
| 51 // There is a TemplateURLService per Profile. | 52 // There is a TemplateURLService per Profile. |
| 52 // | 53 // |
| 53 // TemplateURLService does not load the vector of TemplateURLs in its | 54 // TemplateURLService does not load the vector of TemplateURLs in its |
| 54 // constructor (except for testing). Use the Load method to trigger a load. | 55 // constructor (except for testing). Use the Load method to trigger a load. |
| 55 // When TemplateURLService has completed loading, observers are notified via | 56 // When TemplateURLService has completed loading, observers are notified via |
| 56 // OnTemplateURLServiceChanged as well as the TEMPLATE_URL_SERVICE_LOADED | 57 // OnTemplateURLServiceChanged, or by a callback registered prior to calling |
| 57 // notification message. | 58 // the Load method. |
| 58 // | 59 // |
| 59 // TemplateURLService takes ownership of any TemplateURL passed to it. If there | 60 // TemplateURLService takes ownership of any TemplateURL passed to it. If there |
| 60 // is a WebDataService, deletion is handled by WebDataService, otherwise | 61 // is a WebDataService, deletion is handled by WebDataService, otherwise |
| 61 // TemplateURLService handles deletion. | 62 // TemplateURLService handles deletion. |
| 62 | 63 |
| 63 class TemplateURLService : public WebDataServiceConsumer, | 64 class TemplateURLService : public WebDataServiceConsumer, |
| 64 public BrowserContextKeyedService, | 65 public BrowserContextKeyedService, |
| 65 public content::NotificationObserver, | 66 public content::NotificationObserver, |
| 66 public syncer::SyncableService { | 67 public syncer::SyncableService { |
| 67 public: | 68 public: |
| 68 typedef std::map<std::string, std::string> QueryTerms; | 69 typedef std::map<std::string, std::string> QueryTerms; |
| 69 typedef std::vector<TemplateURL*> TemplateURLVector; | 70 typedef std::vector<TemplateURL*> TemplateURLVector; |
| 70 // Type for a static function pointer that acts as a time source. | 71 // Type for a static function pointer that acts as a time source. |
| 71 typedef base::Time(TimeProvider)(); | 72 typedef base::Time(TimeProvider)(); |
| 72 typedef std::map<std::string, syncer::SyncData> SyncDataMap; | 73 typedef std::map<std::string, syncer::SyncData> SyncDataMap; |
| 74 typedef base::CallbackList<void(void)>::Subscription Subscription; |
| 73 | 75 |
| 74 // Struct used for initializing the data store with fake data. | 76 // Struct used for initializing the data store with fake data. |
| 75 // Each initializer is mapped to a TemplateURL. | 77 // Each initializer is mapped to a TemplateURL. |
| 76 struct Initializer { | 78 struct Initializer { |
| 77 const char* const keyword; | 79 const char* const keyword; |
| 78 const char* const url; | 80 const char* const url; |
| 79 const char* const content; | 81 const char* const content; |
| 80 }; | 82 }; |
| 81 | 83 |
| 82 // Struct describes a search engine added by an extension. | 84 // Struct describes a search engine added by an extension. |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // TemplateURLService does NOT delete the observers when deleted. | 254 // TemplateURLService does NOT delete the observers when deleted. |
| 253 void AddObserver(TemplateURLServiceObserver* observer); | 255 void AddObserver(TemplateURLServiceObserver* observer); |
| 254 void RemoveObserver(TemplateURLServiceObserver* observer); | 256 void RemoveObserver(TemplateURLServiceObserver* observer); |
| 255 | 257 |
| 256 // Loads the keywords. This has no effect if the keywords have already been | 258 // Loads the keywords. This has no effect if the keywords have already been |
| 257 // loaded. | 259 // loaded. |
| 258 // Observers are notified when loading completes via the method | 260 // Observers are notified when loading completes via the method |
| 259 // OnTemplateURLServiceChanged. | 261 // OnTemplateURLServiceChanged. |
| 260 void Load(); | 262 void Load(); |
| 261 | 263 |
| 264 // Registers a callback to be called when the service has loaded. |
| 265 // |
| 266 // If the service has already loaded, this function does nothing. |
| 267 scoped_ptr<Subscription> RegisterOnLoadedCallback( |
| 268 const base::Closure& callback); |
| 269 |
| 262 #if defined(UNIT_TEST) | 270 #if defined(UNIT_TEST) |
| 263 void set_loaded(bool value) { loaded_ = value; } | 271 void set_loaded(bool value) { loaded_ = value; } |
| 264 #endif | 272 #endif |
| 265 | 273 |
| 266 // Whether or not the keywords have been loaded. | 274 // Whether or not the keywords have been loaded. |
| 267 bool loaded() { return loaded_; } | 275 bool loaded() { return loaded_; } |
| 268 | 276 |
| 269 // Notification that the keywords have been loaded. | 277 // Notification that the keywords have been loaded. |
| 270 // This is invoked from WebDataService, and should not be directly | 278 // This is invoked from WebDataService, and should not be directly |
| 271 // invoked. | 279 // invoked. |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 // This does NOT notify the delegate or the database. | 435 // This does NOT notify the delegate or the database. |
| 428 // | 436 // |
| 429 // This transfers ownership of the elements in |urls| to |this|, and may | 437 // This transfers ownership of the elements in |urls| to |this|, and may |
| 430 // delete some elements, so it's not safe for callers to access any elements | 438 // delete some elements, so it's not safe for callers to access any elements |
| 431 // after calling; to reinforce this, this function clears |urls| on exit. | 439 // after calling; to reinforce this, this function clears |urls| on exit. |
| 432 void SetTemplateURLs(TemplateURLVector* urls); | 440 void SetTemplateURLs(TemplateURLVector* urls); |
| 433 | 441 |
| 434 // Transitions to the loaded state. | 442 // Transitions to the loaded state. |
| 435 void ChangeToLoadedState(); | 443 void ChangeToLoadedState(); |
| 436 | 444 |
| 437 // If there is a notification service, sends TEMPLATE_URL_SERVICE_LOADED | 445 // Runs all callbacks in |on_loaded_callbacks_|. |
| 438 // notification. | |
| 439 void NotifyLoaded(); | 446 void NotifyLoaded(); |
| 440 | 447 |
| 441 // Saves enough of url to preferences so that it can be loaded from | 448 // Saves enough of url to preferences so that it can be loaded from |
| 442 // preferences on start up. | 449 // preferences on start up. |
| 443 void SaveDefaultSearchProviderToPrefs(const TemplateURL* url); | 450 void SaveDefaultSearchProviderToPrefs(const TemplateURL* url); |
| 444 | 451 |
| 445 // Creates a TemplateURL that was previously saved to prefs via | 452 // Creates a TemplateURL that was previously saved to prefs via |
| 446 // SaveDefaultSearchProviderToPrefs or set via policy. | 453 // SaveDefaultSearchProviderToPrefs or set via policy. |
| 447 // Returns true if successful, false otherwise. | 454 // Returns true if successful, false otherwise. |
| 448 // If the user or the policy has opted for no default search, this | 455 // If the user or the policy has opted for no default search, this |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 // model or the underlying WebDataService prior to MergeDataAndStartSyncing. | 727 // model or the underlying WebDataService prior to MergeDataAndStartSyncing. |
| 721 // This set is used to determine what entries from the server we want to | 728 // This set is used to determine what entries from the server we want to |
| 722 // ignore locally and return a delete command for. | 729 // ignore locally and return a delete command for. |
| 723 std::set<std::string> pre_sync_deletes_; | 730 std::set<std::string> pre_sync_deletes_; |
| 724 | 731 |
| 725 // This is used to log the origin of changes to the default search provider. | 732 // This is used to log the origin of changes to the default search provider. |
| 726 // We set this value to increasingly specific values when we know what is the | 733 // We set this value to increasingly specific values when we know what is the |
| 727 // cause/origin of a default search change. | 734 // cause/origin of a default search change. |
| 728 DefaultSearchChangeOrigin dsp_change_origin_; | 735 DefaultSearchChangeOrigin dsp_change_origin_; |
| 729 | 736 |
| 737 // Stores a list of callbacks to be run after TemplateURLService has loaded. |
| 738 base::CallbackList<void(void)> on_loaded_callbacks_; |
| 739 |
| 730 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 740 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
| 731 }; | 741 }; |
| 732 | 742 |
| 733 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 743 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
| OLD | NEW |