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/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" 21 #include "url/gurl.h" 22 23 class GoogleURLTrackerNavigationHelper; 24 class InfoBar; 25 class PrefService; 26 class Profile; 27 28 namespace content { 29 class NavigationController; 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 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. 41 // 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 { 48 public: 49 // The contents of the Details for a NOTIFICATION_GOOGLE_URL_UPDATED. 50 typedef std::pair<GURL, GURL> UpdatedDetails; 51 52 // The constructor does different things depending on which of these values 53 // you pass it. Hopefully these are self-explanatory. 54 enum Mode { 55 NORMAL_MODE, 56 UNIT_TEST_MODE, 57 }; 58 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, 64 Mode mode); 65 66 virtual ~GoogleURLTracker(); 67 68 // Returns the current Google URL. This will return a valid URL even if 69 // |profile| is NULL or a testing profile. 70 // 71 // This is the only function most code should ever call. 72 static GURL GoogleURL(Profile* profile); 73 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. 81 // 82 // When |profile| is NULL or a testing profile, this function does nothing. 83 static void RequestServerCheck(Profile* profile, bool force); 84 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. 89 // 90 // When |profile| is NULL or a testing profile, this function does nothing. 91 static void GoogleURLSearchCommitted(Profile* profile); 92 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_; } 98 99 // No one but GoogleURLTrackerMapEntry should call this. 100 void DeleteMapEntryForService(const InfoBarService* infobar_service); 101 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, 113 int pending_id); 114 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); 120 121 // Called by the navigation observer when a tab closes. 122 virtual void OnTabClosed( 123 content::NavigationController* navigation_controller); 124 125 static const char kDefaultGoogleHomepage[]; 126 static const char kSearchDomainCheckURL[]; 127 128 private: 129 friend class GoogleURLTrackerTest; 130 131 typedef std::map<const InfoBarService*, GoogleURLTrackerMapEntry*> EntryMap; 132 133 // net::URLFetcherDelegate: 134 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 135 136 // NetworkChangeNotifier::IPAddressObserver: 137 virtual void OnIPAddressChanged() OVERRIDE; 138 139 // BrowserContextKeyedService: 140 virtual void Shutdown() OVERRIDE; 141 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 144 // changes. 145 void SetNeedToFetch(); 146 147 // Called when the five second startup sleep has finished. Runs any pending 148 // fetch. 149 void FinishSleep(); 150 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(); 154 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(); 159 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 162 // Google TLD. 163 void CloseAllEntries(bool redo_searches); 164 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); 174 175 Profile* profile_; 176 177 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper_; 178 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&)> 183 infobar_creator_; 184 185 GURL google_url_; 186 GURL fetched_google_url_; 187 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; 188 scoped_ptr<net::URLFetcher> fetcher_; 189 int fetcher_id_; 190 bool in_startup_sleep_; // True if we're in the five-second "no fetching" 191 // period that begins at browser start. 192 bool already_fetched_; // True if we've already fetched a URL once this run; 193 // we won't fetch again until after a restart. 194 bool need_to_fetch_; // True if a consumer actually wants us to fetch an 195 // updated URL. If this is never set, we won't 196 // bother to fetch anything. 197 // Consumers should observe 198 // chrome::NOTIFICATION_GOOGLE_URL_UPDATED. 199 bool need_to_prompt_; // True if the last fetched Google URL is not 200 // matched with current user's default Google URL 201 // nor the last prompted Google URL. 202 bool search_committed_; // True when we're expecting a notification of a new 203 // pending search navigation. 204 EntryMap entry_map_; 205 206 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); 207 }; 208 209 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ 210