| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ |
| 6 #define CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ | 6 #define CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 class Profile; | 27 class Profile; |
| 28 | 28 |
| 29 namespace infobars { | 29 namespace infobars { |
| 30 class InfoBar; | 30 class InfoBar; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // This object is responsible for checking the Google URL once per network | 33 // This object is responsible for checking the Google URL once per network |
| 34 // change, and if necessary prompting the user to see if they want to change to | 34 // change, and if necessary prompting the user to see if they want to change to |
| 35 // using it. The current and last prompted values are saved to prefs. | 35 // using it. The current and last prompted values are saved to prefs. |
| 36 // | 36 // |
| 37 // Most consumers should only call GoogleURL(), which is guaranteed to | 37 // Most consumers should only call google_url(). Consumers who need to be |
| 38 // synchronously return a value at all times (even during startup or in unittest | 38 // notified when things change should register a callback that provides the |
| 39 // mode). Consumers who need to be notified when things change should register | 39 // original and updated values via RegisterCallback(). |
| 40 // a callback that provides the original and updated values via | |
| 41 // RegisterCallback(). | |
| 42 // | 40 // |
| 43 // To protect users' privacy and reduce server load, no updates will be | 41 // To protect users' privacy and reduce server load, no updates will be |
| 44 // performed (ever) unless at least one consumer registers interest by calling | 42 // performed (ever) unless at least one consumer registers interest by calling |
| 45 // RequestServerCheck(). | 43 // RequestServerCheck(). |
| 46 class GoogleURLTracker : public net::URLFetcherDelegate, | 44 class GoogleURLTracker : public net::URLFetcherDelegate, |
| 47 public net::NetworkChangeNotifier::IPAddressObserver, | 45 public net::NetworkChangeNotifier::IPAddressObserver, |
| 48 public KeyedService { | 46 public KeyedService { |
| 49 public: | 47 public: |
| 50 // Callback that is called when the Google URL is updated. The arguments are | 48 // Callback that is called when the Google URL is updated. The arguments are |
| 51 // the old and new URLs. | 49 // the old and new URLs. |
| 52 typedef base::Callback<void(GURL, GURL)> OnGoogleURLUpdatedCallback; | 50 typedef base::Callback<void(GURL, GURL)> OnGoogleURLUpdatedCallback; |
| 53 typedef base::CallbackList<void(GURL, GURL)> CallbackList; | 51 typedef base::CallbackList<void(GURL, GURL)> CallbackList; |
| 54 typedef CallbackList::Subscription Subscription; | 52 typedef CallbackList::Subscription Subscription; |
| 55 | 53 |
| 56 // The constructor does different things depending on which of these values | 54 // The constructor does different things depending on which of these values |
| 57 // you pass it. Hopefully these are self-explanatory. | 55 // you pass it. Hopefully these are self-explanatory. |
| 58 enum Mode { | 56 enum Mode { |
| 59 NORMAL_MODE, | 57 NORMAL_MODE, |
| 60 UNIT_TEST_MODE, | 58 UNIT_TEST_MODE, |
| 61 }; | 59 }; |
| 62 | 60 |
| 63 static const char kDefaultGoogleHomepage[]; | 61 static const char kDefaultGoogleHomepage[]; |
| 64 static const char kSearchDomainCheckURL[]; | |
| 65 | 62 |
| 66 // Only the GoogleURLTrackerFactory and tests should call this. No code other | 63 // Only the GoogleURLTrackerFactory and tests should call this. |
| 67 // than the GoogleURLTracker itself should actually use | |
| 68 // GoogleURLTrackerFactory::GetForProfile(). | |
| 69 GoogleURLTracker(Profile* profile, | 64 GoogleURLTracker(Profile* profile, |
| 70 scoped_ptr<GoogleURLTrackerClient> client, | 65 scoped_ptr<GoogleURLTrackerClient> client, |
| 71 Mode mode); | 66 Mode mode); |
| 72 | 67 |
| 73 virtual ~GoogleURLTracker(); | 68 virtual ~GoogleURLTracker(); |
| 74 | 69 |
| 75 // Returns the current Google URL. This will return a valid URL even if | 70 // Returns the current Google homepage URL. |
| 76 // |profile| is NULL or a testing profile. | 71 const GURL& google_url() const { return google_url_; } |
| 77 // | |
| 78 // This is the only function most code should ever call. | |
| 79 static GURL GoogleURL(Profile* profile); | |
| 80 | 72 |
| 81 // Requests that the tracker perform a server check to update the Google URL | 73 // Requests that the tracker perform a server check to update the Google URL |
| 82 // as necessary. If |force| is false, this will happen at most once per | 74 // as necessary. If |force| is false, this will happen at most once per |
| 83 // network change, not sooner than five seconds after startup (checks | 75 // network change, not sooner than five seconds after startup (checks |
| 84 // requested before that time will occur then; checks requested afterwards | 76 // requested before that time will occur then; checks requested afterwards |
| 85 // will occur immediately, if no other checks have been made during this run). | 77 // will occur immediately, if no other checks have been made during this run). |
| 86 // If |force| is true, and the tracker has already performed any requested | 78 // If |force| is true, and the tracker has already performed any requested |
| 87 // check, it will check again. | 79 // check, it will check again. |
| 88 // | 80 void RequestServerCheck(bool force); |
| 89 // When |profile| is NULL or a testing profile, this function does nothing. | |
| 90 static void RequestServerCheck(Profile* profile, bool force); | |
| 91 | 81 |
| 92 // Notifies the tracker that the user has started a Google search. | 82 // Notifies the tracker that the user has started a Google search. |
| 93 // If prompting is necessary, we then listen for the subsequent pending | 83 // If prompting is necessary, we then listen for the subsequent pending |
| 94 // navigation to get the appropriate NavigationController. When the load | 84 // navigation to get the appropriate NavigationHelper. When the load |
| 95 // commits, we'll show the infobar. | 85 // commits, we'll show the infobar. |
| 96 // | 86 void SearchCommitted(); |
| 97 // When |profile| is NULL or a testing profile, this function does nothing. | |
| 98 static void GoogleURLSearchCommitted(Profile* profile); | |
| 99 | 87 |
| 100 // No one but GoogleURLTrackerInfoBarDelegate or test code should call these. | 88 // No one but GoogleURLTrackerInfoBarDelegate or test code should call these. |
| 101 void AcceptGoogleURL(bool redo_searches); | 89 void AcceptGoogleURL(bool redo_searches); |
| 102 void CancelGoogleURL(); | 90 void CancelGoogleURL(); |
| 103 const GURL& google_url() const { return google_url_; } | |
| 104 const GURL& fetched_google_url() const { return fetched_google_url_; } | 91 const GURL& fetched_google_url() const { return fetched_google_url_; } |
| 105 | 92 |
| 106 // No one but GoogleURLTrackerMapEntry should call this. | 93 // No one but GoogleURLTrackerMapEntry should call this. |
| 107 void DeleteMapEntryForManager( | 94 void DeleteMapEntryForManager( |
| 108 const infobars::InfoBarManager* infobar_manager); | 95 const infobars::InfoBarManager* infobar_manager); |
| 109 | 96 |
| 110 // Called by the client after SearchCommitted() registers listeners, to | 97 // Called by the client after SearchCommitted() registers listeners, |
| 111 // indicate that we've received the "load now pending" notification. | 98 // to indicate that we've received the "load now pending" notification. |
| 112 // |nav_helper| is the GoogleURLTrackerNavigationHelper associated with this | 99 // |nav_helper| is the GoogleURLTrackerNavigationHelper associated with this |
| 113 // navigation; |infobar_manager| is the InfoBarManager of the associated tab; | 100 // navigation; |infobar_manager| is the InfoBarManager of the associated tab; |
| 114 // and |pending_id| is the unique ID of the newly pending NavigationEntry. | 101 // and |pending_id| is the unique ID of the newly pending NavigationEntry. |
| 115 // If there is already a visible GoogleURLTracker infobar for this tab, this | 102 // If there is already a visible GoogleURLTracker infobar for this tab, this |
| 116 // function resets its associated pending entry ID to the new ID. Otherwise | 103 // function resets its associated pending entry ID to the new ID. Otherwise |
| 117 // this function creates a map entry for the associated tab. | 104 // this function creates a map entry for the associated tab. |
| 118 virtual void OnNavigationPending( | 105 virtual void OnNavigationPending( |
| 119 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper, | 106 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper, |
| 120 infobars::InfoBarManager* infobar_manager, | 107 infobars::InfoBarManager* infobar_manager, |
| 121 int pending_id); | 108 int pending_id); |
| 122 | 109 |
| 123 // Called by the navigation observer once a load we're watching commits. | 110 // Called by the navigation observer once a load we're watching commits. |
| 124 // |infobar_manager| is the same as for OnNavigationPending(); | 111 // |infobar_manager| is the same as for OnNavigationPending(); |
| 125 // |search_url| is guaranteed to be valid. | 112 // |search_url| is guaranteed to be valid. |
| 126 virtual void OnNavigationCommitted(infobars::InfoBarManager* infobar_manager, | 113 virtual void OnNavigationCommitted(infobars::InfoBarManager* infobar_manager, |
| 127 const GURL& search_url); | 114 const GURL& search_url); |
| 128 | 115 |
| 129 // Called by the navigation observer when a tab closes. | 116 // Called by the navigation observer when a tab closes. |
| 130 virtual void OnTabClosed(GoogleURLTrackerNavigationHelper* nav_helper); | 117 virtual void OnTabClosed(GoogleURLTrackerNavigationHelper* nav_helper); |
| 131 | 118 |
| 132 scoped_ptr<Subscription> RegisterCallback( | 119 scoped_ptr<Subscription> RegisterCallback( |
| 133 const OnGoogleURLUpdatedCallback& cb); | 120 const OnGoogleURLUpdatedCallback& cb); |
| 134 | 121 |
| 135 private: | 122 private: |
| 136 friend class GoogleURLTrackerTest; | 123 friend class GoogleURLTrackerTest; |
| 124 friend class SyncTest; |
| 137 | 125 |
| 138 typedef std::map<const infobars::InfoBarManager*, GoogleURLTrackerMapEntry*> | 126 typedef std::map<const infobars::InfoBarManager*, GoogleURLTrackerMapEntry*> |
| 139 EntryMap; | 127 EntryMap; |
| 140 | 128 |
| 129 static const char kSearchDomainCheckURL[]; |
| 130 |
| 141 // net::URLFetcherDelegate: | 131 // net::URLFetcherDelegate: |
| 142 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 132 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 143 | 133 |
| 144 // NetworkChangeNotifier::IPAddressObserver: | 134 // NetworkChangeNotifier::IPAddressObserver: |
| 145 virtual void OnIPAddressChanged() OVERRIDE; | 135 virtual void OnIPAddressChanged() OVERRIDE; |
| 146 | 136 |
| 147 // KeyedService: | 137 // KeyedService: |
| 148 virtual void Shutdown() OVERRIDE; | 138 virtual void Shutdown() OVERRIDE; |
| 149 | 139 |
| 150 // Registers consumer interest in getting an updated URL from the server. | 140 // Registers consumer interest in getting an updated URL from the server. |
| 151 // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL | 141 // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL |
| 152 // changes. | 142 // changes. |
| 153 void SetNeedToFetch(); | 143 void SetNeedToFetch(); |
| 154 | 144 |
| 155 // Called when the five second startup sleep has finished. Runs any pending | 145 // Called when the five second startup sleep has finished. Runs any pending |
| 156 // fetch. | 146 // fetch. |
| 157 void FinishSleep(); | 147 void FinishSleep(); |
| 158 | 148 |
| 159 // Starts the fetch of the up-to-date Google URL if we actually want to fetch | 149 // Starts the fetch of the up-to-date Google URL if we actually want to fetch |
| 160 // it and can currently do so. | 150 // it and can currently do so. |
| 161 void StartFetchIfDesirable(); | 151 void StartFetchIfDesirable(); |
| 162 | 152 |
| 163 // Called each time the user performs a search. This checks whether we need | |
| 164 // to prompt the user about a domain change, and if so, starts listening for | |
| 165 // the notifications sent when the actual load is triggered. | |
| 166 void SearchCommitted(); | |
| 167 | |
| 168 // Closes all map entries. If |redo_searches| is true, this also triggers | 153 // Closes all map entries. If |redo_searches| is true, this also triggers |
| 169 // each tab with an infobar to re-perform the user's search, but on the new | 154 // each tab with an infobar to re-perform the user's search, but on the new |
| 170 // Google TLD. | 155 // Google TLD. |
| 171 void CloseAllEntries(bool redo_searches); | 156 void CloseAllEntries(bool redo_searches); |
| 172 | 157 |
| 173 // Unregisters any listeners for the navigation helper in |map_entry|. | 158 // Unregisters any listeners for the navigation helper in |map_entry|. |
| 174 // This sanity-DCHECKs that these are registered (or not) in the specific | 159 // This sanity-DCHECKs that these are registered (or not) in the specific |
| 175 // cases we expect. (|must_be_listening_for_commit| is used purely for this | 160 // cases we expect. (|must_be_listening_for_commit| is used purely for this |
| 176 // sanity-checking.) This also unregisters the global navigation pending | 161 // sanity-checking.) This also unregisters the global navigation pending |
| 177 // listener if there are no remaining listeners for navigation commits, as we | 162 // listener if there are no remaining listeners for navigation commits, as we |
| (...skipping 28 matching lines...) Expand all Loading... |
| 206 // nor the last prompted Google URL. | 191 // nor the last prompted Google URL. |
| 207 bool search_committed_; // True when we're expecting a notification of a new | 192 bool search_committed_; // True when we're expecting a notification of a new |
| 208 // pending search navigation. | 193 // pending search navigation. |
| 209 EntryMap entry_map_; | 194 EntryMap entry_map_; |
| 210 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; | 195 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; |
| 211 | 196 |
| 212 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); | 197 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); |
| 213 }; | 198 }; |
| 214 | 199 |
| 215 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ | 200 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ |
| OLD | NEW |