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 3// found in the LICENSE file. 4 5#ifndef COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ 6#define COMPONENTS_GOOGLE_CORE_BROWSER_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 "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" 20#include "net/base/network_change_notifier.h" 21#include "net/url_request/url_fetcher.h" 22#include "net/url_request/url_fetcher_delegate.h" 23#include "url/gurl.h" 24 25class GoogleURLTrackerNavigationHelper; 26class PrefService; 27 28namespace infobars { 29class 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(). 43class GoogleURLTracker 44 : public net::URLFetcherDelegate, 45 public net::NetworkChangeNotifier::NetworkChangeObserver, 46 public KeyedService { 47 public: 48 // Callback that is called when the Google URL is updated. The arguments are 49 // the old and new URLs. 50 typedef base::Callback<void()> OnGoogleURLUpdatedCallback; 51 typedef base::CallbackList<void()> CallbackList; 52 typedef CallbackList::Subscription Subscription; 53 54 // The constructor does different things depending on which of these values 55 // you pass it. Hopefully these are self-explanatory. 56 enum Mode { 57 NORMAL_MODE, 58 UNIT_TEST_MODE, 59 }; 60 61 static const char kDefaultGoogleHomepage[]; 62 63 // Only the GoogleURLTrackerFactory and tests should call this. 64 GoogleURLTracker(scoped_ptr<GoogleURLTrackerClient> client, Mode mode); 65 66 virtual ~GoogleURLTracker(); 67 68 // Returns the current Google homepage URL. 69 const GURL& google_url() const { return google_url_; } 70 71 // 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 73 // network change, not sooner than five seconds after startup (checks 74 // requested before that time will occur then; checks requested afterwards 75 // 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 77 // check, it will check again. 78 void RequestServerCheck(bool force); 79 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( 119 const OnGoogleURLUpdatedCallback& cb); 120 121 private: 122 friend class GoogleURLTrackerTest; 123 friend class SyncTest; 124 125 typedef std::map<const infobars::InfoBarManager*, GoogleURLTrackerMapEntry*> 126 EntryMap; 127 128 static const char kSearchDomainCheckURL[]; 129 130 // net::URLFetcherDelegate: 131 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 132 133 // NetworkChangeNotifier::IPAddressObserver: 134 virtual void OnNetworkChanged( 135 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; 136 137 // KeyedService: 138 virtual void Shutdown() OVERRIDE; 139 140 // Registers consumer interest in getting an updated URL from the server. 141 // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL 142 // changes. 143 void SetNeedToFetch(); 144 145 // Called when the five second startup sleep has finished. Runs any pending 146 // fetch. 147 void FinishSleep(); 148 149 // Starts the fetch of the up-to-date Google URL if we actually want to fetch 150 // it and can currently do so. 151 void StartFetchIfDesirable(); 152 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_; 171 172 scoped_ptr<GoogleURLTrackerClient> client_; 173 174 GURL google_url_; 175 GURL fetched_google_url_; 176 scoped_ptr<net::URLFetcher> fetcher_; 177 int fetcher_id_; 178 bool in_startup_sleep_; // True if we're in the five-second "no fetching" 179 // period that begins at browser start. 180 bool already_fetched_; // True if we've already fetched a URL once this run; 181 // we won't fetch again until after a restart. 182 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 184 // bother to fetch anything. 185 // Consumers should register a callback via 186 // 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_; 194 195 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); 196}; 197 198#endif // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ 199