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_
12 #include "base/callback_forward.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/google/google_url_tracker_map_entry.h"
17 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_fetcher_delegate.h"
23 class GoogleURLTrackerNavigationHelper
;
29 class NavigationController
;
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.
36 // Most consumers should only call GoogleURL(), which is guaranteed to
37 // synchronously return a value at all times (even during startup or in unittest
38 // mode). Consumers who need to be notified when things change should listen to
39 // the notification service for NOTIFICATION_GOOGLE_URL_UPDATED, which provides
40 // the original and updated values.
42 // To protect users' privacy and reduce server load, no updates will be
43 // performed (ever) unless at least one consumer registers interest by calling
44 // RequestServerCheck().
45 class GoogleURLTracker
: public net::URLFetcherDelegate
,
46 public net::NetworkChangeNotifier::IPAddressObserver
,
47 public BrowserContextKeyedService
{
49 // The contents of the Details for a NOTIFICATION_GOOGLE_URL_UPDATED.
50 typedef std::pair
<GURL
, GURL
> UpdatedDetails
;
52 // The constructor does different things depending on which of these values
53 // you pass it. Hopefully these are self-explanatory.
59 // Only the GoogleURLTrackerFactory and tests should call this. No code other
60 // than the GoogleURLTracker itself should actually use
61 // GoogleURLTrackerFactory::GetForProfile().
62 GoogleURLTracker(Profile
* profile
,
63 scoped_ptr
<GoogleURLTrackerNavigationHelper
> nav_helper
,
66 virtual ~GoogleURLTracker();
68 // Returns the current Google URL. This will return a valid URL even if
69 // |profile| is NULL or a testing profile.
71 // This is the only function most code should ever call.
72 static GURL
GoogleURL(Profile
* profile
);
74 // Requests that the tracker perform a server check to update the Google URL
75 // as necessary. If |force| is false, this will happen at most once per
76 // network change, not sooner than five seconds after startup (checks
77 // requested before that time will occur then; checks requested afterwards
78 // will occur immediately, if no other checks have been made during this run).
79 // If |force| is true, and the tracker has already performed any requested
80 // check, it will check again.
82 // When |profile| is NULL or a testing profile, this function does nothing.
83 static void RequestServerCheck(Profile
* profile
, bool force
);
85 // Notifies the tracker that the user has started a Google search.
86 // If prompting is necessary, we then listen for the subsequent pending
87 // navigation to get the appropriate NavigationController. When the load
88 // commits, we'll show the infobar.
90 // When |profile| is NULL or a testing profile, this function does nothing.
91 static void GoogleURLSearchCommitted(Profile
* profile
);
93 // No one but GoogleURLTrackerInfoBarDelegate or test code should call these.
94 void AcceptGoogleURL(bool redo_searches
);
95 void CancelGoogleURL();
96 const GURL
& google_url() const { return google_url_
; }
97 const GURL
& fetched_google_url() const { return fetched_google_url_
; }
99 // No one but GoogleURLTrackerMapEntry should call this.
100 void DeleteMapEntryForService(const InfoBarService
* infobar_service
);
102 // Called by the navigation observer after SearchCommitted() registers
103 // listeners, to indicate that we've received the "load now pending"
104 // notification. |navigation_controller| is the NavigationController for this
105 // load; |infobar_service| is the InfoBarService of the associated tab; and
106 // |pending_id| is the unique ID of the newly pending NavigationEntry.
107 // If there is already a visible GoogleURLTracker infobar for this tab, this
108 // function resets its associated pending entry ID to the new ID. Otherwise
109 // this function creates a map entry for the associated tab.
110 virtual void OnNavigationPending(
111 content::NavigationController
* navigation_controller
,
112 InfoBarService
* infobar_service
,
115 // Called by the navigation observer once a load we're watching commits.
116 // |infobar_service| is the same as for OnNavigationPending();
117 // |search_url| is guaranteed to be valid.
118 virtual void OnNavigationCommitted(InfoBarService
* infobar_service
,
119 const GURL
& search_url
);
121 // Called by the navigation observer when a tab closes.
122 virtual void OnTabClosed(
123 content::NavigationController
* navigation_controller
);
125 static const char kDefaultGoogleHomepage
[];
126 static const char kSearchDomainCheckURL
[];
129 friend class GoogleURLTrackerTest
;
131 typedef std::map
<const InfoBarService
*, GoogleURLTrackerMapEntry
*> EntryMap
;
133 // net::URLFetcherDelegate:
134 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
136 // NetworkChangeNotifier::IPAddressObserver:
137 virtual void OnIPAddressChanged() OVERRIDE
;
139 // BrowserContextKeyedService:
140 virtual void Shutdown() OVERRIDE
;
142 // Registers consumer interest in getting an updated URL from the server.
143 // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL
145 void SetNeedToFetch();
147 // Called when the five second startup sleep has finished. Runs any pending
151 // Starts the fetch of the up-to-date Google URL if we actually want to fetch
152 // it and can currently do so.
153 void StartFetchIfDesirable();
155 // Called each time the user performs a search. This checks whether we need
156 // to prompt the user about a domain change, and if so, starts listening for
157 // the notifications sent when the actual load is triggered.
158 void SearchCommitted();
160 // Closes all map entries. If |redo_searches| is true, this also triggers
161 // each tab with an infobar to re-perform the user's search, but on the new
163 void CloseAllEntries(bool redo_searches
);
165 // Unregisters any listeners for the navigation controller in |map_entry|.
166 // This sanity-DCHECKs that these are registered (or not) in the specific
167 // cases we expect. (|must_be_listening_for_commit| is used purely for this
168 // sanity-checking.) This also unregisters the global navigation pending
169 // listener if there are no remaining listeners for navigation commits, as we
170 // no longer need them until another search is committed.
171 void UnregisterForEntrySpecificNotifications(
172 const GoogleURLTrackerMapEntry
& map_entry
,
173 bool must_be_listening_for_commit
);
177 scoped_ptr
<GoogleURLTrackerNavigationHelper
> nav_helper_
;
179 // Creates an infobar and adds it to the provided InfoBarService. Returns the
180 // infobar on success or NULL on failure. The caller does not own the
181 // returned object, the InfoBarService does.
182 base::Callback
<InfoBar
*(InfoBarService
*, GoogleURLTracker
*, const GURL
&)>
186 GURL fetched_google_url_
;
187 scoped_ptr
<net::URLFetcher
> fetcher_
;
189 bool in_startup_sleep_
; // True if we're in the five-second "no fetching"
190 // period that begins at browser start.
191 bool already_fetched_
; // True if we've already fetched a URL once this run;
192 // we won't fetch again until after a restart.
193 bool need_to_fetch_
; // True if a consumer actually wants us to fetch an
194 // updated URL. If this is never set, we won't
195 // bother to fetch anything.
196 // Consumers should observe
197 // chrome::NOTIFICATION_GOOGLE_URL_UPDATED.
198 bool need_to_prompt_
; // True if the last fetched Google URL is not
199 // matched with current user's default Google URL
200 // nor the last prompted Google URL.
201 bool search_committed_
; // True when we're expecting a notification of a new
202 // pending search navigation.
204 base::WeakPtrFactory
<GoogleURLTracker
> weak_ptr_factory_
;
206 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker
);
209 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_