| 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 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 return content::RESOURCE_TYPE_FONT_RESOURCE; | 350 return content::RESOURCE_TYPE_FONT_RESOURCE; |
| 351 } | 351 } |
| 352 return fallback; | 352 return fallback; |
| 353 } | 353 } |
| 354 | 354 |
| 355 //////////////////////////////////////////////////////////////////////////////// | 355 //////////////////////////////////////////////////////////////////////////////// |
| 356 // ResourcePrefetchPredictor structs. | 356 // ResourcePrefetchPredictor structs. |
| 357 | 357 |
| 358 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() | 358 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() |
| 359 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 359 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), |
| 360 was_cached(false) { | 360 priority(net::IDLE), |
| 361 } | 361 was_cached(false) {} |
| 362 | 362 |
| 363 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( | 363 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( |
| 364 const URLRequestSummary& other) | 364 const URLRequestSummary& other) |
| 365 : navigation_id(other.navigation_id), | 365 : navigation_id(other.navigation_id), |
| 366 resource_url(other.resource_url), | 366 resource_url(other.resource_url), |
| 367 resource_type(other.resource_type), | 367 resource_type(other.resource_type), |
| 368 mime_type(other.mime_type), | 368 priority(other.priority), |
| 369 was_cached(other.was_cached), | 369 mime_type(other.mime_type), |
| 370 redirect_url(other.redirect_url) { | 370 was_cached(other.was_cached), |
| 371 } | 371 redirect_url(other.redirect_url) {} |
| 372 | 372 |
| 373 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { | 373 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { |
| 374 } | 374 } |
| 375 | 375 |
| 376 ResourcePrefetchPredictor::Result::Result( | 376 ResourcePrefetchPredictor::Result::Result( |
| 377 PrefetchKeyType i_key_type, | 377 PrefetchKeyType i_key_type, |
| 378 ResourcePrefetcher::RequestVector* i_requests) | 378 ResourcePrefetcher::RequestVector* i_requests) |
| 379 : key_type(i_key_type), | 379 : key_type(i_key_type), |
| 380 requests(i_requests) { | 380 requests(i_requests) { |
| 381 } | 381 } |
| (...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 for (size_t i = 0; i < new_resources_size; ++i) { | 934 for (size_t i = 0; i < new_resources_size; ++i) { |
| 935 if (resources_seen.find(new_resources[i].resource_url) != | 935 if (resources_seen.find(new_resources[i].resource_url) != |
| 936 resources_seen.end()) { | 936 resources_seen.end()) { |
| 937 continue; | 937 continue; |
| 938 } | 938 } |
| 939 ResourceRow row_to_add; | 939 ResourceRow row_to_add; |
| 940 row_to_add.resource_url = new_resources[i].resource_url; | 940 row_to_add.resource_url = new_resources[i].resource_url; |
| 941 row_to_add.resource_type = new_resources[i].resource_type; | 941 row_to_add.resource_type = new_resources[i].resource_type; |
| 942 row_to_add.number_of_hits = 1; | 942 row_to_add.number_of_hits = 1; |
| 943 row_to_add.average_position = i + 1; | 943 row_to_add.average_position = i + 1; |
| 944 row_to_add.priority = new_resources[i].priority; |
| 944 cache_entry->second.resources.push_back(row_to_add); | 945 cache_entry->second.resources.push_back(row_to_add); |
| 945 resources_seen.insert(new_resources[i].resource_url); | 946 resources_seen.insert(new_resources[i].resource_url); |
| 946 } | 947 } |
| 947 } else { | 948 } else { |
| 948 ResourceRows& old_resources = cache_entry->second.resources; | 949 ResourceRows& old_resources = cache_entry->second.resources; |
| 949 cache_entry->second.last_visit = base::Time::Now(); | 950 cache_entry->second.last_visit = base::Time::Now(); |
| 950 | 951 |
| 951 // Build indices over the data. | 952 // Build indices over the data. |
| 952 std::map<GURL, int> new_index, old_index; | 953 std::map<GURL, int> new_index, old_index; |
| 953 int new_resources_size = static_cast<int>(new_resources.size()); | 954 int new_resources_size = static_cast<int>(new_resources.size()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 971 ++old_row.number_of_misses; | 972 ++old_row.number_of_misses; |
| 972 ++old_row.consecutive_misses; | 973 ++old_row.consecutive_misses; |
| 973 } else { | 974 } else { |
| 974 const URLRequestSummary& new_row = | 975 const URLRequestSummary& new_row = |
| 975 new_resources[new_index[old_row.resource_url]]; | 976 new_resources[new_index[old_row.resource_url]]; |
| 976 | 977 |
| 977 // Update the resource type since it could have changed. | 978 // Update the resource type since it could have changed. |
| 978 if (new_row.resource_type != content::RESOURCE_TYPE_LAST_TYPE) | 979 if (new_row.resource_type != content::RESOURCE_TYPE_LAST_TYPE) |
| 979 old_row.resource_type = new_row.resource_type; | 980 old_row.resource_type = new_row.resource_type; |
| 980 | 981 |
| 982 old_row.priority = new_row.priority; |
| 983 |
| 981 int position = new_index[old_row.resource_url] + 1; | 984 int position = new_index[old_row.resource_url] + 1; |
| 982 int total = old_row.number_of_hits + old_row.number_of_misses; | 985 int total = old_row.number_of_hits + old_row.number_of_misses; |
| 983 old_row.average_position = | 986 old_row.average_position = |
| 984 ((old_row.average_position * total) + position) / (total + 1); | 987 ((old_row.average_position * total) + position) / (total + 1); |
| 985 ++old_row.number_of_hits; | 988 ++old_row.number_of_hits; |
| 986 old_row.consecutive_misses = 0; | 989 old_row.consecutive_misses = 0; |
| 987 } | 990 } |
| 988 } | 991 } |
| 989 | 992 |
| 990 // Add the new ones that we have not seen before. | 993 // Add the new ones that we have not seen before. |
| 991 for (int i = 0; i < new_resources_size; ++i) { | 994 for (int i = 0; i < new_resources_size; ++i) { |
| 992 const URLRequestSummary& summary = new_resources[i]; | 995 const URLRequestSummary& summary = new_resources[i]; |
| 993 if (old_index.find(summary.resource_url) != old_index.end()) | 996 if (old_index.find(summary.resource_url) != old_index.end()) |
| 994 continue; | 997 continue; |
| 995 | 998 |
| 996 // Only need to add new stuff. | 999 // Only need to add new stuff. |
| 997 ResourceRow row_to_add; | 1000 ResourceRow row_to_add; |
| 998 row_to_add.resource_url = summary.resource_url; | 1001 row_to_add.resource_url = summary.resource_url; |
| 999 row_to_add.resource_type = summary.resource_type; | 1002 row_to_add.resource_type = summary.resource_type; |
| 1000 row_to_add.number_of_hits = 1; | 1003 row_to_add.number_of_hits = 1; |
| 1001 row_to_add.average_position = i + 1; | 1004 row_to_add.average_position = i + 1; |
| 1005 row_to_add.priority = summary.priority; |
| 1002 old_resources.push_back(row_to_add); | 1006 old_resources.push_back(row_to_add); |
| 1003 | 1007 |
| 1004 // To ensure we dont add the same url twice. | 1008 // To ensure we dont add the same url twice. |
| 1005 old_index[summary.resource_url] = 0; | 1009 old_index[summary.resource_url] = 0; |
| 1006 } | 1010 } |
| 1007 } | 1011 } |
| 1008 | 1012 |
| 1009 // Trim and sort the resources after the update. | 1013 // Trim and sort the resources after the update. |
| 1010 ResourceRows& resources = cache_entry->second.resources; | 1014 ResourceRows& resources = cache_entry->second.resources; |
| 1011 for (ResourceRows::iterator it = resources.begin(); | 1015 for (ResourceRows::iterator it = resources.begin(); |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1381 // HistoryService is already loaded. Continue with Initialization. | 1385 // HistoryService is already loaded. Continue with Initialization. |
| 1382 OnHistoryAndCacheLoaded(); | 1386 OnHistoryAndCacheLoaded(); |
| 1383 return; | 1387 return; |
| 1384 } | 1388 } |
| 1385 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1389 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1386 history_service_observer_.Add(history_service); | 1390 history_service_observer_.Add(history_service); |
| 1387 return; | 1391 return; |
| 1388 } | 1392 } |
| 1389 | 1393 |
| 1390 } // namespace predictors | 1394 } // namespace predictors |
| OLD | NEW |