| 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 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 return content::RESOURCE_TYPE_STYLESHEET; | 304 return content::RESOURCE_TYPE_STYLESHEET; |
| 305 else | 305 else |
| 306 return fallback; | 306 return fallback; |
| 307 } | 307 } |
| 308 | 308 |
| 309 //////////////////////////////////////////////////////////////////////////////// | 309 //////////////////////////////////////////////////////////////////////////////// |
| 310 // ResourcePrefetchPredictor structs. | 310 // ResourcePrefetchPredictor structs. |
| 311 | 311 |
| 312 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() | 312 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() |
| 313 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 313 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), |
| 314 was_cached(false) { | 314 priority(net::IDLE), |
| 315 } | 315 was_cached(false) {} |
| 316 | 316 |
| 317 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( | 317 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( |
| 318 const URLRequestSummary& other) | 318 const URLRequestSummary& other) |
| 319 : navigation_id(other.navigation_id), | 319 : navigation_id(other.navigation_id), |
| 320 resource_url(other.resource_url), | 320 resource_url(other.resource_url), |
| 321 resource_type(other.resource_type), | 321 resource_type(other.resource_type), |
| 322 mime_type(other.mime_type), | 322 priority(other.priority), |
| 323 was_cached(other.was_cached), | 323 mime_type(other.mime_type), |
| 324 redirect_url(other.redirect_url) { | 324 was_cached(other.was_cached), |
| 325 } | 325 redirect_url(other.redirect_url) {} |
| 326 | 326 |
| 327 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { | 327 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { |
| 328 } | 328 } |
| 329 | 329 |
| 330 ResourcePrefetchPredictor::Result::Result( | 330 ResourcePrefetchPredictor::Result::Result( |
| 331 PrefetchKeyType i_key_type, | 331 PrefetchKeyType i_key_type, |
| 332 ResourcePrefetcher::RequestVector* i_requests) | 332 ResourcePrefetcher::RequestVector* i_requests) |
| 333 : key_type(i_key_type), | 333 : key_type(i_key_type), |
| 334 requests(i_requests) { | 334 requests(i_requests) { |
| 335 } | 335 } |
| (...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 for (size_t i = 0; i < new_resources_size; ++i) { | 888 for (size_t i = 0; i < new_resources_size; ++i) { |
| 889 if (resources_seen.find(new_resources[i].resource_url) != | 889 if (resources_seen.find(new_resources[i].resource_url) != |
| 890 resources_seen.end()) { | 890 resources_seen.end()) { |
| 891 continue; | 891 continue; |
| 892 } | 892 } |
| 893 ResourceRow row_to_add; | 893 ResourceRow row_to_add; |
| 894 row_to_add.resource_url = new_resources[i].resource_url; | 894 row_to_add.resource_url = new_resources[i].resource_url; |
| 895 row_to_add.resource_type = new_resources[i].resource_type; | 895 row_to_add.resource_type = new_resources[i].resource_type; |
| 896 row_to_add.number_of_hits = 1; | 896 row_to_add.number_of_hits = 1; |
| 897 row_to_add.average_position = i + 1; | 897 row_to_add.average_position = i + 1; |
| 898 row_to_add.priority = new_resources[i].priority; |
| 898 cache_entry->second.resources.push_back(row_to_add); | 899 cache_entry->second.resources.push_back(row_to_add); |
| 899 resources_seen.insert(new_resources[i].resource_url); | 900 resources_seen.insert(new_resources[i].resource_url); |
| 900 } | 901 } |
| 901 } else { | 902 } else { |
| 902 ResourceRows& old_resources = cache_entry->second.resources; | 903 ResourceRows& old_resources = cache_entry->second.resources; |
| 903 cache_entry->second.last_visit = base::Time::Now(); | 904 cache_entry->second.last_visit = base::Time::Now(); |
| 904 | 905 |
| 905 // Build indices over the data. | 906 // Build indices over the data. |
| 906 std::map<GURL, int> new_index, old_index; | 907 std::map<GURL, int> new_index, old_index; |
| 907 int new_resources_size = static_cast<int>(new_resources.size()); | 908 int new_resources_size = static_cast<int>(new_resources.size()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 925 ++old_row.number_of_misses; | 926 ++old_row.number_of_misses; |
| 926 ++old_row.consecutive_misses; | 927 ++old_row.consecutive_misses; |
| 927 } else { | 928 } else { |
| 928 const URLRequestSummary& new_row = | 929 const URLRequestSummary& new_row = |
| 929 new_resources[new_index[old_row.resource_url]]; | 930 new_resources[new_index[old_row.resource_url]]; |
| 930 | 931 |
| 931 // Update the resource type since it could have changed. | 932 // Update the resource type since it could have changed. |
| 932 if (new_row.resource_type != content::RESOURCE_TYPE_LAST_TYPE) | 933 if (new_row.resource_type != content::RESOURCE_TYPE_LAST_TYPE) |
| 933 old_row.resource_type = new_row.resource_type; | 934 old_row.resource_type = new_row.resource_type; |
| 934 | 935 |
| 936 old_row.priority = new_row.priority; |
| 937 |
| 935 int position = new_index[old_row.resource_url] + 1; | 938 int position = new_index[old_row.resource_url] + 1; |
| 936 int total = old_row.number_of_hits + old_row.number_of_misses; | 939 int total = old_row.number_of_hits + old_row.number_of_misses; |
| 937 old_row.average_position = | 940 old_row.average_position = |
| 938 ((old_row.average_position * total) + position) / (total + 1); | 941 ((old_row.average_position * total) + position) / (total + 1); |
| 939 ++old_row.number_of_hits; | 942 ++old_row.number_of_hits; |
| 940 old_row.consecutive_misses = 0; | 943 old_row.consecutive_misses = 0; |
| 941 } | 944 } |
| 942 } | 945 } |
| 943 | 946 |
| 944 // Add the new ones that we have not seen before. | 947 // Add the new ones that we have not seen before. |
| 945 for (int i = 0; i < new_resources_size; ++i) { | 948 for (int i = 0; i < new_resources_size; ++i) { |
| 946 const URLRequestSummary& summary = new_resources[i]; | 949 const URLRequestSummary& summary = new_resources[i]; |
| 947 if (old_index.find(summary.resource_url) != old_index.end()) | 950 if (old_index.find(summary.resource_url) != old_index.end()) |
| 948 continue; | 951 continue; |
| 949 | 952 |
| 950 // Only need to add new stuff. | 953 // Only need to add new stuff. |
| 951 ResourceRow row_to_add; | 954 ResourceRow row_to_add; |
| 952 row_to_add.resource_url = summary.resource_url; | 955 row_to_add.resource_url = summary.resource_url; |
| 953 row_to_add.resource_type = summary.resource_type; | 956 row_to_add.resource_type = summary.resource_type; |
| 954 row_to_add.number_of_hits = 1; | 957 row_to_add.number_of_hits = 1; |
| 955 row_to_add.average_position = i + 1; | 958 row_to_add.average_position = i + 1; |
| 959 row_to_add.priority = summary.priority; |
| 956 old_resources.push_back(row_to_add); | 960 old_resources.push_back(row_to_add); |
| 957 | 961 |
| 958 // To ensure we dont add the same url twice. | 962 // To ensure we dont add the same url twice. |
| 959 old_index[summary.resource_url] = 0; | 963 old_index[summary.resource_url] = 0; |
| 960 } | 964 } |
| 961 } | 965 } |
| 962 | 966 |
| 963 // Trim and sort the resources after the update. | 967 // Trim and sort the resources after the update. |
| 964 ResourceRows& resources = cache_entry->second.resources; | 968 ResourceRows& resources = cache_entry->second.resources; |
| 965 for (ResourceRows::iterator it = resources.begin(); | 969 for (ResourceRows::iterator it = resources.begin(); |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1335 // HistoryService is already loaded. Continue with Initialization. | 1339 // HistoryService is already loaded. Continue with Initialization. |
| 1336 OnHistoryAndCacheLoaded(); | 1340 OnHistoryAndCacheLoaded(); |
| 1337 return; | 1341 return; |
| 1338 } | 1342 } |
| 1339 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1343 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1340 history_service_observer_.Add(history_service); | 1344 history_service_observer_.Add(history_service); |
| 1341 return; | 1345 return; |
| 1342 } | 1346 } |
| 1343 | 1347 |
| 1344 } // namespace predictors | 1348 } // namespace predictors |
| OLD | NEW |