| 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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 if (IsNoStore(response)) | 283 if (IsNoStore(response)) |
| 284 resource_status |= RESOURCE_STATUS_NO_STORE; | 284 resource_status |= RESOURCE_STATUS_NO_STORE; |
| 285 | 285 |
| 286 return resource_status == 0; | 286 return resource_status == 0; |
| 287 } | 287 } |
| 288 | 288 |
| 289 // static | 289 // static |
| 290 bool ResourcePrefetchPredictor::IsHandledResourceType( | 290 bool ResourcePrefetchPredictor::IsHandledResourceType( |
| 291 content::ResourceType resource_type, | 291 content::ResourceType resource_type, |
| 292 const std::string& mime_type) { | 292 const std::string& mime_type) { |
| 293 bool handled = resource_type == content::RESOURCE_TYPE_STYLESHEET || | 293 content::ResourceType actual_resource_type = |
| 294 resource_type == content::RESOURCE_TYPE_SCRIPT || | 294 GetResourceType(resource_type, mime_type); |
| 295 resource_type == content::RESOURCE_TYPE_IMAGE || | 295 return actual_resource_type == content::RESOURCE_TYPE_STYLESHEET || |
| 296 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE; | 296 actual_resource_type == content::RESOURCE_TYPE_SCRIPT || |
| 297 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE} to a small set of | 297 actual_resource_type == content::RESOURCE_TYPE_IMAGE || |
| 298 // mime types, because these resource types don't communicate how the | 298 actual_resource_type == content::RESOURCE_TYPE_FONT_RESOURCE; |
| 299 // resources | |
| 300 // will be used. | |
| 301 if ((resource_type == content::RESOURCE_TYPE_PREFETCH || | |
| 302 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE)) { | |
| 303 content::ResourceType resource_type_from_mime = GetResourceTypeFromMimeType( | |
| 304 mime_type, content::RESOURCE_TYPE_LAST_TYPE); | |
| 305 handled = resource_type_from_mime != content::RESOURCE_TYPE_LAST_TYPE; | |
| 306 } | |
| 307 return handled; | |
| 308 } | 299 } |
| 309 | 300 |
| 310 // static | 301 // static |
| 302 content::ResourceType ResourcePrefetchPredictor::GetResourceType( |
| 303 content::ResourceType resource_type, |
| 304 const std::string& mime_type) { |
| 305 // Restricts content::RESOURCE_TYPE_{PREFETCH,SUB_RESOURCE} to a small set of |
| 306 // mime types, because these resource types don't communicate how the |
| 307 // resources will be used. |
| 308 if (resource_type == content::RESOURCE_TYPE_PREFETCH || |
| 309 resource_type == content::RESOURCE_TYPE_SUB_RESOURCE) { |
| 310 return GetResourceTypeFromMimeType(mime_type, |
| 311 content::RESOURCE_TYPE_LAST_TYPE); |
| 312 } |
| 313 return resource_type; |
| 314 } |
| 315 |
| 316 // static |
| 311 bool ResourcePrefetchPredictor::IsNoStore(const net::URLRequest* response) { | 317 bool ResourcePrefetchPredictor::IsNoStore(const net::URLRequest* response) { |
| 312 if (response->was_cached()) | 318 if (response->was_cached()) |
| 313 return false; | 319 return false; |
| 314 | 320 |
| 315 const net::HttpResponseInfo& response_info = response->response_info(); | 321 const net::HttpResponseInfo& response_info = response->response_info(); |
| 316 if (!response_info.headers.get()) | 322 if (!response_info.headers.get()) |
| 317 return false; | 323 return false; |
| 318 return response_info.headers->HasHeaderValue("cache-control", "no-store"); | 324 return response_info.headers->HasHeaderValue("cache-control", "no-store"); |
| 319 } | 325 } |
| 320 | 326 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 341 } | 347 } |
| 342 return fallback; | 348 return fallback; |
| 343 } | 349 } |
| 344 | 350 |
| 345 //////////////////////////////////////////////////////////////////////////////// | 351 //////////////////////////////////////////////////////////////////////////////// |
| 346 // ResourcePrefetchPredictor structs. | 352 // ResourcePrefetchPredictor structs. |
| 347 | 353 |
| 348 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() | 354 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() |
| 349 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 355 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), |
| 350 priority(net::IDLE), | 356 priority(net::IDLE), |
| 351 was_cached(false) {} | 357 was_cached(false), |
| 358 has_validators(false), |
| 359 always_revalidate(false) {} |
| 352 | 360 |
| 353 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( | 361 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( |
| 354 const URLRequestSummary& other) | 362 const URLRequestSummary& other) |
| 355 : navigation_id(other.navigation_id), | 363 : navigation_id(other.navigation_id), |
| 356 resource_url(other.resource_url), | 364 resource_url(other.resource_url), |
| 357 resource_type(other.resource_type), | 365 resource_type(other.resource_type), |
| 358 priority(other.priority), | 366 priority(other.priority), |
| 359 mime_type(other.mime_type), | 367 mime_type(other.mime_type), |
| 360 was_cached(other.was_cached), | 368 was_cached(other.was_cached), |
| 361 redirect_url(other.redirect_url) {} | 369 redirect_url(other.redirect_url), |
| 370 has_validators(other.has_validators), |
| 371 always_revalidate(other.always_revalidate) {} |
| 362 | 372 |
| 363 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { | 373 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { |
| 364 } | 374 } |
| 365 | 375 |
| 366 // static | 376 // static |
| 367 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 377 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
| 368 const net::URLRequest& request, | 378 const net::URLRequest& request, |
| 369 URLRequestSummary* summary) { | 379 URLRequestSummary* summary) { |
| 370 const content::ResourceRequestInfo* info = | 380 const content::ResourceRequestInfo* info = |
| 371 content::ResourceRequestInfo::ForRequest(&request); | 381 content::ResourceRequestInfo::ForRequest(&request); |
| 372 if (!info) | 382 if (!info) |
| 373 return false; | 383 return false; |
| 374 | 384 |
| 375 int render_process_id, render_frame_id; | 385 int render_process_id, render_frame_id; |
| 376 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) | 386 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) |
| 377 return false; | 387 return false; |
| 378 | 388 |
| 379 summary->navigation_id = NavigationID(render_process_id, render_frame_id, | 389 summary->navigation_id = NavigationID(render_process_id, render_frame_id, |
| 380 request.first_party_for_cookies()); | 390 request.first_party_for_cookies()); |
| 381 summary->navigation_id.creation_time = request.creation_time(); | 391 summary->navigation_id.creation_time = request.creation_time(); |
| 382 summary->resource_url = request.original_url(); | 392 summary->resource_url = request.original_url(); |
| 383 summary->resource_type = info->GetResourceType(); | 393 content::ResourceType resource_type_from_request = info->GetResourceType(); |
| 384 summary->priority = request.priority(); | 394 summary->priority = request.priority(); |
| 385 request.GetMimeType(&summary->mime_type); | 395 request.GetMimeType(&summary->mime_type); |
| 386 summary->was_cached = request.was_cached(); | 396 summary->was_cached = request.was_cached(); |
| 397 summary->resource_type = |
| 398 GetResourceType(resource_type_from_request, summary->mime_type); |
| 399 |
| 400 scoped_refptr<net::HttpResponseHeaders> headers = |
| 401 request.response_info().headers; |
| 402 if (headers.get()) { |
| 403 summary->has_validators = headers->HasValidators(); |
| 404 // RFC 2616, section 14.9. |
| 405 summary->always_revalidate = |
| 406 headers->HasHeaderValue("cache-control", "no-cache") || |
| 407 headers->HasHeaderValue("pragma", "no-cache") || |
| 408 headers->HasHeaderValue("vary", "*"); |
| 409 } |
| 387 return true; | 410 return true; |
| 388 } | 411 } |
| 389 | 412 |
| 390 ResourcePrefetchPredictor::Result::Result( | 413 ResourcePrefetchPredictor::Result::Result( |
| 391 PrefetchKeyType i_key_type, | 414 PrefetchKeyType i_key_type, |
| 392 ResourcePrefetcher::RequestVector* i_requests) | 415 ResourcePrefetcher::RequestVector* i_requests) |
| 393 : key_type(i_key_type), | 416 : key_type(i_key_type), |
| 394 requests(i_requests) { | 417 requests(i_requests) { |
| 395 } | 418 } |
| 396 | 419 |
| (...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 949 if (resources_seen.find(new_resources[i].resource_url) != | 972 if (resources_seen.find(new_resources[i].resource_url) != |
| 950 resources_seen.end()) { | 973 resources_seen.end()) { |
| 951 continue; | 974 continue; |
| 952 } | 975 } |
| 953 ResourceRow row_to_add; | 976 ResourceRow row_to_add; |
| 954 row_to_add.resource_url = new_resources[i].resource_url; | 977 row_to_add.resource_url = new_resources[i].resource_url; |
| 955 row_to_add.resource_type = new_resources[i].resource_type; | 978 row_to_add.resource_type = new_resources[i].resource_type; |
| 956 row_to_add.number_of_hits = 1; | 979 row_to_add.number_of_hits = 1; |
| 957 row_to_add.average_position = i + 1; | 980 row_to_add.average_position = i + 1; |
| 958 row_to_add.priority = new_resources[i].priority; | 981 row_to_add.priority = new_resources[i].priority; |
| 982 row_to_add.has_validators = new_resources[i].has_validators; |
| 983 row_to_add.always_revalidate = new_resources[i].always_revalidate; |
| 959 cache_entry->second.resources.push_back(row_to_add); | 984 cache_entry->second.resources.push_back(row_to_add); |
| 960 resources_seen.insert(new_resources[i].resource_url); | 985 resources_seen.insert(new_resources[i].resource_url); |
| 961 } | 986 } |
| 962 } else { | 987 } else { |
| 963 ResourceRows& old_resources = cache_entry->second.resources; | 988 ResourceRows& old_resources = cache_entry->second.resources; |
| 964 cache_entry->second.last_visit = base::Time::Now(); | 989 cache_entry->second.last_visit = base::Time::Now(); |
| 965 | 990 |
| 966 // Build indices over the data. | 991 // Build indices over the data. |
| 967 std::map<GURL, int> new_index, old_index; | 992 std::map<GURL, int> new_index, old_index; |
| 968 int new_resources_size = static_cast<int>(new_resources.size()); | 993 int new_resources_size = static_cast<int>(new_resources.size()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 if (old_index.find(summary.resource_url) != old_index.end()) | 1035 if (old_index.find(summary.resource_url) != old_index.end()) |
| 1011 continue; | 1036 continue; |
| 1012 | 1037 |
| 1013 // Only need to add new stuff. | 1038 // Only need to add new stuff. |
| 1014 ResourceRow row_to_add; | 1039 ResourceRow row_to_add; |
| 1015 row_to_add.resource_url = summary.resource_url; | 1040 row_to_add.resource_url = summary.resource_url; |
| 1016 row_to_add.resource_type = summary.resource_type; | 1041 row_to_add.resource_type = summary.resource_type; |
| 1017 row_to_add.number_of_hits = 1; | 1042 row_to_add.number_of_hits = 1; |
| 1018 row_to_add.average_position = i + 1; | 1043 row_to_add.average_position = i + 1; |
| 1019 row_to_add.priority = summary.priority; | 1044 row_to_add.priority = summary.priority; |
| 1045 row_to_add.has_validators = new_resources[i].has_validators; |
| 1046 row_to_add.always_revalidate = new_resources[i].always_revalidate; |
| 1020 old_resources.push_back(row_to_add); | 1047 old_resources.push_back(row_to_add); |
| 1021 | 1048 |
| 1022 // To ensure we dont add the same url twice. | 1049 // To ensure we dont add the same url twice. |
| 1023 old_index[summary.resource_url] = 0; | 1050 old_index[summary.resource_url] = 0; |
| 1024 } | 1051 } |
| 1025 } | 1052 } |
| 1026 | 1053 |
| 1027 // Trim and sort the resources after the update. | 1054 // Trim and sort the resources after the update. |
| 1028 ResourceRows& resources = cache_entry->second.resources; | 1055 ResourceRows& resources = cache_entry->second.resources; |
| 1029 for (ResourceRows::iterator it = resources.begin(); | 1056 for (ResourceRows::iterator it = resources.begin(); |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1399 // HistoryService is already loaded. Continue with Initialization. | 1426 // HistoryService is already loaded. Continue with Initialization. |
| 1400 OnHistoryAndCacheLoaded(); | 1427 OnHistoryAndCacheLoaded(); |
| 1401 return; | 1428 return; |
| 1402 } | 1429 } |
| 1403 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1430 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1404 history_service_observer_.Add(history_service); | 1431 history_service_observer_.Add(history_service); |
| 1405 return; | 1432 return; |
| 1406 } | 1433 } |
| 1407 | 1434 |
| 1408 } // namespace predictors | 1435 } // namespace predictors |
| OLD | NEW |