Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 5 #ifndef COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
| 6 #define COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 6 #define COMPONENTS_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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 // the Load method. | 65 // the Load method. |
| 66 // | 66 // |
| 67 // TemplateURLService takes ownership of any TemplateURL passed to it. If there | 67 // TemplateURLService takes ownership of any TemplateURL passed to it. If there |
| 68 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService, | 68 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService, |
| 69 // otherwise TemplateURLService handles deletion. | 69 // otherwise TemplateURLService handles deletion. |
| 70 | 70 |
| 71 class TemplateURLService : public WebDataServiceConsumer, | 71 class TemplateURLService : public WebDataServiceConsumer, |
| 72 public KeyedService, | 72 public KeyedService, |
| 73 public syncer::SyncableService { | 73 public syncer::SyncableService { |
| 74 public: | 74 public: |
| 75 typedef std::map<std::string, std::string> QueryTerms; | 75 using QueryTerms = std::map<std::string, std::string>; |
| 76 typedef std::vector<TemplateURL*> TemplateURLVector; | 76 using TemplateURLVector = std::vector<TemplateURL*>; |
| 77 typedef std::map<std::string, syncer::SyncData> SyncDataMap; | 77 using SyncDataMap = std::map<std::string, syncer::SyncData>; |
| 78 typedef base::CallbackList<void(void)>::Subscription Subscription; | 78 using Subscription = base::CallbackList<void(void)>::Subscription; |
| 79 | |
| 80 // We may want to treat the keyword in a TemplateURL as being a different | |
| 81 // length than it actually is. For example, for keywords that end in a | |
| 82 // registry, e.g., '.com', we want to consider the registry characters not a | |
|
Peter Kasting
2015/11/12 20:36:11
Nit: not -> as not
Mark P
2015/11/12 21:42:49
Done.
| |
| 83 // meaningful part of the keyword not penalize for the user not typing those.) | |
|
Peter Kasting
2015/11/12 20:36:11
Nit: not penalize -> and not penalize
Mark P
2015/11/12 21:42:49
Done.
| |
| 84 using TURLAndMeaningfulLength = std::pair<TemplateURL*, size_t>; | |
| 85 using TURLsAndMeaningfulLengths = std::vector<TURLAndMeaningfulLength>; | |
| 79 | 86 |
| 80 // Struct used for initializing the data store with fake data. | 87 // Struct used for initializing the data store with fake data. |
| 81 // Each initializer is mapped to a TemplateURL. | 88 // Each initializer is mapped to a TemplateURL. |
| 82 struct Initializer { | 89 struct Initializer { |
| 83 const char* const keyword; | 90 const char* const keyword; |
| 84 const char* const url; | 91 const char* const url; |
| 85 const char* const content; | 92 const char* const content; |
| 86 }; | 93 }; |
| 87 | 94 |
| 88 struct URLVisitedDetails { | 95 struct URLVisitedDetails { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 113 // keyword/url pair, or there is one but it can be replaced. If there is an | 120 // keyword/url pair, or there is one but it can be replaced. If there is an |
| 114 // existing keyword that can be replaced and template_url_to_replace is | 121 // existing keyword that can be replaced and template_url_to_replace is |
| 115 // non-NULL, template_url_to_replace is set to the keyword to replace. | 122 // non-NULL, template_url_to_replace is set to the keyword to replace. |
| 116 // | 123 // |
| 117 // |url| is the URL of the search query. This is used to prevent auto-adding | 124 // |url| is the URL of the search query. This is used to prevent auto-adding |
| 118 // a keyword for hosts already associated with a manually-edited keyword. | 125 // a keyword for hosts already associated with a manually-edited keyword. |
| 119 bool CanAddAutogeneratedKeyword(const base::string16& keyword, | 126 bool CanAddAutogeneratedKeyword(const base::string16& keyword, |
| 120 const GURL& url, | 127 const GURL& url, |
| 121 TemplateURL** template_url_to_replace); | 128 TemplateURL** template_url_to_replace); |
| 122 | 129 |
| 123 // Returns (in |matches|) all TemplateURLs whose keywords begin with |prefix|, | 130 // Adds to |matches| all TemplateURLs whose keywords begin with |prefix|, |
| 124 // sorted shortest keyword-first. If |support_replacement_only| is true, only | 131 // sorted shortest-keyword-first. If |supports_replacement_only| is true, only |
| 125 // TemplateURLs that support replacement are returned. | 132 // TemplateURLs that support replacement are returned. |
| 126 void FindMatchingKeywords(const base::string16& prefix, | 133 void AddMatchingKeywords(const base::string16& prefix, |
| 127 bool support_replacement_only, | 134 bool supports_replacement_only, |
| 128 TemplateURLVector* matches); | 135 TURLsAndMeaningfulLengths* matches); |
| 136 | |
| 137 // Adds to |matches| all TemplateURLs for search engines with the domain | |
| 138 // name part of the keyword starts with |prefix|, sorted | |
| 139 // shortest-domain-name-first. If |supports_replacement_only| is true, only | |
| 140 // TemplateURLs that support replacement are returned. Does not bother | |
| 141 // searching/returning keywords that would've been found with an identical | |
| 142 // call to FindMatchingKeywords(); i.e., doesn't search keywords for which | |
| 143 // the domain name is the keyword. | |
| 144 void AddMatchingDomainKeywords(const base::string16& prefix, | |
| 145 bool supports_replacement_only, | |
| 146 TURLsAndMeaningfulLengths* matches); | |
| 129 | 147 |
| 130 // Looks up |keyword| and returns the element it maps to. Returns NULL if | 148 // Looks up |keyword| and returns the element it maps to. Returns NULL if |
| 131 // the keyword was not found. | 149 // the keyword was not found. |
| 132 // The caller should not try to delete the returned pointer; the data store | 150 // The caller should not try to delete the returned pointer; the data store |
| 133 // retains ownership of it. | 151 // retains ownership of it. |
| 134 TemplateURL* GetTemplateURLForKeyword(const base::string16& keyword); | 152 TemplateURL* GetTemplateURLForKeyword(const base::string16& keyword); |
| 135 | 153 |
| 136 // Returns that TemplateURL with the specified GUID, or NULL if not found. | 154 // Returns that TemplateURL with the specified GUID, or NULL if not found. |
| 137 // The caller should not try to delete the returned pointer; the data store | 155 // The caller should not try to delete the returned pointer; the data store |
| 138 // retains ownership of it. | 156 // retains ownership of it. |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 393 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 376 IsLocalTemplateURLBetter); | 394 IsLocalTemplateURLBetter); |
| 377 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 395 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 378 ResolveSyncKeywordConflict); | 396 ResolveSyncKeywordConflict); |
| 379 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); | 397 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); |
| 380 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); | 398 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); |
| 381 | 399 |
| 382 friend class InstantUnitTestBase; | 400 friend class InstantUnitTestBase; |
| 383 friend class TemplateURLServiceTestUtil; | 401 friend class TemplateURLServiceTestUtil; |
| 384 | 402 |
| 385 typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; | 403 using GUIDToTemplateMap = std::map<std::string, TemplateURL*>; |
| 386 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; | 404 |
| 405 // A mapping from keywords to the corresponding TemplateURLs and their | |
| 406 // meaningful keyword lengths. A keyword can appear only once here because | |
| 407 // there can be only one active TemplateURL associated with a given keyword. | |
| 408 using KeywordToTemplateMap = | |
| 409 std::map<base::string16, TURLAndMeaningfulLength>; | |
| 410 | |
| 411 // A mapping from domain names to corresponding TemplateURLs and their | |
| 412 // meaningful keyword lengths. Specifically, for a keyword that is a | |
| 413 // hostname containing more than just a domain name, e.g., 'abc.def.com', | |
| 414 // the keyword is added to this map under the domain key 'def.com'. This | |
| 415 // means multiple keywords from the same domain share the same key, so this | |
| 416 // must be a multimap. | |
| 417 using KeywordDomainToTemplateMap = | |
| 418 std::multimap<base::string16, TURLAndMeaningfulLength>; | |
| 387 | 419 |
| 388 // Declaration of values to be used in an enumerated histogram to tally | 420 // Declaration of values to be used in an enumerated histogram to tally |
| 389 // changes to the default search provider from various entry points. In | 421 // changes to the default search provider from various entry points. In |
| 390 // particular, we use this to see what proportion of changes are from Sync | 422 // particular, we use this to see what proportion of changes are from Sync |
| 391 // entry points, to help spot erroneous Sync activity. | 423 // entry points, to help spot erroneous Sync activity. |
| 392 enum DefaultSearchChangeOrigin { | 424 enum DefaultSearchChangeOrigin { |
| 393 // Various known Sync entry points. | 425 // Various known Sync entry points. |
| 394 DSP_CHANGE_SYNC_PREF, | 426 DSP_CHANGE_SYNC_PREF, |
| 395 DSP_CHANGE_SYNC_ADD, | 427 DSP_CHANGE_SYNC_ADD, |
| 396 DSP_CHANGE_SYNC_DELETE, | 428 DSP_CHANGE_SYNC_DELETE, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 417 // Helper functor for FindMatchingKeywords(), for finding the range of | 449 // Helper functor for FindMatchingKeywords(), for finding the range of |
| 418 // keywords which begin with a prefix. | 450 // keywords which begin with a prefix. |
| 419 class LessWithPrefix; | 451 class LessWithPrefix; |
| 420 | 452 |
| 421 void Init(const Initializer* initializers, int num_initializers); | 453 void Init(const Initializer* initializers, int num_initializers); |
| 422 | 454 |
| 423 void RemoveFromMaps(TemplateURL* template_url); | 455 void RemoveFromMaps(TemplateURL* template_url); |
| 424 | 456 |
| 425 void AddToMaps(TemplateURL* template_url); | 457 void AddToMaps(TemplateURL* template_url); |
| 426 | 458 |
| 459 // *** | |
| 460 void RemoveFromDomainMap(const TemplateURL* template_url); | |
| 461 | |
| 462 // *** | |
| 463 void AddToDomainMap(TemplateURL* template_url); | |
| 464 | |
| 427 // Sets the keywords. This is used once the keywords have been loaded. | 465 // Sets the keywords. This is used once the keywords have been loaded. |
| 428 // This does NOT notify the delegate or the database. | 466 // This does NOT notify the delegate or the database. |
| 429 // | 467 // |
| 430 // 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 |
| 431 // 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 |
| 432 // after calling; to reinforce this, this function clears |urls| on exit. | 470 // after calling; to reinforce this, this function clears |urls| on exit. |
| 433 void SetTemplateURLs(TemplateURLVector* urls); | 471 void SetTemplateURLs(TemplateURLVector* urls); |
| 434 | 472 |
| 435 // Transitions to the loaded state. | 473 // Transitions to the loaded state. |
| 436 void ChangeToLoadedState(); | 474 void ChangeToLoadedState(); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 601 void OnSyncedDefaultSearchProviderGUIDChanged(); | 639 void OnSyncedDefaultSearchProviderGUIDChanged(); |
| 602 | 640 |
| 603 // Adds |template_urls| to |template_urls_|. | 641 // Adds |template_urls| to |template_urls_|. |
| 604 // | 642 // |
| 605 // This transfers ownership of the elements in |template_urls| to |this|, and | 643 // This transfers ownership of the elements in |template_urls| to |this|, and |
| 606 // may delete some elements, so it's not safe for callers to access any | 644 // may delete some elements, so it's not safe for callers to access any |
| 607 // elements after calling; to reinforce this, this function clears | 645 // elements after calling; to reinforce this, this function clears |
| 608 // |template_urls| on exit. | 646 // |template_urls| on exit. |
| 609 void AddTemplateURLs(TemplateURLVector* template_urls); | 647 void AddTemplateURLs(TemplateURLVector* template_urls); |
| 610 | 648 |
| 649 // Adds to |matches| all TemplateURLs stored in |keyword_to_template_map| | |
| 650 // whose keywords begin with |prefix|, sorted shortest-keyword-first. If | |
| 651 // |supports_replacement_only| is true, only TemplateURLs that support | |
| 652 // replacement are returned. | |
| 653 template <typename Container> | |
| 654 void AddMatchingKeywordsHelper( | |
| 655 const Container& keyword_to_template_map, | |
| 656 const base::string16& prefix, | |
| 657 bool supports_replacement_only, | |
| 658 TURLsAndMeaningfulLengths* matches); | |
| 659 | |
| 611 // Returns the TemplateURL corresponding to |prepopulated_id|, if any. | 660 // Returns the TemplateURL corresponding to |prepopulated_id|, if any. |
| 612 TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id); | 661 TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id); |
| 613 | 662 |
| 614 // Returns the TemplateURL associated with |extension_id|, if any. | 663 // Returns the TemplateURL associated with |extension_id|, if any. |
| 615 TemplateURL* FindTemplateURLForExtension(const std::string& extension_id, | 664 TemplateURL* FindTemplateURLForExtension(const std::string& extension_id, |
| 616 TemplateURL::Type type); | 665 TemplateURL::Type type); |
| 617 | 666 |
| 618 // Finds the extension-supplied TemplateURL that matches |data|, if any. | 667 // Finds the extension-supplied TemplateURL that matches |data|, if any. |
| 619 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, | 668 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, |
| 620 TemplateURL::Type type); | 669 TemplateURL::Type type); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 644 | 693 |
| 645 // This closure is run when the default search provider is set to Google. | 694 // This closure is run when the default search provider is set to Google. |
| 646 base::Closure dsp_change_callback_; | 695 base::Closure dsp_change_callback_; |
| 647 | 696 |
| 648 | 697 |
| 649 PrefChangeRegistrar pref_change_registrar_; | 698 PrefChangeRegistrar pref_change_registrar_; |
| 650 | 699 |
| 651 // Mapping from keyword to the TemplateURL. | 700 // Mapping from keyword to the TemplateURL. |
| 652 KeywordToTemplateMap keyword_to_template_map_; | 701 KeywordToTemplateMap keyword_to_template_map_; |
| 653 | 702 |
| 703 // Mapping from keyword domain to the TemplateURL. | |
| 704 // Entries are only allowed here if there is a corresponding entry in | |
| 705 // |keyword_to_template_map_|, i.e., if a template URL doesn't have an entry | |
| 706 // in |keyword_to_template_map_| because it's subsumed by another template | |
| 707 // URL with an identical keyword, the template URL will not have an entry in | |
| 708 // this map either. This map will also not bother including entries for | |
| 709 // keywords in which the keyword is the domain name, no host/sub-domain | |
|
Peter Kasting
2015/11/12 20:36:11
Nit: no host/sub-domain -> with no subdomain
Mark P
2015/11/12 21:42:49
Done.
| |
| 710 // before the domain name. (The ordinary |keyword_to_template_map_| | |
| 711 // suffices for that.) | |
| 712 KeywordDomainToTemplateMap keyword_domain_to_template_map_; | |
| 713 | |
| 654 // Mapping from Sync GUIDs to the TemplateURL. | 714 // Mapping from Sync GUIDs to the TemplateURL. |
| 655 GUIDToTemplateMap guid_to_template_map_; | 715 GUIDToTemplateMap guid_to_template_map_; |
| 656 | 716 |
| 657 TemplateURLVector template_urls_; | 717 TemplateURLVector template_urls_; |
| 658 | 718 |
| 659 base::ObserverList<TemplateURLServiceObserver> model_observers_; | 719 base::ObserverList<TemplateURLServiceObserver> model_observers_; |
| 660 | 720 |
| 661 // Maps from host to set of TemplateURLs whose search url host is host. | 721 // Maps from host to set of TemplateURLs whose search url host is host. |
| 662 // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular | 722 // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular |
| 663 // header dependencies. | 723 // header dependencies. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 729 | 789 |
| 730 // Helper class to manage the default search engine. | 790 // Helper class to manage the default search engine. |
| 731 DefaultSearchManager default_search_manager_; | 791 DefaultSearchManager default_search_manager_; |
| 732 | 792 |
| 733 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; | 793 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; |
| 734 | 794 |
| 735 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 795 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
| 736 }; | 796 }; |
| 737 | 797 |
| 738 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 798 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
| OLD | NEW |