| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "chrome/browser/profiles/profile_keyed_service.h" | 16 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 17 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | 17 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
| 18 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 20 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/common/url_fetcher.h" | 21 #include "content/public/common/url_fetcher.h" |
| 21 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 22 #include "net/base/network_change_notifier.h" | 23 #include "net/base/network_change_notifier.h" |
| 23 #include "net/url_request/url_fetcher_delegate.h" | 24 #include "net/url_request/url_fetcher_delegate.h" |
| 24 | 25 |
| 25 class GoogleURLTrackerInfoBarDelegate; | 26 class GoogleURLTrackerInfoBarDelegate; |
| 26 class PrefService; | 27 class PrefService; |
| 27 class Profile; | 28 class Profile; |
| 28 | 29 |
| 29 namespace content { | 30 namespace content { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // When |profile| is NULL or a testing profile, this function does nothing. | 90 // When |profile| is NULL or a testing profile, this function does nothing. |
| 90 static void GoogleURLSearchCommitted(Profile* profile); | 91 static void GoogleURLSearchCommitted(Profile* profile); |
| 91 | 92 |
| 92 static const char kDefaultGoogleHomepage[]; | 93 static const char kDefaultGoogleHomepage[]; |
| 93 static const char kSearchDomainCheckURL[]; | 94 static const char kSearchDomainCheckURL[]; |
| 94 | 95 |
| 95 private: | 96 private: |
| 96 friend class GoogleURLTrackerInfoBarDelegate; | 97 friend class GoogleURLTrackerInfoBarDelegate; |
| 97 friend class GoogleURLTrackerTest; | 98 friend class GoogleURLTrackerTest; |
| 98 | 99 |
| 99 typedef std::map<const InfoBarTabHelper*, | 100 struct MapEntry { |
| 100 GoogleURLTrackerInfoBarDelegate*> InfoBarMap; | 101 MapEntry(); // Required by STL. |
| 102 MapEntry(GoogleURLTrackerInfoBarDelegate* infobar, |
| 103 const content::NotificationSource& navigation_controller_source, |
| 104 const content::NotificationSource& tab_contents_source); |
| 105 ~MapEntry(); |
| 106 |
| 107 GoogleURLTrackerInfoBarDelegate* infobar; |
| 108 content::NotificationSource navigation_controller_source; |
| 109 content::NotificationSource tab_contents_source; |
| 110 }; |
| 111 |
| 112 typedef std::map<const InfoBarTabHelper*, MapEntry> InfoBarMap; |
| 101 typedef GoogleURLTrackerInfoBarDelegate* (*InfoBarCreator)( | 113 typedef GoogleURLTrackerInfoBarDelegate* (*InfoBarCreator)( |
| 102 InfoBarTabHelper* infobar_helper, | 114 InfoBarTabHelper* infobar_helper, |
| 103 const GURL& search_url, | |
| 104 GoogleURLTracker* google_url_tracker, | 115 GoogleURLTracker* google_url_tracker, |
| 105 const GURL& new_google_url); | 116 const GURL& new_google_url); |
| 106 | 117 |
| 107 // net::URLFetcherDelegate: | 118 // net::URLFetcherDelegate: |
| 108 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 119 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 109 | 120 |
| 110 // content::NotificationObserver: | 121 // content::NotificationObserver: |
| 111 virtual void Observe(int type, | 122 virtual void Observe(int type, |
| 112 const content::NotificationSource& source, | 123 const content::NotificationSource& source, |
| 113 const content::NotificationDetails& details) OVERRIDE; | 124 const content::NotificationDetails& details) OVERRIDE; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 141 // the notifications sent when the actual load is triggered. | 152 // the notifications sent when the actual load is triggered. |
| 142 void SearchCommitted(); | 153 void SearchCommitted(); |
| 143 | 154 |
| 144 // Called by Observe() after SearchCommitted() registers notification | 155 // Called by Observe() after SearchCommitted() registers notification |
| 145 // listeners, to indicate that we've received the "load now pending" | 156 // listeners, to indicate that we've received the "load now pending" |
| 146 // notification. |navigation_controller_source| and |tab_contents_source| are | 157 // notification. |navigation_controller_source| and |tab_contents_source| are |
| 147 // NotificationSources pointing to the associated NavigationController and | 158 // NotificationSources pointing to the associated NavigationController and |
| 148 // TabContents, respectively, for this load; |infobar_helper| is the | 159 // TabContents, respectively, for this load; |infobar_helper| is the |
| 149 // InfoBarTabHelper of the associated tab; and |search_url| is the actual | 160 // InfoBarTabHelper of the associated tab; and |search_url| is the actual |
| 150 // search performed by the user, which if necessary we'll re-do on a new | 161 // search performed by the user, which if necessary we'll re-do on a new |
| 151 // domain later. This function creates a (still-invisible) InfoBarDelegate | 162 // domain later. If there is already a visible GoogleURLTracker infobar for |
| 152 // for the associated tab and begins listening for the "load committed" | 163 // this tab, this function resets its associated navigation entry to point at |
| 153 // notification that will tell us it's safe to show the infobar. | 164 // the new pending entry. Otherwise this function creates a (still-invisible) |
| 165 // InfoBarDelegate for the associated tab. |
| 154 void OnNavigationPending( | 166 void OnNavigationPending( |
| 155 const content::NotificationSource& navigation_controller_source, | 167 const content::NotificationSource& navigation_controller_source, |
| 156 const content::NotificationSource& tab_contents_source, | 168 const content::NotificationSource& tab_contents_source, |
| 157 InfoBarTabHelper* infobar_helper, | 169 InfoBarTabHelper* infobar_helper, |
| 158 const GURL& search_url); | 170 int pending_id); |
| 159 | 171 |
| 160 // Called by Observe() once a load we're watching commits, or the associated | 172 // Called by Observe() once a load we're watching commits, or the associated |
| 161 // tab is closed. The first three args are the same as for | 173 // tab is closed. |infobar_helper| is the same as for OnNavigationPending(); |
| 162 // OnNavigationPending(); |navigated| is true when this call is due to a | 174 // |search_url| is valid when this call is due to a successful navigation |
| 163 // successful navigation (indicating that we should show our infobar) as | 175 // (indicating that we should show or update the relevant infobar) as opposed |
| 164 // opposed to tab closue (which means we should delete the infobar). | 176 // to tab closure (which means we should delete the infobar). |
| 165 void OnNavigationCommittedOrTabClosed( | 177 void OnNavigationCommittedOrTabClosed(const InfoBarTabHelper* infobar_helper, |
| 166 const content::NotificationSource& navigation_controller_source, | 178 const GURL& search_url); |
| 167 const content::NotificationSource& tab_contents_source, | |
| 168 const InfoBarTabHelper* infobar_helper, | |
| 169 bool navigated); | |
| 170 | 179 |
| 171 // Closes all open infobars. If |redo_searches| is true, this also triggers | 180 // Closes all open infobars. If |redo_searches| is true, this also triggers |
| 172 // each tab to re-perform the user's search, but on the new Google TLD. | 181 // each tab to re-perform the user's search, but on the new Google TLD. |
| 173 void CloseAllInfoBars(bool redo_searches); | 182 void CloseAllInfoBars(bool redo_searches); |
| 174 | 183 |
| 184 // Unregisters any listeners for the notification sources in |map_entry|. |
| 185 // This also sanity-DCHECKs that these are registered (or not) in the specific |
| 186 // cases we expect. (|must_be_listening_for_commit| is used purely for this |
| 187 // sanity-checking.) |
| 188 void UnregisterForEntrySpecificNotifications( |
| 189 const MapEntry& map_entry, |
| 190 bool must_be_listening_for_commit); |
| 191 |
| 175 Profile* profile_; | 192 Profile* profile_; |
| 176 content::NotificationRegistrar registrar_; | 193 content::NotificationRegistrar registrar_; |
| 177 InfoBarCreator infobar_creator_; | 194 InfoBarCreator infobar_creator_; |
| 178 // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) | 195 // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) |
| 179 // rather than URL (e.g. http://www.google.co.uk/), so that user could | 196 // rather than URL (e.g. http://www.google.co.uk/), so that user could |
| 180 // configure to use https in search engine templates. | 197 // configure to use https in search engine templates. |
| 181 GURL google_url_; | 198 GURL google_url_; |
| 182 GURL fetched_google_url_; | 199 GURL fetched_google_url_; |
| 183 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; | 200 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; |
| 184 scoped_ptr<net::URLFetcher> fetcher_; | 201 scoped_ptr<net::URLFetcher> fetcher_; |
| 185 int fetcher_id_; | 202 int fetcher_id_; |
| 186 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" |
| 187 // period that begins at browser start. | 204 // period that begins at browser start. |
| 188 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; |
| 189 // we won't fetch again until after a restart. | 206 // we won't fetch again until after a restart. |
| 190 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 |
| 191 // updated URL. If this is never set, we won't | 208 // updated URL. If this is never set, we won't |
| 192 // bother to fetch anything. | 209 // bother to fetch anything. |
| 193 // Consumers should observe | 210 // Consumers should observe |
| 194 // chrome::NOTIFICATION_GOOGLE_URL_UPDATED. | 211 // chrome::NOTIFICATION_GOOGLE_URL_UPDATED. |
| 195 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 |
| 196 // matched with current user's default Google URL | 213 // matched with current user's default Google URL |
| 197 // nor the last prompted Google URL. | 214 // nor the last prompted Google URL. |
| 215 bool search_committed_; // True when we're expecting a notification of a new |
| 216 // pending search navigation. |
| 198 InfoBarMap infobar_map_; | 217 InfoBarMap infobar_map_; |
| 199 | 218 |
| 200 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); | 219 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); |
| 201 }; | 220 }; |
| 202 | 221 |
| 203 | 222 |
| 204 // This infobar delegate is declared here (rather than in the .cc file) so test | 223 // This infobar delegate is declared here (rather than in the .cc file) so test |
| 205 // code can subclass it. | 224 // code can subclass it. |
| 206 class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { | 225 class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 207 public: | 226 public: |
| 208 GoogleURLTrackerInfoBarDelegate(InfoBarTabHelper* infobar_helper, | 227 GoogleURLTrackerInfoBarDelegate(InfoBarTabHelper* infobar_helper, |
| 209 const GURL& search_url, | |
| 210 GoogleURLTracker* google_url_tracker, | 228 GoogleURLTracker* google_url_tracker, |
| 211 const GURL& new_google_url); | 229 const GURL& new_google_url); |
| 212 | 230 |
| 213 // ConfirmInfoBarDelegate: | 231 // ConfirmInfoBarDelegate: |
| 214 virtual bool Accept() OVERRIDE; | 232 virtual bool Accept() OVERRIDE; |
| 215 virtual bool Cancel() OVERRIDE; | 233 virtual bool Cancel() OVERRIDE; |
| 216 virtual string16 GetLinkText() const OVERRIDE; | 234 virtual string16 GetLinkText() const OVERRIDE; |
| 217 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; | 235 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; |
| 236 virtual bool ShouldExpireInternal( |
| 237 const content::LoadCommittedDetails& details) const OVERRIDE; |
| 218 | 238 |
| 219 // Allows GoogleURLTracker to change the Google base URL after the infobar has | 239 // Allows GoogleURLTracker to change the Google base URL after the infobar has |
| 220 // been instantiated. This should only be called with an URL with the same | 240 // been instantiated. This should only be called with an URL with the same |
| 221 // TLD as the existing one, so that the prompt we're displaying will still be | 241 // TLD as the existing one, so that the prompt we're displaying will still be |
| 222 // correct. | 242 // correct. |
| 223 void SetGoogleURL(const GURL& new_google_url); | 243 void SetGoogleURL(const GURL& new_google_url); |
| 224 | 244 |
| 245 bool showing() const { return showing_; } |
| 246 void set_pending_id(int pending_id) { pending_id_ = pending_id; } |
| 247 |
| 225 // These are virtual so test code can override them in a subclass. | 248 // These are virtual so test code can override them in a subclass. |
| 226 virtual void Show(); | 249 virtual void Show(const GURL& search_url); |
| 227 virtual void Close(bool redo_search); | 250 virtual void Close(bool redo_search); |
| 228 | 251 |
| 229 protected: | 252 protected: |
| 230 virtual ~GoogleURLTrackerInfoBarDelegate(); | 253 virtual ~GoogleURLTrackerInfoBarDelegate(); |
| 231 | 254 |
| 232 InfoBarTabHelper* map_key_; // What |google_url_tracker_| uses to track us. | 255 InfoBarTabHelper* map_key_; // What |google_url_tracker_| uses to track us. |
| 233 const GURL search_url_; | 256 GURL search_url_; |
| 234 GoogleURLTracker* google_url_tracker_; | 257 GoogleURLTracker* google_url_tracker_; |
| 235 GURL new_google_url_; | 258 GURL new_google_url_; |
| 236 bool showing_; // True if this delegate has been added to a TabContents. | 259 bool showing_; // True if this delegate has been added to a TabContents. |
| 260 int pending_id_; |
| 237 | 261 |
| 238 private: | 262 private: |
| 239 // ConfirmInfoBarDelegate: | 263 // ConfirmInfoBarDelegate: |
| 240 virtual string16 GetMessageText() const OVERRIDE; | 264 virtual string16 GetMessageText() const OVERRIDE; |
| 241 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | 265 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
| 242 | 266 |
| 243 DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerInfoBarDelegate); | 267 DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerInfoBarDelegate); |
| 244 }; | 268 }; |
| 245 | 269 |
| 246 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ | 270 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ |
| OLD | NEW |