Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: chrome/browser/search_engines/template_url_service.h

Issue 263823007: Handle TemplateURLService load failure better (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
420 friend class TemplateURLServiceSyncTest;
424 friend class TemplateURLServiceTestUtilBase; 421 friend class TemplateURLServiceTestUtilBase;
425 422
426 typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; 423 typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap;
427 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; 424 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap;
428 425
429 // Declaration of values to be used in an enumerated histogram to tally 426 // Declaration of values to be used in an enumerated histogram to tally
430 // changes to the default search provider from various entry points. In 427 // 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 428 // particular, we use this to see what proportion of changes are from Sync
432 // entry points, to help spot erroneous Sync activity. 429 // entry points, to help spot erroneous Sync activity.
433 enum DefaultSearchChangeOrigin { 430 enum DefaultSearchChangeOrigin {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 // This does NOT notify the delegate or the database. 466 // This does NOT notify the delegate or the database.
470 // 467 //
471 // This transfers ownership of the elements in |urls| to |this|, and may 468 // This transfers ownership of the elements in |urls| to |this|, and may
472 // delete some elements, so it's not safe for callers to access any elements 469 // delete some elements, so it's not safe for callers to access any elements
473 // after calling; to reinforce this, this function clears |urls| on exit. 470 // after calling; to reinforce this, this function clears |urls| on exit.
474 void SetTemplateURLs(TemplateURLVector* urls); 471 void SetTemplateURLs(TemplateURLVector* urls);
475 472
476 // Transitions to the loaded state. 473 // Transitions to the loaded state.
477 void ChangeToLoadedState(); 474 void ChangeToLoadedState();
478 475
476 // Called when we transition to the loaded state without having loaded the web
477 // database from disk. This may be because of profile corruption, or because
478 // we're in a test. Disables further attempts to access the web database and
479 // ensures we won't attempt to sync any changes, then copies
480 // |initial_default_search_provider_| in as the default search provider, so
481 // the TemplateURLService consistently reports the same default provider
482 // before and after the attempted load.
483 void OnFailedLoad();
484
479 // Clears user preferences describing the default search engine. 485 // Clears user preferences describing the default search engine.
480 void ClearDefaultProviderFromPrefs(); 486 void ClearDefaultProviderFromPrefs();
481 487
482 // Returns true if there is no TemplateURL that has a search url with the 488 // Returns true if there is no TemplateURL that has a search url with the
483 // specified host, or the only TemplateURLs matching the specified host can 489 // specified host, or the only TemplateURLs matching the specified host can
484 // be replaced. 490 // be replaced.
485 bool CanReplaceKeywordForHost(const std::string& host, 491 bool CanReplaceKeywordForHost(const std::string& host,
486 TemplateURL** to_replace); 492 TemplateURL** to_replace);
487 493
488 // Returns true if the TemplateURL is replaceable. This doesn't look at the 494 // Returns true if the TemplateURL is replaceable. This doesn't look at the
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 scoped_ptr<SearchHostToURLsMap> provider_map_; 712 scoped_ptr<SearchHostToURLsMap> provider_map_;
707 713
708 // Used to obtain the WebDataService. 714 // Used to obtain the WebDataService.
709 // When Load is invoked, if we haven't yet loaded, the WebDataService is 715 // 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. 716 // obtained from the Profile. This allows us to lazily access the database.
711 Profile* profile_; 717 Profile* profile_;
712 718
713 // Whether the keywords have been loaded. 719 // Whether the keywords have been loaded.
714 bool loaded_; 720 bool loaded_;
715 721
716 // Did loading fail? This is only valid if loaded_ is true. 722 // Set by OnFailedLoad(); prevents writing to prefs or syncing.
717 bool load_failed_; 723 bool load_failed_;
718 724
719 // If non-zero, we're waiting on a load. 725 // If non-zero, we're waiting on a load.
720 WebDataService::Handle load_handle_; 726 WebDataService::Handle load_handle_;
721 727
722 // Service used to store entries. 728 // Service used to store entries.
723 scoped_refptr<WebDataService> service_; 729 scoped_refptr<WebDataService> service_;
724 730
725 // All visits that occurred before we finished loading. Once loaded 731 // All visits that occurred before we finished loading. Once loaded
726 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. 732 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
782 base::CallbackList<void(void)> on_loaded_callbacks_; 788 base::CallbackList<void(void)> on_loaded_callbacks_;
783 789
784 // Helper class to manage the default search engine. This will be NULL when 790 // Helper class to manage the default search engine. This will be NULL when
785 // using the testing-specific constructor. 791 // using the testing-specific constructor.
786 scoped_ptr<DefaultSearchManager> default_search_manager_; 792 scoped_ptr<DefaultSearchManager> default_search_manager_;
787 793
788 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); 794 DISALLOW_COPY_AND_ASSIGN(TemplateURLService);
789 }; 795 };
790 796
791 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ 797 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698