| 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> |
| 11 | 11 |
| 12 #include "base/callback_forward.h" |
| 12 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "chrome/browser/google/google_url_tracker_map_entry.h" |
| 15 #include "chrome/browser/profiles/profile_keyed_service.h" | 17 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 16 #include "content/public/browser/notification_observer.h" | |
| 17 #include "content/public/browser/notification_registrar.h" | |
| 18 #include "content/public/browser/notification_source.h" | |
| 19 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 20 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
| 21 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 21 #include "net/url_request/url_fetcher_delegate.h" |
| 23 | 22 |
| 24 class GoogleURLTrackerInfoBarDelegate; | |
| 25 class InfoBarTabHelper; | |
| 26 class PrefService; | 23 class PrefService; |
| 27 class Profile; | 24 class Profile; |
| 28 | 25 |
| 29 namespace content { | 26 namespace content { |
| 30 class NavigationController; | 27 class NavigationController; |
| 31 class WebContents; | 28 class WebContents; |
| 32 } | 29 } |
| 33 | 30 |
| 34 // This object is responsible for checking the Google URL once per network | 31 // This object is responsible for checking the Google URL once per network |
| 35 // change, and if necessary prompting the user to see if they want to change to | 32 // change, and if necessary prompting the user to see if they want to change to |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 static void RequestServerCheck(Profile* profile); | 79 static void RequestServerCheck(Profile* profile); |
| 83 | 80 |
| 84 // Notifies the tracker that the user has started a Google search. | 81 // Notifies the tracker that the user has started a Google search. |
| 85 // If prompting is necessary, we then listen for the subsequent | 82 // If prompting is necessary, we then listen for the subsequent |
| 86 // NAV_ENTRY_PENDING notification to get the appropriate NavigationController. | 83 // NAV_ENTRY_PENDING notification to get the appropriate NavigationController. |
| 87 // When the load commits, we'll show the infobar. | 84 // When the load commits, we'll show the infobar. |
| 88 // | 85 // |
| 89 // When |profile| is NULL or a testing profile, this function does nothing. | 86 // When |profile| is NULL or a testing profile, this function does nothing. |
| 90 static void GoogleURLSearchCommitted(Profile* profile); | 87 static void GoogleURLSearchCommitted(Profile* profile); |
| 91 | 88 |
| 89 // No one but GoogleURLTrackerInfoBarDelegate or test code should call these. |
| 90 void AcceptGoogleURL(bool redo_searches); |
| 91 void CancelGoogleURL(); |
| 92 const GURL& google_url() const { return google_url_; } |
| 93 const GURL& fetched_google_url() const { return fetched_google_url_; } |
| 94 |
| 95 // No one but GoogleURLTrackerMapEntry should call this. |
| 96 void DeleteMapEntryForHelper(const InfoBarTabHelper* infobar_helper); |
| 97 |
| 92 static const char kDefaultGoogleHomepage[]; | 98 static const char kDefaultGoogleHomepage[]; |
| 93 static const char kSearchDomainCheckURL[]; | 99 static const char kSearchDomainCheckURL[]; |
| 94 | 100 |
| 95 private: | 101 private: |
| 96 friend class GoogleURLTrackerInfoBarDelegate; | |
| 97 friend class GoogleURLTrackerTest; | 102 friend class GoogleURLTrackerTest; |
| 98 | 103 |
| 99 struct MapEntry { | 104 typedef std::map<const InfoBarTabHelper*, |
| 100 MapEntry(); // Required by STL. | 105 GoogleURLTrackerMapEntry*> InfoBarMap; |
| 101 MapEntry(GoogleURLTrackerInfoBarDelegate* infobar, | |
| 102 const content::NotificationSource& navigation_controller_source, | |
| 103 const content::NotificationSource& web_contents_source); | |
| 104 ~MapEntry(); | |
| 105 | |
| 106 GoogleURLTrackerInfoBarDelegate* infobar; | |
| 107 content::NotificationSource navigation_controller_source; | |
| 108 content::NotificationSource web_contents_source; | |
| 109 }; | |
| 110 | |
| 111 typedef std::map<const InfoBarTabHelper*, MapEntry> InfoBarMap; | |
| 112 typedef GoogleURLTrackerInfoBarDelegate* (*InfoBarCreator)( | |
| 113 InfoBarTabHelper* infobar_helper, | |
| 114 GoogleURLTracker* google_url_tracker, | |
| 115 const GURL& new_google_url); | |
| 116 | 106 |
| 117 // net::URLFetcherDelegate: | 107 // net::URLFetcherDelegate: |
| 118 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 108 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 119 | 109 |
| 120 // content::NotificationObserver: | 110 // content::NotificationObserver: |
| 121 virtual void Observe(int type, | 111 virtual void Observe(int type, |
| 122 const content::NotificationSource& source, | 112 const content::NotificationSource& source, |
| 123 const content::NotificationDetails& details) OVERRIDE; | 113 const content::NotificationDetails& details) OVERRIDE; |
| 124 | 114 |
| 125 // NetworkChangeNotifier::IPAddressObserver: | 115 // NetworkChangeNotifier::IPAddressObserver: |
| 126 virtual void OnIPAddressChanged() OVERRIDE; | 116 virtual void OnIPAddressChanged() OVERRIDE; |
| 127 | 117 |
| 128 // ProfileKeyedService: | 118 // ProfileKeyedService: |
| 129 virtual void Shutdown() OVERRIDE; | 119 virtual void Shutdown() OVERRIDE; |
| 130 | 120 |
| 131 // Callbacks from GoogleURLTrackerInfoBarDelegate: | |
| 132 void AcceptGoogleURL(const GURL& google_url, bool redo_searches); | |
| 133 void CancelGoogleURL(const GURL& google_url); | |
| 134 void InfoBarClosed(const InfoBarTabHelper* infobar_helper); | |
| 135 | |
| 136 // Registers consumer interest in getting an updated URL from the server. | 121 // Registers consumer interest in getting an updated URL from the server. |
| 137 // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL | 122 // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL |
| 138 // changes. | 123 // changes. |
| 139 void SetNeedToFetch(); | 124 void SetNeedToFetch(); |
| 140 | 125 |
| 141 // Called when the five second startup sleep has finished. Runs any pending | 126 // Called when the five second startup sleep has finished. Runs any pending |
| 142 // fetch. | 127 // fetch. |
| 143 void FinishSleep(); | 128 void FinishSleep(); |
| 144 | 129 |
| 145 // Starts the fetch of the up-to-date Google URL if we actually want to fetch | 130 // Starts the fetch of the up-to-date Google URL if we actually want to fetch |
| (...skipping 19 matching lines...) Expand all Loading... |
| 165 const content::NotificationSource& navigation_controller_source, | 150 const content::NotificationSource& navigation_controller_source, |
| 166 const content::NotificationSource& web_contents_source, | 151 const content::NotificationSource& web_contents_source, |
| 167 InfoBarTabHelper* infobar_helper, | 152 InfoBarTabHelper* infobar_helper, |
| 168 int pending_id); | 153 int pending_id); |
| 169 | 154 |
| 170 // Called by Observe() once a load we're watching commits, or the associated | 155 // Called by Observe() once a load we're watching commits, or the associated |
| 171 // tab is closed. |infobar_helper| is the same as for OnNavigationPending(); | 156 // tab is closed. |infobar_helper| is the same as for OnNavigationPending(); |
| 172 // |search_url| is valid when this call is due to a successful navigation | 157 // |search_url| is valid when this call is due to a successful navigation |
| 173 // (indicating that we should show or update the relevant infobar) as opposed | 158 // (indicating that we should show or update the relevant infobar) as opposed |
| 174 // to tab closure (which means we should delete the infobar). | 159 // to tab closure (which means we should delete the infobar). |
| 175 void OnNavigationCommittedOrTabClosed(const InfoBarTabHelper* infobar_helper, | 160 void OnNavigationCommittedOrTabClosed(InfoBarTabHelper* infobar_helper, |
| 176 const GURL& search_url); | 161 const GURL& search_url); |
| 177 | 162 |
| 178 // Called by Observe() when an instant navigation occurs. This will call | 163 // Called by Observe() when an instant navigation occurs. This will call |
| 179 // OnNavigationPending(), and, depending on whether this is a search we were | 164 // OnNavigationPending(), and, depending on whether this is a search we were |
| 180 // listening for, may then also call OnNavigationCommittedOrTabClosed(). | 165 // listening for, may then also call OnNavigationCommittedOrTabClosed(). |
| 181 void OnInstantCommitted( | 166 void OnInstantCommitted( |
| 182 const content::NotificationSource& navigation_controller_source, | 167 const content::NotificationSource& navigation_controller_source, |
| 183 const content::NotificationSource& web_contents_source, | 168 const content::NotificationSource& web_contents_source, |
| 184 InfoBarTabHelper* infobar_helper, | 169 InfoBarTabHelper* infobar_helper, |
| 185 const GURL& search_url); | 170 const GURL& search_url); |
| 186 | 171 |
| 187 // Closes all open infobars. If |redo_searches| is true, this also triggers | 172 // Closes all open infobars. If |redo_searches| is true, this also triggers |
| 188 // each tab to re-perform the user's search, but on the new Google TLD. | 173 // each tab to re-perform the user's search, but on the new Google TLD. |
| 189 void CloseAllInfoBars(bool redo_searches); | 174 void CloseAllInfoBars(bool redo_searches); |
| 190 | 175 |
| 191 // Unregisters any listeners for the notification sources in |map_entry|. | 176 // Unregisters any listeners for the notification sources in |map_entry|. |
| 192 // This sanity-DCHECKs that these are registered (or not) in the specific | 177 // This sanity-DCHECKs that these are registered (or not) in the specific |
| 193 // cases we expect. (|must_be_listening_for_commit| is used purely for this | 178 // cases we expect. (|must_be_listening_for_commit| is used purely for this |
| 194 // sanity-checking.) This also unregisters our global NAV_ENTRY_PENDING/ | 179 // sanity-checking.) This also unregisters our global NAV_ENTRY_PENDING/ |
| 195 // INSTANT_COMMITTED listeners if there are no remaining listeners for | 180 // INSTANT_COMMITTED listeners if there are no remaining listeners for |
| 196 // NAV_ENTRY_COMMITTED, as we no longer need them until another search is | 181 // NAV_ENTRY_COMMITTED, as we no longer need them until another search is |
| 197 // committed. | 182 // committed. |
| 198 void UnregisterForEntrySpecificNotifications( | 183 void UnregisterForEntrySpecificNotifications( |
| 199 const MapEntry& map_entry, | 184 const GoogleURLTrackerMapEntry& map_entry, |
| 200 bool must_be_listening_for_commit); | 185 bool must_be_listening_for_commit); |
| 201 | 186 |
| 202 Profile* profile_; | 187 Profile* profile_; |
| 203 content::NotificationRegistrar registrar_; | 188 content::NotificationRegistrar registrar_; |
| 204 InfoBarCreator infobar_creator_; | 189 |
| 205 // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) | 190 // Creates an infobar delegate and adds it to the provided InfoBarHelper. |
| 206 // rather than URL (e.g. http://www.google.co.uk/), so that user could | 191 // Returns the delegate pointer on success or NULL on failure. The caller |
| 207 // configure to use https in search engine templates. | 192 // does not own the returned object, the InfoBarTabHelper does. |
| 193 base::Callback<GoogleURLTrackerInfoBarDelegate*( |
| 194 InfoBarTabHelper*, |
| 195 GoogleURLTracker*, |
| 196 const GURL&)> infobar_creator_; |
| 197 |
| 208 GURL google_url_; | 198 GURL google_url_; |
| 209 GURL fetched_google_url_; | 199 GURL fetched_google_url_; |
| 210 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; | 200 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; |
| 211 scoped_ptr<net::URLFetcher> fetcher_; | 201 scoped_ptr<net::URLFetcher> fetcher_; |
| 212 int fetcher_id_; | 202 int fetcher_id_; |
| 213 bool in_startup_sleep_; // True if we're in the five-second "no fetching" | 203 bool in_startup_sleep_; // True if we're in the five-second "no fetching" |
| 214 // period that begins at browser start. | 204 // period that begins at browser start. |
| 215 bool already_fetched_; // True if we've already fetched a URL once this run; | 205 bool already_fetched_; // True if we've already fetched a URL once this run; |
| 216 // we won't fetch again until after a restart. | 206 // we won't fetch again until after a restart. |
| 217 bool need_to_fetch_; // True if a consumer actually wants us to fetch an | 207 bool need_to_fetch_; // True if a consumer actually wants us to fetch an |
| 218 // updated URL. If this is never set, we won't | 208 // updated URL. If this is never set, we won't |
| 219 // bother to fetch anything. | 209 // bother to fetch anything. |
| 220 // Consumers should observe | 210 // Consumers should observe |
| 221 // chrome::NOTIFICATION_GOOGLE_URL_UPDATED. | 211 // chrome::NOTIFICATION_GOOGLE_URL_UPDATED. |
| 222 bool need_to_prompt_; // True if the last fetched Google URL is not | 212 bool need_to_prompt_; // True if the last fetched Google URL is not |
| 223 // matched with current user's default Google URL | 213 // matched with current user's default Google URL |
| 224 // nor the last prompted Google URL. | 214 // nor the last prompted Google URL. |
| 225 bool search_committed_; // True when we're expecting a notification of a new | 215 bool search_committed_; // True when we're expecting a notification of a new |
| 226 // pending search navigation. | 216 // pending search navigation. |
| 227 InfoBarMap infobar_map_; | 217 InfoBarMap infobar_map_; |
| 228 | 218 |
| 229 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); | 219 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); |
| 230 }; | 220 }; |
| 231 | 221 |
| 232 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ | 222 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ |
| OLD | NEW |