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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <list> | 10 #include <list> |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 // TemplateURLService takes ownership of any TemplateURL passed to it. If there | 71 // TemplateURLService takes ownership of any TemplateURL passed to it. If there |
72 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService, | 72 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService, |
73 // otherwise TemplateURLService handles deletion. | 73 // otherwise TemplateURLService handles deletion. |
74 | 74 |
75 class TemplateURLService : public WebDataServiceConsumer, | 75 class TemplateURLService : public WebDataServiceConsumer, |
76 public KeyedService, | 76 public KeyedService, |
77 public syncer::SyncableService { | 77 public syncer::SyncableService { |
78 public: | 78 public: |
79 using QueryTerms = std::map<std::string, std::string>; | 79 using QueryTerms = std::map<std::string, std::string>; |
80 using TemplateURLVector = std::vector<TemplateURL*>; | 80 using TemplateURLVector = std::vector<TemplateURL*>; |
81 using OwnedTemplateURLVector = std::vector<std::unique_ptr<TemplateURL>>; | |
81 using SyncDataMap = std::map<std::string, syncer::SyncData>; | 82 using SyncDataMap = std::map<std::string, syncer::SyncData>; |
82 using Subscription = base::CallbackList<void(void)>::Subscription; | 83 using Subscription = base::CallbackList<void(void)>::Subscription; |
83 | 84 |
84 // We may want to treat the keyword in a TemplateURL as being a different | 85 // We may want to treat the keyword in a TemplateURL as being a different |
85 // length than it actually is. For example, for keywords that end in a | 86 // length than it actually is. For example, for keywords that end in a |
86 // registry, e.g., '.com', we want to consider the registry characters as not | 87 // registry, e.g., '.com', we want to consider the registry characters as not |
87 // a meaningful part of the keyword and not penalize for the user not typing | 88 // a meaningful part of the keyword and not penalize for the user not typing |
88 // those.) | 89 // those.) |
89 using TURLAndMeaningfulLength = std::pair<TemplateURL*, size_t>; | 90 using TURLAndMeaningfulLength = std::pair<TemplateURL*, size_t>; |
90 using TURLsAndMeaningfulLengths = std::vector<TURLAndMeaningfulLength>; | 91 using TURLsAndMeaningfulLengths = std::vector<TURLAndMeaningfulLength>; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 // retains ownership of it. | 162 // retains ownership of it. |
162 TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid); | 163 TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid); |
163 | 164 |
164 // Returns the first TemplateURL found with a URL using the specified |host|, | 165 // Returns the first TemplateURL found with a URL using the specified |host|, |
165 // or NULL if there are no such TemplateURLs | 166 // or NULL if there are no such TemplateURLs |
166 TemplateURL* GetTemplateURLForHost(const std::string& host); | 167 TemplateURL* GetTemplateURLForHost(const std::string& host); |
167 | 168 |
168 // Takes ownership of |template_url| and adds it to this model. For obvious | 169 // Takes ownership of |template_url| and adds it to this model. For obvious |
169 // reasons, it is illegal to Add() the same |template_url| pointer twice. | 170 // reasons, it is illegal to Add() the same |template_url| pointer twice. |
170 // Returns true if the Add is successful. | 171 // Returns true if the Add is successful. |
171 bool Add(TemplateURL* template_url); | 172 bool Add(std::unique_ptr<TemplateURL> template_url); |
Peter Kasting
2016/08/31 04:12:57
I have an idea for a signature change here that wi
Avi (use Gerrit)
2016/09/01 00:34:27
Pretty clever. Let's give it a try.
| |
172 | 173 |
173 // Like Add(), but overwrites the |template_url|'s values with the provided | 174 // Like Add(), but overwrites the |template_url|'s values with the provided |
174 // ones. | 175 // ones. |
175 void AddWithOverrides(TemplateURL* template_url, | 176 void AddWithOverrides(std::unique_ptr<TemplateURL> template_url, |
176 const base::string16& short_name, | 177 const base::string16& short_name, |
177 const base::string16& keyword, | 178 const base::string16& keyword, |
178 const std::string& url); | 179 const std::string& url); |
179 | 180 |
180 // Adds a search engine with the specified info. | 181 // Adds a search engine with the specified info. |
181 void AddExtensionControlledTURL( | 182 void AddExtensionControlledTURL( |
182 TemplateURL* template_url, | 183 std::unique_ptr<TemplateURL> template_url, |
183 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info); | 184 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info); |
184 | 185 |
185 // Removes the keyword from the model. This deletes the supplied TemplateURL. | 186 // Removes the keyword from the model. This deletes the supplied TemplateURL. |
186 // This fails if the supplied template_url is the default search provider. | 187 // This fails if the supplied template_url is the default search provider. |
187 void Remove(TemplateURL* template_url); | 188 void Remove(TemplateURL* template_url); |
188 | 189 |
189 // Removes any TemplateURL of the specified |type| associated with | 190 // Removes any TemplateURL of the specified |type| associated with |
190 // |extension_id|. Unlike with Remove(), this can be called when the | 191 // |extension_id|. Unlike with Remove(), this can be called when the |
191 // TemplateURL in question is the current default search provider. | 192 // TemplateURL in question is the current default search provider. |
192 void RemoveExtensionControlledTURL(const std::string& extension_id, | 193 void RemoveExtensionControlledTURL(const std::string& extension_id, |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 | 484 |
484 // Helper fuction for adding an element to | 485 // Helper fuction for adding an element to |
485 // |keyword_domain_to_turl_and_length_| if appropriate. | 486 // |keyword_domain_to_turl_and_length_| if appropriate. |
486 void AddToDomainMap(TemplateURL* template_url); | 487 void AddToDomainMap(TemplateURL* template_url); |
487 | 488 |
488 // Helper function for adding an element to |keyword_to_turl_and_length_|. | 489 // Helper function for adding an element to |keyword_to_turl_and_length_|. |
489 void AddToMap(TemplateURL* template_url); | 490 void AddToMap(TemplateURL* template_url); |
490 | 491 |
491 // Sets the keywords. This is used once the keywords have been loaded. | 492 // Sets the keywords. This is used once the keywords have been loaded. |
492 // This does NOT notify the delegate or the database. | 493 // This does NOT notify the delegate or the database. |
493 // | 494 void SetTemplateURLs(std::unique_ptr<OwnedTemplateURLVector> urls); |
494 // This transfers ownership of the elements in |urls| to |this|, and may | |
495 // delete some elements, so it's not safe for callers to access any elements | |
496 // after calling; to reinforce this, this function clears |urls| on exit. | |
497 void SetTemplateURLs(TemplateURLVector* urls); | |
498 | 495 |
499 // Transitions to the loaded state. | 496 // Transitions to the loaded state. |
500 void ChangeToLoadedState(); | 497 void ChangeToLoadedState(); |
501 | 498 |
502 // Called by DefaultSearchManager when the effective default search engine has | 499 // Called by DefaultSearchManager when the effective default search engine has |
503 // changed. | 500 // changed. |
504 void OnDefaultSearchChange(const TemplateURLData* new_dse_data, | 501 void OnDefaultSearchChange(const TemplateURLData* new_dse_data, |
505 DefaultSearchManager::Source source); | 502 DefaultSearchManager::Source source); |
506 | 503 |
507 // Applies a DSE change and reports metrics if appropriate. | 504 // Applies a DSE change and reports metrics if appropriate. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 | 557 |
561 // Requests the Google URL tracker to check the server if necessary. | 558 // Requests the Google URL tracker to check the server if necessary. |
562 void RequestGoogleURLTrackerServerCheckIfNecessary(); | 559 void RequestGoogleURLTrackerServerCheckIfNecessary(); |
563 | 560 |
564 // Invoked when the Google base URL has changed. Updates the mapping for all | 561 // Invoked when the Google base URL has changed. Updates the mapping for all |
565 // TemplateURLs that have a replacement term of {google:baseURL} or | 562 // TemplateURLs that have a replacement term of {google:baseURL} or |
566 // {google:baseSuggestURL}. | 563 // {google:baseSuggestURL}. |
567 void GoogleBaseURLChanged(); | 564 void GoogleBaseURLChanged(); |
568 | 565 |
569 // Adds a new TemplateURL to this model. TemplateURLService will own the | 566 // Adds a new TemplateURL to this model. TemplateURLService will own the |
570 // reference, and delete it when the TemplateURL is removed. | 567 // reference, and delete it when the TemplateURL is removed. |
Peter Kasting
2016/08/31 04:12:57
Nit: Second sentence here is now redundant.
Avi (use Gerrit)
2016/09/01 00:34:27
Done.
| |
571 // If |newly_adding| is false, we assume that this TemplateURL was already | 568 // If |newly_adding| is false, we assume that this TemplateURL was already |
572 // part of the model in the past, and therefore we don't need to do things | 569 // part of the model in the past, and therefore we don't need to do things |
573 // like assign it an ID or notify sync. | 570 // like assign it an ID or notify sync. |
574 // This function guarantees that on return the model will not have two | 571 // This function guarantees that on return the model will not have two |
575 // non-extension TemplateURLs with the same keyword. If that means that it | 572 // non-extension TemplateURLs with the same keyword. If that means that it |
576 // cannot add the provided argument, it will delete it and return false. | 573 // cannot add the provided argument, it will delete it and return false. |
577 // Caller is responsible for notifying observers if this function returns | 574 // Caller is responsible for notifying observers if this function returns |
578 // true. | 575 // true. |
579 bool AddNoNotify(TemplateURL* template_url, bool newly_adding); | 576 bool AddNoNotify(std::unique_ptr<TemplateURL> template_url, |
577 bool newly_adding); | |
580 | 578 |
581 // Removes the keyword from the model. This deletes the supplied TemplateURL. | 579 // Removes the keyword from the model. This deletes the supplied TemplateURL. |
582 // This fails if the supplied template_url is the default search provider. | 580 // This fails if the supplied template_url is the default search provider. |
583 // Caller is responsible for notifying observers. | 581 // Caller is responsible for notifying observers. |
584 void RemoveNoNotify(TemplateURL* template_url); | 582 void RemoveNoNotify(TemplateURL* template_url); |
585 | 583 |
586 // Like ResetTemplateURL(), but instead of notifying observers, returns | 584 // Like ResetTemplateURL(), but instead of notifying observers, returns |
587 // whether anything has changed. | 585 // whether anything has changed. |
588 bool ResetTemplateURLNoNotify(TemplateURL* url, | 586 bool ResetTemplateURLNoNotify(TemplateURL* url, |
589 const base::string16& title, | 587 const base::string16& title, |
590 const base::string16& keyword, | 588 const base::string16& keyword, |
591 const std::string& search_url); | 589 const std::string& search_url); |
592 | 590 |
593 // Notify the observers that the model has changed. This is done only if the | 591 // Notify the observers that the model has changed. This is done only if the |
594 // model is loaded. | 592 // model is loaded. |
595 void NotifyObservers(); | 593 void NotifyObservers(); |
596 | 594 |
597 // Updates |template_urls| so that the only "created by policy" entry is | 595 // Updates |template_urls| so that the only "created by policy" entry is |
598 // |default_from_prefs|. |default_from_prefs| may be NULL if there is no | 596 // |default_from_prefs|. |default_from_prefs| may be NULL if there is no |
599 // policy-defined DSE in effect. | 597 // policy-defined DSE in effect. |
600 void UpdateProvidersCreatedByPolicy( | 598 void UpdateProvidersCreatedByPolicy( |
601 TemplateURLVector* template_urls, | 599 OwnedTemplateURLVector* template_urls, |
602 const TemplateURLData* default_from_prefs); | 600 const TemplateURLData* default_from_prefs); |
603 | 601 |
604 // Resets the sync GUID of the specified TemplateURL and persists the change | 602 // Resets the sync GUID of the specified TemplateURL and persists the change |
605 // to the database. This does not notify observers. | 603 // to the database. This does not notify observers. |
606 void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid); | 604 void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid); |
607 | 605 |
608 // Attempts to generate a unique keyword for |turl| based on its original | 606 // Attempts to generate a unique keyword for |turl| based on its original |
609 // keyword. If its keyword is already unique, that is returned. Otherwise, it | 607 // keyword. If its keyword is already unique, that is returned. Otherwise, it |
610 // tries to return the autogenerated keyword if that is unique to the Service, | 608 // tries to return the autogenerated keyword if that is unique to the Service, |
611 // and finally it repeatedly appends special characters to the keyword until | 609 // and finally it repeatedly appends special characters to the keyword until |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
655 // This should only be called from MergeDataAndStartSyncing. | 653 // This should only be called from MergeDataAndStartSyncing. |
656 void MergeInSyncTemplateURL(TemplateURL* sync_turl, | 654 void MergeInSyncTemplateURL(TemplateURL* sync_turl, |
657 const SyncDataMap& sync_data, | 655 const SyncDataMap& sync_data, |
658 syncer::SyncChangeList* change_list, | 656 syncer::SyncChangeList* change_list, |
659 SyncDataMap* local_data, | 657 SyncDataMap* local_data, |
660 syncer::SyncMergeResult* merge_result); | 658 syncer::SyncMergeResult* merge_result); |
661 | 659 |
662 // Goes through a vector of TemplateURLs and ensure that both the in-memory | 660 // Goes through a vector of TemplateURLs and ensure that both the in-memory |
663 // and database copies have valid sync_guids. This is to fix crbug.com/102038, | 661 // and database copies have valid sync_guids. This is to fix crbug.com/102038, |
664 // where old entries were being pushed to Sync without a sync_guid. | 662 // where old entries were being pushed to Sync without a sync_guid. |
665 void PatchMissingSyncGUIDs(TemplateURLVector* template_urls); | 663 void PatchMissingSyncGUIDs(OwnedTemplateURLVector* template_urls); |
666 | 664 |
667 void OnSyncedDefaultSearchProviderGUIDChanged(); | 665 void OnSyncedDefaultSearchProviderGUIDChanged(); |
668 | 666 |
669 // Adds |template_urls| to |template_urls_|. | |
670 // | |
671 // This transfers ownership of the elements in |template_urls| to |this|, and | |
672 // may delete some elements, so it's not safe for callers to access any | |
673 // elements after calling; to reinforce this, this function clears | |
674 // |template_urls| on exit. | |
675 void AddTemplateURLs(TemplateURLVector* template_urls); | |
676 | |
677 // Adds to |matches| all TemplateURLs stored in |keyword_to_turl_and_length| | 667 // Adds to |matches| all TemplateURLs stored in |keyword_to_turl_and_length| |
678 // whose keywords begin with |prefix|, sorted shortest-keyword-first. If | 668 // whose keywords begin with |prefix|, sorted shortest-keyword-first. If |
679 // |supports_replacement_only| is true, only TemplateURLs that support | 669 // |supports_replacement_only| is true, only TemplateURLs that support |
680 // replacement are returned. | 670 // replacement are returned. |
681 template <typename Container> | 671 template <typename Container> |
682 void AddMatchingKeywordsHelper( | 672 void AddMatchingKeywordsHelper( |
683 const Container& keyword_to_turl_and_length, | 673 const Container& keyword_to_turl_and_length, |
684 const base::string16& prefix, | 674 const base::string16& prefix, |
685 bool supports_replacement_only, | 675 bool supports_replacement_only, |
686 TURLsAndMeaningfulLengths* matches); | 676 TURLsAndMeaningfulLengths* matches); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
735 // template URL with an identical keyword, the template URL will not have an | 725 // template URL with an identical keyword, the template URL will not have an |
736 // entry in this map either. This map will also not bother including entries | 726 // entry in this map either. This map will also not bother including entries |
737 // for keywords in which the keyword is the domain name, with no subdomain | 727 // for keywords in which the keyword is the domain name, with no subdomain |
738 // before the domain name. (The ordinary |keyword_to_turl_and_length| | 728 // before the domain name. (The ordinary |keyword_to_turl_and_length| |
739 // suffices for that.) | 729 // suffices for that.) |
740 KeywordDomainToTURLAndMeaningfulLength keyword_domain_to_turl_and_length_; | 730 KeywordDomainToTURLAndMeaningfulLength keyword_domain_to_turl_and_length_; |
741 | 731 |
742 // Mapping from Sync GUIDs to the TemplateURL. | 732 // Mapping from Sync GUIDs to the TemplateURL. |
743 GUIDToTURL guid_to_turl_; | 733 GUIDToTURL guid_to_turl_; |
744 | 734 |
745 TemplateURLVector template_urls_; | 735 OwnedTemplateURLVector template_urls_; |
746 | 736 |
747 base::ObserverList<TemplateURLServiceObserver> model_observers_; | 737 base::ObserverList<TemplateURLServiceObserver> model_observers_; |
748 | 738 |
749 // Maps from host to set of TemplateURLs whose search url host is host. | 739 // Maps from host to set of TemplateURLs whose search url host is host. |
750 // NOTE: This is always non-NULL; we use a std::unique_ptr<> to avoid circular | 740 // NOTE: This is always non-NULL; we use a std::unique_ptr<> to avoid circular |
751 // header dependencies. | 741 // header dependencies. |
752 std::unique_ptr<SearchHostToURLsMap> provider_map_; | 742 std::unique_ptr<SearchHostToURLsMap> provider_map_; |
753 | 743 |
754 // Whether the keywords have been loaded. | 744 // Whether the keywords have been loaded. |
755 bool loaded_; | 745 bool loaded_; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
821 // Helper class to manage the default search engine. | 811 // Helper class to manage the default search engine. |
822 DefaultSearchManager default_search_manager_; | 812 DefaultSearchManager default_search_manager_; |
823 | 813 |
824 std::unique_ptr<GoogleURLTracker::Subscription> | 814 std::unique_ptr<GoogleURLTracker::Subscription> |
825 google_url_updated_subscription_; | 815 google_url_updated_subscription_; |
826 | 816 |
827 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 817 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
828 }; | 818 }; |
829 | 819 |
830 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 820 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
OLD | NEW |