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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 159 |
159 // Returns that TemplateURL with the specified GUID, or NULL if not found. | 160 // Returns that TemplateURL with the specified GUID, or NULL if not found. |
160 // The caller should not try to delete the returned pointer; the data store | 161 // The caller should not try to delete the returned pointer; the data store |
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 // Adds |template_url| to this model. Returns a raw pointer to |template_url| |
169 // reasons, it is illegal to Add() the same |template_url| pointer twice. | 170 // if the addition succeeded, or null on failure. (Many callers need still |
170 // Returns true if the Add is successful. | 171 // need a raw pointer to the TemplateURL so they can access it later.) |
171 bool Add(TemplateURL* template_url); | 172 TemplateURL* Add(std::unique_ptr<TemplateURL> template_url); |
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 TemplateURL* 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 TemplateURL* 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 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 | 485 |
485 // Helper fuction for adding an element to | 486 // Helper fuction for adding an element to |
486 // |keyword_domain_to_turl_and_length_| if appropriate. | 487 // |keyword_domain_to_turl_and_length_| if appropriate. |
487 void AddToDomainMap(TemplateURL* template_url); | 488 void AddToDomainMap(TemplateURL* template_url); |
488 | 489 |
489 // Helper function for adding an element to |keyword_to_turl_and_length_|. | 490 // Helper function for adding an element to |keyword_to_turl_and_length_|. |
490 void AddToMap(TemplateURL* template_url); | 491 void AddToMap(TemplateURL* template_url); |
491 | 492 |
492 // Sets the keywords. This is used once the keywords have been loaded. | 493 // Sets the keywords. This is used once the keywords have been loaded. |
493 // This does NOT notify the delegate or the database. | 494 // This does NOT notify the delegate or the database. |
494 // | 495 void SetTemplateURLs(std::unique_ptr<OwnedTemplateURLVector> urls); |
495 // This transfers ownership of the elements in |urls| to |this|, and may | |
496 // delete some elements, so it's not safe for callers to access any elements | |
497 // after calling; to reinforce this, this function clears |urls| on exit. | |
498 void SetTemplateURLs(TemplateURLVector* urls); | |
499 | 496 |
500 // Transitions to the loaded state. | 497 // Transitions to the loaded state. |
501 void ChangeToLoadedState(); | 498 void ChangeToLoadedState(); |
502 | 499 |
503 // Called by DefaultSearchManager when the effective default search engine has | 500 // Called by DefaultSearchManager when the effective default search engine has |
504 // changed. | 501 // changed. |
505 void OnDefaultSearchChange(const TemplateURLData* new_dse_data, | 502 void OnDefaultSearchChange(const TemplateURLData* new_dse_data, |
506 DefaultSearchManager::Source source); | 503 DefaultSearchManager::Source source); |
507 | 504 |
508 // Applies a DSE change and reports metrics if appropriate. | 505 // Applies a DSE change and reports metrics if appropriate. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 void AddTabToSearchVisit(const TemplateURL& t_url); | 557 void AddTabToSearchVisit(const TemplateURL& t_url); |
561 | 558 |
562 // Requests the Google URL tracker to check the server if necessary. | 559 // Requests the Google URL tracker to check the server if necessary. |
563 void RequestGoogleURLTrackerServerCheckIfNecessary(); | 560 void RequestGoogleURLTrackerServerCheckIfNecessary(); |
564 | 561 |
565 // Invoked when the Google base URL has changed. Updates the mapping for all | 562 // Invoked when the Google base URL has changed. Updates the mapping for all |
566 // TemplateURLs that have a replacement term of {google:baseURL} or | 563 // TemplateURLs that have a replacement term of {google:baseURL} or |
567 // {google:baseSuggestURL}. | 564 // {google:baseSuggestURL}. |
568 void GoogleBaseURLChanged(); | 565 void GoogleBaseURLChanged(); |
569 | 566 |
570 // Adds a new TemplateURL to this model. TemplateURLService will own the | 567 // Adds a new TemplateURL to this model. |
571 // reference, and delete it when the TemplateURL is removed. | 568 // |
572 // If |newly_adding| is false, we assume that this TemplateURL was already | 569 // If |newly_adding| is false, we assume that this TemplateURL was already |
573 // part of the model in the past, and therefore we don't need to do things | 570 // part of the model in the past, and therefore we don't need to do things |
574 // like assign it an ID or notify sync. | 571 // like assign it an ID or notify sync. |
575 // This function guarantees that on return the model will not have two | 572 // |
576 // non-extension TemplateURLs with the same keyword. If that means that it | 573 // This function guarantees that on return the model will not have two non- |
577 // cannot add the provided argument, it will delete it and return false. | 574 // extension TemplateURLs with the same keyword. If that means that it cannot |
578 // Caller is responsible for notifying observers if this function returns | 575 // add the provided argument, it will return null. Otherwise it will return |
579 // true. | 576 // the raw pointer to the TemplateURL. The caller is responsible for |
580 bool AddNoNotify(TemplateURL* template_url, bool newly_adding); | 577 // notifying observers if this function succeeds. |
| 578 TemplateURL* AddNoNotify(std::unique_ptr<TemplateURL> template_url, |
| 579 bool newly_adding); |
581 | 580 |
582 // Removes the keyword from the model. This deletes the supplied TemplateURL. | 581 // Removes the keyword from the model. This deletes the supplied TemplateURL. |
583 // This fails if the supplied template_url is the default search provider. | 582 // This fails if the supplied template_url is the default search provider. |
584 // Caller is responsible for notifying observers. | 583 // Caller is responsible for notifying observers. |
585 void RemoveNoNotify(TemplateURL* template_url); | 584 void RemoveNoNotify(TemplateURL* template_url); |
586 | 585 |
587 // Like ResetTemplateURL(), but instead of notifying observers, returns | 586 // Like ResetTemplateURL(), but instead of notifying observers, returns |
588 // whether anything has changed. | 587 // whether anything has changed. |
589 bool ResetTemplateURLNoNotify(TemplateURL* url, | 588 bool ResetTemplateURLNoNotify(TemplateURL* url, |
590 const base::string16& title, | 589 const base::string16& title, |
591 const base::string16& keyword, | 590 const base::string16& keyword, |
592 const std::string& search_url); | 591 const std::string& search_url); |
593 | 592 |
594 // Notify the observers that the model has changed. This is done only if the | 593 // Notify the observers that the model has changed. This is done only if the |
595 // model is loaded. | 594 // model is loaded. |
596 void NotifyObservers(); | 595 void NotifyObservers(); |
597 | 596 |
598 // Updates |template_urls| so that the only "created by policy" entry is | 597 // Updates |template_urls| so that the only "created by policy" entry is |
599 // |default_from_prefs|. |default_from_prefs| may be NULL if there is no | 598 // |default_from_prefs|. |default_from_prefs| may be NULL if there is no |
600 // policy-defined DSE in effect. | 599 // policy-defined DSE in effect. |
601 void UpdateProvidersCreatedByPolicy( | 600 void UpdateProvidersCreatedByPolicy( |
602 TemplateURLVector* template_urls, | 601 OwnedTemplateURLVector* template_urls, |
603 const TemplateURLData* default_from_prefs); | 602 const TemplateURLData* default_from_prefs); |
604 | 603 |
605 // Resets the sync GUID of the specified TemplateURL and persists the change | 604 // Resets the sync GUID of the specified TemplateURL and persists the change |
606 // to the database. This does not notify observers. | 605 // to the database. This does not notify observers. |
607 void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid); | 606 void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid); |
608 | 607 |
609 // Attempts to generate a unique keyword for |turl| based on its original | 608 // Attempts to generate a unique keyword for |turl| based on its original |
610 // keyword. If its keyword is already unique, that is returned. Otherwise, it | 609 // keyword. If its keyword is already unique, that is returned. Otherwise, it |
611 // tries to return the autogenerated keyword if that is unique to the Service, | 610 // tries to return the autogenerated keyword if that is unique to the Service, |
612 // and finally it repeatedly appends special characters to the keyword until | 611 // and finally it repeatedly appends special characters to the keyword until |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 // This should only be called from MergeDataAndStartSyncing. | 655 // This should only be called from MergeDataAndStartSyncing. |
657 void MergeInSyncTemplateURL(TemplateURL* sync_turl, | 656 void MergeInSyncTemplateURL(TemplateURL* sync_turl, |
658 const SyncDataMap& sync_data, | 657 const SyncDataMap& sync_data, |
659 syncer::SyncChangeList* change_list, | 658 syncer::SyncChangeList* change_list, |
660 SyncDataMap* local_data, | 659 SyncDataMap* local_data, |
661 syncer::SyncMergeResult* merge_result); | 660 syncer::SyncMergeResult* merge_result); |
662 | 661 |
663 // Goes through a vector of TemplateURLs and ensure that both the in-memory | 662 // Goes through a vector of TemplateURLs and ensure that both the in-memory |
664 // and database copies have valid sync_guids. This is to fix crbug.com/102038, | 663 // and database copies have valid sync_guids. This is to fix crbug.com/102038, |
665 // where old entries were being pushed to Sync without a sync_guid. | 664 // where old entries were being pushed to Sync without a sync_guid. |
666 void PatchMissingSyncGUIDs(TemplateURLVector* template_urls); | 665 void PatchMissingSyncGUIDs(OwnedTemplateURLVector* template_urls); |
667 | 666 |
668 void OnSyncedDefaultSearchProviderGUIDChanged(); | 667 void OnSyncedDefaultSearchProviderGUIDChanged(); |
669 | 668 |
670 // Adds |template_urls| to |template_urls_|. | |
671 // | |
672 // This transfers ownership of the elements in |template_urls| to |this|, and | |
673 // may delete some elements, so it's not safe for callers to access any | |
674 // elements after calling; to reinforce this, this function clears | |
675 // |template_urls| on exit. | |
676 void AddTemplateURLs(TemplateURLVector* template_urls); | |
677 | |
678 // Adds to |matches| all TemplateURLs stored in |keyword_to_turl_and_length| | 669 // Adds to |matches| all TemplateURLs stored in |keyword_to_turl_and_length| |
679 // whose keywords begin with |prefix|, sorted shortest-keyword-first. If | 670 // whose keywords begin with |prefix|, sorted shortest-keyword-first. If |
680 // |supports_replacement_only| is true, only TemplateURLs that support | 671 // |supports_replacement_only| is true, only TemplateURLs that support |
681 // replacement are returned. | 672 // replacement are returned. |
682 template <typename Container> | 673 template <typename Container> |
683 void AddMatchingKeywordsHelper( | 674 void AddMatchingKeywordsHelper( |
684 const Container& keyword_to_turl_and_length, | 675 const Container& keyword_to_turl_and_length, |
685 const base::string16& prefix, | 676 const base::string16& prefix, |
686 bool supports_replacement_only, | 677 bool supports_replacement_only, |
687 TURLsAndMeaningfulLengths* matches); | 678 TURLsAndMeaningfulLengths* matches); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
736 // template URL with an identical keyword, the template URL will not have an | 727 // template URL with an identical keyword, the template URL will not have an |
737 // entry in this map either. This map will also not bother including entries | 728 // entry in this map either. This map will also not bother including entries |
738 // for keywords in which the keyword is the domain name, with no subdomain | 729 // for keywords in which the keyword is the domain name, with no subdomain |
739 // before the domain name. (The ordinary |keyword_to_turl_and_length| | 730 // before the domain name. (The ordinary |keyword_to_turl_and_length| |
740 // suffices for that.) | 731 // suffices for that.) |
741 KeywordDomainToTURLAndMeaningfulLength keyword_domain_to_turl_and_length_; | 732 KeywordDomainToTURLAndMeaningfulLength keyword_domain_to_turl_and_length_; |
742 | 733 |
743 // Mapping from Sync GUIDs to the TemplateURL. | 734 // Mapping from Sync GUIDs to the TemplateURL. |
744 GUIDToTURL guid_to_turl_; | 735 GUIDToTURL guid_to_turl_; |
745 | 736 |
746 TemplateURLVector template_urls_; | 737 OwnedTemplateURLVector template_urls_; |
747 | 738 |
748 base::ObserverList<TemplateURLServiceObserver> model_observers_; | 739 base::ObserverList<TemplateURLServiceObserver> model_observers_; |
749 | 740 |
750 // 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. |
751 // NOTE: This is always non-NULL; we use a std::unique_ptr<> to avoid circular | 742 // NOTE: This is always non-NULL; we use a std::unique_ptr<> to avoid circular |
752 // header dependencies. | 743 // header dependencies. |
753 std::unique_ptr<SearchHostToURLsMap> provider_map_; | 744 std::unique_ptr<SearchHostToURLsMap> provider_map_; |
754 | 745 |
755 // Whether the keywords have been loaded. | 746 // Whether the keywords have been loaded. |
756 bool loaded_; | 747 bool loaded_; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 // Helper class to manage the default search engine. | 813 // Helper class to manage the default search engine. |
823 DefaultSearchManager default_search_manager_; | 814 DefaultSearchManager default_search_manager_; |
824 | 815 |
825 std::unique_ptr<GoogleURLTracker::Subscription> | 816 std::unique_ptr<GoogleURLTracker::Subscription> |
826 google_url_updated_subscription_; | 817 google_url_updated_subscription_; |
827 | 818 |
828 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); | 819 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
829 }; | 820 }; |
830 | 821 |
831 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ | 822 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ |
OLD | NEW |