Chromium Code Reviews| 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 std::string extension_id; | 94 std::string extension_id; |
| 95 std::string extension_name; | 95 std::string extension_name; |
| 96 std::string extension_keyword; | 96 std::string extension_keyword; |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 explicit TemplateURLService(Profile* profile); | 99 explicit TemplateURLService(Profile* profile); |
| 100 // The following is for testing. | 100 // The following is for testing. |
| 101 TemplateURLService(const Initializer* initializers, const int count); | 101 TemplateURLService(const Initializer* initializers, const int count); |
| 102 virtual ~TemplateURLService(); | 102 virtual ~TemplateURLService(); |
| 103 | 103 |
| 104 // Creates a TemplateURLData that was previously saved to |prefs| via | |
| 105 // SaveDefaultSearchProviderToPrefs or set via policy. | |
| 106 // Returns true if successful, false otherwise. | |
| 107 // If the user or the policy has opted for no default search, this | |
| 108 // returns true but default_provider is set to NULL. | |
| 109 // |*is_managed| specifies whether the default is managed via policy. | |
| 110 static bool LoadDefaultSearchProviderFromPrefs( | |
| 111 PrefService* prefs, | |
| 112 scoped_ptr<TemplateURLData>* default_provider_data, | |
| 113 bool* is_managed); | |
| 114 | |
| 115 // Generates a suitable keyword for the specified url, which must be valid. | 104 // Generates a suitable keyword for the specified url, which must be valid. |
| 116 // This is guaranteed not to return an empty string, since TemplateURLs should | 105 // This is guaranteed not to return an empty string, since TemplateURLs should |
| 117 // never have an empty keyword. | 106 // never have an empty keyword. |
| 118 static base::string16 GenerateKeyword(const GURL& url); | 107 static base::string16 GenerateKeyword(const GURL& url); |
| 119 | 108 |
| 120 // Removes any unnecessary characters from a user input keyword. | 109 // Removes any unnecessary characters from a user input keyword. |
| 121 // This removes the leading scheme, "www." and any trailing slash. | 110 // This removes the leading scheme, "www." and any trailing slash. |
| 122 static base::string16 CleanUserInputKeyword(const base::string16& keyword); | 111 static base::string16 CleanUserInputKeyword(const base::string16& keyword); |
| 123 | 112 |
| 124 // Returns the search url for t_url. Returns an empty GURL if t_url has no | 113 // Returns the search url for t_url. Returns an empty GURL if t_url has no |
| 125 // url(). | 114 // url(). |
| 126 // NOTE: |t_url| is non-const in this version because of the need to access | 115 // NOTE: |t_url| is non-const in this version because of the need to access |
| 127 // t_url->profile(). | 116 // t_url->profile(). |
| 128 static GURL GenerateSearchURL(TemplateURL* t_url); | 117 static GURL GenerateSearchURL(TemplateURL* t_url); |
| 129 | 118 |
| 130 // Just like GenerateSearchURL except that it takes SearchTermsData to supply | 119 // Just like GenerateSearchURL except that it takes SearchTermsData to supply |
| 131 // the data for some search terms, e.g. so this can be used on threads other | 120 // the data for some search terms, e.g. so this can be used on threads other |
| 132 // than the UI thread. See the various TemplateURLRef::XXXUsingTermsData() | 121 // than the UI thread. See the various TemplateURLRef::XXXUsingTermsData() |
| 133 // functions. | 122 // functions. |
| 134 static GURL GenerateSearchURLUsingTermsData( | 123 static GURL GenerateSearchURLUsingTermsData( |
| 135 const TemplateURL* t_url, | 124 const TemplateURL* t_url, |
| 136 const SearchTermsData& search_terms_data); | 125 const SearchTermsData& search_terms_data); |
| 137 | 126 |
| 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 | 127 // 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 | 128 // 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 | 129 // 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. | 130 // non-NULL, template_url_to_replace is set to the keyword to replace. |
| 147 // | 131 // |
| 148 // url gives the url of the search query. The url is used to avoid generating | 132 // 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. | 133 // a TemplateURL for an existing TemplateURL that shares the same host. |
| 150 bool CanReplaceKeyword(const base::string16& keyword, | 134 bool CanReplaceKeyword(const base::string16& keyword, |
| 151 const GURL& url, | 135 const GURL& url, |
| 152 TemplateURL** template_url_to_replace); | 136 TemplateURL** template_url_to_replace); |
| 153 | 137 |
| 154 // Returns (in |matches|) all TemplateURLs whose keywords begin with |prefix|, | 138 // Returns (in |matches|) all TemplateURLs whose keywords begin with |prefix|, |
| 155 // sorted shortest keyword-first. If |support_replacement_only| is true, only | 139 // sorted shortest keyword-first. If |support_replacement_only| is true, only |
| 156 // TemplateURLs that support replacement are returned. | 140 // TemplateURLs that support replacement are returned. |
| 157 void FindMatchingKeywords(const base::string16& prefix, | 141 void FindMatchingKeywords(const base::string16& prefix, |
| 158 bool support_replacement_only, | 142 bool support_replacement_only, |
| 159 TemplateURLVector* matches) const; | 143 TemplateURLVector* matches); |
| 160 | 144 |
| 161 // Looks up |keyword| and returns the element it maps to. Returns NULL if | 145 // Looks up |keyword| and returns the element it maps to. Returns NULL if |
| 162 // the keyword was not found. | 146 // the keyword was not found. |
| 163 // The caller should not try to delete the returned pointer; the data store | 147 // The caller should not try to delete the returned pointer; the data store |
| 164 // retains ownership of it. | 148 // retains ownership of it. |
| 165 TemplateURL* GetTemplateURLForKeyword(const base::string16& keyword); | 149 TemplateURL* GetTemplateURLForKeyword(const base::string16& keyword); |
| 166 | 150 |
| 167 // Returns that TemplateURL with the specified GUID, or NULL if not found. | 151 // Returns that TemplateURL with the specified GUID, or NULL if not found. |
| 168 // The caller should not try to delete the returned pointer; the data store | 152 // The caller should not try to delete the returned pointer; the data store |
| 169 // retains ownership of it. | 153 // retains ownership of it. |
| 170 TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid); | 154 TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid); |
| 171 | 155 |
| 172 // Returns the first TemplateURL found with a URL using the specified |host|, | 156 // Returns the first TemplateURL found with a URL using the specified |host|, |
| 173 // or NULL if there are no such TemplateURLs | 157 // or NULL if there are no such TemplateURLs |
| 174 TemplateURL* GetTemplateURLForHost(const std::string& host); | 158 TemplateURL* GetTemplateURLForHost(const std::string& host); |
| 175 | 159 |
| 176 // Takes ownership of |template_url| and adds it to this model. For obvious | 160 // Takes ownership of |template_url| and adds it to this model. For obvious |
| 177 // reasons, it is illegal to Add() the same |template_url| pointer twice. | 161 // reasons, it is illegal to Add() the same |template_url| pointer twice. |
| 178 void Add(TemplateURL* template_url); | 162 // Returns true if the Add is successful. |
| 163 bool Add(TemplateURL* template_url); | |
| 179 | 164 |
| 180 // Like Add(), but overwrites the |template_url|'s values with the provided | 165 // Like Add(), but overwrites the |template_url|'s values with the provided |
| 181 // ones. | 166 // ones. |
| 182 void AddAndSetProfile(TemplateURL* template_url, Profile* profile); | 167 void AddAndSetProfile(TemplateURL* template_url, Profile* profile); |
| 183 void AddWithOverrides(TemplateURL* template_url, | 168 void AddWithOverrides(TemplateURL* template_url, |
| 184 const base::string16& short_name, | 169 const base::string16& short_name, |
| 185 const base::string16& keyword, | 170 const base::string16& keyword, |
| 186 const std::string& url); | 171 const std::string& url); |
| 187 | 172 |
| 188 // Add the search engine of type NORMAL_CONTROLLED_BY_EXTENSION. | 173 // Add the search engine of type NORMAL_CONTROLLED_BY_EXTENSION. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 // loaded, the default search provider is pulled from preferences. | 239 // loaded, the default search provider is pulled from preferences. |
| 255 // | 240 // |
| 256 // NOTE: At least in unittest mode, this may return NULL. | 241 // NOTE: At least in unittest mode, this may return NULL. |
| 257 TemplateURL* GetDefaultSearchProvider(); | 242 TemplateURL* GetDefaultSearchProvider(); |
| 258 | 243 |
| 259 // Returns true if the |url| is a search results page from the default search | 244 // Returns true if the |url| is a search results page from the default search |
| 260 // provider. | 245 // provider. |
| 261 bool IsSearchResultsPageFromDefaultSearchProvider(const GURL& url); | 246 bool IsSearchResultsPageFromDefaultSearchProvider(const GURL& url); |
| 262 | 247 |
| 263 // Returns true if the default search is managed through group policy. | 248 // Returns true if the default search is managed through group policy. |
| 264 bool is_default_search_managed() const { return is_default_search_managed_; } | 249 bool is_default_search_managed() const { |
| 250 return default_search_provider_source_ == DefaultSearchManager::FROM_POLICY; | |
| 251 } | |
| 265 | 252 |
| 266 // Returns true if the default search provider is controlled by an extension. | 253 // Returns true if the default search provider is controlled by an extension. |
| 267 bool IsExtensionControlledDefaultSearch(); | 254 bool IsExtensionControlledDefaultSearch(); |
| 268 | 255 |
| 269 // Returns the default search specified in the prepopulated data, if it | 256 // Returns the default search specified in the prepopulated data, if it |
| 270 // exists. If not, returns first URL in |template_urls_|, or NULL if that's | 257 // exists. If not, returns first URL in |template_urls_|, or NULL if that's |
| 271 // empty. The returned object is owned by TemplateURLService and can be | 258 // empty. The returned object is owned by TemplateURLService and can be |
| 272 // destroyed at any time so should be used right after the call. | 259 // destroyed at any time so should be used right after the call. |
| 273 TemplateURL* FindNewDefaultSearchProvider(); | 260 TemplateURL* FindNewDefaultSearchProvider(); |
| 274 | 261 |
| 275 // Performs the same actions that happen when the prepopulate data version is | 262 // Performs the same actions that happen when the prepopulate data version is |
| 276 // revved: all existing prepopulated entries are checked against the current | 263 // revved: all existing prepopulated entries are checked against the current |
| 277 // prepopulate data, any now-extraneous safe_for_autoreplace() entries are | 264 // prepopulate data, any now-extraneous safe_for_autoreplace() entries are |
| 278 // removed, any existing engines are reset to the provided data (except for | 265 // removed, any existing engines are reset to the provided data (except for |
| 279 // user-edited names or keywords), and any new prepopulated anegines are | 266 // user-edited names or keywords), and any new prepopulated engines are |
| 280 // added. | 267 // added. |
| 281 // | 268 // |
| 282 // After this, the default search engine is reset to the default entry in the | 269 // After this, the default search engine is reset to the default entry in the |
| 283 // prepopulate data. | 270 // prepopulate data. |
| 284 void RepairPrepopulatedSearchEngines(); | 271 void RepairPrepopulatedSearchEngines(); |
| 285 | 272 |
| 286 // Observers used to listen for changes to the model. | 273 // Observers used to listen for changes to the model. |
| 287 // TemplateURLService does NOT delete the observers when deleted. | 274 // TemplateURLService does NOT delete the observers when deleted. |
| 288 void AddObserver(TemplateURLServiceObserver* observer); | 275 void AddObserver(TemplateURLServiceObserver* observer); |
| 289 void RemoveObserver(TemplateURLServiceObserver* observer); | 276 void RemoveObserver(TemplateURLServiceObserver* observer); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 static TemplateURL* CreateTemplateURLFromTemplateURLAndSyncData( | 364 static TemplateURL* CreateTemplateURLFromTemplateURLAndSyncData( |
| 378 Profile* profile, | 365 Profile* profile, |
| 379 TemplateURL* existing_turl, | 366 TemplateURL* existing_turl, |
| 380 const syncer::SyncData& sync_data, | 367 const syncer::SyncData& sync_data, |
| 381 syncer::SyncChangeList* change_list); | 368 syncer::SyncChangeList* change_list); |
| 382 | 369 |
| 383 // Returns a map mapping Sync GUIDs to pointers to syncer::SyncData. | 370 // Returns a map mapping Sync GUIDs to pointers to syncer::SyncData. |
| 384 static SyncDataMap CreateGUIDToSyncDataMap( | 371 static SyncDataMap CreateGUIDToSyncDataMap( |
| 385 const syncer::SyncDataList& sync_data); | 372 const syncer::SyncDataList& sync_data); |
| 386 | 373 |
| 374 // Used to determine if the pre-populated TemplateURLs (this will be true for | |
| 375 // some tests, specifically TemplateURLServiceSyncTests). | |
|
Peter Kasting
2014/05/07 23:38:29
Nit: This is not a complete sentence.
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
| 376 static bool FallbackSearchEnginesDisabled() { | |
|
Peter Kasting
2014/05/07 23:38:29
Nit: This should be named unix_hacker()-style if i
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
| 377 return fallback_search_engines_disabled_; | |
| 378 } | |
| 379 | |
| 380 // Used to disable the pre-populated TemplateURLs for testing purposes. | |
| 381 static void DisableFallbackSearchEngines(); | |
|
Peter Kasting
2014/05/07 23:38:29
The implementation of this is just one line. This
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
| 382 | |
| 387 #if defined(UNIT_TEST) | 383 #if defined(UNIT_TEST) |
| 388 // Set a different time provider function, such as | 384 // Set a different time provider function, such as |
| 389 // base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now. | 385 // base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now. |
| 390 void set_time_provider(TimeProvider* time_provider) { | 386 void set_time_provider(TimeProvider* time_provider) { |
| 391 time_provider_ = time_provider; | 387 time_provider_ = time_provider; |
| 392 } | 388 } |
| 393 #endif | 389 #endif |
| 394 | 390 |
| 395 protected: | 391 protected: |
| 396 // Cover method for the method of the same name on the HistoryService. | 392 // Cover method for the method of the same name on the HistoryService. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 415 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 411 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 416 ResolveSyncKeywordConflict); | 412 ResolveSyncKeywordConflict); |
| 417 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); | 413 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); |
| 418 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); | 414 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); |
| 419 | 415 |
| 420 friend class TemplateURLServiceTestUtilBase; | 416 friend class TemplateURLServiceTestUtilBase; |
| 421 | 417 |
| 422 typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; | 418 typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; |
| 423 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; | 419 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; |
| 424 | 420 |
| 425 // Declaration of values to be used in an enumerated histogram to tally | |
| 426 // changes to the default search provider from various entry points. In | |
| 427 // particular, we use this to see what proportion of changes are from Sync | |
| 428 // entry points, to help spot erroneous Sync activity. | |
| 429 enum DefaultSearchChangeOrigin { | |
| 430 // Various known Sync entry points. | |
| 431 DSP_CHANGE_SYNC_PREF, | |
| 432 DSP_CHANGE_SYNC_ADD, | |
| 433 DSP_CHANGE_SYNC_DELETE, | |
| 434 DSP_CHANGE_SYNC_NOT_MANAGED, | |
| 435 // "Other" origins. We differentiate between Sync and not Sync so we know if | |
| 436 // certain changes were intentionally from the system, or possibly some | |
| 437 // unintentional change from when we were Syncing. | |
| 438 DSP_CHANGE_SYNC_UNINTENTIONAL, | |
| 439 // All changes that don't fall into another category; we can't reorder the | |
| 440 // list for clarity as this would screw up stat collection. | |
| 441 DSP_CHANGE_OTHER, | |
| 442 // Changed through "Profile Reset" feature. | |
| 443 DSP_CHANGE_PROFILE_RESET, | |
| 444 // Changed by an extension through the Override Settings API. | |
| 445 DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION, | |
| 446 // New DSP during database/prepopulate data load, which was not previously | |
| 447 // in the known engine set, and with no previous value in prefs. The | |
| 448 // typical time to see this is during first run. | |
| 449 DSP_CHANGE_NEW_ENGINE_NO_PREFS, | |
| 450 // Boundary value. | |
| 451 DSP_CHANGE_MAX, | |
| 452 }; | |
| 453 | |
| 454 // Helper functor for FindMatchingKeywords(), for finding the range of | 421 // Helper functor for FindMatchingKeywords(), for finding the range of |
| 455 // keywords which begin with a prefix. | 422 // keywords which begin with a prefix. |
| 456 class LessWithPrefix; | 423 class LessWithPrefix; |
| 457 | 424 |
| 458 void Init(const Initializer* initializers, int num_initializers); | 425 void Init(const Initializer* initializers, int num_initializers); |
| 459 | 426 |
| 460 void RemoveFromMaps(TemplateURL* template_url); | 427 void RemoveFromMaps(TemplateURL* template_url); |
| 461 | 428 |
| 462 void AddToMaps(TemplateURL* template_url); | 429 void AddToMaps(TemplateURL* template_url); |
| 463 | 430 |
| 464 // Sets the keywords. This is used once the keywords have been loaded. | 431 // Sets the keywords. This is used once the keywords have been loaded. |
| 465 // This does NOT notify the delegate or the database. | 432 // This does NOT notify the delegate or the database. |
| 466 // | 433 // |
| 467 // This transfers ownership of the elements in |urls| to |this|, and may | 434 // This transfers ownership of the elements in |urls| to |this|, and may |
| 468 // delete some elements, so it's not safe for callers to access any elements | 435 // delete some elements, so it's not safe for callers to access any elements |
| 469 // after calling; to reinforce this, this function clears |urls| on exit. | 436 // after calling; to reinforce this, this function clears |urls| on exit. |
| 470 void SetTemplateURLs(TemplateURLVector* urls); | 437 void SetTemplateURLs(TemplateURLVector* urls); |
| 471 | 438 |
| 472 // Transitions to the loaded state. | 439 // Transitions to the loaded state. |
| 473 void ChangeToLoadedState(); | 440 void ChangeToLoadedState(); |
| 474 | 441 |
| 475 // Clears user preferences describing the default search engine. | 442 // Called when we transition to the loaded state without having loaded the web |
| 476 void ClearDefaultProviderFromPrefs(); | 443 // database from disk. This may be because of profile corruption, or because |
| 444 // we're in a test. Disables further attempts to access the web database and | |
| 445 // ensures we won't attempt to sync any changes, then copies | |
| 446 // |initial_default_search_provider_| in as the default search provider, so | |
| 447 // the TemplateURLService consistently reports the same default provider | |
| 448 // before and after the attempted load. | |
|
Peter Kasting
2014/05/07 23:38:29
This commentary looks to be from an out-of-date ve
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
| 449 void OnFailedLoad(); | |
| 450 | |
| 451 // Receives notice that the DSE has changed. | |
|
Peter Kasting
2014/05/07 23:38:29
Receives from where? Prefer to phrase such things
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
| 452 void OnDefaultSearchChange(const TemplateURLData* new_dse_data, | |
| 453 DefaultSearchManager::Source source); | |
| 454 | |
| 455 // Applies a DSE change. May be called at startup or after transitioning to | |
| 456 // loaded state. | |
|
Peter Kasting
2014/05/07 23:38:29
Nit: loaded -> the loaded
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
| 457 void ApplyDefaultSearchChange(const TemplateURLData* new_dse_data, | |
| 458 DefaultSearchManager::Source source); | |
| 459 | |
| 460 // Receives notice that the DSE GUID in prefs has changed. | |
| 461 void OnDSEGUIDPrefChanged(); | |
| 477 | 462 |
| 478 // Returns true if there is no TemplateURL that has a search url with the | 463 // Returns true if there is no TemplateURL that has a search url with the |
| 479 // specified host, or the only TemplateURLs matching the specified host can | 464 // specified host, or the only TemplateURLs matching the specified host can |
| 480 // be replaced. | 465 // be replaced. |
| 481 bool CanReplaceKeywordForHost(const std::string& host, | 466 bool CanReplaceKeywordForHost(const std::string& host, |
| 482 TemplateURL** to_replace); | 467 TemplateURL** to_replace); |
| 483 | 468 |
| 484 // Returns true if the TemplateURL is replaceable. This doesn't look at the | 469 // Returns true if the TemplateURL is replaceable. This doesn't look at the |
| 485 // uniqueness of the keyword or host and is intended to be called after those | 470 // uniqueness of the keyword or host and is intended to be called after those |
| 486 // checks have been done. This returns true if the TemplateURL doesn't appear | 471 // checks have been done. This returns true if the TemplateURL doesn't appear |
| 487 // in the default list and is marked as safe_for_autoreplace. | 472 // in the default list and is marked as safe_for_autoreplace. |
| 488 bool CanReplace(const TemplateURL* t_url); | 473 bool CanReplace(const TemplateURL* t_url); |
| 489 | 474 |
| 490 // Like GetTemplateURLForKeyword(), but ignores extension-provided keywords. | 475 // Like GetTemplateURLForKeyword(), but ignores extension-provided keywords. |
| 491 TemplateURL* FindNonExtensionTemplateURLForKeyword( | 476 TemplateURL* FindNonExtensionTemplateURLForKeyword( |
| 492 const base::string16& keyword); | 477 const base::string16& keyword); |
| 493 | 478 |
| 494 // Updates the information in |existing_turl| using the information from | 479 // Updates the information in |existing_turl| using the information from |
| 495 // |new_values|, but the ID for |existing_turl| is retained. Notifying | 480 // |new_values|, but the ID for |existing_turl| is retained. Notifying |
| 496 // observers is the responsibility of the caller. Returns whether | 481 // observers is the responsibility of the caller. Returns whether |
| 497 // |existing_turl| was found in |template_urls_| and thus could be updated. | 482 // |existing_turl| was found in |template_urls_| and thus could be updated. |
| 498 // |old_search_terms_data| is passed to SearchHostToURLsMap::Remove(). | 483 // |old_search_terms_data| is passed to SearchHostToURLsMap::Remove(). |
| 499 // | 484 // |
| 500 // NOTE: This should not be called with an extension keyword as there are no | 485 // NOTE: This should not be called with an extension keyword as there are no |
| 501 // updates needed in that case. | 486 // updates needed in that case. |
| 502 bool UpdateNoNotify(TemplateURL* existing_turl, | 487 bool UpdateNoNotify(TemplateURL* existing_turl, |
| 503 const TemplateURL& new_values, | 488 const TemplateURLData& new_values, |
| 504 const SearchTermsData& old_search_terms_data); | 489 const SearchTermsData& old_search_terms_data); |
| 505 | 490 |
| 506 // If the TemplateURL comes from a prepopulated URL available in the current | 491 // If the TemplateURL comes from a prepopulated URL available in the current |
| 507 // country, update all its fields save for the keyword, short name and id so | 492 // country, update all its fields save for the keyword, short name and id so |
| 508 // that they match the internal prepopulated URL. TemplateURLs not coming from | 493 // that they match the internal prepopulated URL. TemplateURLs not coming from |
| 509 // a prepopulated URL are not modified. | 494 // a prepopulated URL are not modified. |
| 510 static void UpdateTemplateURLIfPrepopulated(TemplateURL* existing_turl, | 495 static void UpdateTemplateURLIfPrepopulated(TemplateURL* existing_turl, |
| 511 Profile* profile); | 496 Profile* profile); |
| 512 | 497 |
| 498 // If the TemplateURL's sync GUID matches the kSyncedDefaultSearchProviderGUID | |
| 499 // preference it will be used to update the DSE in memory and as persisted in | |
| 500 // preferences. | |
| 501 void MaybeUpdateDSEAfterSync(TemplateURL* synced_turl); | |
| 502 | |
| 513 // Returns the preferences we use. | 503 // Returns the preferences we use. |
| 514 PrefService* GetPrefs(); | 504 PrefService* GetPrefs(); |
| 515 | 505 |
| 516 // Iterates through the TemplateURLs to see if one matches the visited url. | 506 // Iterates through the TemplateURLs to see if one matches the visited url. |
| 517 // For each TemplateURL whose url matches the visited url | 507 // For each TemplateURL whose url matches the visited url |
| 518 // SetKeywordSearchTermsForURL is invoked. | 508 // SetKeywordSearchTermsForURL is invoked. |
| 519 void UpdateKeywordSearchTermsForURL( | 509 void UpdateKeywordSearchTermsForURL( |
| 520 const history::URLVisitedDetails& details); | 510 const history::URLVisitedDetails& details); |
| 521 | 511 |
| 522 // If necessary, generates a visit for the site http:// + t_url.keyword(). | 512 // If necessary, generates a visit for the site http:// + t_url.keyword(). |
| 523 void AddTabToSearchVisit(const TemplateURL& t_url); | 513 void AddTabToSearchVisit(const TemplateURL& t_url); |
| 524 | 514 |
| 525 // Invoked when the Google base URL has changed. Updates the mapping for all | 515 // Invoked when the Google base URL has changed. Updates the mapping for all |
| 526 // TemplateURLs that have a replacement term of {google:baseURL} or | 516 // TemplateURLs that have a replacement term of {google:baseURL} or |
| 527 // {google:baseSuggestURL}. | 517 // {google:baseSuggestURL}. |
| 528 void GoogleBaseURLChanged(const GURL& old_base_url); | 518 void GoogleBaseURLChanged(const GURL& old_base_url); |
| 529 | 519 |
| 530 // Update the default search. Called at initialization or when a managed | |
| 531 // preference has changed. | |
| 532 void UpdateDefaultSearch(); | |
| 533 | |
| 534 // Set the default search provider. |url| may be user-selected or | |
| 535 // automatically selected and may be null. | |
| 536 // This will assert if the default search is managed. | |
| 537 void SetDefaultSearchProvider(TemplateURL* url); | |
| 538 | |
| 539 // Set the default search provider even if it is managed. |url| may be null. | |
| 540 // Caller is responsible for notifying observers. Returns whether |url| was | |
| 541 // found in |template_urls_|. | |
| 542 // If |url| is an extension-controlled search engine then preferences and the | |
| 543 // database are left untouched. | |
| 544 // If |url| is a normal search engine and the existing default search engine | |
| 545 // is controlled by an extension then |url| is propagated to the database and | |
| 546 // prefs but the extension-controlled default engine will continue to hide | |
| 547 // this value until the extension is uninstalled. | |
| 548 bool SetDefaultSearchProviderNoNotify(TemplateURL* url); | |
| 549 | |
| 550 // Adds a new TemplateURL to this model. TemplateURLService will own the | 520 // Adds a new TemplateURL to this model. TemplateURLService will own the |
| 551 // reference, and delete it when the TemplateURL is removed. | 521 // reference, and delete it when the TemplateURL is removed. |
| 552 // If |newly_adding| is false, we assume that this TemplateURL was already | 522 // If |newly_adding| is false, we assume that this TemplateURL was already |
| 553 // part of the model in the past, and therefore we don't need to do things | 523 // part of the model in the past, and therefore we don't need to do things |
| 554 // like assign it an ID or notify sync. | 524 // like assign it an ID or notify sync. |
| 555 // This function guarantees that on return the model will not have two | 525 // This function guarantees that on return the model will not have two |
| 556 // non-extension TemplateURLs with the same keyword. If that means that it | 526 // non-extension TemplateURLs with the same keyword. If that means that it |
| 557 // cannot add the provided argument, it will delete it and return false. | 527 // cannot add the provided argument, it will delete it and return false. |
| 558 // Caller is responsible for notifying observers if this function returns | 528 // Caller is responsible for notifying observers if this function returns |
| 559 // true. | 529 // true. |
| 560 bool AddNoNotify(TemplateURL* template_url, bool newly_adding); | 530 bool AddNoNotify(TemplateURL* template_url, bool newly_adding); |
| 561 | 531 |
| 562 // Removes the keyword from the model. This deletes the supplied TemplateURL. | 532 // Removes the keyword from the model. This deletes the supplied TemplateURL. |
| 563 // This fails if the supplied template_url is the default search provider. | 533 // This fails if the supplied template_url is the default search provider. |
| 564 // Caller is responsible for notifying observers. | 534 // Caller is responsible for notifying observers. |
| 565 void RemoveNoNotify(TemplateURL* template_url); | 535 void RemoveNoNotify(TemplateURL* template_url); |
| 566 | 536 |
| 567 // Notify the observers that the model has changed. This is done only if the | 537 // Notify the observers that the model has changed. This is done only if the |
| 568 // model is loaded. | 538 // model is loaded. |
| 569 void NotifyObservers(); | 539 void NotifyObservers(); |
| 570 | 540 |
| 571 // Removes from the vector any template URL that was created because of | 541 // Updates |template_urls| so that the only "created by policy" entry is |
| 572 // policy. These TemplateURLs are freed and removed from the database. | 542 // |default_from_prefs|. |default_from_prefs| may be NULL if there is no |
| 573 // Sets default_search_provider to NULL if it was one of them, unless it is | 543 // policy-defined DSE in effect. |
| 574 // the same as the current default from preferences and it is managed. | 544 void UpdateProvidersCreatedByPolicy( |
| 575 void RemoveProvidersCreatedByPolicy( | |
| 576 TemplateURLVector* template_urls, | 545 TemplateURLVector* template_urls, |
| 577 TemplateURL** default_search_provider, | 546 const TemplateURLData* default_from_prefs); |
| 578 TemplateURLData* default_from_prefs); | |
| 579 | 547 |
| 580 // Resets the sync GUID of the specified TemplateURL and persists the change | 548 // Resets the sync GUID of the specified TemplateURL and persists the change |
| 581 // to the database. This does not notify observers. | 549 // to the database. This does not notify observers. |
| 582 void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid); | 550 void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid); |
| 583 | 551 |
| 584 // Attempts to generate a unique keyword for |turl| based on its original | 552 // Attempts to generate a unique keyword for |turl| based on its original |
| 585 // keyword. If its keyword is already unique, that is returned. Otherwise, it | 553 // keyword. If its keyword is already unique, that is returned. Otherwise, it |
| 586 // tries to return the autogenerated keyword if that is unique to the Service, | 554 // tries to return the autogenerated keyword if that is unique to the Service, |
| 587 // and finally it repeatedly appends special characters to the keyword until | 555 // and finally it repeatedly appends special characters to the keyword until |
| 588 // it is unique to the Service. If |force| is true, then this will only | 556 // it is unique to the Service. If |force| is true, then this will only |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 626 // sent up to Sync. | 594 // sent up to Sync. |
| 627 // |merge_result| tracks the changes made to the local model. Added/modified/ | 595 // |merge_result| tracks the changes made to the local model. Added/modified/ |
| 628 // deleted are updated depending on how the |sync_turl| is merged in. | 596 // deleted are updated depending on how the |sync_turl| is merged in. |
| 629 // This should only be called from MergeDataAndStartSyncing. | 597 // This should only be called from MergeDataAndStartSyncing. |
| 630 void MergeInSyncTemplateURL(TemplateURL* sync_turl, | 598 void MergeInSyncTemplateURL(TemplateURL* sync_turl, |
| 631 const SyncDataMap& sync_data, | 599 const SyncDataMap& sync_data, |
| 632 syncer::SyncChangeList* change_list, | 600 syncer::SyncChangeList* change_list, |
| 633 SyncDataMap* local_data, | 601 SyncDataMap* local_data, |
| 634 syncer::SyncMergeResult* merge_result); | 602 syncer::SyncMergeResult* merge_result); |
| 635 | 603 |
| 636 // Checks a newly added TemplateURL from Sync by its sync_guid and sets it as | |
| 637 // the default search provider if we were waiting for it. | |
| 638 void SetDefaultSearchProviderIfNewlySynced(const std::string& guid); | |
| 639 | |
| 640 // Retrieve the pending default search provider according to Sync. Returns | |
| 641 // NULL if there was no pending search provider from Sync. | |
| 642 TemplateURL* GetPendingSyncedDefaultSearchProvider(); | |
| 643 | |
| 644 // Goes through a vector of TemplateURLs and ensure that both the in-memory | 604 // Goes through a vector of TemplateURLs and ensure that both the in-memory |
| 645 // and database copies have valid sync_guids. This is to fix crbug.com/102038, | 605 // and database copies have valid sync_guids. This is to fix crbug.com/102038, |
| 646 // where old entries were being pushed to Sync without a sync_guid. | 606 // where old entries were being pushed to Sync without a sync_guid. |
| 647 void PatchMissingSyncGUIDs(TemplateURLVector* template_urls); | 607 void PatchMissingSyncGUIDs(TemplateURLVector* template_urls); |
| 648 | 608 |
| 649 void OnSyncedDefaultSearchProviderGUIDChanged(); | 609 // Adds |template_urls| to |template_urls_|. |
| 650 | |
| 651 // Adds |template_urls| to |template_urls_| and sets up the default search | |
| 652 // provider. If |default_search_provider| is non-NULL, it must refer to one | |
| 653 // of the |template_urls|, and will be used as the new default. | |
| 654 // | 610 // |
| 655 // This transfers ownership of the elements in |template_urls| to |this|, and | 611 // This transfers ownership of the elements in |template_urls| to |this|, and |
| 656 // may delete some elements, so it's not safe for callers to access any | 612 // may delete some elements, so it's not safe for callers to access any |
| 657 // elements after calling; to reinforce this, this function clears | 613 // elements after calling; to reinforce this, this function clears |
| 658 // |template_urls| on exit. | 614 // |template_urls| on exit. |
| 659 void AddTemplateURLsAndSetupDefaultEngine( | 615 void AddTemplateURLs(TemplateURLVector* template_urls); |
| 660 TemplateURLVector* template_urls, | |
| 661 TemplateURL* default_search_provider); | |
| 662 | |
| 663 // If there is no current default search provider, sets the default to the | |
| 664 // result of calling FindNewDefaultSearchProvider(). | |
| 665 void EnsureDefaultSearchProviderExists(); | |
| 666 | 616 |
| 667 // Returns a new TemplateURL for the given extension. | 617 // Returns a new TemplateURL for the given extension. |
| 668 TemplateURL* CreateTemplateURLForExtension( | 618 TemplateURL* CreateTemplateURLForExtension( |
| 669 const ExtensionKeyword& extension_keyword) const; | 619 const ExtensionKeyword& extension_keyword); |
| 620 | |
| 621 // Returns the TemplateURL corresponding to |prepopulated_id|, if any. | |
| 622 TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id); | |
| 670 | 623 |
| 671 // Returns the TemplateURL associated with |extension_id|, if any. | 624 // Returns the TemplateURL associated with |extension_id|, if any. |
| 672 TemplateURL* FindTemplateURLForExtension(const std::string& extension_id, | 625 TemplateURL* FindTemplateURLForExtension(const std::string& extension_id, |
| 673 TemplateURL::Type type) const; | 626 TemplateURL::Type type); |
| 627 | |
| 628 // Finds the extension-supplied TemplateURL that matches |data|, if any. | |
| 629 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, | |
| 630 TemplateURL::Type type); | |
| 674 | 631 |
| 675 // Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine | 632 // Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine |
| 676 // that supports replacement and wants to be default, if any. | 633 // that supports replacement and wants to be default, if any. Notifies the |
| 677 TemplateURL* FindExtensionDefaultSearchEngine() const; | 634 // DefaultSearchManager, which might change the effective default search |
| 678 | 635 // engine. |
| 679 // Sets the default search provider to: | 636 void UpdateExtensionDefaultSearchEngine(); |
| 680 // (1) BestDefaultExtensionControlledTURL(), if any; or, | |
| 681 // (2) LoadDefaultSearchProviderFromPrefs(), if we have a TURL with that ID; | |
| 682 // or, | |
| 683 // (3) FindNewDefaultSearchProvider(). | |
| 684 void SetDefaultSearchProviderAfterRemovingDefaultExtension(); | |
| 685 | 637 |
| 686 content::NotificationRegistrar notification_registrar_; | 638 content::NotificationRegistrar notification_registrar_; |
| 687 PrefChangeRegistrar pref_change_registrar_; | |
| 688 | 639 |
| 689 // Mapping from keyword to the TemplateURL. | 640 // Mapping from keyword to the TemplateURL. |
| 690 KeywordToTemplateMap keyword_to_template_map_; | 641 KeywordToTemplateMap keyword_to_template_map_; |
| 691 | 642 |
| 692 // Mapping from Sync GUIDs to the TemplateURL. | 643 // Mapping from Sync GUIDs to the TemplateURL. |
| 693 GUIDToTemplateMap guid_to_template_map_; | 644 GUIDToTemplateMap guid_to_template_map_; |
| 694 | 645 |
| 695 TemplateURLVector template_urls_; | 646 TemplateURLVector template_urls_; |
| 696 | 647 |
| 697 ObserverList<TemplateURLServiceObserver> model_observers_; | 648 ObserverList<TemplateURLServiceObserver> model_observers_; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 718 WebDataService::Handle load_handle_; | 669 WebDataService::Handle load_handle_; |
| 719 | 670 |
| 720 // Service used to store entries. | 671 // Service used to store entries. |
| 721 scoped_refptr<WebDataService> service_; | 672 scoped_refptr<WebDataService> service_; |
| 722 | 673 |
| 723 // All visits that occurred before we finished loading. Once loaded | 674 // All visits that occurred before we finished loading. Once loaded |
| 724 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. | 675 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. |
| 725 std::vector<history::URLVisitedDetails> visits_to_add_; | 676 std::vector<history::URLVisitedDetails> visits_to_add_; |
| 726 | 677 |
| 727 // Once loaded, the default search provider. This is a pointer to a | 678 // Once loaded, the default search provider. This is a pointer to a |
| 728 // TemplateURL owned by template_urls_. | 679 // TemplateURL owned by |template_urls_|. |
| 729 TemplateURL* default_search_provider_; | 680 TemplateURL* default_search_provider_; |
| 730 | 681 |
| 731 // The initial search provider extracted from preferences. This is only valid | 682 // A temporary location for the DSE until Web Data has been loaded and it can |
| 732 // if we haven't been loaded or loading failed. | 683 // be merged into |template_urls_|. |
| 733 scoped_ptr<TemplateURL> initial_default_search_provider_; | 684 scoped_ptr<TemplateURL> initial_default_search_provider_; |
| 734 | 685 |
| 735 // Whether the default search is managed via policy. | 686 // Source of the default search provider. |
| 736 bool is_default_search_managed_; | 687 DefaultSearchManager::Source default_search_provider_source_; |
| 737 | 688 |
| 738 // ID assigned to next TemplateURL added to this model. This is an ever | 689 // ID assigned to next TemplateURL added to this model. This is an ever |
| 739 // increasing integer that is initialized from the database. | 690 // increasing integer that is initialized from the database. |
| 740 TemplateURLID next_id_; | 691 TemplateURLID next_id_; |
| 741 | 692 |
| 742 // Function returning current time in base::Time units. | 693 // Function returning current time in base::Time units. |
| 743 TimeProvider* time_provider_; | 694 TimeProvider* time_provider_; |
| 744 | 695 |
| 745 // Do we have an active association between the TemplateURLs and sync models? | 696 // Do we have an active association between the TemplateURLs and sync models? |
| 746 // Set in MergeDataAndStartSyncing, reset in StopSyncing. While this is not | 697 // Set in MergeDataAndStartSyncing, reset in StopSyncing. While this is not |
| 747 // set, we ignore any local search engine changes (when we start syncing we | 698 // set, we ignore any local search engine changes (when we start syncing we |
| 748 // will look up the most recent values anyways). | 699 // will look up the most recent values anyways). |
| 749 bool models_associated_; | 700 bool models_associated_; |
| 750 | 701 |
| 751 // Whether we're currently processing changes from the syncer. While this is | 702 // Whether we're currently processing changes from the syncer. While this is |
| 752 // true, we ignore any local search engine changes, since we triggered them. | 703 // true, we ignore any local search engine changes, since we triggered them. |
| 753 bool processing_syncer_changes_; | 704 bool processing_syncer_changes_; |
| 754 | 705 |
| 755 // Sync's syncer::SyncChange handler. We push all our changes through this. | 706 // Sync's syncer::SyncChange handler. We push all our changes through this. |
| 756 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 707 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
| 757 | 708 |
| 758 // Sync's error handler. We use it to create a sync error. | 709 // Sync's error handler. We use it to create a sync error. |
| 759 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; | 710 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; |
| 760 | 711 |
| 761 // Whether or not we are waiting on the default search provider to come in | |
| 762 // from Sync. This is to facilitate the fact that changes to the value of | |
| 763 // prefs::kSyncedDefaultSearchProviderGUID do not always come before the | |
| 764 // TemplateURL entry it refers to, and to handle the case when we want to use | |
| 765 // the Synced default when the default search provider becomes unmanaged. | |
| 766 bool pending_synced_default_search_; | |
| 767 | |
| 768 // A set of sync GUIDs denoting TemplateURLs that have been removed from this | 712 // A set of sync GUIDs denoting TemplateURLs that have been removed from this |
| 769 // model or the underlying WebDataService prior to MergeDataAndStartSyncing. | 713 // model or the underlying WebDataService prior to MergeDataAndStartSyncing. |
| 770 // This set is used to determine what entries from the server we want to | 714 // This set is used to determine what entries from the server we want to |
| 771 // ignore locally and return a delete command for. | 715 // ignore locally and return a delete command for. |
| 772 std::set<std::string> pre_sync_deletes_; | 716 std::set<std::string> pre_sync_deletes_; |
| 773 | 717 |
| 774 // This is used to log the origin of changes to the default search provider. | |
| 775 // We set this value to increasingly specific values when we know what is the | |
| 776 // cause/origin of a default search change. | |
| 777 DefaultSearchChangeOrigin dsp_change_origin_; | |
| 778 | |
| 779 // Stores a list of callbacks to be run after TemplateURLService has loaded. | 718 // Stores a list of callbacks to be run after TemplateURLService has loaded. |
| 780 base::CallbackList<void(void)> on_loaded_callbacks_; | 719 base::CallbackList<void(void)> on_loaded_callbacks_; |
| 781 | 720 |
| 782 // Helper class to manage the default search engine. This will be NULL when | 721 // Helper class to manage the default search engine. |
| 783 // using the testing-specific constructor. | 722 DefaultSearchManager default_search_manager_; |
| 784 scoped_ptr<DefaultSearchManager> default_search_manager_; | 723 |
| 724 // Used to observe the DSE GUID in prefs. | |
| 725 PrefChangeRegistrar pref_change_registrar_; | |
| 726 | |
| 727 // Used to disable the prepopulated search engines in tests. | |
| 728 static bool fallback_search_engines_disabled_; | |
|
Peter Kasting
2014/05/07 23:38:29
Nit: This name shouldn't have a trailing '_' as it
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
| 785 | 729 |
| 786 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 730 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
| 787 }; | 731 }; |
| 788 | 732 |
| 789 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 733 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
| OLD | NEW |