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> |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // SaveDefaultSearchProviderToPrefs or set via policy. | 105 // SaveDefaultSearchProviderToPrefs or set via policy. |
106 // Returns true if successful, false otherwise. | 106 // Returns true if successful, false otherwise. |
107 // If the user or the policy has opted for no default search, this | 107 // If the user or the policy has opted for no default search, this |
108 // returns true but default_provider is set to NULL. | 108 // returns true but default_provider is set to NULL. |
109 // |*is_managed| specifies whether the default is managed via policy. | 109 // |*is_managed| specifies whether the default is managed via policy. |
110 static bool LoadDefaultSearchProviderFromPrefs( | 110 static bool LoadDefaultSearchProviderFromPrefs( |
111 PrefService* prefs, | 111 PrefService* prefs, |
112 scoped_ptr<TemplateURLData>* default_provider_data, | 112 scoped_ptr<TemplateURLData>* default_provider_data, |
113 bool* is_managed); | 113 bool* is_managed); |
114 | 114 |
115 // Saves enough of url to |prefs| so that it can be loaded from preferences on | |
116 // start up. | |
117 static void SaveDefaultSearchProviderToPrefs(const TemplateURL* url, | |
118 PrefService* prefs); | |
119 | |
120 // Generates a suitable keyword for the specified url, which must be valid. | 115 // Generates a suitable keyword for the specified url, which must be valid. |
121 // This is guaranteed not to return an empty string, since TemplateURLs should | 116 // This is guaranteed not to return an empty string, since TemplateURLs should |
122 // never have an empty keyword. | 117 // never have an empty keyword. |
123 static base::string16 GenerateKeyword(const GURL& url); | 118 static base::string16 GenerateKeyword(const GURL& url); |
124 | 119 |
125 // Removes any unnecessary characters from a user input keyword. | 120 // Removes any unnecessary characters from a user input keyword. |
126 // This removes the leading scheme, "www." and any trailing slash. | 121 // This removes the leading scheme, "www." and any trailing slash. |
127 static base::string16 CleanUserInputKeyword(const base::string16& keyword); | 122 static base::string16 CleanUserInputKeyword(const base::string16& keyword); |
128 | 123 |
129 // Returns the search url for t_url. Returns an empty GURL if t_url has no | 124 // Returns the search url for t_url. Returns an empty GURL if t_url has no |
130 // url(). | 125 // url(). |
131 // NOTE: |t_url| is non-const in this version because of the need to access | 126 // NOTE: |t_url| is non-const in this version because of the need to access |
132 // t_url->profile(). | 127 // t_url->profile(). |
133 static GURL GenerateSearchURL(TemplateURL* t_url); | 128 static GURL GenerateSearchURL(TemplateURL* t_url); |
134 | 129 |
135 // Just like GenerateSearchURL except that it takes SearchTermsData to supply | 130 // Just like GenerateSearchURL except that it takes SearchTermsData to supply |
136 // the data for some search terms, e.g. so this can be used on threads other | 131 // the data for some search terms, e.g. so this can be used on threads other |
137 // than the UI thread. See the various TemplateURLRef::XXXUsingTermsData() | 132 // than the UI thread. See the various TemplateURLRef::XXXUsingTermsData() |
138 // functions. | 133 // functions. |
139 static GURL GenerateSearchURLUsingTermsData( | 134 static GURL GenerateSearchURLUsingTermsData( |
140 const TemplateURL* t_url, | 135 const TemplateURL* t_url, |
141 const SearchTermsData& search_terms_data); | 136 const SearchTermsData& search_terms_data); |
142 | 137 |
| 138 // Saves enough of url to |prefs| so that it can be loaded from preferences on |
| 139 // start up. |
| 140 void SaveDefaultSearchProviderToPrefs(const TemplateURL* url, |
| 141 PrefService* prefs) const; |
| 142 |
143 // Returns true if there is no TemplateURL that conflicts with the | 143 // Returns true if there is no TemplateURL that conflicts with the |
144 // keyword/url pair, or there is one but it can be replaced. If there is an | 144 // keyword/url pair, or there is one but it can be replaced. If there is an |
145 // existing keyword that can be replaced and template_url_to_replace is | 145 // existing keyword that can be replaced and template_url_to_replace is |
146 // non-NULL, template_url_to_replace is set to the keyword to replace. | 146 // non-NULL, template_url_to_replace is set to the keyword to replace. |
147 // | 147 // |
148 // url gives the url of the search query. The url is used to avoid generating | 148 // url gives the url of the search query. The url is used to avoid generating |
149 // a TemplateURL for an existing TemplateURL that shares the same host. | 149 // a TemplateURL for an existing TemplateURL that shares the same host. |
150 bool CanReplaceKeyword(const base::string16& keyword, | 150 bool CanReplaceKeyword(const base::string16& keyword, |
151 const GURL& url, | 151 const GURL& url, |
152 TemplateURL** template_url_to_replace); | 152 TemplateURL** template_url_to_replace); |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 const base::string16& term); | 402 const base::string16& term); |
403 | 403 |
404 private: | 404 private: |
405 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, TestManagedDefaultSearch); | 405 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, TestManagedDefaultSearch); |
406 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, | 406 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, |
407 UpdateKeywordSearchTermsForURL); | 407 UpdateKeywordSearchTermsForURL); |
408 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, | 408 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, |
409 DontUpdateKeywordSearchForNonReplaceable); | 409 DontUpdateKeywordSearchForNonReplaceable); |
410 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue); | 410 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue); |
411 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders); | 411 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders); |
| 412 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, UniquifyKeyword); |
412 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 413 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
413 CreateSyncDataFromTemplateURL); | 414 IsLocalTemplateURLBetter); |
414 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | |
415 CreateTemplateURLFromSyncData); | |
416 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, UniquifyKeyword); | |
417 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 415 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
418 ResolveSyncKeywordConflict); | 416 ResolveSyncKeywordConflict); |
419 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); | 417 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); |
420 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | |
421 IsLocalTemplateURLBetter); | |
422 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); | 418 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); |
423 | 419 |
424 friend class TemplateURLServiceTestUtilBase; | 420 friend class TemplateURLServiceTestUtilBase; |
425 | 421 |
426 typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; | 422 typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; |
427 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; | 423 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; |
428 | 424 |
429 // Declaration of values to be used in an enumerated histogram to tally | 425 // Declaration of values to be used in an enumerated histogram to tally |
430 // changes to the default search provider from various entry points. In | 426 // changes to the default search provider from various entry points. In |
431 // particular, we use this to see what proportion of changes are from Sync | 427 // particular, we use this to see what proportion of changes are from Sync |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 // cannot add the provided argument, it will delete it and return false. | 557 // cannot add the provided argument, it will delete it and return false. |
562 // Caller is responsible for notifying observers if this function returns | 558 // Caller is responsible for notifying observers if this function returns |
563 // true. | 559 // true. |
564 bool AddNoNotify(TemplateURL* template_url, bool newly_adding); | 560 bool AddNoNotify(TemplateURL* template_url, bool newly_adding); |
565 | 561 |
566 // Removes the keyword from the model. This deletes the supplied TemplateURL. | 562 // Removes the keyword from the model. This deletes the supplied TemplateURL. |
567 // This fails if the supplied template_url is the default search provider. | 563 // This fails if the supplied template_url is the default search provider. |
568 // Caller is responsible for notifying observers. | 564 // Caller is responsible for notifying observers. |
569 void RemoveNoNotify(TemplateURL* template_url); | 565 void RemoveNoNotify(TemplateURL* template_url); |
570 | 566 |
| 567 // Like ResetTemplateURL(), but instead of notifying observers, returns |
| 568 // whether anything has changed. |
| 569 bool ResetTemplateURLNoNotify(TemplateURL* url, |
| 570 const base::string16& title, |
| 571 const base::string16& keyword, |
| 572 const std::string& search_url); |
| 573 |
571 // Notify the observers that the model has changed. This is done only if the | 574 // Notify the observers that the model has changed. This is done only if the |
572 // model is loaded. | 575 // model is loaded. |
573 void NotifyObservers(); | 576 void NotifyObservers(); |
574 | 577 |
575 // Removes from the vector any template URL that was created because of | 578 // Removes from the vector any template URL that was created because of |
576 // policy. These TemplateURLs are freed and removed from the database. | 579 // policy. These TemplateURLs are freed and removed from the database. |
577 // Sets default_search_provider to NULL if it was one of them, unless it is | 580 // Sets default_search_provider to NULL if it was one of them, unless it is |
578 // the same as the current default from preferences and it is managed. | 581 // the same as the current default from preferences and it is managed. |
579 void RemoveProvidersCreatedByPolicy( | 582 void RemoveProvidersCreatedByPolicy( |
580 TemplateURLVector* template_urls, | 583 TemplateURLVector* template_urls, |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 scoped_ptr<SearchHostToURLsMap> provider_map_; | 709 scoped_ptr<SearchHostToURLsMap> provider_map_; |
707 | 710 |
708 // Used to obtain the WebDataService. | 711 // Used to obtain the WebDataService. |
709 // When Load is invoked, if we haven't yet loaded, the WebDataService is | 712 // When Load is invoked, if we haven't yet loaded, the WebDataService is |
710 // obtained from the Profile. This allows us to lazily access the database. | 713 // obtained from the Profile. This allows us to lazily access the database. |
711 Profile* profile_; | 714 Profile* profile_; |
712 | 715 |
713 // Whether the keywords have been loaded. | 716 // Whether the keywords have been loaded. |
714 bool loaded_; | 717 bool loaded_; |
715 | 718 |
716 // Did loading fail? This is only valid if loaded_ is true. | 719 // Set when the web data service fails to load properly. This prevents |
| 720 // further communication with sync or writing to prefs, so we don't persist |
| 721 // inconsistent state data anywhere. |
717 bool load_failed_; | 722 bool load_failed_; |
718 | 723 |
719 // If non-zero, we're waiting on a load. | 724 // If non-zero, we're waiting on a load. |
720 WebDataService::Handle load_handle_; | 725 WebDataService::Handle load_handle_; |
721 | 726 |
722 // Service used to store entries. | 727 // Service used to store entries. |
723 scoped_refptr<WebDataService> service_; | 728 scoped_refptr<WebDataService> service_; |
724 | 729 |
725 // All visits that occurred before we finished loading. Once loaded | 730 // All visits that occurred before we finished loading. Once loaded |
726 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. | 731 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 base::CallbackList<void(void)> on_loaded_callbacks_; | 787 base::CallbackList<void(void)> on_loaded_callbacks_; |
783 | 788 |
784 // Helper class to manage the default search engine. This will be NULL when | 789 // Helper class to manage the default search engine. This will be NULL when |
785 // using the testing-specific constructor. | 790 // using the testing-specific constructor. |
786 scoped_ptr<DefaultSearchManager> default_search_manager_; | 791 scoped_ptr<DefaultSearchManager> default_search_manager_; |
787 | 792 |
788 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 793 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
789 }; | 794 }; |
790 | 795 |
791 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 796 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
OLD | NEW |