OLD | NEW |
1 // Copyright (c) 2006-2008 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_URL_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_GOOGLE_URL_TRACKER_H_ |
6 #define CHROME_BROWSER_GOOGLE_URL_TRACKER_H_ | 6 #define CHROME_BROWSER_GOOGLE_URL_TRACKER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
13 #include "chrome/common/net/url_fetcher.h" | 13 #include "chrome/common/net/url_fetcher.h" |
14 #include "chrome/common/notification_registrar.h" | 14 #include "chrome/common/notification_registrar.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/network_change_notifier.h" |
16 | 17 |
| 18 class InfoBarDelegate; |
| 19 class NavigationController; |
17 class PrefService; | 20 class PrefService; |
| 21 class TabContents; |
| 22 class TemplateURL; |
18 | 23 |
19 // This object is responsible for updating the Google URL at most once per run, | 24 // This object is responsible for checking the Google URL once per network |
20 // and tracking the currently known value, which is also saved to a pref. | 25 // change, and if necessary prompting the user to see if they want to change to |
| 26 // using it. The current and last prompted values are saved to prefs. |
21 // | 27 // |
22 // Most consumers should only call GoogleURL(), which is guaranteed to | 28 // Most consumers should only call GoogleURL(), which is guaranteed to |
23 // synchronously return a value at all times (even during startup or in unittest | 29 // synchronously return a value at all times (even during startup or in unittest |
24 // mode). Consumers who need to be notified when things change should listen to | 30 // mode). Consumers who need to be notified when things change should listen to |
25 // the notification service for NOTIFY_GOOGLE_URL_UPDATED, and call GoogleURL() | 31 // the notification service for NOTIFY_GOOGLE_URL_UPDATED, and call GoogleURL() |
26 // again after receiving it, in order to get the updated value. | 32 // again after receiving it, in order to get the updated value. |
27 // | 33 // |
28 // To protect users' privacy and reduce server load, no updates will be | 34 // To protect users' privacy and reduce server load, no updates will be |
29 // performed (ever) unless at least one consumer registers interest by calling | 35 // performed (ever) unless at least one consumer registers interest by calling |
30 // RequestServerCheck(). | 36 // RequestServerCheck(). |
31 class GoogleURLTracker : public URLFetcher::Delegate, | 37 class GoogleURLTracker : public URLFetcher::Delegate, |
32 public NotificationObserver { | 38 public NotificationObserver, |
| 39 public net::NetworkChangeNotifier::Observer { |
33 public: | 40 public: |
| 41 class InfoBarDelegateFactory { |
| 42 public: |
| 43 virtual ~InfoBarDelegateFactory() {} |
| 44 virtual InfoBarDelegate* CreateInfoBar(TabContents* tab_contents, |
| 45 GoogleURLTracker* google_url_tracker, |
| 46 const GURL& new_google_url); |
| 47 }; |
| 48 |
34 // Only the main browser process loop should call this, when setting up | 49 // Only the main browser process loop should call this, when setting up |
35 // g_browser_process->google_url_tracker_. No code other than the | 50 // g_browser_process->google_url_tracker_. No code other than the |
36 // GoogleURLTracker itself should actually use | 51 // GoogleURLTracker itself should actually use |
37 // g_browser_process->google_url_tracker() (which shouldn't be hard, since | 52 // g_browser_process->google_url_tracker() (which shouldn't be hard, since |
38 // there aren't useful public functions on this object for consumers to access | 53 // there aren't useful public functions on this object for consumers to access |
39 // anyway). | 54 // anyway). |
40 GoogleURLTracker(); | 55 GoogleURLTracker(); |
41 | 56 |
42 ~GoogleURLTracker(); | 57 virtual ~GoogleURLTracker(); |
43 | 58 |
44 // Returns the current Google URL. This will return a valid URL even in | 59 // Returns the current Google URL. This will return a valid URL even in |
45 // unittest mode. | 60 // unittest mode. |
46 // | 61 // |
47 // This is the only function most code should ever call. | 62 // This is the only function most code should ever call. |
48 static GURL GoogleURL(); | 63 static GURL GoogleURL(); |
49 | 64 |
50 // Requests that the tracker perform a server check to update the Google URL | 65 // Requests that the tracker perform a server check to update the Google URL |
51 // as necessary. This will happen at most once per run, not sooner than five | 66 // as necessary. This will happen at most once per network change, not |
52 // seconds after startup (checks requested before that time will occur then; | 67 // sooner than five seconds after startup (checks requested before that time |
53 // checks requested afterwards will occur immediately, if no other checks have | 68 // will occur then; checks requested afterwards will occur immediately, if |
54 // been made during this run). | 69 // no other checks have been made during this run). |
55 // | 70 // |
56 // In unittest mode, this function does nothing. | 71 // In unittest mode, this function does nothing. |
57 static void RequestServerCheck(); | 72 static void RequestServerCheck(); |
58 | 73 |
59 static void RegisterPrefs(PrefService* prefs); | 74 static void RegisterPrefs(PrefService* prefs); |
60 | 75 |
| 76 // Notifies the tracker that the user has started a Google search. |
| 77 // If prompting is necessary, we then listen for the subsequent |
| 78 // NAV_ENTRY_PENDING notification to get the appropriate NavigationController. |
| 79 // When the load commits, we'll show the infobar. |
| 80 static void GoogleURLSearchCommitted(); |
| 81 |
61 static const char kDefaultGoogleHomepage[]; | 82 static const char kDefaultGoogleHomepage[]; |
| 83 static const char kSearchDomainCheckURL[]; |
| 84 |
| 85 // Methods called from InfoBar delegate. |
| 86 void AcceptGoogleURL(const GURL& google_url); |
| 87 void InfoBarClosed(); |
| 88 void RedoSearch(); |
| 89 |
| 90 NavigationController* controller() const { return controller_; } |
62 | 91 |
63 private: | 92 private: |
64 FRIEND_TEST_ALL_PREFIXES(GoogleURLTrackerTest, CheckAndConvertURL); | 93 friend class GoogleURLTrackerTest; |
65 | |
66 // Determines if |url| is an appropriate source for a new Google base URL, and | |
67 // update |base_url| to the appropriate base URL if so. Returns whether the | |
68 // check succeeded (and thus whether |base_url| was actually updated). | |
69 static bool CheckAndConvertToGoogleBaseURL(const GURL& url, GURL* base_url); | |
70 | 94 |
71 // Registers consumer interest in getting an updated URL from the server. | 95 // Registers consumer interest in getting an updated URL from the server. |
| 96 // It will be notified as NotificationType::GOOGLE_URL_UPDATED, so the |
| 97 // consumer should observe this notification before calling this. |
72 void SetNeedToFetch(); | 98 void SetNeedToFetch(); |
73 | 99 |
74 // Called when the five second startup sleep has finished. Runs any pending | 100 // Called when the five second startup sleep has finished. Runs any pending |
75 // fetch. | 101 // fetch. |
76 void FinishSleep(); | 102 void FinishSleep(); |
77 | 103 |
78 // Starts the fetch of the up-to-date Google URL if we actually want to fetch | 104 // Starts the fetch of the up-to-date Google URL if we actually want to fetch |
79 // it and can currently do so. | 105 // it and can currently do so. |
80 void StartFetchIfDesirable(); | 106 void StartFetchIfDesirable(); |
81 | 107 |
82 // URLFetcher::Delegate | 108 // URLFetcher::Delegate |
83 virtual void OnURLFetchComplete(const URLFetcher *source, | 109 virtual void OnURLFetchComplete(const URLFetcher *source, |
84 const GURL& url, | 110 const GURL& url, |
85 const URLRequestStatus& status, | 111 const URLRequestStatus& status, |
86 int response_code, | 112 int response_code, |
87 const ResponseCookies& cookies, | 113 const ResponseCookies& cookies, |
88 const std::string& data); | 114 const std::string& data); |
89 | 115 |
90 // NotificationObserver | 116 // NotificationObserver |
91 virtual void Observe(NotificationType type, | 117 virtual void Observe(NotificationType type, |
92 const NotificationSource& source, | 118 const NotificationSource& source, |
93 const NotificationDetails& details); | 119 const NotificationDetails& details); |
94 | 120 |
| 121 // NetworkChangeNotifier::Observer |
| 122 virtual void OnIPAddressChanged(); |
| 123 |
| 124 void SearchCommitted(); |
| 125 |
| 126 void ShowGoogleURLInfoBarIfNecessary(TabContents* tab_contents); |
| 127 |
95 NotificationRegistrar registrar_; | 128 NotificationRegistrar registrar_; |
| 129 // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) |
| 130 // rather than URL (e.g. http://www.google.co.uk/), so that user could |
| 131 // configure to use https in search engine templates. |
96 GURL google_url_; | 132 GURL google_url_; |
97 ScopedRunnableMethodFactory<GoogleURLTracker> fetcher_factory_; | 133 GURL fetched_google_url_; |
| 134 ScopedRunnableMethodFactory<GoogleURLTracker> runnable_method_factory_; |
98 scoped_ptr<URLFetcher> fetcher_; | 135 scoped_ptr<URLFetcher> fetcher_; |
| 136 int fetcher_id_; |
99 bool in_startup_sleep_; // True if we're in the five-second "no fetching" | 137 bool in_startup_sleep_; // True if we're in the five-second "no fetching" |
100 // period that begins at browser start. | 138 // period that begins at browser start. |
101 bool already_fetched_; // True if we've already fetched a URL once this run; | 139 bool already_fetched_; // True if we've already fetched a URL once this run; |
102 // we won't fetch again until after a restart. | 140 // we won't fetch again until after a restart. |
103 bool need_to_fetch_; // True if a consumer actually wants us to fetch an | 141 bool need_to_fetch_; // True if a consumer actually wants us to fetch an |
104 // updated URL. If this is never set, we won't | 142 // updated URL. If this is never set, we won't |
105 // bother to fetch anything. | 143 // bother to fetch anything. |
| 144 // Consumers should observe |
| 145 // NotificationType::GOOGLE_URL_UPDATED. |
106 bool request_context_available_; | 146 bool request_context_available_; |
107 // True when the profile has been loaded and the | 147 // True when the profile has been loaded and the |
108 // default request context created, so we can | 148 // default request context created, so we can |
109 // actually do the fetch with the right data. | 149 // actually do the fetch with the right data. |
| 150 bool need_to_prompt_; // True if the last fetched Google URL is not |
| 151 // matched with current user's default Google URL |
| 152 // nor the last prompted Google URL. |
| 153 NavigationController* controller_; |
| 154 scoped_ptr<InfoBarDelegateFactory> infobar_factory_; |
| 155 InfoBarDelegate* infobar_; |
| 156 GURL search_url_; |
110 | 157 |
111 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); | 158 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); |
112 }; | 159 }; |
113 | 160 |
114 #endif // CHROME_BROWSER_GOOGLE_URL_TRACKER_H_ | 161 #endif // CHROME_BROWSER_GOOGLE_URL_TRACKER_H_ |
OLD | NEW |