[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / google / google_url_tracker.h
blob5e8ba1aea1d238f3f5b3ee030f81f3225ad850bf
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.
5 #ifndef CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_
6 #define CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_
8 #include <map>
9 #include <string>
10 #include <utility>
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"
24 class GoogleURLTrackerClient;
25 class GoogleURLTrackerNavigationHelper;
26 class PrefService;
27 class Profile;
29 namespace content {
30 class NavigationController;
33 namespace infobars {
34 class InfoBar;
37 // This object is responsible for checking the Google URL once per network
38 // change, and if necessary prompting the user to see if they want to change to
39 // using it. The current and last prompted values are saved to prefs.
41 // Most consumers should only call GoogleURL(), which is guaranteed to
42 // synchronously return a value at all times (even during startup or in unittest
43 // mode). Consumers who need to be notified when things change should register
44 // a callback that provides the original and updated values via
45 // RegisterCallback().
47 // To protect users' privacy and reduce server load, no updates will be
48 // performed (ever) unless at least one consumer registers interest by calling
49 // RequestServerCheck().
50 class GoogleURLTracker : public net::URLFetcherDelegate,
51 public net::NetworkChangeNotifier::IPAddressObserver,
52 public KeyedService {
53 public:
54 // Callback that is called when the Google URL is updated. The arguments are
55 // the old and new URLs.
56 typedef base::Callback<void(GURL, GURL)> OnGoogleURLUpdatedCallback;
57 typedef base::CallbackList<void(GURL, GURL)> CallbackList;
58 typedef CallbackList::Subscription Subscription;
60 // The constructor does different things depending on which of these values
61 // you pass it. Hopefully these are self-explanatory.
62 enum Mode {
63 NORMAL_MODE,
64 UNIT_TEST_MODE,
67 static const char kDefaultGoogleHomepage[];
68 static const char kSearchDomainCheckURL[];
70 // Only the GoogleURLTrackerFactory and tests should call this. No code other
71 // than the GoogleURLTracker itself should actually use
72 // GoogleURLTrackerFactory::GetForProfile().
73 GoogleURLTracker(Profile* profile,
74 scoped_ptr<GoogleURLTrackerClient> client,
75 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper,
76 Mode mode);
78 virtual ~GoogleURLTracker();
80 // Returns the current Google URL. This will return a valid URL even if
81 // |profile| is NULL or a testing profile.
83 // This is the only function most code should ever call.
84 static GURL GoogleURL(Profile* profile);
86 // Requests that the tracker perform a server check to update the Google URL
87 // as necessary. If |force| is false, this will happen at most once per
88 // network change, not sooner than five seconds after startup (checks
89 // requested before that time will occur then; checks requested afterwards
90 // will occur immediately, if no other checks have been made during this run).
91 // If |force| is true, and the tracker has already performed any requested
92 // check, it will check again.
94 // When |profile| is NULL or a testing profile, this function does nothing.
95 static void RequestServerCheck(Profile* profile, bool force);
97 // Notifies the tracker that the user has started a Google search.
98 // If prompting is necessary, we then listen for the subsequent pending
99 // navigation to get the appropriate NavigationController. When the load
100 // commits, we'll show the infobar.
102 // When |profile| is NULL or a testing profile, this function does nothing.
103 static void GoogleURLSearchCommitted(Profile* profile);
105 // No one but GoogleURLTrackerInfoBarDelegate or test code should call these.
106 void AcceptGoogleURL(bool redo_searches);
107 void CancelGoogleURL();
108 const GURL& google_url() const { return google_url_; }
109 const GURL& fetched_google_url() const { return fetched_google_url_; }
111 // No one but GoogleURLTrackerMapEntry should call this.
112 void DeleteMapEntryForService(const InfoBarService* infobar_service);
114 // Called by the client after SearchCommitted() registers listeners, to
115 // indicate that we've received the "load now pending" notification.
116 // |navigation_controller| is the NavigationController for this load;
117 // |infobar_service| is the InfoBarService of the associated tab; and
118 // |pending_id| is the unique ID of the newly pending NavigationEntry. If
119 // there is already a visible GoogleURLTracker infobar for this tab, this
120 // function resets its associated pending entry ID to the new ID. Otherwise
121 // this function creates a map entry for the associated tab.
122 virtual void OnNavigationPending(
123 content::NavigationController* navigation_controller,
124 InfoBarService* infobar_service,
125 int pending_id);
127 // Called by the navigation observer once a load we're watching commits.
128 // |infobar_service| is the same as for OnNavigationPending();
129 // |search_url| is guaranteed to be valid.
130 virtual void OnNavigationCommitted(InfoBarService* infobar_service,
131 const GURL& search_url);
133 // Called by the navigation observer when a tab closes.
134 virtual void OnTabClosed(
135 content::NavigationController* navigation_controller);
137 scoped_ptr<Subscription> RegisterCallback(
138 const OnGoogleURLUpdatedCallback& cb);
140 private:
141 friend class GoogleURLTrackerTest;
143 typedef std::map<const InfoBarService*, GoogleURLTrackerMapEntry*> EntryMap;
145 // net::URLFetcherDelegate:
146 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
148 // NetworkChangeNotifier::IPAddressObserver:
149 virtual void OnIPAddressChanged() OVERRIDE;
151 // KeyedService:
152 virtual void Shutdown() OVERRIDE;
154 // Registers consumer interest in getting an updated URL from the server.
155 // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL
156 // changes.
157 void SetNeedToFetch();
159 // Called when the five second startup sleep has finished. Runs any pending
160 // fetch.
161 void FinishSleep();
163 // Starts the fetch of the up-to-date Google URL if we actually want to fetch
164 // it and can currently do so.
165 void StartFetchIfDesirable();
167 // Called each time the user performs a search. This checks whether we need
168 // to prompt the user about a domain change, and if so, starts listening for
169 // the notifications sent when the actual load is triggered.
170 void SearchCommitted();
172 // Closes all map entries. If |redo_searches| is true, this also triggers
173 // each tab with an infobar to re-perform the user's search, but on the new
174 // Google TLD.
175 void CloseAllEntries(bool redo_searches);
177 // Unregisters any listeners for the navigation controller in |map_entry|.
178 // This sanity-DCHECKs that these are registered (or not) in the specific
179 // cases we expect. (|must_be_listening_for_commit| is used purely for this
180 // sanity-checking.) This also unregisters the global navigation pending
181 // listener if there are no remaining listeners for navigation commits, as we
182 // no longer need them until another search is committed.
183 void UnregisterForEntrySpecificNotifications(
184 const GoogleURLTrackerMapEntry& map_entry,
185 bool must_be_listening_for_commit);
187 void NotifyGoogleURLUpdated(GURL old_url, GURL new_url);
189 CallbackList callback_list_;
191 Profile* profile_;
193 scoped_ptr<GoogleURLTrackerClient> client_;
195 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper_;
197 // Creates an infobar and adds it to the provided InfoBarService. Returns the
198 // infobar on success or NULL on failure. The caller does not own the
199 // returned object, the InfoBarService does.
200 base::Callback<
201 infobars::InfoBar*(InfoBarService*, GoogleURLTracker*, const GURL&)>
202 infobar_creator_;
204 GURL google_url_;
205 GURL fetched_google_url_;
206 scoped_ptr<net::URLFetcher> fetcher_;
207 int fetcher_id_;
208 bool in_startup_sleep_; // True if we're in the five-second "no fetching"
209 // period that begins at browser start.
210 bool already_fetched_; // True if we've already fetched a URL once this run;
211 // we won't fetch again until after a restart.
212 bool need_to_fetch_; // True if a consumer actually wants us to fetch an
213 // updated URL. If this is never set, we won't
214 // bother to fetch anything.
215 // Consumers should register a callback via
216 // RegisterCallback().
217 bool need_to_prompt_; // True if the last fetched Google URL is not
218 // matched with current user's default Google URL
219 // nor the last prompted Google URL.
220 bool search_committed_; // True when we're expecting a notification of a new
221 // pending search navigation.
222 EntryMap entry_map_;
223 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_;
225 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker);
228 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_