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_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ | 5 #ifndef COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ |
6 #define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ | 6 #define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ |
7 | 7 |
8 #include <map> | |
9 #include <string> | |
10 #include <utility> | |
11 | |
12 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
13 #include "base/callback_list.h" | 9 #include "base/callback_list.h" |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
17 #include "components/google/core/browser/google_url_tracker_client.h" | 12 #include "components/google/core/browser/google_url_tracker_client.h" |
18 #include "components/google/core/browser/google_url_tracker_map_entry.h" | |
19 #include "components/keyed_service/core/keyed_service.h" | 13 #include "components/keyed_service/core/keyed_service.h" |
20 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
21 #include "net/url_request/url_fetcher.h" | 15 #include "net/url_request/url_fetcher.h" |
22 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
23 #include "url/gurl.h" | 17 #include "url/gurl.h" |
24 | 18 |
25 class GoogleURLTrackerNavigationHelper; | 19 class GoogleURLTrackerNavigationHelper; |
26 class PrefService; | 20 class PrefService; |
27 | 21 |
28 namespace infobars { | 22 namespace infobars { |
29 class InfoBar; | 23 class InfoBar; |
30 } | 24 } |
31 | 25 |
32 // This object is responsible for checking the Google URL once per network | 26 // This object is responsible for checking the Google URL once per network |
33 // change, and if necessary prompting the user to see if they want to change to | 27 // change. The current values is saved to prefs. |
Ilya Sherman
2015/01/06 00:39:58
nit: "values is" -> "value is"
| |
34 // using it. The current and last prompted values are saved to prefs. | |
35 // | 28 // |
36 // Most consumers should only call google_url(). Consumers who need to be | 29 // Most consumers should only call google_url(). Consumers who need to be |
37 // notified when things change should register a callback that provides the | 30 // notified when things change should register a callback that provides the |
38 // original and updated values via RegisterCallback(). | 31 // original and updated values via RegisterCallback(). |
39 // | 32 // |
40 // To protect users' privacy and reduce server load, no updates will be | 33 // To protect users' privacy and reduce server load, no updates will be |
41 // performed (ever) unless at least one consumer registers interest by calling | 34 // performed (ever) unless at least one consumer registers interest by calling |
42 // RequestServerCheck(). | 35 // RequestServerCheck(). |
43 class GoogleURLTracker | 36 class GoogleURLTracker |
44 : public net::URLFetcherDelegate, | 37 : public net::URLFetcherDelegate, |
(...skipping 25 matching lines...) Expand all Loading... | |
70 | 63 |
71 // Requests that the tracker perform a server check to update the Google URL | 64 // Requests that the tracker perform a server check to update the Google URL |
72 // as necessary. If |force| is false, this will happen at most once per | 65 // as necessary. If |force| is false, this will happen at most once per |
73 // network change, not sooner than five seconds after startup (checks | 66 // network change, not sooner than five seconds after startup (checks |
74 // requested before that time will occur then; checks requested afterwards | 67 // requested before that time will occur then; checks requested afterwards |
75 // will occur immediately, if no other checks have been made during this run). | 68 // will occur immediately, if no other checks have been made during this run). |
76 // If |force| is true, and the tracker has already performed any requested | 69 // If |force| is true, and the tracker has already performed any requested |
77 // check, it will check again. | 70 // check, it will check again. |
78 void RequestServerCheck(bool force); | 71 void RequestServerCheck(bool force); |
79 | 72 |
80 // Notifies the tracker that the user has started a Google search. | |
81 // If prompting is necessary, we then listen for the subsequent pending | |
82 // navigation to get the appropriate NavigationHelper. When the load | |
83 // commits, we'll show the infobar. | |
84 void SearchCommitted(); | |
85 | |
86 // No one but GoogleURLTrackerInfoBarDelegate or test code should call these. | |
87 void AcceptGoogleURL(bool redo_searches); | |
88 void CancelGoogleURL(); | |
89 const GURL& fetched_google_url() const { return fetched_google_url_; } | |
90 GoogleURLTrackerClient* client() { return client_.get(); } | |
91 | |
92 // No one but GoogleURLTrackerMapEntry should call this. | |
93 void DeleteMapEntryForManager( | |
94 const infobars::InfoBarManager* infobar_manager); | |
95 | |
96 // Called by the client after SearchCommitted() registers listeners, | |
97 // to indicate that we've received the "load now pending" notification. | |
98 // |nav_helper| is the GoogleURLTrackerNavigationHelper associated with this | |
99 // navigation; |infobar_manager| is the InfoBarManager of the associated tab; | |
100 // and |pending_id| is the unique ID of the newly pending NavigationEntry. | |
101 // If there is already a visible GoogleURLTracker infobar for this tab, this | |
102 // function resets its associated pending entry ID to the new ID. Otherwise | |
103 // this function creates a map entry for the associated tab. | |
104 virtual void OnNavigationPending( | |
105 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper, | |
106 infobars::InfoBarManager* infobar_manager, | |
107 int pending_id); | |
108 | |
109 // Called by the navigation observer once a load we're watching commits. | |
110 // |infobar_manager| is the same as for OnNavigationPending(); | |
111 // |search_url| is guaranteed to be valid. | |
112 virtual void OnNavigationCommitted(infobars::InfoBarManager* infobar_manager, | |
113 const GURL& search_url); | |
114 | |
115 // Called by the navigation observer when a tab closes. | |
116 virtual void OnTabClosed(GoogleURLTrackerNavigationHelper* nav_helper); | |
117 | |
118 scoped_ptr<Subscription> RegisterCallback( | 73 scoped_ptr<Subscription> RegisterCallback( |
119 const OnGoogleURLUpdatedCallback& cb); | 74 const OnGoogleURLUpdatedCallback& cb); |
120 | 75 |
121 private: | 76 private: |
122 friend class GoogleURLTrackerTest; | 77 friend class GoogleURLTrackerTest; |
123 friend class SyncTest; | 78 friend class SyncTest; |
124 | 79 |
125 typedef std::map<const infobars::InfoBarManager*, GoogleURLTrackerMapEntry*> | |
126 EntryMap; | |
127 | |
128 static const char kSearchDomainCheckURL[]; | 80 static const char kSearchDomainCheckURL[]; |
129 | 81 |
130 // net::URLFetcherDelegate: | 82 // net::URLFetcherDelegate: |
131 void OnURLFetchComplete(const net::URLFetcher* source) override; | 83 void OnURLFetchComplete(const net::URLFetcher* source) override; |
132 | 84 |
133 // NetworkChangeNotifier::IPAddressObserver: | 85 // NetworkChangeNotifier::IPAddressObserver: |
134 void OnNetworkChanged( | 86 void OnNetworkChanged( |
135 net::NetworkChangeNotifier::ConnectionType type) override; | 87 net::NetworkChangeNotifier::ConnectionType type) override; |
136 | 88 |
137 // KeyedService: | 89 // KeyedService: |
138 void Shutdown() override; | 90 void Shutdown() override; |
139 | 91 |
140 // Registers consumer interest in getting an updated URL from the server. | 92 // Sets |need_to_fetch_| and attempts to start a fetch. |
141 // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL | |
142 // changes. | |
143 void SetNeedToFetch(); | 93 void SetNeedToFetch(); |
144 | 94 |
145 // Called when the five second startup sleep has finished. Runs any pending | 95 // Called when the five second startup sleep has finished. Runs any pending |
146 // fetch. | 96 // fetch. |
147 void FinishSleep(); | 97 void FinishSleep(); |
148 | 98 |
149 // Starts the fetch of the up-to-date Google URL if we actually want to fetch | 99 // Starts the fetch of the up-to-date Google URL if we actually want to fetch |
150 // it and can currently do so. | 100 // it and can currently do so. |
151 void StartFetchIfDesirable(); | 101 void StartFetchIfDesirable(); |
152 | 102 |
153 // Closes all map entries. If |redo_searches| is true, this also triggers | |
154 // each tab with an infobar to re-perform the user's search, but on the new | |
155 // Google TLD. | |
156 void CloseAllEntries(bool redo_searches); | |
157 | |
158 // Unregisters any listeners for the navigation helper in |map_entry|. | |
159 // This sanity-DCHECKs that these are registered (or not) in the specific | |
160 // cases we expect. (|must_be_listening_for_commit| is used purely for this | |
161 // sanity-checking.) This also unregisters the global navigation pending | |
162 // listener if there are no remaining listeners for navigation commits, as we | |
163 // no longer need them until another search is committed. | |
164 void UnregisterForEntrySpecificNotifications( | |
165 GoogleURLTrackerMapEntry* map_entry, | |
166 bool must_be_listening_for_commit); | |
167 | |
168 void NotifyGoogleURLUpdated(); | |
169 | |
170 CallbackList callback_list_; | 103 CallbackList callback_list_; |
171 | 104 |
172 scoped_ptr<GoogleURLTrackerClient> client_; | 105 scoped_ptr<GoogleURLTrackerClient> client_; |
173 | 106 |
174 GURL google_url_; | 107 GURL google_url_; |
175 GURL fetched_google_url_; | |
176 scoped_ptr<net::URLFetcher> fetcher_; | 108 scoped_ptr<net::URLFetcher> fetcher_; |
177 int fetcher_id_; | 109 int fetcher_id_; |
178 bool in_startup_sleep_; // True if we're in the five-second "no fetching" | 110 bool in_startup_sleep_; // True if we're in the five-second "no fetching" |
179 // period that begins at browser start. | 111 // period that begins at browser start. |
180 bool already_fetched_; // True if we've already fetched a URL once this run; | 112 bool already_fetched_; // True if we've already fetched a URL once this run; |
181 // we won't fetch again until after a restart. | 113 // we won't fetch again until after a restart. |
182 bool need_to_fetch_; // True if a consumer actually wants us to fetch an | 114 bool need_to_fetch_; // True if a consumer actually wants us to fetch an |
183 // updated URL. If this is never set, we won't | 115 // updated URL. If this is never set, we won't |
184 // bother to fetch anything. | 116 // bother to fetch anything. |
185 // Consumers should register a callback via | 117 // Consumers should register a callback via |
186 // RegisterCallback(). | 118 // RegisterCallback(). |
187 bool need_to_prompt_; // True if the last fetched Google URL is not | |
188 // matched with current user's default Google URL | |
189 // nor the last prompted Google URL. | |
190 bool search_committed_; // True when we're expecting a notification of a new | |
191 // pending search navigation. | |
192 EntryMap entry_map_; | |
193 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; | 119 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; |
194 | 120 |
195 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); | 121 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); |
196 }; | 122 }; |
197 | 123 |
198 #endif // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ | 124 #endif // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ |
OLD | NEW |