OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
13 #include "chrome/browser/tab_contents/infobar_delegate.h" | 14 #include "chrome/browser/tab_contents/infobar_delegate.h" |
14 #include "chrome/common/net/url_fetcher.h" | 15 #include "chrome/common/net/url_fetcher.h" |
15 #include "chrome/common/notification_observer.h" | 16 #include "chrome/common/notification_observer.h" |
16 #include "chrome/common/notification_registrar.h" | 17 #include "chrome/common/notification_registrar.h" |
17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
18 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
19 | 20 |
21 class GoogleURLTrackerInfoBarDelegate; | |
20 class NavigationController; | 22 class NavigationController; |
21 class PrefService; | 23 class PrefService; |
22 class TabContents; | 24 class TabContents; |
23 class TemplateURL; | 25 class TemplateURL; |
24 | 26 |
25 // This object is responsible for checking the Google URL once per network | 27 // This object is responsible for checking the Google URL once per network |
26 // change, and if necessary prompting the user to see if they want to change to | 28 // change, and if necessary prompting the user to see if they want to change to |
27 // using it. The current and last prompted values are saved to prefs. | 29 // using it. The current and last prompted values are saved to prefs. |
28 // | 30 // |
29 // Most consumers should only call GoogleURL(), which is guaranteed to | 31 // Most consumers should only call GoogleURL(), which is guaranteed to |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 | 70 |
69 // Notifies the tracker that the user has started a Google search. | 71 // Notifies the tracker that the user has started a Google search. |
70 // If prompting is necessary, we then listen for the subsequent | 72 // If prompting is necessary, we then listen for the subsequent |
71 // NAV_ENTRY_PENDING notification to get the appropriate NavigationController. | 73 // NAV_ENTRY_PENDING notification to get the appropriate NavigationController. |
72 // When the load commits, we'll show the infobar. | 74 // When the load commits, we'll show the infobar. |
73 static void GoogleURLSearchCommitted(); | 75 static void GoogleURLSearchCommitted(); |
74 | 76 |
75 static const char kDefaultGoogleHomepage[]; | 77 static const char kDefaultGoogleHomepage[]; |
76 static const char kSearchDomainCheckURL[]; | 78 static const char kSearchDomainCheckURL[]; |
77 | 79 |
78 // Methods called from InfoBar delegate. | 80 // Methods called from GoogleURLTrackerInfoBarDelegate. |
79 void AcceptGoogleURL(const GURL& google_url); | 81 void AcceptGoogleURL(const GURL& google_url); |
80 void CancelGoogleURL(const GURL& google_url); | 82 void CancelGoogleURL(const GURL& google_url); |
81 void InfoBarClosed(); | 83 void InfoBarClosed(GoogleURLTrackerInfoBarDelegate* infobar, |
82 void RedoSearch(); | 84 TabContents* tab_contents); |
83 | 85 |
84 private: | 86 private: |
85 friend class GoogleURLTrackerTest; | 87 friend class GoogleURLTrackerTest; |
86 | 88 |
87 typedef InfoBarDelegate* (*InfobarCreator)(TabContents*, | 89 typedef std::map<TabContents*, GoogleURLTrackerInfoBarDelegate*> Infobars; |
88 GoogleURLTracker*, | 90 typedef GoogleURLTrackerInfoBarDelegate* (*InfobarCreator)( |
89 const GURL&); | 91 TabContents*, |
92 const GURL&, | |
93 GoogleURLTracker*, | |
94 const GURL&); | |
Ilya Sherman
2010/11/16 00:17:47
nit: Could you add comments describing the semanti
Ilya Sherman
2010/11/16 00:19:57
By which I mean something very brief, along the li
Peter Kasting
2010/11/16 00:38:23
I'll go one better -- I just added the names direc
| |
90 | 95 |
91 // Registers consumer interest in getting an updated URL from the server. | 96 // Registers consumer interest in getting an updated URL from the server. |
92 // It will be notified as NotificationType::GOOGLE_URL_UPDATED, so the | 97 // It will be notified as NotificationType::GOOGLE_URL_UPDATED, so the |
93 // consumer should observe this notification before calling this. | 98 // consumer should observe this notification before calling this. |
94 void SetNeedToFetch(); | 99 void SetNeedToFetch(); |
95 | 100 |
96 // Begins the five-second startup sleep period, unless a test has cleared | 101 // Begins the five-second startup sleep period, unless a test has cleared |
97 // |queue_wakeup_task_|. | 102 // |queue_wakeup_task_|. |
98 void QueueWakeupTask(); | 103 void QueueWakeupTask(); |
99 | 104 |
(...skipping 16 matching lines...) Expand all Loading... | |
116 // NotificationObserver | 121 // NotificationObserver |
117 virtual void Observe(NotificationType type, | 122 virtual void Observe(NotificationType type, |
118 const NotificationSource& source, | 123 const NotificationSource& source, |
119 const NotificationDetails& details); | 124 const NotificationDetails& details); |
120 | 125 |
121 // NetworkChangeNotifier::Observer | 126 // NetworkChangeNotifier::Observer |
122 virtual void OnIPAddressChanged(); | 127 virtual void OnIPAddressChanged(); |
123 | 128 |
124 void SearchCommitted(); | 129 void SearchCommitted(); |
125 void OnNavigationPending(const NotificationSource& source, | 130 void OnNavigationPending(const NotificationSource& source, |
131 TabContents* tab_contents, | |
126 const GURL& pending_url); | 132 const GURL& pending_url); |
127 void OnNavigationCommittedOrTabClosed(TabContents* tab_contents, | 133 void OnNavigationCommittedOrTabClosed(const NotificationSource& source, |
134 TabContents* tab_contents, | |
128 NotificationType::Type type); | 135 NotificationType::Type type); |
129 void ShowGoogleURLInfoBarIfNecessary(TabContents* tab_contents); | 136 void CloseAllInfobars(bool redo_searches); |
130 | 137 |
131 NotificationRegistrar registrar_; | 138 NotificationRegistrar registrar_; |
132 InfobarCreator infobar_creator_; | 139 InfobarCreator infobar_creator_; |
133 // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) | 140 // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) |
134 // rather than URL (e.g. http://www.google.co.uk/), so that user could | 141 // rather than URL (e.g. http://www.google.co.uk/), so that user could |
135 // configure to use https in search engine templates. | 142 // configure to use https in search engine templates. |
136 GURL google_url_; | 143 GURL google_url_; |
137 GURL fetched_google_url_; | 144 GURL fetched_google_url_; |
138 ScopedRunnableMethodFactory<GoogleURLTracker> runnable_method_factory_; | 145 ScopedRunnableMethodFactory<GoogleURLTracker> runnable_method_factory_; |
139 scoped_ptr<URLFetcher> fetcher_; | 146 scoped_ptr<URLFetcher> fetcher_; |
140 int fetcher_id_; | 147 int fetcher_id_; |
141 bool queue_wakeup_task_; | 148 bool queue_wakeup_task_; |
142 bool in_startup_sleep_; // True if we're in the five-second "no fetching" | 149 bool in_startup_sleep_; // True if we're in the five-second "no fetching" |
143 // period that begins at browser start. | 150 // period that begins at browser start. |
144 bool already_fetched_; // True if we've already fetched a URL once this run; | 151 bool already_fetched_; // True if we've already fetched a URL once this run; |
145 // we won't fetch again until after a restart. | 152 // we won't fetch again until after a restart. |
146 bool need_to_fetch_; // True if a consumer actually wants us to fetch an | 153 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 | 154 // updated URL. If this is never set, we won't |
148 // bother to fetch anything. | 155 // bother to fetch anything. |
149 // Consumers should observe | 156 // Consumers should observe |
150 // NotificationType::GOOGLE_URL_UPDATED. | 157 // NotificationType::GOOGLE_URL_UPDATED. |
151 bool request_context_available_; | 158 bool request_context_available_; |
152 // True when the profile has been loaded and the | 159 // True when the profile has been loaded and the |
153 // default request context created, so we can | 160 // default request context created, so we can |
154 // actually do the fetch with the right data. | 161 // actually do the fetch with the right data. |
155 bool need_to_prompt_; // True if the last fetched Google URL is not | 162 bool need_to_prompt_; // True if the last fetched Google URL is not |
156 // matched with current user's default Google URL | 163 // matched with current user's default Google URL |
157 // nor the last prompted Google URL. | 164 // nor the last prompted Google URL. |
158 NavigationController* controller_; | 165 Infobars infobars_; |
159 InfoBarDelegate* infobar_; | |
160 GURL search_url_; | |
161 | 166 |
162 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); | 167 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); |
163 }; | 168 }; |
164 | 169 |
165 | 170 |
166 // This infobar delegate is declared here (rather than in the .cc file) so test | 171 // This infobar delegate is declared here (rather than in the .cc file) so test |
167 // code can subclass it. | 172 // code can subclass it. |
168 class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { | 173 class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { |
169 public: | 174 public: |
170 GoogleURLTrackerInfoBarDelegate(TabContents* tab_contents, | 175 GoogleURLTrackerInfoBarDelegate(TabContents* tab_contents, |
176 const GURL& search_url, | |
171 GoogleURLTracker* google_url_tracker, | 177 GoogleURLTracker* google_url_tracker, |
172 const GURL& new_google_url); | 178 const GURL& new_google_url); |
173 | 179 |
174 // ConfirmInfoBarDelegate | 180 // ConfirmInfoBarDelegate |
175 virtual bool Accept(); | 181 virtual bool Accept(); |
176 virtual bool Cancel(); | 182 virtual bool Cancel(); |
177 virtual void InfoBarClosed(); | 183 virtual void InfoBarClosed(); |
178 | 184 |
185 // These are virtual so test code can override them in a subclass. | |
186 virtual void Show(); | |
187 virtual void Close(bool redo_search); | |
188 | |
179 protected: | 189 protected: |
180 virtual ~GoogleURLTrackerInfoBarDelegate(); | 190 virtual ~GoogleURLTrackerInfoBarDelegate(); |
181 | 191 |
192 TabContents* tab_contents_; | |
193 const GURL search_url_; | |
182 GoogleURLTracker* google_url_tracker_; | 194 GoogleURLTracker* google_url_tracker_; |
183 const GURL new_google_url_; | 195 const GURL new_google_url_; |
196 bool showing_; // True if this delegate has been added to a TabContents. | |
184 | 197 |
185 private: | 198 private: |
186 // ConfirmInfoBarDelegate | 199 // ConfirmInfoBarDelegate |
187 virtual string16 GetMessageText() const; | 200 virtual string16 GetMessageText() const; |
188 virtual int GetButtons() const; | 201 virtual int GetButtons() const; |
189 virtual string16 GetButtonLabel(InfoBarButton button) const; | 202 virtual string16 GetButtonLabel(InfoBarButton button) const; |
190 | 203 |
191 DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerInfoBarDelegate); | 204 DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerInfoBarDelegate); |
192 }; | 205 }; |
193 | 206 |
194 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ | 207 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_H_ |
OLD | NEW |