Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/predictors/resource_prefetch_predictor.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 "font/woff", | 45 "font/woff", |
| 46 "font/ttf", | 46 "font/ttf", |
| 47 "application/x-font-otf", | 47 "application/x-font-otf", |
| 48 "x-font/woff", | 48 "x-font/woff", |
| 49 "application/font-sfnt", | 49 "application/font-sfnt", |
| 50 "application/font-ttf"}; | 50 "application/font-ttf"}; |
| 51 | 51 |
| 52 const size_t kNumSampleHosts = 50; | 52 const size_t kNumSampleHosts = 50; |
| 53 const size_t kReportReadinessThreshold = 50; | 53 const size_t kReportReadinessThreshold = 50; |
| 54 | 54 |
| 55 bool g_allow_port_in_urls = false; | |
|
alexilin
2017/03/15 17:56:26
Why global flag and static setter instead of membe
| |
| 56 | |
| 55 // For reporting events of interest that are not tied to any navigation. | 57 // For reporting events of interest that are not tied to any navigation. |
| 56 enum ReportingEvent { | 58 enum ReportingEvent { |
| 57 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, | 59 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, |
| 58 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, | 60 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, |
| 59 REPORTING_EVENT_COUNT = 2 | 61 REPORTING_EVENT_COUNT = 2 |
| 60 }; | 62 }; |
| 61 | 63 |
| 62 float ComputeRedirectConfidence(const predictors::RedirectStat& redirect) { | 64 float ComputeRedirectConfidence(const predictors::RedirectStat& redirect) { |
| 63 return (redirect.number_of_hits() + 0.0) / | 65 return (redirect.number_of_hits() + 0.0) / |
| 64 (redirect.number_of_hits() + redirect.number_of_misses()); | 66 (redirect.number_of_hits() + redirect.number_of_misses()); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 | 268 |
| 267 if (!request_info->IsMainFrame()) | 269 if (!request_info->IsMainFrame()) |
| 268 return false; | 270 return false; |
| 269 | 271 |
| 270 return request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME && | 272 return request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME && |
| 271 IsHandledMainPage(response); | 273 IsHandledMainPage(response); |
| 272 } | 274 } |
| 273 | 275 |
| 274 // static | 276 // static |
| 275 bool ResourcePrefetchPredictor::IsHandledMainPage(net::URLRequest* request) { | 277 bool ResourcePrefetchPredictor::IsHandledMainPage(net::URLRequest* request) { |
| 276 return request->url().SchemeIsHTTPOrHTTPS(); | 278 const GURL& url = request->url(); |
| 279 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
| 280 return url.SchemeIsHTTPOrHTTPS() && !bad_port; | |
| 277 } | 281 } |
| 278 | 282 |
| 279 // static | 283 // static |
| 280 bool ResourcePrefetchPredictor::IsHandledSubresource( | 284 bool ResourcePrefetchPredictor::IsHandledSubresource( |
| 281 net::URLRequest* response, | 285 net::URLRequest* response, |
| 282 content::ResourceType resource_type) { | 286 content::ResourceType resource_type) { |
| 287 const GURL& url = response->url(); | |
| 288 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
| 283 if (!response->first_party_for_cookies().SchemeIsHTTPOrHTTPS() || | 289 if (!response->first_party_for_cookies().SchemeIsHTTPOrHTTPS() || |
| 284 !response->url().SchemeIsHTTPOrHTTPS()) | 290 !url.SchemeIsHTTPOrHTTPS() || bad_port) { |
| 285 return false; | 291 return false; |
| 292 } | |
| 286 | 293 |
| 287 std::string mime_type; | 294 std::string mime_type; |
| 288 response->GetMimeType(&mime_type); | 295 response->GetMimeType(&mime_type); |
| 289 if (!IsHandledResourceType(resource_type, mime_type)) | 296 if (!IsHandledResourceType(resource_type, mime_type)) |
| 290 return false; | 297 return false; |
| 291 | 298 |
| 292 if (response->method() != "GET") | 299 if (response->method() != "GET") |
| 293 return false; | 300 return false; |
| 294 | 301 |
| 295 if (response->original_url().spec().length() > | 302 if (response->original_url().spec().length() > |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 if (best_redirect == redirect_data.redirect_endpoints().end() || | 398 if (best_redirect == redirect_data.redirect_endpoints().end() || |
| 392 ComputeRedirectConfidence(*best_redirect) < | 399 ComputeRedirectConfidence(*best_redirect) < |
| 393 kMinRedirectConfidenceToTriggerPrefetch || | 400 kMinRedirectConfidenceToTriggerPrefetch || |
| 394 best_redirect->number_of_hits() < kMinRedirectHitsToTriggerPrefetch) | 401 best_redirect->number_of_hits() < kMinRedirectHitsToTriggerPrefetch) |
| 395 return false; | 402 return false; |
| 396 | 403 |
| 397 *final_redirect = best_redirect->url(); | 404 *final_redirect = best_redirect->url(); |
| 398 return true; | 405 return true; |
| 399 } | 406 } |
| 400 | 407 |
| 408 // static | |
| 409 void ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(bool state) { | |
| 410 g_allow_port_in_urls = state; | |
| 411 } | |
| 412 | |
| 401 //////////////////////////////////////////////////////////////////////////////// | 413 //////////////////////////////////////////////////////////////////////////////// |
| 402 // ResourcePrefetchPredictor nested types. | 414 // ResourcePrefetchPredictor nested types. |
| 403 | 415 |
| 404 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() | 416 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() |
| 405 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 417 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), |
| 406 priority(net::IDLE), | 418 priority(net::IDLE), |
| 407 was_cached(false), | 419 was_cached(false), |
| 408 has_validators(false), | 420 has_validators(false), |
| 409 always_revalidate(false) {} | 421 always_revalidate(false) {} |
| 410 | 422 |
| (...skipping 1021 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1432 TestObserver::~TestObserver() { | 1444 TestObserver::~TestObserver() { |
| 1433 predictor_->SetObserverForTesting(nullptr); | 1445 predictor_->SetObserverForTesting(nullptr); |
| 1434 } | 1446 } |
| 1435 | 1447 |
| 1436 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1448 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1437 : predictor_(predictor) { | 1449 : predictor_(predictor) { |
| 1438 predictor_->SetObserverForTesting(this); | 1450 predictor_->SetObserverForTesting(this); |
| 1439 } | 1451 } |
| 1440 | 1452 |
| 1441 } // namespace predictors | 1453 } // namespace predictors |
| OLD | NEW |