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 #include "chrome/browser/google/google_url_tracker.h" | 5 #include "chrome/browser/google/google_url_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/google/google_url_tracker_factory.h" | 12 #include "chrome/browser/google/google_url_tracker_factory.h" |
13 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" | 13 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" |
14 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" | 14 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" |
15 #include "chrome/browser/google/google_util.h" | 15 #include "chrome/browser/google/google_util.h" |
16 #include "chrome/browser/infobars/infobar_service.h" | 16 #include "chrome/browser/infobars/infobar_service.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "components/infobars/core/infobar.h" | 20 #include "components/infobars/core/infobar.h" |
21 #include "content/public/browser/navigation_controller.h" | 21 #include "content/public/browser/navigation_controller.h" |
22 #include "content/public/browser/navigation_entry.h" | 22 #include "content/public/browser/navigation_entry.h" |
23 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
24 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
25 #include "net/base/net_util.h" | |
26 #include "net/url_request/url_fetcher.h" | 25 #include "net/url_request/url_fetcher.h" |
27 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
28 | 27 |
29 | 28 |
30 const char GoogleURLTracker::kDefaultGoogleHomepage[] = | 29 const char GoogleURLTracker::kDefaultGoogleHomepage[] = |
31 "http://www.google.com/"; | 30 "https://www.google.com/"; |
32 const char GoogleURLTracker::kSearchDomainCheckURL[] = | 31 const char GoogleURLTracker::kSearchDomainCheckURL[] = |
33 "https://www.google.com/searchdomaincheck?format=url&type=chrome"; | 32 "https://www.google.com/searchdomaincheck?format=domain&type=chrome"; |
34 | 33 |
35 GoogleURLTracker::GoogleURLTracker( | 34 GoogleURLTracker::GoogleURLTracker( |
36 Profile* profile, | 35 Profile* profile, |
37 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper, | 36 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper, |
38 Mode mode) | 37 Mode mode) |
39 : profile_(profile), | 38 : profile_(profile), |
40 nav_helper_(nav_helper.Pass()), | 39 nav_helper_(nav_helper.Pass()), |
41 infobar_creator_(base::Bind(&GoogleURLTrackerInfoBarDelegate::Create)), | 40 infobar_creator_(base::Bind(&GoogleURLTrackerInfoBarDelegate::Create)), |
42 google_url_(mode == UNIT_TEST_MODE ? kDefaultGoogleHomepage : | 41 google_url_(mode == UNIT_TEST_MODE ? kDefaultGoogleHomepage : |
43 profile->GetPrefs()->GetString(prefs::kLastKnownGoogleURL)), | 42 profile->GetPrefs()->GetString(prefs::kLastKnownGoogleURL)), |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 } | 93 } |
95 } | 94 } |
96 | 95 |
97 // static | 96 // static |
98 void GoogleURLTracker::GoogleURLSearchCommitted(Profile* profile) { | 97 void GoogleURLTracker::GoogleURLSearchCommitted(Profile* profile) { |
99 GoogleURLTracker* tracker = GoogleURLTrackerFactory::GetForProfile(profile); | 98 GoogleURLTracker* tracker = GoogleURLTrackerFactory::GetForProfile(profile); |
100 if (tracker) | 99 if (tracker) |
101 tracker->SearchCommitted(); | 100 tracker->SearchCommitted(); |
102 } | 101 } |
103 | 102 |
104 void GoogleURLTracker::AcceptGoogleURL(bool redo_searches) { | 103 void GoogleURLTracker::AcceptGoogleURL() { |
105 UpdatedDetails urls(google_url_, fetched_google_url_); | 104 UpdatedDetails urls(google_url_, fetched_google_url_); |
106 google_url_ = fetched_google_url_; | 105 google_url_ = fetched_google_url_; |
107 PrefService* prefs = profile_->GetPrefs(); | 106 PrefService* prefs = profile_->GetPrefs(); |
108 prefs->SetString(prefs::kLastKnownGoogleURL, google_url_.spec()); | 107 prefs->SetString(prefs::kLastKnownGoogleURL, google_url_.spec()); |
109 prefs->SetString(prefs::kLastPromptedGoogleURL, google_url_.spec()); | 108 prefs->SetString(prefs::kLastPromptedGoogleURL, google_url_.spec()); |
110 content::NotificationService::current()->Notify( | 109 content::NotificationService::current()->Notify( |
111 chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 110 chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
112 content::Source<Profile>(profile_), | 111 content::Source<Profile>(profile_), |
113 content::Details<UpdatedDetails>(&urls)); | 112 content::Details<UpdatedDetails>(&urls)); |
114 need_to_prompt_ = false; | 113 need_to_prompt_ = false; |
115 CloseAllEntries(redo_searches); | 114 CloseAllEntries(true); |
116 } | 115 } |
117 | 116 |
118 void GoogleURLTracker::CancelGoogleURL() { | 117 void GoogleURLTracker::CancelGoogleURL() { |
119 profile_->GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, | 118 profile_->GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, |
120 fetched_google_url_.spec()); | 119 fetched_google_url_.spec()); |
121 need_to_prompt_ = false; | 120 need_to_prompt_ = false; |
122 CloseAllEntries(false); | 121 CloseAllEntries(false); |
123 } | 122 } |
124 | 123 |
125 void GoogleURLTracker::OnURLFetchComplete(const net::URLFetcher* source) { | 124 void GoogleURLTracker::OnURLFetchComplete(const net::URLFetcher* source) { |
126 // Delete the fetcher on this function's exit. | 125 // Delete the fetcher on this function's exit. |
127 scoped_ptr<net::URLFetcher> clean_up_fetcher(fetcher_.release()); | 126 scoped_ptr<net::URLFetcher> clean_up_fetcher(fetcher_.release()); |
128 | 127 |
129 // Don't update the URL if the request didn't succeed. | 128 // Don't update the URL if the request didn't succeed. |
130 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) { | 129 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) { |
131 already_fetched_ = false; | 130 already_fetched_ = false; |
132 return; | 131 return; |
133 } | 132 } |
134 | 133 |
135 // See if the response data was valid. It should be | 134 // See if the response data was valid. It should be ".google.<TLD>". |
136 // "<scheme>://[www.]google.<TLD>/". | |
137 std::string url_str; | 135 std::string url_str; |
138 source->GetResponseAsString(&url_str); | 136 source->GetResponseAsString(&url_str); |
139 base::TrimWhitespace(url_str, base::TRIM_ALL, &url_str); | 137 base::TrimWhitespace(url_str, base::TRIM_ALL, &url_str); |
140 GURL url(url_str); | 138 if (!StartsWithASCII(url_str, ".google.", false)) |
141 if (!url.is_valid() || (url.path().length() > 1) || url.has_query() || | 139 return; |
142 url.has_ref() || | 140 GURL url("https://www" + url_str); |
141 if ((url.path().length() > 1) || url.has_query() || url.has_ref() || | |
143 !google_util::IsGoogleDomainUrl(url, google_util::DISALLOW_SUBDOMAIN, | 142 !google_util::IsGoogleDomainUrl(url, google_util::DISALLOW_SUBDOMAIN, |
144 google_util::DISALLOW_NON_STANDARD_PORTS)) | 143 google_util::DISALLOW_NON_STANDARD_PORTS)) |
145 return; | 144 return; |
146 | 145 |
147 std::swap(url, fetched_google_url_); | 146 std::swap(url, fetched_google_url_); |
148 GURL last_prompted_url( | 147 GURL last_prompted_url( |
149 profile_->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); | 148 profile_->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); |
150 | 149 |
151 if (last_prompted_url.is_empty()) { | 150 if (last_prompted_url.is_empty()) { |
152 // On the very first run of Chrome, when we've never looked up the URL at | 151 // On the very first run of Chrome, when we've never looked up the URL at |
153 // all, we should just silently switch over to whatever we get immediately. | 152 // all, we should just silently switch over to whatever we get immediately. |
154 AcceptGoogleURL(true); // Arg is irrelevant. | 153 AcceptGoogleURL(); |
155 return; | 154 } else if ((fetched_google_url_ == google_url_) || |
156 } | 155 (fetched_google_url_ == last_prompted_url)) { |
Ilya Sherman
2014/04/24 06:39:45
nit: I'd prefer that you align the two "fetched_go
Peter Kasting
2014/04/24 21:32:05
Done.
| |
157 | 156 // If we re-fetched the current URL, then either the user has continually |
158 base::string16 fetched_host(net::StripWWWFromHost(fetched_google_url_)); | 157 // been on this URL, or we prompted for a different URL but have now changed |
159 if (fetched_google_url_ == google_url_) { | 158 // back before they responded to any of the prompts. |
160 // Either the user has continually been on this URL, or we prompted for a | 159 // |
161 // different URL but have now changed back before they responded to any of | 160 // If we re-fetched the last prompted URL, then we've refetched something |
162 // the prompts. In this latter case we want to close any infobars and stop | 161 // the user previously turned down, and we should preserve their decision. |
163 // prompting. | 162 // Once again, we might have fetched a different URL in the meantime, which |
164 CancelGoogleURL(); | 163 // we have infobars prompting about. |
165 } else if (fetched_host == net::StripWWWFromHost(google_url_)) { | 164 // |
166 // Similar to the above case, but this time the new URL differs from the | 165 // In both these cases, any existing infobars are about an irrelevant URL, |
167 // existing one, probably due to switching between HTTP and HTTPS searching. | 166 // so we should close them. |
168 // Like before we want to close any infobars and stop prompting; we also | |
169 // want to silently accept the change in scheme. We don't redo open | |
170 // searches so as to avoid suddenly changing a page the user might be | |
171 // interacting with; it's enough to simply get future searches right. | |
172 AcceptGoogleURL(false); | |
173 } else if (fetched_host == net::StripWWWFromHost(last_prompted_url)) { | |
174 // We've re-fetched a TLD the user previously turned down. Although the new | |
175 // URL might have a different scheme than the old, we want to preserve the | |
176 // user's decision. Note that it's possible that, like in the above two | |
177 // cases, we fetched yet another different URL in the meantime, which we | |
178 // have infobars prompting about; in this case, as in those above, we want | |
179 // to go ahead and close the infobars and stop prompting, since we've | |
180 // switched back away from that URL. | |
181 CancelGoogleURL(); | 167 CancelGoogleURL(); |
182 } else { | 168 } else { |
183 // We've fetched a URL with a different TLD than the user is currently using | 169 // We've fetched a URL with a different TLD than the user is currently using |
184 // or was previously prompted about. This means we need to prompt again. | 170 // or was previously prompted about. This means we need to prompt again. |
185 need_to_prompt_ = true; | 171 need_to_prompt_ = true; |
186 | 172 |
187 // As in all the above cases, there could be infobars prompting about some | 173 // As in all the above cases, there could be infobars prompting about some |
188 // URL. If these URLs have the same TLD (e.g. for scheme changes), we can | 174 // URL. We go ahead and close them so we can display a new message. |
189 // simply leave the existing infobars open as their messages will still be | 175 CloseAllEntries(false); |
190 // accurate. Otherwise we go ahead and close them because we need to | |
191 // display a new message. | |
192 // Note: |url| is the previous |fetched_google_url_|. | |
193 if (url.is_valid() && (fetched_host != net::StripWWWFromHost(url))) | |
194 CloseAllEntries(false); | |
195 } | 176 } |
196 } | 177 } |
197 | 178 |
198 void GoogleURLTracker::OnIPAddressChanged() { | 179 void GoogleURLTracker::OnIPAddressChanged() { |
199 already_fetched_ = false; | 180 already_fetched_ = false; |
200 StartFetchIfDesirable(); | 181 StartFetchIfDesirable(); |
201 } | 182 } |
202 | 183 |
203 void GoogleURLTracker::Shutdown() { | 184 void GoogleURLTracker::Shutdown() { |
204 nav_helper_.reset(); | 185 nav_helper_.reset(); |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 i->second->navigation_controller())) { | 395 i->second->navigation_controller())) { |
415 DCHECK(nav_helper_->IsListeningForNavigationStart()); | 396 DCHECK(nav_helper_->IsListeningForNavigationStart()); |
416 return; | 397 return; |
417 } | 398 } |
418 } | 399 } |
419 if (nav_helper_->IsListeningForNavigationStart()) { | 400 if (nav_helper_->IsListeningForNavigationStart()) { |
420 DCHECK(!search_committed_); | 401 DCHECK(!search_committed_); |
421 nav_helper_->SetListeningForNavigationStart(false); | 402 nav_helper_->SetListeningForNavigationStart(false); |
422 } | 403 } |
423 } | 404 } |
OLD | NEW |