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