| 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/callback_list.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "base/prefs/pref_change_registrar.h" | 18 #include "base/prefs/pref_change_registrar.h" |
| 19 #include "chrome/browser/webdata/keyword_web_data_service.h" | 19 #include "chrome/browser/webdata/keyword_web_data_service.h" |
| 20 #include "components/google/core/browser/google_url_tracker.h" | 20 #include "components/google/core/browser/google_url_tracker.h" |
| 21 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 22 #include "components/search_engines/default_search_manager.h" | 22 #include "components/search_engines/default_search_manager.h" |
| 23 #include "components/search_engines/template_url.h" | 23 #include "components/search_engines/template_url.h" |
| 24 #include "components/search_engines/template_url_id.h" | 24 #include "components/search_engines/template_url_id.h" |
| 25 #include "components/webdata/common/web_data_service_consumer.h" | 25 #include "components/webdata/common/web_data_service_consumer.h" |
| 26 #include "content/public/browser/notification_observer.h" | |
| 27 #include "content/public/browser/notification_registrar.h" | |
| 28 #include "sync/api/sync_change.h" | 26 #include "sync/api/sync_change.h" |
| 29 #include "sync/api/syncable_service.h" | 27 #include "sync/api/syncable_service.h" |
| 30 | 28 |
| 31 class GURL; | 29 class GURL; |
| 32 class PrefService; | 30 class PrefService; |
| 33 class Profile; | |
| 34 class SearchHostToURLsMap; | 31 class SearchHostToURLsMap; |
| 35 class SearchTermsData; | 32 class SearchTermsData; |
| 36 class TemplateURL; | 33 class TemplateURL; |
| 37 struct TemplateURLData; | 34 struct TemplateURLData; |
| 35 class TemplateURLServiceClient; |
| 38 class TemplateURLServiceObserver; | 36 class TemplateURLServiceObserver; |
| 39 | 37 |
| 40 namespace rappor { | 38 namespace rappor { |
| 41 class RapporService; | 39 class RapporService; |
| 42 } | 40 } |
| 43 | 41 |
| 44 namespace syncer { | 42 namespace syncer { |
| 45 class SyncData; | 43 class SyncData; |
| 46 class SyncErrorFactory; | 44 class SyncErrorFactory; |
| 47 } | 45 } |
| 48 | 46 |
| 49 namespace history { | |
| 50 struct URLVisitedDetails; | |
| 51 } | |
| 52 | |
| 53 // TemplateURLService is the backend for keywords. It's used by | 47 // TemplateURLService is the backend for keywords. It's used by |
| 54 // KeywordAutocomplete. | 48 // KeywordAutocomplete. |
| 55 // | 49 // |
| 56 // TemplateURLService stores a vector of TemplateURLs. The TemplateURLs are | 50 // TemplateURLService stores a vector of TemplateURLs. The TemplateURLs are |
| 57 // persisted to the database maintained by KeywordWebDataService. | 51 // persisted to the database maintained by KeywordWebDataService. |
| 58 // *ALL* mutations to the TemplateURLs must funnel through TemplateURLService. | 52 // *ALL* mutations to the TemplateURLs must funnel through TemplateURLService. |
| 59 // This allows TemplateURLService to notify listeners of changes as well as keep | 53 // This allows TemplateURLService to notify listeners of changes as well as keep |
| 60 // the database in sync. | 54 // the database in sync. |
| 61 // | 55 // |
| 62 // There is a TemplateURLService per Profile. | |
| 63 // | |
| 64 // TemplateURLService does not load the vector of TemplateURLs in its | 56 // TemplateURLService does not load the vector of TemplateURLs in its |
| 65 // constructor (except for testing). Use the Load method to trigger a load. | 57 // constructor (except for testing). Use the Load method to trigger a load. |
| 66 // When TemplateURLService has completed loading, observers are notified via | 58 // When TemplateURLService has completed loading, observers are notified via |
| 67 // OnTemplateURLServiceChanged, or by a callback registered prior to calling | 59 // OnTemplateURLServiceChanged, or by a callback registered prior to calling |
| 68 // the Load method. | 60 // the Load method. |
| 69 // | 61 // |
| 70 // TemplateURLService takes ownership of any TemplateURL passed to it. If there | 62 // TemplateURLService takes ownership of any TemplateURL passed to it. If there |
| 71 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService, | 63 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService, |
| 72 // otherwise TemplateURLService handles deletion. | 64 // otherwise TemplateURLService handles deletion. |
| 73 | 65 |
| 74 class TemplateURLService : public WebDataServiceConsumer, | 66 class TemplateURLService : public WebDataServiceConsumer, |
| 75 public KeyedService, | 67 public KeyedService, |
| 76 public content::NotificationObserver, | |
| 77 public syncer::SyncableService { | 68 public syncer::SyncableService { |
| 78 public: | 69 public: |
| 79 typedef std::map<std::string, std::string> QueryTerms; | 70 typedef std::map<std::string, std::string> QueryTerms; |
| 80 typedef std::vector<TemplateURL*> TemplateURLVector; | 71 typedef std::vector<TemplateURL*> TemplateURLVector; |
| 81 // Type for a static function pointer that acts as a time source. | 72 // Type for a static function pointer that acts as a time source. |
| 82 typedef base::Time(TimeProvider)(); | 73 typedef base::Time(TimeProvider)(); |
| 83 typedef std::map<std::string, syncer::SyncData> SyncDataMap; | 74 typedef std::map<std::string, syncer::SyncData> SyncDataMap; |
| 84 typedef base::CallbackList<void(void)>::Subscription Subscription; | 75 typedef base::CallbackList<void(void)>::Subscription Subscription; |
| 85 | 76 |
| 86 // Struct used for initializing the data store with fake data. | 77 // Struct used for initializing the data store with fake data. |
| 87 // Each initializer is mapped to a TemplateURL. | 78 // Each initializer is mapped to a TemplateURL. |
| 88 struct Initializer { | 79 struct Initializer { |
| 89 const char* const keyword; | 80 const char* const keyword; |
| 90 const char* const url; | 81 const char* const url; |
| 91 const char* const content; | 82 const char* const content; |
| 92 }; | 83 }; |
| 93 | 84 |
| 94 TemplateURLService(Profile* profile, | 85 struct URLVisitedDetails { |
| 86 GURL url; |
| 87 bool is_keyword_transition; |
| 88 }; |
| 89 |
| 90 TemplateURLService(PrefService* prefs, |
| 91 scoped_ptr<SearchTermsData> search_terms_data, |
| 92 KeywordWebDataService* web_data_service, |
| 93 scoped_ptr<TemplateURLServiceClient> client, |
| 94 GoogleURLTracker* google_url_tracker, |
| 95 rappor::RapporService* rappor_service, | 95 rappor::RapporService* rappor_service, |
| 96 const base::Closure& dsp_change_callback); | 96 const base::Closure& dsp_change_callback); |
| 97 // The following is for testing. | 97 // The following is for testing. |
| 98 TemplateURLService(const Initializer* initializers, const int count); | 98 TemplateURLService(const Initializer* initializers, const int count); |
| 99 virtual ~TemplateURLService(); | 99 virtual ~TemplateURLService(); |
| 100 | 100 |
| 101 // Creates a TemplateURLData that was previously saved to |prefs| via | 101 // Creates a TemplateURLData that was previously saved to |prefs| via |
| 102 // SaveDefaultSearchProviderToPrefs or set via policy. | 102 // SaveDefaultSearchProviderToPrefs or set via policy. |
| 103 // Returns true if successful, false otherwise. | 103 // Returns true if successful, false otherwise. |
| 104 // If the user or the policy has opted for no default search, this | 104 // If the user or the policy has opted for no default search, this |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 virtual void OnWebDataServiceRequestDone( | 292 virtual void OnWebDataServiceRequestDone( |
| 293 KeywordWebDataService::Handle h, | 293 KeywordWebDataService::Handle h, |
| 294 const WDTypedResult* result) OVERRIDE; | 294 const WDTypedResult* result) OVERRIDE; |
| 295 | 295 |
| 296 // Returns the locale-direction-adjusted short name for the given keyword. | 296 // Returns the locale-direction-adjusted short name for the given keyword. |
| 297 // Also sets the out param to indicate whether the keyword belongs to an | 297 // Also sets the out param to indicate whether the keyword belongs to an |
| 298 // Omnibox extension. | 298 // Omnibox extension. |
| 299 base::string16 GetKeywordShortName(const base::string16& keyword, | 299 base::string16 GetKeywordShortName(const base::string16& keyword, |
| 300 bool* is_omnibox_api_extension_keyword); | 300 bool* is_omnibox_api_extension_keyword); |
| 301 | 301 |
| 302 // content::NotificationObserver implementation. | 302 // Called by the history service when a URL is visited. |
| 303 virtual void Observe(int type, | 303 void OnHistoryURLVisited(const URLVisitedDetails& details); |
| 304 const content::NotificationSource& source, | |
| 305 const content::NotificationDetails& details) OVERRIDE; | |
| 306 | 304 |
| 307 // KeyedService implementation. | 305 // KeyedService implementation. |
| 308 virtual void Shutdown() OVERRIDE; | 306 virtual void Shutdown() OVERRIDE; |
| 309 | 307 |
| 310 // syncer::SyncableService implementation. | 308 // syncer::SyncableService implementation. |
| 311 | 309 |
| 312 // Returns all syncable TemplateURLs from this model as SyncData. This should | 310 // Returns all syncable TemplateURLs from this model as SyncData. This should |
| 313 // include every search engine and no Extension keywords. | 311 // include every search engine and no Extension keywords. |
| 314 virtual syncer::SyncDataList GetAllSyncData( | 312 virtual syncer::SyncDataList GetAllSyncData( |
| 315 syncer::ModelType type) const OVERRIDE; | 313 syncer::ModelType type) const OVERRIDE; |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 PrefService* prefs); | 506 PrefService* prefs); |
| 509 | 507 |
| 510 // If the TemplateURL's sync GUID matches the kSyncedDefaultSearchProviderGUID | 508 // If the TemplateURL's sync GUID matches the kSyncedDefaultSearchProviderGUID |
| 511 // preference it will be used to update the DSE in memory and as persisted in | 509 // preference it will be used to update the DSE in memory and as persisted in |
| 512 // preferences. | 510 // preferences. |
| 513 void MaybeUpdateDSEAfterSync(TemplateURL* synced_turl); | 511 void MaybeUpdateDSEAfterSync(TemplateURL* synced_turl); |
| 514 | 512 |
| 515 // Iterates through the TemplateURLs to see if one matches the visited url. | 513 // Iterates through the TemplateURLs to see if one matches the visited url. |
| 516 // For each TemplateURL whose url matches the visited url | 514 // For each TemplateURL whose url matches the visited url |
| 517 // SetKeywordSearchTermsForURL is invoked. | 515 // SetKeywordSearchTermsForURL is invoked. |
| 518 void UpdateKeywordSearchTermsForURL( | 516 void UpdateKeywordSearchTermsForURL(const URLVisitedDetails& details); |
| 519 const history::URLVisitedDetails& details); | |
| 520 | 517 |
| 521 // If necessary, generates a visit for the site http:// + t_url.keyword(). | 518 // If necessary, generates a visit for the site http:// + t_url.keyword(). |
| 522 void AddTabToSearchVisit(const TemplateURL& t_url); | 519 void AddTabToSearchVisit(const TemplateURL& t_url); |
| 523 | 520 |
| 521 // Requests the Google URL tracker to check the server if necessary. |
| 522 void RequestGoogleURLTrackerServerCheckIfNecessary(); |
| 523 |
| 524 // Invoked when the Google base URL has changed. Updates the mapping for all | 524 // Invoked when the Google base URL has changed. Updates the mapping for all |
| 525 // TemplateURLs that have a replacement term of {google:baseURL} or | 525 // TemplateURLs that have a replacement term of {google:baseURL} or |
| 526 // {google:baseSuggestURL}. | 526 // {google:baseSuggestURL}. |
| 527 void GoogleBaseURLChanged(); | 527 void GoogleBaseURLChanged(); |
| 528 | 528 |
| 529 // Adds a new TemplateURL to this model. TemplateURLService will own the | 529 // Adds a new TemplateURL to this model. TemplateURLService will own the |
| 530 // reference, and delete it when the TemplateURL is removed. | 530 // reference, and delete it when the TemplateURL is removed. |
| 531 // If |newly_adding| is false, we assume that this TemplateURL was already | 531 // If |newly_adding| is false, we assume that this TemplateURL was already |
| 532 // part of the model in the past, and therefore we don't need to do things | 532 // part of the model in the past, and therefore we don't need to do things |
| 533 // like assign it an ID or notify sync. | 533 // like assign it an ID or notify sync. |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 // Finds the extension-supplied TemplateURL that matches |data|, if any. | 642 // Finds the extension-supplied TemplateURL that matches |data|, if any. |
| 643 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, | 643 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, |
| 644 TemplateURL::Type type); | 644 TemplateURL::Type type); |
| 645 | 645 |
| 646 // Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine | 646 // Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine |
| 647 // that supports replacement and wants to be default, if any. Notifies the | 647 // that supports replacement and wants to be default, if any. Notifies the |
| 648 // DefaultSearchManager, which might change the effective default search | 648 // DefaultSearchManager, which might change the effective default search |
| 649 // engine. | 649 // engine. |
| 650 void UpdateExtensionDefaultSearchEngine(); | 650 void UpdateExtensionDefaultSearchEngine(); |
| 651 | 651 |
| 652 content::NotificationRegistrar notification_registrar_; | 652 |
| 653 // ---------- Browser state related members --------------------------------- |
| 654 PrefService* prefs_; |
| 655 |
| 656 scoped_ptr<SearchTermsData> search_terms_data_; |
| 657 |
| 658 // ---------- Dependencies on other components ------------------------------ |
| 659 // Service used to store entries. |
| 660 scoped_refptr<KeywordWebDataService> web_data_service_; |
| 661 |
| 662 scoped_ptr<TemplateURLServiceClient> client_; |
| 663 |
| 664 GoogleURLTracker* google_url_tracker_; |
| 665 |
| 666 // ---------- Metrics related members --------------------------------------- |
| 667 rappor::RapporService* rappor_service_; |
| 668 |
| 669 // This closure is run when the default search provider is set to Google. |
| 670 base::Closure dsp_change_callback_; |
| 671 |
| 672 |
| 653 PrefChangeRegistrar pref_change_registrar_; | 673 PrefChangeRegistrar pref_change_registrar_; |
| 654 | 674 |
| 655 // Mapping from keyword to the TemplateURL. | 675 // Mapping from keyword to the TemplateURL. |
| 656 KeywordToTemplateMap keyword_to_template_map_; | 676 KeywordToTemplateMap keyword_to_template_map_; |
| 657 | 677 |
| 658 // Mapping from Sync GUIDs to the TemplateURL. | 678 // Mapping from Sync GUIDs to the TemplateURL. |
| 659 GUIDToTemplateMap guid_to_template_map_; | 679 GUIDToTemplateMap guid_to_template_map_; |
| 660 | 680 |
| 661 TemplateURLVector template_urls_; | 681 TemplateURLVector template_urls_; |
| 662 | 682 |
| 663 ObserverList<TemplateURLServiceObserver> model_observers_; | 683 ObserverList<TemplateURLServiceObserver> model_observers_; |
| 664 | 684 |
| 665 // Maps from host to set of TemplateURLs whose search url host is host. | 685 // Maps from host to set of TemplateURLs whose search url host is host. |
| 666 // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular | 686 // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular |
| 667 // header dependencies. | 687 // header dependencies. |
| 668 scoped_ptr<SearchHostToURLsMap> provider_map_; | 688 scoped_ptr<SearchHostToURLsMap> provider_map_; |
| 669 | 689 |
| 670 // Used to obtain the WebDataService. | |
| 671 // When Load is invoked, if we haven't yet loaded, the WebDataService is | |
| 672 // obtained from the Profile. This allows us to lazily access the database. | |
| 673 Profile* profile_; | |
| 674 | |
| 675 PrefService* prefs_; | |
| 676 | |
| 677 rappor::RapporService* rappor_service_; | |
| 678 | |
| 679 scoped_ptr<SearchTermsData> search_terms_data_; | |
| 680 | |
| 681 // This closure is run when the default search provider is set to Google. | |
| 682 base::Closure dsp_change_callback_; | |
| 683 | |
| 684 // Whether the keywords have been loaded. | 690 // Whether the keywords have been loaded. |
| 685 bool loaded_; | 691 bool loaded_; |
| 686 | 692 |
| 687 // Set when the web data service fails to load properly. This prevents | 693 // Set when the web data service fails to load properly. This prevents |
| 688 // further communication with sync or writing to prefs, so we don't persist | 694 // further communication with sync or writing to prefs, so we don't persist |
| 689 // inconsistent state data anywhere. | 695 // inconsistent state data anywhere. |
| 690 bool load_failed_; | 696 bool load_failed_; |
| 691 | 697 |
| 692 // If non-zero, we're waiting on a load. | 698 // If non-zero, we're waiting on a load. |
| 693 KeywordWebDataService::Handle load_handle_; | 699 KeywordWebDataService::Handle load_handle_; |
| 694 | 700 |
| 695 // Service used to store entries. | |
| 696 scoped_refptr<KeywordWebDataService> web_data_service_; | |
| 697 | |
| 698 // All visits that occurred before we finished loading. Once loaded | 701 // All visits that occurred before we finished loading. Once loaded |
| 699 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. | 702 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. |
| 700 std::vector<history::URLVisitedDetails> visits_to_add_; | 703 std::vector<URLVisitedDetails> visits_to_add_; |
| 701 | 704 |
| 702 // Once loaded, the default search provider. This is a pointer to a | 705 // Once loaded, the default search provider. This is a pointer to a |
| 703 // TemplateURL owned by |template_urls_|. | 706 // TemplateURL owned by |template_urls_|. |
| 704 TemplateURL* default_search_provider_; | 707 TemplateURL* default_search_provider_; |
| 705 | 708 |
| 706 // A temporary location for the DSE until Web Data has been loaded and it can | 709 // A temporary location for the DSE until Web Data has been loaded and it can |
| 707 // be merged into |template_urls_|. | 710 // be merged into |template_urls_|. |
| 708 scoped_ptr<TemplateURL> initial_default_search_provider_; | 711 scoped_ptr<TemplateURL> initial_default_search_provider_; |
| 709 | 712 |
| 710 // Source of the default search provider. | 713 // Source of the default search provider. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 | 753 |
| 751 // Helper class to manage the default search engine. | 754 // Helper class to manage the default search engine. |
| 752 DefaultSearchManager default_search_manager_; | 755 DefaultSearchManager default_search_manager_; |
| 753 | 756 |
| 754 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; | 757 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; |
| 755 | 758 |
| 756 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 759 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
| 757 }; | 760 }; |
| 758 | 761 |
| 759 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 762 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
| OLD | NEW |