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

Side by Side Diff: components/search_engines/template_url_service.h

Issue 1411543011: Omnibox: Make Keyword Provide More Generous with Matching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: all peter's comments (except one), including previously-promised refactoring, and additional unit t… Created 5 years, 1 month 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
OLDNEW
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
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
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
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 GUIDToTemplateMap = 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 KeywordToTemplateMap =
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 KeywordDomainToTemplateMap =
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
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_template_map_|, |keyword_domain_to_template_map_|,
458 // |guid_to_template_map_|, |provider_map_|).
423 void RemoveFromMaps(TemplateURL* template_url); 459 void RemoveFromMaps(TemplateURL* template_url);
424 460
461 // Adds |template_url| from various internal maps
Peter Kasting 2015/11/12 22:32:11 Nit: from -> to
Mark P 2015/11/12 23:18:42 Done.
462 // (|keyword_to_template_map_|, |keyword_domain_to_template_map_|,
463 // |guid_to_template_map_|, |provider_map_|) if appropriate. (It might
464 // not be appropriate if, for instance, |template_url|'s keyword conflicts
465 // with the keyword of a custom search engine already existing in the
466 // maps that 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_template_map_|.
471 void RemoveFromDomainMap(const TemplateURL* template_url);
472
473 // Helper fuction for adding an element to |keyword_domain_to_template_map_|
474 // if appropriate.
475 void AddToDomainMap(TemplateURL* template_url);
476
477 // Generic helper function for adding to |keyword_to_template_map|
478 // the template URL |template_url| under the key |keyword|.
Peter Kasting 2015/11/12 22:32:11 Nit: Awkward; maybe: Generic helper function for
Mark P 2015/11/12 23:18:42 Okay. Done.
479 template <typename Container>
480 static void AddToMap(
481 Container* keyword_to_template_map,
482 const base::string16& keyword,
483 TemplateURL* template_url);
484
427 // Sets the keywords. This is used once the keywords have been loaded. 485 // Sets the keywords. This is used once the keywords have been loaded.
428 // This does NOT notify the delegate or the database. 486 // This does NOT notify the delegate or the database.
429 // 487 //
430 // This transfers ownership of the elements in |urls| to |this|, and may 488 // 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 489 // 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. 490 // after calling; to reinforce this, this function clears |urls| on exit.
433 void SetTemplateURLs(TemplateURLVector* urls); 491 void SetTemplateURLs(TemplateURLVector* urls);
434 492
435 // Transitions to the loaded state. 493 // Transitions to the loaded state.
436 void ChangeToLoadedState(); 494 void ChangeToLoadedState();
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 void OnSyncedDefaultSearchProviderGUIDChanged(); 659 void OnSyncedDefaultSearchProviderGUIDChanged();
602 660
603 // Adds |template_urls| to |template_urls_|. 661 // Adds |template_urls| to |template_urls_|.
604 // 662 //
605 // This transfers ownership of the elements in |template_urls| to |this|, and 663 // 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 664 // may delete some elements, so it's not safe for callers to access any
607 // elements after calling; to reinforce this, this function clears 665 // elements after calling; to reinforce this, this function clears
608 // |template_urls| on exit. 666 // |template_urls| on exit.
609 void AddTemplateURLs(TemplateURLVector* template_urls); 667 void AddTemplateURLs(TemplateURLVector* template_urls);
610 668
669 // Adds to |matches| all TemplateURLs stored in |keyword_to_template_map|
670 // whose keywords begin with |prefix|, sorted shortest-keyword-first. If
671 // |supports_replacement_only| is true, only TemplateURLs that support
672 // replacement are returned.
673 template <typename Container>
674 void AddMatchingKeywordsHelper(
675 const Container& keyword_to_template_map,
676 const base::string16& prefix,
677 bool supports_replacement_only,
678 TURLsAndMeaningfulLengths* matches);
679
611 // Returns the TemplateURL corresponding to |prepopulated_id|, if any. 680 // Returns the TemplateURL corresponding to |prepopulated_id|, if any.
612 TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id); 681 TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id);
613 682
614 // Returns the TemplateURL associated with |extension_id|, if any. 683 // Returns the TemplateURL associated with |extension_id|, if any.
615 TemplateURL* FindTemplateURLForExtension(const std::string& extension_id, 684 TemplateURL* FindTemplateURLForExtension(const std::string& extension_id,
616 TemplateURL::Type type); 685 TemplateURL::Type type);
617 686
618 // Finds the extension-supplied TemplateURL that matches |data|, if any. 687 // Finds the extension-supplied TemplateURL that matches |data|, if any.
619 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, 688 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data,
620 TemplateURL::Type type); 689 TemplateURL::Type type);
(...skipping 23 matching lines...) Expand all
644 713
645 // This closure is run when the default search provider is set to Google. 714 // This closure is run when the default search provider is set to Google.
646 base::Closure dsp_change_callback_; 715 base::Closure dsp_change_callback_;
647 716
648 717
649 PrefChangeRegistrar pref_change_registrar_; 718 PrefChangeRegistrar pref_change_registrar_;
650 719
651 // Mapping from keyword to the TemplateURL. 720 // Mapping from keyword to the TemplateURL.
652 KeywordToTemplateMap keyword_to_template_map_; 721 KeywordToTemplateMap keyword_to_template_map_;
653 722
723 // Mapping from keyword domain to the TemplateURL.
724 // Entries are only allowed here if there is a corresponding entry in
725 // |keyword_to_template_map_|, i.e., if a template URL doesn't have an entry
726 // in |keyword_to_template_map_| because it's subsumed by another template
727 // URL with an identical keyword, the template URL will not have an entry in
728 // this map either. This map will also not bother including entries for
729 // keywords in which the keyword is the domain name, with no subdomain
730 // before the domain name. (The ordinary |keyword_to_template_map_|
731 // suffices for that.)
732 KeywordDomainToTemplateMap keyword_domain_to_template_map_;
733
654 // Mapping from Sync GUIDs to the TemplateURL. 734 // Mapping from Sync GUIDs to the TemplateURL.
655 GUIDToTemplateMap guid_to_template_map_; 735 GUIDToTemplateMap guid_to_template_map_;
656 736
657 TemplateURLVector template_urls_; 737 TemplateURLVector template_urls_;
658 738
659 base::ObserverList<TemplateURLServiceObserver> model_observers_; 739 base::ObserverList<TemplateURLServiceObserver> model_observers_;
660 740
661 // Maps from host to set of TemplateURLs whose search url host is host. 741 // 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 742 // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular
663 // header dependencies. 743 // header dependencies.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 809
730 // Helper class to manage the default search engine. 810 // Helper class to manage the default search engine.
731 DefaultSearchManager default_search_manager_; 811 DefaultSearchManager default_search_manager_;
732 812
733 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; 813 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_;
734 814
735 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); 815 DISALLOW_COPY_AND_ASSIGN(TemplateURLService);
736 }; 816 };
737 817
738 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ 818 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698