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

Side by Side Diff: chrome/browser/search_engines/template_url_service.h

Issue 367863005: No chrome dependencies in TemplateURLService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ 6 #define CHROME_BROWSER_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>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/callback_list.h" 14 #include "base/callback_list.h"
15 #include "base/gtest_prod_util.h" 15 #include "base/gtest_prod_util.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/observer_list.h" 17 #include "base/observer_list.h"
18 #include "base/prefs/pref_change_registrar.h" 18 #include "base/prefs/pref_change_registrar.h"
19 #include "chrome/browser/webdata/keyword_web_data_service.h" 19 #include "chrome/browser/webdata/keyword_web_data_service.h"
20 #include "components/google/core/browser/google_url_tracker.h" 20 #include "components/google/core/browser/google_url_tracker.h"
21 #include "components/keyed_service/core/keyed_service.h" 21 #include "components/keyed_service/core/keyed_service.h"
22 #include "components/search_engines/default_search_manager.h" 22 #include "components/search_engines/default_search_manager.h"
23 #include "components/search_engines/template_url.h" 23 #include "components/search_engines/template_url.h"
24 #include "components/search_engines/template_url_id.h" 24 #include "components/search_engines/template_url_id.h"
25 #include "components/webdata/common/web_data_service_consumer.h" 25 #include "components/webdata/common/web_data_service_consumer.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
28 #include "sync/api/sync_change.h" 26 #include "sync/api/sync_change.h"
29 #include "sync/api/syncable_service.h" 27 #include "sync/api/syncable_service.h"
30 28
31 class GURL; 29 class GURL;
32 class PrefService; 30 class PrefService;
33 class Profile;
34 class SearchHostToURLsMap; 31 class SearchHostToURLsMap;
35 class SearchTermsData; 32 class SearchTermsData;
36 class TemplateURL; 33 class TemplateURL;
37 struct TemplateURLData; 34 struct TemplateURLData;
35 class TemplateURLService;
38 class TemplateURLServiceObserver; 36 class TemplateURLServiceObserver;
39 37
40 namespace rappor { 38 namespace rappor {
41 class RapporService; 39 class RapporService;
42 } 40 }
43 41
44 namespace syncer { 42 namespace syncer {
45 class SyncData; 43 class SyncData;
46 class SyncErrorFactory; 44 class SyncErrorFactory;
47 } 45 }
48 46
49 namespace history { 47 // This interface provides history related functionalities required by
Peter Kasting 2014/07/02 20:45:57 Nit: functionalities -> functionality
hashimoto 2014/07/03 00:05:07 Done.
50 struct URLVisitedDetails; 48 // TemplateURLService.
51 } 49 // TODO(hashimoto): Get rid of this once HistoryService gets componentized.
50 class KeywordHistoryServiceInterface {
Peter Kasting 2014/07/02 20:45:57 Nit: I think this interface should be declared in
hashimoto 2014/07/03 00:05:07 Some components have "XXXClient" classes which are
hashimoto 2014/07/03 07:13:31 Colin, I'm going to introduce a new interface Keyw
51 public:
52 virtual ~KeywordHistoryServiceInterface() {}
53
54 // Sets the pointer to the owner of this object.
55 virtual void SetOwner(TemplateURLService* owner) = 0;
56
57 // Deletes all search terms for the specified keyword.
58 virtual void DeleteAllSearchTermsForKeyword(TemplateURLID id) = 0;
59
60 // Sets the search terms for the specified url and keyword.
61 virtual void SetKeywordSearchTermsForURL(const GURL& url,
62 TemplateURLID id,
63 const base::string16& term) = 0;
64
65 // Adds the given URL to history as a keyword generated visit.
66 virtual void AddKeywordGeneratedVisit(const GURL& url) = 0;
67 };
52 68
53 // TemplateURLService is the backend for keywords. It's used by 69 // TemplateURLService is the backend for keywords. It's used by
54 // KeywordAutocomplete. 70 // KeywordAutocomplete.
55 // 71 //
56 // TemplateURLService stores a vector of TemplateURLs. The TemplateURLs are 72 // TemplateURLService stores a vector of TemplateURLs. The TemplateURLs are
57 // persisted to the database maintained by KeywordWebDataService. 73 // persisted to the database maintained by KeywordWebDataService.
58 // *ALL* mutations to the TemplateURLs must funnel through TemplateURLService. 74 // *ALL* mutations to the TemplateURLs must funnel through TemplateURLService.
59 // This allows TemplateURLService to notify listeners of changes as well as keep 75 // This allows TemplateURLService to notify listeners of changes as well as keep
60 // the database in sync. 76 // the database in sync.
61 // 77 //
62 // There is a TemplateURLService per Profile.
63 //
64 // TemplateURLService does not load the vector of TemplateURLs in its 78 // TemplateURLService does not load the vector of TemplateURLs in its
65 // constructor (except for testing). Use the Load method to trigger a load. 79 // constructor (except for testing). Use the Load method to trigger a load.
66 // When TemplateURLService has completed loading, observers are notified via 80 // When TemplateURLService has completed loading, observers are notified via
67 // OnTemplateURLServiceChanged, or by a callback registered prior to calling 81 // OnTemplateURLServiceChanged, or by a callback registered prior to calling
68 // the Load method. 82 // the Load method.
69 // 83 //
70 // TemplateURLService takes ownership of any TemplateURL passed to it. If there 84 // TemplateURLService takes ownership of any TemplateURL passed to it. If there
71 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService, 85 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService,
72 // otherwise TemplateURLService handles deletion. 86 // otherwise TemplateURLService handles deletion.
73 87
74 class TemplateURLService : public WebDataServiceConsumer, 88 class TemplateURLService : public WebDataServiceConsumer,
75 public KeyedService, 89 public KeyedService,
76 public content::NotificationObserver,
77 public syncer::SyncableService { 90 public syncer::SyncableService {
78 public: 91 public:
79 typedef std::map<std::string, std::string> QueryTerms; 92 typedef std::map<std::string, std::string> QueryTerms;
80 typedef std::vector<TemplateURL*> TemplateURLVector; 93 typedef std::vector<TemplateURL*> TemplateURLVector;
81 // Type for a static function pointer that acts as a time source. 94 // Type for a static function pointer that acts as a time source.
82 typedef base::Time(TimeProvider)(); 95 typedef base::Time(TimeProvider)();
83 typedef std::map<std::string, syncer::SyncData> SyncDataMap; 96 typedef std::map<std::string, syncer::SyncData> SyncDataMap;
84 typedef base::CallbackList<void(void)>::Subscription Subscription; 97 typedef base::CallbackList<void(void)>::Subscription Subscription;
85 98
86 // Struct used for initializing the data store with fake data. 99 // Struct used for initializing the data store with fake data.
87 // Each initializer is mapped to a TemplateURL. 100 // Each initializer is mapped to a TemplateURL.
88 struct Initializer { 101 struct Initializer {
89 const char* const keyword; 102 const char* const keyword;
90 const char* const url; 103 const char* const url;
91 const char* const content; 104 const char* const content;
92 }; 105 };
93 106
94 TemplateURLService(Profile* profile, 107 struct URLVisitedDetails {
108 GURL url;
109 bool is_keyword_transition;
110 };
111
112 TemplateURLService(PrefService* prefs,
113 scoped_ptr<SearchTermsData> search_terms_data,
114 KeywordWebDataService* web_data_service,
115 scoped_ptr<KeywordHistoryServiceInterface> history_service,
116 GoogleURLTracker* google_url_tracker,
95 rappor::RapporService* rappor_service, 117 rappor::RapporService* rappor_service,
96 const base::Closure& dsp_change_callback); 118 const base::Closure& dsp_change_callback);
97 // The following is for testing. 119 // The following is for testing.
98 TemplateURLService(const Initializer* initializers, const int count); 120 TemplateURLService(const Initializer* initializers, const int count);
99 virtual ~TemplateURLService(); 121 virtual ~TemplateURLService();
100 122
101 // Creates a TemplateURLData that was previously saved to |prefs| via 123 // Creates a TemplateURLData that was previously saved to |prefs| via
102 // SaveDefaultSearchProviderToPrefs or set via policy. 124 // SaveDefaultSearchProviderToPrefs or set via policy.
103 // Returns true if successful, false otherwise. 125 // Returns true if successful, false otherwise.
104 // If the user or the policy has opted for no default search, this 126 // If the user or the policy has opted for no default search, this
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 virtual void OnWebDataServiceRequestDone( 314 virtual void OnWebDataServiceRequestDone(
293 KeywordWebDataService::Handle h, 315 KeywordWebDataService::Handle h,
294 const WDTypedResult* result) OVERRIDE; 316 const WDTypedResult* result) OVERRIDE;
295 317
296 // Returns the locale-direction-adjusted short name for the given keyword. 318 // Returns the locale-direction-adjusted short name for the given keyword.
297 // Also sets the out param to indicate whether the keyword belongs to an 319 // Also sets the out param to indicate whether the keyword belongs to an
298 // Omnibox extension. 320 // Omnibox extension.
299 base::string16 GetKeywordShortName(const base::string16& keyword, 321 base::string16 GetKeywordShortName(const base::string16& keyword,
300 bool* is_omnibox_api_extension_keyword); 322 bool* is_omnibox_api_extension_keyword);
301 323
302 // content::NotificationObserver implementation. 324 // Called by the history service when a URL is visited.
303 virtual void Observe(int type, 325 void OnHistoryURLVisited(const URLVisitedDetails& details);
304 const content::NotificationSource& source,
305 const content::NotificationDetails& details) OVERRIDE;
306 326
307 // KeyedService implementation. 327 // KeyedService implementation.
308 virtual void Shutdown() OVERRIDE; 328 virtual void Shutdown() OVERRIDE;
309 329
310 // syncer::SyncableService implementation. 330 // syncer::SyncableService implementation.
311 331
312 // Returns all syncable TemplateURLs from this model as SyncData. This should 332 // Returns all syncable TemplateURLs from this model as SyncData. This should
313 // include every search engine and no Extension keywords. 333 // include every search engine and no Extension keywords.
314 virtual syncer::SyncDataList GetAllSyncData( 334 virtual syncer::SyncDataList GetAllSyncData(
315 syncer::ModelType type) const OVERRIDE; 335 syncer::ModelType type) const OVERRIDE;
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 PrefService* prefs); 528 PrefService* prefs);
509 529
510 // If the TemplateURL's sync GUID matches the kSyncedDefaultSearchProviderGUID 530 // If the TemplateURL's sync GUID matches the kSyncedDefaultSearchProviderGUID
511 // preference it will be used to update the DSE in memory and as persisted in 531 // preference it will be used to update the DSE in memory and as persisted in
512 // preferences. 532 // preferences.
513 void MaybeUpdateDSEAfterSync(TemplateURL* synced_turl); 533 void MaybeUpdateDSEAfterSync(TemplateURL* synced_turl);
514 534
515 // Iterates through the TemplateURLs to see if one matches the visited url. 535 // Iterates through the TemplateURLs to see if one matches the visited url.
516 // For each TemplateURL whose url matches the visited url 536 // For each TemplateURL whose url matches the visited url
517 // SetKeywordSearchTermsForURL is invoked. 537 // SetKeywordSearchTermsForURL is invoked.
518 void UpdateKeywordSearchTermsForURL( 538 void UpdateKeywordSearchTermsForURL(const URLVisitedDetails& details);
519 const history::URLVisitedDetails& details);
520 539
521 // If necessary, generates a visit for the site http:// + t_url.keyword(). 540 // If necessary, generates a visit for the site http:// + t_url.keyword().
522 void AddTabToSearchVisit(const TemplateURL& t_url); 541 void AddTabToSearchVisit(const TemplateURL& t_url);
523 542
524 // Invoked when the Google base URL has changed. Updates the mapping for all 543 // Invoked when the Google base URL has changed. Updates the mapping for all
525 // TemplateURLs that have a replacement term of {google:baseURL} or 544 // TemplateURLs that have a replacement term of {google:baseURL} or
526 // {google:baseSuggestURL}. 545 // {google:baseSuggestURL}.
527 void GoogleBaseURLChanged(); 546 void GoogleBaseURLChanged();
528 547
529 // Adds a new TemplateURL to this model. TemplateURLService will own the 548 // Adds a new TemplateURL to this model. TemplateURLService will own the
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 // Finds the extension-supplied TemplateURL that matches |data|, if any. 661 // Finds the extension-supplied TemplateURL that matches |data|, if any.
643 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data, 662 TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data,
644 TemplateURL::Type type); 663 TemplateURL::Type type);
645 664
646 // Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine 665 // Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine
647 // that supports replacement and wants to be default, if any. Notifies the 666 // that supports replacement and wants to be default, if any. Notifies the
648 // DefaultSearchManager, which might change the effective default search 667 // DefaultSearchManager, which might change the effective default search
649 // engine. 668 // engine.
650 void UpdateExtensionDefaultSearchEngine(); 669 void UpdateExtensionDefaultSearchEngine();
651 670
652 content::NotificationRegistrar notification_registrar_;
653 PrefChangeRegistrar pref_change_registrar_; 671 PrefChangeRegistrar pref_change_registrar_;
654 672
655 // Mapping from keyword to the TemplateURL. 673 // Mapping from keyword to the TemplateURL.
656 KeywordToTemplateMap keyword_to_template_map_; 674 KeywordToTemplateMap keyword_to_template_map_;
657 675
658 // Mapping from Sync GUIDs to the TemplateURL. 676 // Mapping from Sync GUIDs to the TemplateURL.
659 GUIDToTemplateMap guid_to_template_map_; 677 GUIDToTemplateMap guid_to_template_map_;
660 678
661 TemplateURLVector template_urls_; 679 TemplateURLVector template_urls_;
662 680
663 ObserverList<TemplateURLServiceObserver> model_observers_; 681 ObserverList<TemplateURLServiceObserver> model_observers_;
664 682
665 // Maps from host to set of TemplateURLs whose search url host is host. 683 // Maps from host to set of TemplateURLs whose search url host is host.
666 // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular 684 // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular
667 // header dependencies. 685 // header dependencies.
668 scoped_ptr<SearchHostToURLsMap> provider_map_; 686 scoped_ptr<SearchHostToURLsMap> provider_map_;
669 687
670 // Used to obtain the WebDataService.
671 // When Load is invoked, if we haven't yet loaded, the WebDataService is
672 // obtained from the Profile. This allows us to lazily access the database.
673 Profile* profile_;
674
675 PrefService* prefs_; 688 PrefService* prefs_;
676 689
690 scoped_ptr<SearchTermsData> search_terms_data_;
Peter Kasting 2014/07/02 20:45:57 Tiny nit: The order of these members feels a littl
hashimoto 2014/07/03 00:05:07 My logic behind this ordering is based on how ofte
Peter Kasting 2014/07/03 00:12:31 Hmm. That ordering is not at all obvious to a rea
hashimoto 2014/07/03 00:36:24 Also this ordering divides these members into 3 ca
691
692 // Service used to store entries.
693 scoped_refptr<KeywordWebDataService> web_data_service_;
694
695 scoped_ptr<KeywordHistoryServiceInterface> history_service_;
696
697 GoogleURLTracker* google_url_tracker_;
698
677 rappor::RapporService* rappor_service_; 699 rappor::RapporService* rappor_service_;
678 700
679 scoped_ptr<SearchTermsData> search_terms_data_;
680
681 // This closure is run when the default search provider is set to Google. 701 // This closure is run when the default search provider is set to Google.
682 base::Closure dsp_change_callback_; 702 base::Closure dsp_change_callback_;
683 703
684 // Whether the keywords have been loaded. 704 // Whether the keywords have been loaded.
685 bool loaded_; 705 bool loaded_;
686 706
687 // Set when the web data service fails to load properly. This prevents 707 // Set when the web data service fails to load properly. This prevents
688 // further communication with sync or writing to prefs, so we don't persist 708 // further communication with sync or writing to prefs, so we don't persist
689 // inconsistent state data anywhere. 709 // inconsistent state data anywhere.
690 bool load_failed_; 710 bool load_failed_;
691 711
692 // If non-zero, we're waiting on a load. 712 // If non-zero, we're waiting on a load.
693 KeywordWebDataService::Handle load_handle_; 713 KeywordWebDataService::Handle load_handle_;
694 714
695 // Service used to store entries.
696 scoped_refptr<KeywordWebDataService> web_data_service_;
697
698 // All visits that occurred before we finished loading. Once loaded 715 // All visits that occurred before we finished loading. Once loaded
699 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector. 716 // UpdateKeywordSearchTermsForURL is invoked for each element of the vector.
700 std::vector<history::URLVisitedDetails> visits_to_add_; 717 std::vector<URLVisitedDetails> visits_to_add_;
701 718
702 // Once loaded, the default search provider. This is a pointer to a 719 // Once loaded, the default search provider. This is a pointer to a
703 // TemplateURL owned by |template_urls_|. 720 // TemplateURL owned by |template_urls_|.
704 TemplateURL* default_search_provider_; 721 TemplateURL* default_search_provider_;
705 722
706 // A temporary location for the DSE until Web Data has been loaded and it can 723 // A temporary location for the DSE until Web Data has been loaded and it can
707 // be merged into |template_urls_|. 724 // be merged into |template_urls_|.
708 scoped_ptr<TemplateURL> initial_default_search_provider_; 725 scoped_ptr<TemplateURL> initial_default_search_provider_;
709 726
710 // Source of the default search provider. 727 // Source of the default search provider.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 767
751 // Helper class to manage the default search engine. 768 // Helper class to manage the default search engine.
752 DefaultSearchManager default_search_manager_; 769 DefaultSearchManager default_search_manager_;
753 770
754 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; 771 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_;
755 772
756 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); 773 DISALLOW_COPY_AND_ASSIGN(TemplateURLService);
757 }; 774 };
758 775
759 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ 776 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698