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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 "font/ttf", | 47 "font/ttf", |
48 "application/x-font-otf", | 48 "application/x-font-otf", |
49 "x-font/woff", | 49 "x-font/woff", |
50 "application/font-sfnt", | 50 "application/font-sfnt", |
51 "application/font-ttf"}; | 51 "application/font-ttf"}; |
52 | 52 |
53 const size_t kMaxManifestByteSize = 16 * 1024; | 53 const size_t kMaxManifestByteSize = 16 * 1024; |
54 const size_t kNumSampleHosts = 50; | 54 const size_t kNumSampleHosts = 50; |
55 const size_t kReportReadinessThreshold = 50; | 55 const size_t kReportReadinessThreshold = 50; |
56 | 56 |
57 bool g_allow_port_in_urls = false; | |
58 | |
59 // 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. |
60 enum ReportingEvent { | 58 enum ReportingEvent { |
61 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, | 59 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, |
62 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, | 60 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, |
63 REPORTING_EVENT_COUNT = 2 | 61 REPORTING_EVENT_COUNT = 2 |
64 }; | 62 }; |
65 | 63 |
66 float ComputeRedirectConfidence(const predictors::RedirectStat& redirect) { | 64 float ComputeRedirectConfidence(const predictors::RedirectStat& redirect) { |
67 return (redirect.number_of_hits() + 0.0) / | 65 return (redirect.number_of_hits() + 0.0) / |
68 (redirect.number_of_hits() + redirect.number_of_misses()); | 66 (redirect.number_of_hits() + redirect.number_of_misses()); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 internal::kResourcePrefetchPredictorRedirectStatusHistogram, | 257 internal::kResourcePrefetchPredictorRedirectStatusHistogram, |
260 static_cast<int>(redirect_status), static_cast<int>(RedirectStatus::MAX)); | 258 static_cast<int>(redirect_status), static_cast<int>(RedirectStatus::MAX)); |
261 } | 259 } |
262 | 260 |
263 } // namespace | 261 } // namespace |
264 | 262 |
265 //////////////////////////////////////////////////////////////////////////////// | 263 //////////////////////////////////////////////////////////////////////////////// |
266 // ResourcePrefetchPredictor static functions. | 264 // ResourcePrefetchPredictor static functions. |
267 | 265 |
268 // static | 266 // static |
269 bool ResourcePrefetchPredictor::ShouldRecordRequest( | |
270 net::URLRequest* request, | |
271 content::ResourceType resource_type) { | |
272 const content::ResourceRequestInfo* request_info = | |
273 content::ResourceRequestInfo::ForRequest(request); | |
274 if (!request_info) | |
275 return false; | |
276 | |
277 if (!request_info->IsMainFrame()) | |
278 return false; | |
279 | |
280 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME && | |
281 IsHandledMainPage(request); | |
282 } | |
283 | |
284 // static | |
285 bool ResourcePrefetchPredictor::ShouldRecordResponse( | |
286 net::URLRequest* response) { | |
287 const content::ResourceRequestInfo* request_info = | |
288 content::ResourceRequestInfo::ForRequest(response); | |
289 if (!request_info) | |
290 return false; | |
291 | |
292 if (!request_info->IsMainFrame()) | |
293 return false; | |
294 | |
295 content::ResourceType resource_type = request_info->GetResourceType(); | |
296 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME | |
297 ? IsHandledMainPage(response) | |
298 : IsHandledSubresource(response, resource_type); | |
299 } | |
300 | |
301 // static | |
302 bool ResourcePrefetchPredictor::ShouldRecordRedirect( | |
303 net::URLRequest* response) { | |
304 return ShouldRecordResponse(response); | |
305 } | |
306 | |
307 // static | |
308 bool ResourcePrefetchPredictor::IsHandledMainPage(net::URLRequest* request) { | |
309 const GURL& url = request->url(); | |
310 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
311 return url.SchemeIsHTTPOrHTTPS() && !bad_port; | |
312 } | |
313 | |
314 // static | |
315 bool ResourcePrefetchPredictor::IsHandledSubresource( | |
316 net::URLRequest* response, | |
317 content::ResourceType resource_type) { | |
318 const GURL& url = response->url(); | |
319 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
320 if (!response->first_party_for_cookies().SchemeIsHTTPOrHTTPS() || | |
321 !url.SchemeIsHTTPOrHTTPS() || bad_port) { | |
322 return false; | |
323 } | |
324 | |
325 std::string mime_type; | |
326 response->GetMimeType(&mime_type); | |
327 if (!IsHandledResourceType(resource_type, mime_type)) | |
328 return false; | |
329 | |
330 if (response->method() != "GET") | |
331 return false; | |
332 | |
333 if (response->original_url().spec().length() > | |
334 ResourcePrefetchPredictorTables::kMaxStringLength) { | |
335 return false; | |
336 } | |
337 | |
338 if (!response->response_info().headers.get()) | |
339 return false; | |
340 | |
341 return true; | |
342 } | |
343 | |
344 // static | |
345 bool ResourcePrefetchPredictor::IsHandledResourceType( | |
346 content::ResourceType resource_type, | |
347 const std::string& mime_type) { | |
348 content::ResourceType actual_resource_type = | |
349 GetResourceType(resource_type, mime_type); | |
350 return actual_resource_type == content::RESOURCE_TYPE_STYLESHEET || | |
351 actual_resource_type == content::RESOURCE_TYPE_SCRIPT || | |
352 actual_resource_type == content::RESOURCE_TYPE_IMAGE || | |
353 actual_resource_type == content::RESOURCE_TYPE_FONT_RESOURCE; | |
354 } | |
355 | |
356 // static | |
357 content::ResourceType ResourcePrefetchPredictor::GetResourceType( | 267 content::ResourceType ResourcePrefetchPredictor::GetResourceType( |
358 content::ResourceType resource_type, | 268 content::ResourceType resource_type, |
359 const std::string& mime_type) { | 269 const std::string& mime_type) { |
360 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE,XHR} to a small set | 270 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE,XHR} to a small set |
361 // of mime types, because these resource types don't communicate how the | 271 // of mime types, because these resource types don't communicate how the |
362 // resources will be used. | 272 // resources will be used. |
363 if (resource_type == content::RESOURCE_TYPE_PREFETCH || | 273 if (resource_type == content::RESOURCE_TYPE_PREFETCH || |
364 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE || | 274 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE || |
365 resource_type == content::RESOURCE_TYPE_XHR) { | 275 resource_type == content::RESOURCE_TYPE_XHR) { |
366 return GetResourceTypeFromMimeType(mime_type, | 276 return GetResourceTypeFromMimeType(mime_type, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 kMinRedirectConfidenceToTriggerPrefetch || | 350 kMinRedirectConfidenceToTriggerPrefetch || |
441 (redirect.number_of_hits() < kMinRedirectHitsToTriggerPrefetch && | 351 (redirect.number_of_hits() < kMinRedirectHitsToTriggerPrefetch && |
442 redirect.url() != entry_point)) { | 352 redirect.url() != entry_point)) { |
443 return false; | 353 return false; |
444 } | 354 } |
445 | 355 |
446 *redirect_endpoint = redirect.url(); | 356 *redirect_endpoint = redirect.url(); |
447 return true; | 357 return true; |
448 } | 358 } |
449 | 359 |
450 // static | |
451 void ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(bool state) { | |
Benoit L
2017/05/29 08:55:36
Do the tests still pass?
I think this function is
| |
452 g_allow_port_in_urls = state; | |
453 } | |
454 | |
455 //////////////////////////////////////////////////////////////////////////////// | 360 //////////////////////////////////////////////////////////////////////////////// |
456 // ResourcePrefetchPredictor nested types. | 361 // ResourcePrefetchPredictor nested types. |
457 | 362 |
458 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary() | 363 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary() |
459 : origin(), | 364 : origin(), |
460 always_access_network(false), | 365 always_access_network(false), |
461 accessed_network(false), | 366 accessed_network(false), |
462 first_occurrence(0) {} | 367 first_occurrence(0) {} |
463 | 368 |
464 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( | 369 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( |
(...skipping 1386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1851 TestObserver::~TestObserver() { | 1756 TestObserver::~TestObserver() { |
1852 predictor_->SetObserverForTesting(nullptr); | 1757 predictor_->SetObserverForTesting(nullptr); |
1853 } | 1758 } |
1854 | 1759 |
1855 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1760 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
1856 : predictor_(predictor) { | 1761 : predictor_(predictor) { |
1857 predictor_->SetObserverForTesting(this); | 1762 predictor_->SetObserverForTesting(this); |
1858 } | 1763 } |
1859 | 1764 |
1860 } // namespace predictors | 1765 } // namespace predictors |
OLD | NEW |