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 <string> | 10 #include <string> |
10 | 11 |
11 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | 15 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
15 #include "content/public/common/url_fetcher.h" | 16 #include "content/public/common/url_fetcher.h" |
16 #include "content/public/common/url_fetcher_delegate.h" | 17 #include "content/public/common/url_fetcher_delegate.h" |
17 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
(...skipping 25 matching lines...) Expand all Loading... | |
44 public content::NotificationObserver, | 45 public content::NotificationObserver, |
45 public net::NetworkChangeNotifier::IPAddressObserver { | 46 public net::NetworkChangeNotifier::IPAddressObserver { |
46 public: | 47 public: |
47 // The constructor does different things depending on which of these values | 48 // The constructor does different things depending on which of these values |
48 // you pass it. Hopefully these are self-explanatory. | 49 // you pass it. Hopefully these are self-explanatory. |
49 enum Mode { | 50 enum Mode { |
50 NORMAL_MODE, | 51 NORMAL_MODE, |
51 UNIT_TEST_MODE, | 52 UNIT_TEST_MODE, |
52 }; | 53 }; |
53 | 54 |
54 // Only the main browser process loop should call this, when setting up | 55 // Only the main browser process loop and tests should call this, when setting |
55 // g_browser_process->google_url_tracker_. No code other than the | 56 // up g_browser_process->google_url_tracker_. No code other than the |
56 // GoogleURLTracker itself should actually use | 57 // GoogleURLTracker itself should actually use |
57 // g_browser_process->google_url_tracker(). | 58 // g_browser_process->google_url_tracker(). |
58 explicit GoogleURLTracker(Mode mode); | 59 explicit GoogleURLTracker(Mode mode); |
59 | 60 |
60 virtual ~GoogleURLTracker(); | 61 virtual ~GoogleURLTracker(); |
61 | 62 |
62 // Returns the current Google URL. This will return a valid URL even in | 63 // Returns the current Google URL. This will return a valid URL even in |
63 // unittest mode. | 64 // unittest mode. |
64 // | 65 // |
65 // This is the only function most code should ever call. | 66 // This is the only function most code should ever call. |
(...skipping 16 matching lines...) Expand all Loading... | |
82 // When the load commits, we'll show the infobar. | 83 // When the load commits, we'll show the infobar. |
83 static void GoogleURLSearchCommitted(); | 84 static void GoogleURLSearchCommitted(); |
84 | 85 |
85 static const char kDefaultGoogleHomepage[]; | 86 static const char kDefaultGoogleHomepage[]; |
86 static const char kSearchDomainCheckURL[]; | 87 static const char kSearchDomainCheckURL[]; |
87 | 88 |
88 private: | 89 private: |
89 friend class GoogleURLTrackerInfoBarDelegate; | 90 friend class GoogleURLTrackerInfoBarDelegate; |
90 friend class GoogleURLTrackerTest; | 91 friend class GoogleURLTrackerTest; |
91 | 92 |
92 typedef InfoBarDelegate* (*InfoBarCreator)(InfoBarTabHelper*, | 93 typedef std::map<const InfoBarTabHelper*, |
93 GoogleURLTracker*, | 94 GoogleURLTrackerInfoBarDelegate*> InfoBars; |
Ilya Sherman
2012/04/24 00:30:47
nit: Perhaps name this "InfoBarMap", so there's at
Peter Kasting
2012/04/24 01:53:01
Good idea. Changed type and variable name.
| |
94 const GURL&); | 95 typedef GoogleURLTrackerInfoBarDelegate* (*InfoBarCreator)( |
96 InfoBarTabHelper* infobar_helper, | |
97 const GURL& search_url, | |
98 GoogleURLTracker* google_url_tracker, | |
99 const GURL& new_google_url); | |
95 | 100 |
96 void AcceptGoogleURL(const GURL& google_url); | 101 void AcceptGoogleURL(const GURL& google_url); |
97 void CancelGoogleURL(const GURL& google_url); | 102 void CancelGoogleURL(const GURL& google_url); |
98 void InfoBarClosed(); | 103 void InfoBarClosed(const InfoBarTabHelper* infobar_helper); |
Ilya Sherman
2012/04/24 00:30:47
nit: I think we generally prefer passing by const-
Peter Kasting
2012/04/24 01:53:01
Doing this would break the test code that needs to
| |
99 void RedoSearch(); | |
100 | 104 |
101 // Registers consumer interest in getting an updated URL from the server. | 105 // Registers consumer interest in getting an updated URL from the server. |
102 // It will be notified as chrome::GOOGLE_URL_UPDATED, so the | 106 // It will be notified as chrome::GOOGLE_URL_UPDATED, so the |
103 // consumer should observe this notification before calling this. | 107 // consumer should observe this notification before calling this. |
104 void SetNeedToFetch(); | 108 void SetNeedToFetch(); |
105 | 109 |
106 // Called when the five second startup sleep has finished. Runs any pending | 110 // Called when the five second startup sleep has finished. Runs any pending |
107 // fetch. | 111 // fetch. |
108 void FinishSleep(); | 112 void FinishSleep(); |
109 | 113 |
110 // Starts the fetch of the up-to-date Google URL if we actually want to fetch | 114 // Starts the fetch of the up-to-date Google URL if we actually want to fetch |
111 // it and can currently do so. | 115 // it and can currently do so. |
112 void StartFetchIfDesirable(); | 116 void StartFetchIfDesirable(); |
113 | 117 |
114 // content::URLFetcherDelegate | 118 // content::URLFetcherDelegate |
115 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 119 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
116 | 120 |
117 // content::NotificationObserver | 121 // content::NotificationObserver |
118 virtual void Observe(int type, | 122 virtual void Observe(int type, |
119 const content::NotificationSource& source, | 123 const content::NotificationSource& source, |
120 const content::NotificationDetails& details) OVERRIDE; | 124 const content::NotificationDetails& details) OVERRIDE; |
121 | 125 |
122 // NetworkChangeNotifier::IPAddressObserver | 126 // NetworkChangeNotifier::IPAddressObserver |
123 virtual void OnIPAddressChanged() OVERRIDE; | 127 virtual void OnIPAddressChanged() OVERRIDE; |
124 | 128 |
125 void SearchCommitted(); | 129 void SearchCommitted(); |
126 void OnNavigationPending(const content::NotificationSource& source, | 130 void OnNavigationPending(const content::NotificationSource& source, |
127 const GURL& pending_url); | 131 const content::NotificationSource& contents_source, |
128 void OnNavigationCommittedOrTabClosed(content::WebContents* web_contents, | 132 InfoBarTabHelper* infobar_helper, |
129 int type); | 133 const GURL& search_url); |
Ilya Sherman
2012/04/24 00:30:47
nit: const params generally should precede non-con
Peter Kasting
2012/04/24 01:53:01
|infobar_helper| isn't an outparam here, but can't
| |
130 void ShowGoogleURLInfoBarIfNecessary(content::WebContents* web_contents); | 134 void OnNavigationCommittedOrTabClosed( |
135 const content::NotificationSource& source, | |
136 const content::NotificationSource& contents_source, | |
137 InfoBarTabHelper* infobar_helper, | |
138 int type); | |
Ilya Sherman
2012/04/24 00:30:47
nit: const params generally should precede non-con
Peter Kasting
2012/04/24 01:53:01
Made this one const.
| |
139 void CloseAllInfoBars(bool redo_searches); | |
131 | 140 |
132 content::NotificationRegistrar registrar_; | 141 content::NotificationRegistrar registrar_; |
133 InfoBarCreator infobar_creator_; | 142 InfoBarCreator infobar_creator_; |
134 // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) | 143 // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) |
135 // rather than URL (e.g. http://www.google.co.uk/), so that user could | 144 // rather than URL (e.g. http://www.google.co.uk/), so that user could |
136 // configure to use https in search engine templates. | 145 // configure to use https in search engine templates. |
137 GURL google_url_; | 146 GURL google_url_; |
138 GURL fetched_google_url_; | 147 GURL fetched_google_url_; |
139 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; | 148 base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_; |
140 scoped_ptr<content::URLFetcher> fetcher_; | 149 scoped_ptr<content::URLFetcher> fetcher_; |
141 int fetcher_id_; | 150 int fetcher_id_; |
142 bool in_startup_sleep_; // True if we're in the five-second "no fetching" | 151 bool in_startup_sleep_; // True if we're in the five-second "no fetching" |
143 // period that begins at browser start. | 152 // period that begins at browser start. |
144 bool already_fetched_; // True if we've already fetched a URL once this run; | 153 bool already_fetched_; // True if we've already fetched a URL once this run; |
145 // we won't fetch again until after a restart. | 154 // we won't fetch again until after a restart. |
146 bool need_to_fetch_; // True if a consumer actually wants us to fetch an | 155 bool need_to_fetch_; // True if a consumer actually wants us to fetch an |
147 // updated URL. If this is never set, we won't | 156 // updated URL. If this is never set, we won't |
148 // bother to fetch anything. | 157 // bother to fetch anything. |
149 // Consumers should observe | 158 // Consumers should observe |
150 // chrome::GOOGLE_URL_UPDATED. | 159 // chrome::GOOGLE_URL_UPDATED. |
151 bool need_to_prompt_; // True if the last fetched Google URL is not | 160 bool need_to_prompt_; // True if the last fetched Google URL is not |
152 // matched with current user's default Google URL | 161 // matched with current user's default Google URL |
153 // nor the last prompted Google URL. | 162 // nor the last prompted Google URL. |
154 content::NavigationController* controller_; | 163 InfoBars infobars_; |
155 InfoBarDelegate* infobar_; | |
156 GURL search_url_; | |
157 | 164 |
158 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); | 165 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); |
159 }; | 166 }; |
160 | 167 |
161 | 168 |
162 // This infobar delegate is declared here (rather than in the .cc file) so test | 169 // This infobar delegate is declared here (rather than in the .cc file) so test |
163 // code can subclass it. | 170 // code can subclass it. |
164 class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { | 171 class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { |
165 public: | 172 public: |
166 GoogleURLTrackerInfoBarDelegate(InfoBarTabHelper* infobar_helper, | 173 GoogleURLTrackerInfoBarDelegate(InfoBarTabHelper* infobar_helper, |
174 const GURL& search_url, | |
167 GoogleURLTracker* google_url_tracker, | 175 GoogleURLTracker* google_url_tracker, |
168 const GURL& new_google_url); | 176 const GURL& new_google_url); |
169 | 177 |
170 // ConfirmInfoBarDelegate: | 178 // ConfirmInfoBarDelegate: |
171 virtual bool Accept() OVERRIDE; | 179 virtual bool Accept() OVERRIDE; |
172 virtual bool Cancel() OVERRIDE; | 180 virtual bool Cancel() OVERRIDE; |
173 virtual string16 GetLinkText() const OVERRIDE; | 181 virtual string16 GetLinkText() const OVERRIDE; |
174 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; | 182 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; |
175 | 183 |
184 // These are virtual so test code can override them in a subclass. | |
185 virtual void Show(); | |
186 virtual void Close(bool redo_search); | |
187 | |
176 protected: | 188 protected: |
177 virtual ~GoogleURLTrackerInfoBarDelegate(); | 189 virtual ~GoogleURLTrackerInfoBarDelegate(); |
178 | 190 |
191 InfoBarTabHelper* map_key_; // What |google_url_tracker_| uses to track us. | |
192 const GURL search_url_; | |
179 GoogleURLTracker* google_url_tracker_; | 193 GoogleURLTracker* google_url_tracker_; |
180 const GURL new_google_url_; | 194 const GURL new_google_url_; |
195 bool showing_; // True if this delegate has been added to a TabContents. | |
181 | 196 |
182 private: | 197 private: |
183 // ConfirmInfoBarDelegate: | 198 // ConfirmInfoBarDelegate: |
184 virtual string16 GetMessageText() const OVERRIDE; | 199 virtual string16 GetMessageText() const OVERRIDE; |
185 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | 200 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
186 | 201 |
187 // Returns the portion of the appropriate hostname to display. | 202 // Returns the portion of the appropriate hostname to display. |
188 string16 GetHost(bool new_host) const; | 203 string16 GetHost(bool new_host) const; |
189 | 204 |
190 DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerInfoBarDelegate); | 205 DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerInfoBarDelegate); |
191 }; | 206 }; |
192 | 207 |
193 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ | 208 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ |
OLD | NEW |