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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 const precache::PrecacheManifest& rhs) const { | 268 const precache::PrecacheManifest& rhs) const { |
271 return lhs.id().id() < rhs.id().id(); | 269 return lhs.id().id() < rhs.id().id(); |
272 } | 270 } |
273 | 271 |
274 } // namespace internal | 272 } // namespace internal |
275 | 273 |
276 //////////////////////////////////////////////////////////////////////////////// | 274 //////////////////////////////////////////////////////////////////////////////// |
277 // ResourcePrefetchPredictor static functions. | 275 // ResourcePrefetchPredictor static functions. |
278 | 276 |
279 // static | 277 // static |
280 bool ResourcePrefetchPredictor::ShouldRecordRequest( | |
281 net::URLRequest* request, | |
282 content::ResourceType resource_type) { | |
283 const content::ResourceRequestInfo* request_info = | |
284 content::ResourceRequestInfo::ForRequest(request); | |
285 if (!request_info) | |
286 return false; | |
287 | |
288 if (!request_info->IsMainFrame()) | |
289 return false; | |
290 | |
291 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME && | |
292 IsHandledMainPage(request); | |
293 } | |
294 | |
295 // static | |
296 bool ResourcePrefetchPredictor::ShouldRecordResponse( | |
297 net::URLRequest* response) { | |
298 const content::ResourceRequestInfo* request_info = | |
299 content::ResourceRequestInfo::ForRequest(response); | |
300 if (!request_info) | |
301 return false; | |
302 | |
303 if (!request_info->IsMainFrame()) | |
304 return false; | |
305 | |
306 content::ResourceType resource_type = request_info->GetResourceType(); | |
307 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME | |
308 ? IsHandledMainPage(response) | |
309 : IsHandledSubresource(response, resource_type); | |
310 } | |
311 | |
312 // static | |
313 bool ResourcePrefetchPredictor::ShouldRecordRedirect( | |
314 net::URLRequest* response) { | |
315 return ShouldRecordResponse(response); | |
316 } | |
317 | |
318 // static | |
319 bool ResourcePrefetchPredictor::IsHandledMainPage(net::URLRequest* request) { | |
320 const GURL& url = request->url(); | |
321 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
322 return url.SchemeIsHTTPOrHTTPS() && !bad_port; | |
323 } | |
324 | |
325 // static | |
326 bool ResourcePrefetchPredictor::IsHandledSubresource( | |
327 net::URLRequest* response, | |
328 content::ResourceType resource_type) { | |
329 const GURL& url = response->url(); | |
330 bool bad_port = !g_allow_port_in_urls && url.has_port(); | |
331 if (!response->first_party_for_cookies().SchemeIsHTTPOrHTTPS() || | |
332 !url.SchemeIsHTTPOrHTTPS() || bad_port) { | |
333 return false; | |
334 } | |
335 | |
336 std::string mime_type; | |
337 response->GetMimeType(&mime_type); | |
338 if (!IsHandledResourceType(resource_type, mime_type)) | |
339 return false; | |
340 | |
341 if (response->method() != "GET") | |
342 return false; | |
343 | |
344 if (response->original_url().spec().length() > | |
345 ResourcePrefetchPredictorTables::kMaxStringLength) { | |
346 return false; | |
347 } | |
348 | |
349 if (!response->response_info().headers.get()) | |
350 return false; | |
351 | |
352 return true; | |
353 } | |
354 | |
355 // static | |
356 bool ResourcePrefetchPredictor::IsHandledResourceType( | |
357 content::ResourceType resource_type, | |
358 const std::string& mime_type) { | |
359 content::ResourceType actual_resource_type = | |
360 GetResourceType(resource_type, mime_type); | |
361 return actual_resource_type == content::RESOURCE_TYPE_STYLESHEET || | |
362 actual_resource_type == content::RESOURCE_TYPE_SCRIPT || | |
363 actual_resource_type == content::RESOURCE_TYPE_IMAGE || | |
364 actual_resource_type == content::RESOURCE_TYPE_FONT_RESOURCE; | |
365 } | |
366 | |
367 // static | |
368 content::ResourceType ResourcePrefetchPredictor::GetResourceType( | 278 content::ResourceType ResourcePrefetchPredictor::GetResourceType( |
369 content::ResourceType resource_type, | 279 content::ResourceType resource_type, |
370 const std::string& mime_type) { | 280 const std::string& mime_type) { |
371 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE,XHR} to a small set | 281 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE,XHR} to a small set |
372 // of mime types, because these resource types don't communicate how the | 282 // of mime types, because these resource types don't communicate how the |
373 // resources will be used. | 283 // resources will be used. |
374 if (resource_type == content::RESOURCE_TYPE_PREFETCH || | 284 if (resource_type == content::RESOURCE_TYPE_PREFETCH || |
375 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE || | 285 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE || |
376 resource_type == content::RESOURCE_TYPE_XHR) { | 286 resource_type == content::RESOURCE_TYPE_XHR) { |
377 return GetResourceTypeFromMimeType(mime_type, | 287 return GetResourceTypeFromMimeType(mime_type, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 kMinRedirectConfidenceToTriggerPrefetch || | 361 kMinRedirectConfidenceToTriggerPrefetch || |
452 (redirect.number_of_hits() < kMinRedirectHitsToTriggerPrefetch && | 362 (redirect.number_of_hits() < kMinRedirectHitsToTriggerPrefetch && |
453 redirect.url() != entry_point)) { | 363 redirect.url() != entry_point)) { |
454 return false; | 364 return false; |
455 } | 365 } |
456 | 366 |
457 *redirect_endpoint = redirect.url(); | 367 *redirect_endpoint = redirect.url(); |
458 return true; | 368 return true; |
459 } | 369 } |
460 | 370 |
461 // static | |
462 void ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(bool state) { | |
463 g_allow_port_in_urls = state; | |
464 } | |
465 | |
466 //////////////////////////////////////////////////////////////////////////////// | 371 //////////////////////////////////////////////////////////////////////////////// |
467 // ResourcePrefetchPredictor nested types. | 372 // ResourcePrefetchPredictor nested types. |
468 | 373 |
469 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary() | 374 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary() |
470 : origin(), | 375 : origin(), |
471 always_access_network(false), | 376 always_access_network(false), |
472 accessed_network(false), | 377 accessed_network(false), |
473 first_occurrence(0) {} | 378 first_occurrence(0) {} |
474 | 379 |
475 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( | 380 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( |
(...skipping 1080 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1556 TestObserver::~TestObserver() { | 1461 TestObserver::~TestObserver() { |
1557 predictor_->SetObserverForTesting(nullptr); | 1462 predictor_->SetObserverForTesting(nullptr); |
1558 } | 1463 } |
1559 | 1464 |
1560 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1465 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
1561 : predictor_(predictor) { | 1466 : predictor_(predictor) { |
1562 predictor_->SetObserverForTesting(this); | 1467 predictor_->SetObserverForTesting(this); |
1563 } | 1468 } |
1564 | 1469 |
1565 } // namespace predictors | 1470 } // namespace predictors |
OLD | NEW |