| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 "application/font-ttf"}; | 56 "application/font-ttf"}; |
| 57 | 57 |
| 58 // For reporting whether a subresource is handled or not, and for what reasons. | 58 // For reporting whether a subresource is handled or not, and for what reasons. |
| 59 enum ResourceStatus { | 59 enum ResourceStatus { |
| 60 RESOURCE_STATUS_HANDLED = 0, | 60 RESOURCE_STATUS_HANDLED = 0, |
| 61 RESOURCE_STATUS_NOT_HTTP_OR_HTTPS_PAGE = 1, | 61 RESOURCE_STATUS_NOT_HTTP_OR_HTTPS_PAGE = 1, |
| 62 RESOURCE_STATUS_NOT_HTTP_OR_HTTPS_RESOURCE = 2, | 62 RESOURCE_STATUS_NOT_HTTP_OR_HTTPS_RESOURCE = 2, |
| 63 RESOURCE_STATUS_UNSUPPORTED_RESOURCE_TYPE = 4, | 63 RESOURCE_STATUS_UNSUPPORTED_RESOURCE_TYPE = 4, |
| 64 RESOURCE_STATUS_NOT_GET = 8, | 64 RESOURCE_STATUS_NOT_GET = 8, |
| 65 RESOURCE_STATUS_URL_TOO_LONG = 16, | 65 RESOURCE_STATUS_URL_TOO_LONG = 16, |
| 66 RESOURCE_STATUS_NOT_CACHEABLE = 32, | 66 RESOURCE_STATUS_NO_STORE = 32, |
| 67 RESOURCE_STATUS_HEADERS_MISSING = 64, | 67 RESOURCE_STATUS_HEADERS_MISSING = 64, |
| 68 RESOURCE_STATUS_MAX = 128, | 68 RESOURCE_STATUS_MAX = 128, |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 // For reporting various interesting events that occur during the loading of a | 71 // For reporting various interesting events that occur during the loading of a |
| 72 // single main frame. | 72 // single main frame. |
| 73 enum NavigationEvent { | 73 enum NavigationEvent { |
| 74 NAVIGATION_EVENT_REQUEST_STARTED = 0, | 74 NAVIGATION_EVENT_REQUEST_STARTED = 0, |
| 75 NAVIGATION_EVENT_REQUEST_REDIRECTED = 1, | 75 NAVIGATION_EVENT_REQUEST_REDIRECTED = 1, |
| 76 NAVIGATION_EVENT_REQUEST_REDIRECTED_EMPTY_URL = 2, | 76 NAVIGATION_EVENT_REQUEST_REDIRECTED_EMPTY_URL = 2, |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 resource_status |= RESOURCE_STATUS_NOT_GET; | 273 resource_status |= RESOURCE_STATUS_NOT_GET; |
| 274 | 274 |
| 275 if (response->original_url().spec().length() > | 275 if (response->original_url().spec().length() > |
| 276 ResourcePrefetchPredictorTables::kMaxStringLength) { | 276 ResourcePrefetchPredictorTables::kMaxStringLength) { |
| 277 resource_status |= RESOURCE_STATUS_URL_TOO_LONG; | 277 resource_status |= RESOURCE_STATUS_URL_TOO_LONG; |
| 278 } | 278 } |
| 279 | 279 |
| 280 if (!response->response_info().headers.get()) | 280 if (!response->response_info().headers.get()) |
| 281 resource_status |= RESOURCE_STATUS_HEADERS_MISSING; | 281 resource_status |= RESOURCE_STATUS_HEADERS_MISSING; |
| 282 | 282 |
| 283 if (!IsCacheable(response)) | 283 if (IsNoStore(response)) |
| 284 resource_status |= RESOURCE_STATUS_NOT_CACHEABLE; | 284 resource_status |= RESOURCE_STATUS_NO_STORE; |
| 285 | |
| 286 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ResourceStatus", | |
| 287 resource_status, | |
| 288 RESOURCE_STATUS_MAX); | |
| 289 | 285 |
| 290 return resource_status == 0; | 286 return resource_status == 0; |
| 291 } | 287 } |
| 292 | 288 |
| 293 // static | 289 // static |
| 294 bool ResourcePrefetchPredictor::IsHandledResourceType( | 290 bool ResourcePrefetchPredictor::IsHandledResourceType( |
| 295 content::ResourceType resource_type, | 291 content::ResourceType resource_type, |
| 296 const std::string& mime_type) { | 292 const std::string& mime_type) { |
| 297 bool handled = resource_type == content::RESOURCE_TYPE_STYLESHEET || | 293 bool handled = resource_type == content::RESOURCE_TYPE_STYLESHEET || |
| 298 resource_type == content::RESOURCE_TYPE_SCRIPT || | 294 resource_type == content::RESOURCE_TYPE_SCRIPT || |
| 299 resource_type == content::RESOURCE_TYPE_IMAGE || | 295 resource_type == content::RESOURCE_TYPE_IMAGE || |
| 300 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE; | 296 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE; |
| 301 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE} to a small set of | 297 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE} to a small set of |
| 302 // mime types, because these resource types don't communicate how the | 298 // mime types, because these resource types don't communicate how the |
| 303 // resources | 299 // resources |
| 304 // will be used. | 300 // will be used. |
| 305 if ((resource_type == content::RESOURCE_TYPE_PREFETCH || | 301 if ((resource_type == content::RESOURCE_TYPE_PREFETCH || |
| 306 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE)) { | 302 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE)) { |
| 307 content::ResourceType resource_type_from_mime = GetResourceTypeFromMimeType( | 303 content::ResourceType resource_type_from_mime = GetResourceTypeFromMimeType( |
| 308 mime_type, content::RESOURCE_TYPE_LAST_TYPE); | 304 mime_type, content::RESOURCE_TYPE_LAST_TYPE); |
| 309 handled = resource_type_from_mime != content::RESOURCE_TYPE_LAST_TYPE; | 305 handled = resource_type_from_mime != content::RESOURCE_TYPE_LAST_TYPE; |
| 310 } | 306 } |
| 311 return handled; | 307 return handled; |
| 312 } | 308 } |
| 313 | 309 |
| 314 // static | 310 // static |
| 315 bool ResourcePrefetchPredictor::IsCacheable(const net::URLRequest* response) { | 311 bool ResourcePrefetchPredictor::IsNoStore(const net::URLRequest* response) { |
| 316 if (response->was_cached()) | 312 if (response->was_cached()) |
| 317 return true; | 313 return false; |
| 318 | 314 |
| 319 // For non cached responses, we will ensure that the freshness lifetime is | |
| 320 // some sane value. | |
| 321 const net::HttpResponseInfo& response_info = response->response_info(); | 315 const net::HttpResponseInfo& response_info = response->response_info(); |
| 322 if (!response_info.headers.get()) | 316 if (!response_info.headers.get()) |
| 323 return false; | 317 return false; |
| 324 base::Time response_time(response_info.response_time); | 318 return response_info.headers->HasHeaderValue("cache-control", "no-store"); |
| 325 response_time += base::TimeDelta::FromSeconds(1); | |
| 326 base::TimeDelta freshness = | |
| 327 response_info.headers->GetFreshnessLifetimes(response_time).freshness; | |
| 328 return freshness > base::TimeDelta(); | |
| 329 } | 319 } |
| 330 | 320 |
| 331 // static | 321 // static |
| 332 content::ResourceType ResourcePrefetchPredictor::GetResourceTypeFromMimeType( | 322 content::ResourceType ResourcePrefetchPredictor::GetResourceTypeFromMimeType( |
| 333 const std::string& mime_type, | 323 const std::string& mime_type, |
| 334 content::ResourceType fallback) { | 324 content::ResourceType fallback) { |
| 335 if (mime_type.empty()) { | 325 if (mime_type.empty()) { |
| 336 return fallback; | 326 return fallback; |
| 337 } else if (mime_util::IsSupportedImageMimeType(mime_type)) { | 327 } else if (mime_util::IsSupportedImageMimeType(mime_type)) { |
| 338 return content::RESOURCE_TYPE_IMAGE; | 328 return content::RESOURCE_TYPE_IMAGE; |
| (...skipping 1046 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1385 // HistoryService is already loaded. Continue with Initialization. | 1375 // HistoryService is already loaded. Continue with Initialization. |
| 1386 OnHistoryAndCacheLoaded(); | 1376 OnHistoryAndCacheLoaded(); |
| 1387 return; | 1377 return; |
| 1388 } | 1378 } |
| 1389 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1379 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1390 history_service_observer_.Add(history_service); | 1380 history_service_observer_.Add(history_service); |
| 1391 return; | 1381 return; |
| 1392 } | 1382 } |
| 1393 | 1383 |
| 1394 } // namespace predictors | 1384 } // namespace predictors |
| OLD | NEW |