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/net/predictor.h" | 5 #include "chrome/browser/net/predictor.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <iterator> | 9 #include <iterator> |
10 #include <set> | 10 #include <set> |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 base::BindOnce(&Predictor::FinalizeInitializationOnIOThread, | 164 base::BindOnce(&Predictor::FinalizeInitializationOnIOThread, |
165 base::Unretained(this), urls, | 165 base::Unretained(this), urls, |
166 base::Passed(std::move(referral_list)), io_thread, | 166 base::Passed(std::move(referral_list)), io_thread, |
167 profile_io_data)); | 167 profile_io_data)); |
168 } | 168 } |
169 | 169 |
170 void Predictor::AnticipateOmniboxUrl(const GURL& url, bool preconnectable) { | 170 void Predictor::AnticipateOmniboxUrl(const GURL& url, bool preconnectable) { |
171 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
172 if (!PredictorEnabled()) | 172 if (!PredictorEnabled()) |
173 return; | 173 return; |
174 if (!url.is_valid() || !url.has_host()) | 174 GURL canonicalized_url = CanonicalizeUrl(url); |
Charlie Harrison
2017/05/15 00:01:07
Here and below can you make sure we canoncalize th
xunjieli
2017/05/15 16:41:44
Done.
| |
175 if (!canonicalized_url.is_valid() || !canonicalized_url.has_host()) | |
175 return; | 176 return; |
176 if (!CanPreresolveAndPreconnect()) | 177 if (!CanPreresolveAndPreconnect()) |
177 return; | 178 return; |
178 | 179 |
179 std::string host = url.HostNoBrackets(); | 180 std::string host = canonicalized_url.HostNoBrackets(); |
180 bool is_new_host_request = (host != last_omnibox_host_); | 181 bool is_new_host_request = (host != last_omnibox_host_); |
181 last_omnibox_host_ = host; | 182 last_omnibox_host_ = host; |
182 | 183 |
183 UrlInfo::ResolutionMotivation motivation(UrlInfo::OMNIBOX_MOTIVATED); | 184 UrlInfo::ResolutionMotivation motivation(UrlInfo::OMNIBOX_MOTIVATED); |
184 base::TimeTicks now = base::TimeTicks::Now(); | 185 base::TimeTicks now = base::TimeTicks::Now(); |
185 | 186 |
186 if (preconnectable && !is_new_host_request) { | 187 if (preconnectable && !is_new_host_request) { |
187 ++consecutive_omnibox_preconnect_count_; | 188 ++consecutive_omnibox_preconnect_count_; |
188 // The omnibox suggests a search URL (for which we can preconnect) after | 189 // The omnibox suggests a search URL (for which we can preconnect) after |
189 // one or two characters are typed, even though such typing often (1 in | 190 // one or two characters are typed, even though such typing often (1 in |
(...skipping 15 matching lines...) Expand all Loading... | |
205 // pool. Currently, we just do a connect, which MAY be reset if we | 206 // pool. Currently, we just do a connect, which MAY be reset if we |
206 // don't use it in 10 secondes!!! As a result, we may do more | 207 // don't use it in 10 secondes!!! As a result, we may do more |
207 // connections, and actually cost the server more than if we did a real | 208 // connections, and actually cost the server more than if we did a real |
208 // get with a fake request (/gen_204 might be the good path on Google). | 209 // get with a fake request (/gen_204 might be the good path on Google). |
209 const int kMaxSearchKeepaliveSeconds(10); | 210 const int kMaxSearchKeepaliveSeconds(10); |
210 if ((now - last_omnibox_preconnect_).InSeconds() < | 211 if ((now - last_omnibox_preconnect_).InSeconds() < |
211 kMaxSearchKeepaliveSeconds) | 212 kMaxSearchKeepaliveSeconds) |
212 return; // We've done a preconnect recently. | 213 return; // We've done a preconnect recently. |
213 last_omnibox_preconnect_ = now; | 214 last_omnibox_preconnect_ = now; |
214 const int kConnectionsNeeded = 1; | 215 const int kConnectionsNeeded = 1; |
215 PreconnectUrl(CanonicalizeUrl(url), GURL(), motivation, | 216 PreconnectUrl(canonicalized_url, GURL(), motivation, |
216 kAllowCredentialsOnPreconnectByDefault, kConnectionsNeeded); | 217 kAllowCredentialsOnPreconnectByDefault, kConnectionsNeeded); |
217 return; // Skip pre-resolution, since we'll open a connection. | 218 return; // Skip pre-resolution, since we'll open a connection. |
218 } | 219 } |
219 } else { | 220 } else { |
220 consecutive_omnibox_preconnect_count_ = 0; | 221 consecutive_omnibox_preconnect_count_ = 0; |
221 } | 222 } |
222 | 223 |
223 // Fall through and consider pre-resolution. | 224 // Fall through and consider pre-resolution. |
224 | 225 |
225 // Omnibox tends to call in pairs (just a few milliseconds apart), and we | 226 // Omnibox tends to call in pairs (just a few milliseconds apart), and we |
226 // really don't need to keep resolving a name that often. | 227 // really don't need to keep resolving a name that often. |
227 // TODO(jar): A/B tests could check for perf impact of the early returns. | 228 // TODO(jar): A/B tests could check for perf impact of the early returns. |
228 if (!is_new_host_request) { | 229 if (!is_new_host_request) { |
229 const int kMinPreresolveSeconds(10); | 230 const int kMinPreresolveSeconds(10); |
230 if (kMinPreresolveSeconds > (now - last_omnibox_preresolve_).InSeconds()) | 231 if (kMinPreresolveSeconds > (now - last_omnibox_preresolve_).InSeconds()) |
231 return; | 232 return; |
232 } | 233 } |
233 last_omnibox_preresolve_ = now; | 234 last_omnibox_preresolve_ = now; |
234 | 235 |
235 BrowserThread::PostTask( | 236 BrowserThread::PostTask( |
236 BrowserThread::IO, FROM_HERE, | 237 BrowserThread::IO, FROM_HERE, |
237 base::BindOnce(&Predictor::Resolve, base::Unretained(this), | 238 base::BindOnce(&Predictor::Resolve, base::Unretained(this), |
238 CanonicalizeUrl(url), motivation)); | 239 canonicalized_url, motivation)); |
239 } | 240 } |
240 | 241 |
241 void Predictor::PreconnectUrlAndSubresources(const GURL& url, | 242 void Predictor::PreconnectUrlAndSubresources(const GURL& url, |
242 const GURL& first_party_for_cookies) { | 243 const GURL& first_party_for_cookies) { |
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
244 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 245 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
245 if (!PredictorEnabled() || !url.is_valid() || | 246 GURL canonicalized_url = CanonicalizeUrl(url); |
246 !url.has_host()) | 247 if (!PredictorEnabled() || !canonicalized_url.is_valid() || |
248 !canonicalized_url.has_host()) | |
247 return; | 249 return; |
248 if (!CanPreresolveAndPreconnect()) | 250 if (!CanPreresolveAndPreconnect()) |
249 return; | 251 return; |
250 | 252 |
251 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); | 253 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); |
252 const int kConnectionsNeeded = 1; | 254 const int kConnectionsNeeded = 1; |
253 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, motivation, | 255 PreconnectUrl(canonicalized_url, first_party_for_cookies, motivation, |
254 kConnectionsNeeded, kAllowCredentialsOnPreconnectByDefault); | 256 kConnectionsNeeded, kAllowCredentialsOnPreconnectByDefault); |
255 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); | 257 PredictFrameSubresources(canonicalized_url.GetWithEmptyPath(), |
258 first_party_for_cookies); | |
256 } | 259 } |
257 | 260 |
258 std::vector<GURL> Predictor::GetPredictedUrlListAtStartup( | 261 std::vector<GURL> Predictor::GetPredictedUrlListAtStartup( |
259 PrefService* user_prefs) { | 262 PrefService* user_prefs) { |
260 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 263 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
261 std::vector<GURL> urls; | 264 std::vector<GURL> urls; |
262 // Recall list of URLs we learned about during last session. | 265 // Recall list of URLs we learned about during last session. |
263 // This may catch secondary hostnames, pulled in by the homepages. It will | 266 // This may catch secondary hostnames, pulled in by the homepages. It will |
264 // also catch more of the "primary" home pages, since that was (presumably) | 267 // also catch more of the "primary" home pages, since that was (presumably) |
265 // rendered first (and will be rendered first this time too). | 268 // rendered first (and will be rendered first this time too). |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
718 SerializeReferrers(referral_list); | 721 SerializeReferrers(referral_list); |
719 } | 722 } |
720 | 723 |
721 void Predictor::PreconnectUrl(const GURL& url, | 724 void Predictor::PreconnectUrl(const GURL& url, |
722 const GURL& first_party_for_cookies, | 725 const GURL& first_party_for_cookies, |
723 UrlInfo::ResolutionMotivation motivation, | 726 UrlInfo::ResolutionMotivation motivation, |
724 bool allow_credentials, | 727 bool allow_credentials, |
725 int count) { | 728 int count) { |
726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 729 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
727 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 730 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
731 DCHECK(url.is_valid()); | |
728 | 732 |
729 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 733 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
730 PreconnectUrlOnIOThread(url, first_party_for_cookies, motivation, | 734 PreconnectUrlOnIOThread(url, first_party_for_cookies, motivation, |
731 allow_credentials, count); | 735 allow_credentials, count); |
732 } else { | 736 } else { |
733 BrowserThread::PostTask( | 737 BrowserThread::PostTask( |
734 BrowserThread::IO, FROM_HERE, | 738 BrowserThread::IO, FROM_HERE, |
735 base::BindOnce(&Predictor::PreconnectUrlOnIOThread, | 739 base::BindOnce(&Predictor::PreconnectUrlOnIOThread, |
736 base::Unretained(this), url, first_party_for_cookies, | 740 base::Unretained(this), url, first_party_for_cookies, |
737 motivation, allow_credentials, count)); | 741 motivation, allow_credentials, count)); |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1197 } | 1201 } |
1198 | 1202 |
1199 void SimplePredictor::ShutdownOnUIThread() { | 1203 void SimplePredictor::ShutdownOnUIThread() { |
1200 SetShutdown(true); | 1204 SetShutdown(true); |
1201 } | 1205 } |
1202 | 1206 |
1203 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } | 1207 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } |
1204 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } | 1208 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } |
1205 | 1209 |
1206 } // namespace chrome_browser_net | 1210 } // namespace chrome_browser_net |
OLD | NEW |