| 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 as not |
| 83 // a meaningful part of the keyword and not penalize for the user not typing |
| 84 // those.) |
| 85 using TURLAndMeaningfulLength = std::pair<TemplateURL*, size_t>; |
| 86 using TURLsAndMeaningfulLengths = std::vector<TURLAndMeaningfulLength>; |
| 79 | 87 |
| 80 // Struct used for initializing the data store with fake data. | 88 // Struct used for initializing the data store with fake data. |
| 81 // Each initializer is mapped to a TemplateURL. | 89 // Each initializer is mapped to a TemplateURL. |
| 82 struct Initializer { | 90 struct Initializer { |
| 83 const char* const keyword; | 91 const char* const keyword; |
| 84 const char* const url; | 92 const char* const url; |
| 85 const char* const content; | 93 const char* const content; |
| 86 }; | 94 }; |
| 87 | 95 |
| 88 struct URLVisitedDetails { | 96 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 | 121 // 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 | 122 // 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. | 123 // non-NULL, template_url_to_replace is set to the keyword to replace. |
| 116 // | 124 // |
| 117 // |url| is the URL of the search query. This is used to prevent auto-adding | 125 // |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. | 126 // a keyword for hosts already associated with a manually-edited keyword. |
| 119 bool CanAddAutogeneratedKeyword(const base::string16& keyword, | 127 bool CanAddAutogeneratedKeyword(const base::string16& keyword, |
| 120 const GURL& url, | 128 const GURL& url, |
| 121 TemplateURL** template_url_to_replace); | 129 TemplateURL** template_url_to_replace); |
| 122 | 130 |
| 123 // Returns (in |matches|) all TemplateURLs whose keywords begin with |prefix|, | 131 // Adds to |matches| all TemplateURLs whose keywords begin with |prefix|, |
| 124 // sorted shortest keyword-first. If |support_replacement_only| is true, only | 132 // sorted shortest-keyword-first. If |supports_replacement_only| is true, only |
| 125 // TemplateURLs that support replacement are returned. | 133 // TemplateURLs that support replacement are returned. |
| 126 void FindMatchingKeywords(const base::string16& prefix, | 134 void AddMatchingKeywords(const base::string16& prefix, |
| 127 bool support_replacement_only, | 135 bool supports_replacement_only, |
| 128 TemplateURLVector* matches); | 136 TURLsAndMeaningfulLengths* matches); |
| 137 |
| 138 // Adds to |matches| all TemplateURLs for search engines with the domain |
| 139 // name part of the keyword starts with |prefix|, sorted |
| 140 // shortest-domain-name-first. If |supports_replacement_only| is true, only |
| 141 // TemplateURLs that support replacement are returned. Does not bother |
| 142 // searching/returning keywords that would've been found with an identical |
| 143 // call to FindMatchingKeywords(); i.e., doesn't search keywords for which |
| 144 // the domain name is the keyword. |
| 145 void AddMatchingDomainKeywords(const base::string16& prefix, |
| 146 bool supports_replacement_only, |
| 147 TURLsAndMeaningfulLengths* matches); |
| 129 | 148 |
| 130 // Looks up |keyword| and returns the element it maps to. Returns NULL if | 149 // Looks up |keyword| and returns the element it maps to. Returns NULL if |
| 131 // the keyword was not found. | 150 // the keyword was not found. |
| 132 // The caller should not try to delete the returned pointer; the data store | 151 // The caller should not try to delete the returned pointer; the data store |
| 133 // retains ownership of it. | 152 // retains ownership of it. |
| 134 TemplateURL* GetTemplateURLForKeyword(const base::string16& keyword); | 153 TemplateURL* GetTemplateURLForKeyword(const base::string16& keyword); |
| 135 | 154 |
| 136 // Returns that TemplateURL with the specified GUID, or NULL if not found. | 155 // 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 | 156 // The caller should not try to delete the returned pointer; the data store |
| 138 // retains ownership of it. | 157 // retains ownership of it. |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 394 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 376 IsLocalTemplateURLBetter); | 395 IsLocalTemplateURLBetter); |
| 377 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 396 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
| 378 ResolveSyncKeywordConflict); | 397 ResolveSyncKeywordConflict); |
| 379 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); | 398 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); |
| 380 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); | 399 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); |
| 381 | 400 |
| 382 friend class InstantUnitTestBase; | 401 friend class InstantUnitTestBase; |
| 383 friend class TemplateURLServiceTestUtil; | 402 friend class TemplateURLServiceTestUtil; |
| 384 | 403 |
| 385 typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; | 404 using GUIDToTURL = std::map<std::string, TemplateURL*>; |
| 386 typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; | 405 |
| 406 // A mapping from keywords to the corresponding TemplateURLs and their |
| 407 // meaningful keyword lengths. A keyword can appear only once here because |
| 408 // there can be only one active TemplateURL associated with a given keyword. |
| 409 using KeywordToTURLAndMeaningfulLength = |
| 410 std::map<base::string16, TURLAndMeaningfulLength>; |
| 411 |
| 412 // A mapping from domain names to corresponding TemplateURLs and their |
| 413 // meaningful keyword lengths. Specifically, for a keyword that is a |
| 414 // hostname containing more than just a domain name, e.g., 'abc.def.com', |
| 415 // the keyword is added to this map under the domain key 'def.com'. This |
| 416 // means multiple keywords from the same domain share the same key, so this |
| 417 // must be a multimap. |
| 418 using KeywordDomainToTURLAndMeaningfulLength = |
| 419 std::multimap<base::string16, TURLAndMeaningfulLength>; |
| 387 | 420 |
| 388 // Declaration of values to be used in an enumerated histogram to tally | 421 // Declaration of values to be used in an enumerated histogram to tally |
| 389 // changes to the default search provider from various entry points. In | 422 // 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 | 423 // particular, we use this to see what proportion of changes are from Sync |
| 391 // entry points, to help spot erroneous Sync activity. | 424 // entry points, to help spot erroneous Sync activity. |
| 392 enum DefaultSearchChangeOrigin { | 425 enum DefaultSearchChangeOrigin { |
| 393 // Various known Sync entry points. | 426 // Various known Sync entry points. |
| 394 DSP_CHANGE_SYNC_PREF, | 427 DSP_CHANGE_SYNC_PREF, |
| 395 DSP_CHANGE_SYNC_ADD, | 428 DSP_CHANGE_SYNC_ADD, |
| 396 DSP_CHANGE_SYNC_DELETE, | 429 DSP_CHANGE_SYNC_DELETE, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 413 // Boundary value. | 446 // Boundary value. |
| 414 DSP_CHANGE_MAX, | 447 DSP_CHANGE_MAX, |
| 415 }; | 448 }; |
| 416 | 449 |
| 417 // Helper functor for FindMatchingKeywords(), for finding the range of | 450 // Helper functor for FindMatchingKeywords(), for finding the range of |
| 418 // keywords which begin with a prefix. | 451 // keywords which begin with a prefix. |
| 419 class LessWithPrefix; | 452 class LessWithPrefix; |
| 420 | 453 |
| 421 void Init(const Initializer* initializers, int num_initializers); | 454 void Init(const Initializer* initializers, int num_initializers); |
| 422 | 455 |
| 456 // Removes |template_url| from various internal maps |
| 457 // (|keyword_to_turl_and_length_|, |keyword_domain_to_turl_and_length_|, |
| 458 // |guid_to_turl_|, |provider_map_|). |
| 423 void RemoveFromMaps(TemplateURL* template_url); | 459 void RemoveFromMaps(TemplateURL* template_url); |
| 424 | 460 |
| 461 // Adds |template_url| to various internal maps |
| 462 // (|keyword_to_turl_and_length_|, |keyword_domain_to_turl_and_length_|, |
| 463 // |guid_to_turl_|, |provider_map_|) if appropriate. (It might not be |
| 464 // appropriate if, for instance, |template_url|'s keyword conflicts with |
| 465 // the keyword of a custom search engine already existing in the maps that |
| 466 // is not allowed to be replaced.) |
| 425 void AddToMaps(TemplateURL* template_url); | 467 void AddToMaps(TemplateURL* template_url); |
| 426 | 468 |
| 469 // Helper function for removing an element from |
| 470 // |keyword_domain_to_turl_and_length_|. |
| 471 void RemoveFromDomainMap(const TemplateURL* template_url); |
| 472 |
| 473 // Helper fuction for adding an element to |
| 474 // |keyword_domain_to_turl_and_length_| if appropriate. |
| 475 void AddToDomainMap(TemplateURL* template_url); |
| 476 |
| 477 // Helper function for adding an element to |keyword_to_turl_and_length_|. |
| 478 void AddToMap(TemplateURL* template_url); |
| 479 |
| 427 // Sets the keywords. This is used once the keywords have been loaded. | 480 // Sets the keywords. This is used once the keywords have been loaded. |
| 428 // This does NOT notify the delegate or the database. | 481 // This does NOT notify the delegate or the database. |
| 429 // | 482 // |
| 430 // This transfers ownership of the elements in |urls| to |this|, and may | 483 // 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 | 484 // 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. | 485 // after calling; to reinforce this, this function clears |urls| on exit. |
| 433 void SetTemplateURLs(TemplateURLVector* urls); | 486 void SetTemplateURLs(TemplateURLVector* urls); |
| 434 | 487 |
| 435 // Transitions to the loaded state. | 488 // Transitions to the loaded state. |
| 436 void ChangeToLoadedState(); | 489 void ChangeToLoadedState(); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 void OnSyncedDefaultSearchProviderGUIDChanged(); | 654 void OnSyncedDefaultSearchProviderGUIDChanged(); |
| 602 | 655 |
| 603 // Adds |template_urls| to |template_urls_|. | 656 // Adds |template_urls| to |template_urls_|. |
| 604 // | 657 // |
| 605 // This transfers ownership of the elements in |template_urls| to |this|, and | 658 // 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 | 659 // may delete some elements, so it's not safe for callers to access any |
| 607 // elements after calling; to reinforce this, this function clears | 660 // elements after calling; to reinforce this, this function clears |
| 608 // |template_urls| on exit. | 661 // |template_urls| on exit. |
| 609 void AddTemplateURLs(TemplateURLVector* template_urls); | 662 void AddTemplateURLs(TemplateURLVector* template_urls); |
| 610 | 663 |
| 664 // Adds to |matches| all TemplateURLs stored in |keyword_to_turl_and_length| |
| 665 // whose keywords begin with |prefix|, sorted shortest-keyword-first. If |
| 666 // |supports_replacement_only| is true, only TemplateURLs that support |
| 667 // replacement are returned. |
| 668 template <typename Container> |
| 669 void AddMatchingKeywordsHelper( |
| 670 const Container& keyword_to_turl_and_length, |
| 671 const base::string16& prefix, |
| 672 bool supports_replacement_only, |
| 673 TURLsAndMeaningfulLengths* matches); |
| 674 |
| 611 // Returns the TemplateURL corresponding to |prepopulated_id|, if any. | 675 // Returns the TemplateURL corresponding to |prepopulated_id|, if any. |
| 612 TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id); | 676 TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id); |
| 613 | 677 |
| 614 // Returns the TemplateURL associated with |extension_id|, if any. | 678 // Returns the TemplateURL associated with |extension_id|, if any. |
| 615 TemplateURL* FindTemplateURLForExtension(const std::string& extension_id, | 679 TemplateURL* FindTemplateURLForExtension(const std::string& extension_id, |
| 616 TemplateURL::Type type); | 680 TemplateURL::Type type); |
| 617 | 681 |
| 618 // Finds the extension-supplied TemplateURL that matches |data|, if any. | 682 // Finds the extension-supplied TemplateURL that matches |data|, if any. |
| 619 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, | 683 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, |
| 620 TemplateURL::Type type); | 684 TemplateURL::Type type); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 642 // ---------- Metrics related members --------------------------------------- | 706 // ---------- Metrics related members --------------------------------------- |
| 643 rappor::RapporService* rappor_service_; | 707 rappor::RapporService* rappor_service_; |
| 644 | 708 |
| 645 // This closure is run when the default search provider is set to Google. | 709 // This closure is run when the default search provider is set to Google. |
| 646 base::Closure dsp_change_callback_; | 710 base::Closure dsp_change_callback_; |
| 647 | 711 |
| 648 | 712 |
| 649 PrefChangeRegistrar pref_change_registrar_; | 713 PrefChangeRegistrar pref_change_registrar_; |
| 650 | 714 |
| 651 // Mapping from keyword to the TemplateURL. | 715 // Mapping from keyword to the TemplateURL. |
| 652 KeywordToTemplateMap keyword_to_template_map_; | 716 KeywordToTURLAndMeaningfulLength keyword_to_turl_and_length_; |
| 717 |
| 718 // Mapping from keyword domain to the TemplateURL. |
| 719 // Entries are only allowed here if there is a corresponding entry in |
| 720 // |keyword_to_turl_and_length_|, i.e., if a template URL doesn't have an |
| 721 // entry in |keyword_to_turl_and_length_| because it's subsumed by another |
| 722 // template URL with an identical keyword, the template URL will not have an |
| 723 // entry in this map either. This map will also not bother including entries |
| 724 // for keywords in which the keyword is the domain name, with no subdomain |
| 725 // before the domain name. (The ordinary |keyword_to_turl_and_length| |
| 726 // suffices for that.) |
| 727 KeywordDomainToTURLAndMeaningfulLength keyword_domain_to_turl_and_length_; |
| 653 | 728 |
| 654 // Mapping from Sync GUIDs to the TemplateURL. | 729 // Mapping from Sync GUIDs to the TemplateURL. |
| 655 GUIDToTemplateMap guid_to_template_map_; | 730 GUIDToTURL guid_to_turl_; |
| 656 | 731 |
| 657 TemplateURLVector template_urls_; | 732 TemplateURLVector template_urls_; |
| 658 | 733 |
| 659 base::ObserverList<TemplateURLServiceObserver> model_observers_; | 734 base::ObserverList<TemplateURLServiceObserver> model_observers_; |
| 660 | 735 |
| 661 // Maps from host to set of TemplateURLs whose search url host is host. | 736 // 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 | 737 // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular |
| 663 // header dependencies. | 738 // header dependencies. |
| 664 scoped_ptr<SearchHostToURLsMap> provider_map_; | 739 scoped_ptr<SearchHostToURLsMap> provider_map_; |
| 665 | 740 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 | 804 |
| 730 // Helper class to manage the default search engine. | 805 // Helper class to manage the default search engine. |
| 731 DefaultSearchManager default_search_manager_; | 806 DefaultSearchManager default_search_manager_; |
| 732 | 807 |
| 733 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; | 808 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; |
| 734 | 809 |
| 735 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 810 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
| 736 }; | 811 }; |
| 737 | 812 |
| 738 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 813 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
| OLD | NEW |