| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 "application/font-woff2", | 42 "application/font-woff2", |
| 43 "font/x-woff", | 43 "font/x-woff", |
| 44 "application/x-font-ttf", | 44 "application/x-font-ttf", |
| 45 "font/woff", | 45 "font/woff", |
| 46 "font/ttf", | 46 "font/ttf", |
| 47 "application/x-font-otf", | 47 "application/x-font-otf", |
| 48 "x-font/woff", | 48 "x-font/woff", |
| 49 "application/font-sfnt", | 49 "application/font-sfnt", |
| 50 "application/font-ttf"}; | 50 "application/font-ttf"}; |
| 51 | 51 |
| 52 const size_t kMaxManifestByteSize = 16 * 1024; |
| 52 const size_t kNumSampleHosts = 50; | 53 const size_t kNumSampleHosts = 50; |
| 53 const size_t kReportReadinessThreshold = 50; | 54 const size_t kReportReadinessThreshold = 50; |
| 54 | 55 |
| 55 bool g_allow_port_in_urls = false; | 56 bool g_allow_port_in_urls = false; |
| 56 | 57 |
| 57 // For reporting events of interest that are not tied to any navigation. | 58 // For reporting events of interest that are not tied to any navigation. |
| 58 enum ReportingEvent { | 59 enum ReportingEvent { |
| 59 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, | 60 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, |
| 60 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, | 61 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, |
| 61 REPORTING_EVENT_COUNT = 2 | 62 REPORTING_EVENT_COUNT = 2 |
| (...skipping 1018 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1080 } | 1081 } |
| 1081 | 1082 |
| 1082 data_map->erase(key_to_delete); | 1083 data_map->erase(key_to_delete); |
| 1083 BrowserThread::PostTask( | 1084 BrowserThread::PostTask( |
| 1084 BrowserThread::DB, FROM_HERE, | 1085 BrowserThread::DB, FROM_HERE, |
| 1085 base::Bind( | 1086 base::Bind( |
| 1086 &ResourcePrefetchPredictorTables::DeleteSingleRedirectDataPoint, | 1087 &ResourcePrefetchPredictorTables::DeleteSingleRedirectDataPoint, |
| 1087 tables_, key_to_delete, key_type)); | 1088 tables_, key_to_delete, key_type)); |
| 1088 } | 1089 } |
| 1089 | 1090 |
| 1091 void ResourcePrefetchPredictor::RemoveOldestEntryInManifestDataMap( |
| 1092 ManifestDataMap* data_map) { |
| 1093 if (data_map->empty()) |
| 1094 return; |
| 1095 |
| 1096 auto oldest_entry = std::min_element( |
| 1097 data_map->begin(), data_map->end(), |
| 1098 [](const std::pair<const std::string, precache::PrecacheManifest>& lhs, |
| 1099 const std::pair<const std::string, precache::PrecacheManifest>& rhs) { |
| 1100 return lhs.second.id().id() < rhs.second.id().id(); |
| 1101 }); |
| 1102 |
| 1103 std::string key_to_delete = oldest_entry->first; |
| 1104 data_map->erase(oldest_entry); |
| 1105 BrowserThread::PostTask( |
| 1106 BrowserThread::DB, FROM_HERE, |
| 1107 base::Bind(&ResourcePrefetchPredictorTables::DeleteManifestData, tables_, |
| 1108 std::vector<std::string>({key_to_delete}))); |
| 1109 } |
| 1110 |
| 1090 void ResourcePrefetchPredictor::OnVisitCountLookup( | 1111 void ResourcePrefetchPredictor::OnVisitCountLookup( |
| 1091 size_t url_visit_count, | 1112 size_t url_visit_count, |
| 1092 const PageRequestSummary& summary) { | 1113 const PageRequestSummary& summary) { |
| 1093 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1114 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1094 | 1115 |
| 1095 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HistoryVisitCountForUrl", | 1116 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HistoryVisitCountForUrl", |
| 1096 url_visit_count); | 1117 url_visit_count); |
| 1097 | 1118 |
| 1098 // TODO(alexilin): make only one request to DB thread. | 1119 // TODO(alexilin): make only one request to DB thread. |
| 1099 | 1120 |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1416 } | 1437 } |
| 1417 } | 1438 } |
| 1418 | 1439 |
| 1419 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( | 1440 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( |
| 1420 history::HistoryService* history_service) { | 1441 history::HistoryService* history_service) { |
| 1421 if (initialization_state_ == INITIALIZING) { | 1442 if (initialization_state_ == INITIALIZING) { |
| 1422 OnHistoryAndCacheLoaded(); | 1443 OnHistoryAndCacheLoaded(); |
| 1423 } | 1444 } |
| 1424 } | 1445 } |
| 1425 | 1446 |
| 1447 void ResourcePrefetchPredictor::OnManifestFetched( |
| 1448 const std::string& host, |
| 1449 const precache::PrecacheManifest& manifest) { |
| 1450 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1451 if (initialization_state_ != INITIALIZED) |
| 1452 return; |
| 1453 |
| 1454 if (host.length() > ResourcePrefetchPredictorTables::kMaxStringLength || |
| 1455 static_cast<uint32_t>(manifest.ByteSize()) > kMaxManifestByteSize) { |
| 1456 return; |
| 1457 } |
| 1458 |
| 1459 auto cache_entry = manifest_table_cache_->find(host); |
| 1460 if (cache_entry == manifest_table_cache_->end()) { |
| 1461 if (manifest_table_cache_->size() >= config_.max_hosts_to_track) |
| 1462 RemoveOldestEntryInManifestDataMap(manifest_table_cache_.get()); |
| 1463 cache_entry = |
| 1464 manifest_table_cache_->insert(std::make_pair(host, manifest)).first; |
| 1465 } else { |
| 1466 cache_entry->second = manifest; |
| 1467 } |
| 1468 |
| 1469 BrowserThread::PostTask( |
| 1470 BrowserThread::DB, FROM_HERE, |
| 1471 base::Bind(&ResourcePrefetchPredictorTables::UpdateManifestData, tables_, |
| 1472 host, cache_entry->second)); |
| 1473 } |
| 1474 |
| 1426 void ResourcePrefetchPredictor::ConnectToHistoryService() { | 1475 void ResourcePrefetchPredictor::ConnectToHistoryService() { |
| 1427 // Register for HistoryServiceLoading if it is not ready. | 1476 // Register for HistoryServiceLoading if it is not ready. |
| 1428 history::HistoryService* history_service = | 1477 history::HistoryService* history_service = |
| 1429 HistoryServiceFactory::GetForProfile(profile_, | 1478 HistoryServiceFactory::GetForProfile(profile_, |
| 1430 ServiceAccessType::EXPLICIT_ACCESS); | 1479 ServiceAccessType::EXPLICIT_ACCESS); |
| 1431 if (!history_service) | 1480 if (!history_service) |
| 1432 return; | 1481 return; |
| 1433 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1482 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1434 history_service_observer_.Add(history_service); | 1483 history_service_observer_.Add(history_service); |
| 1435 if (history_service->BackendLoaded()) { | 1484 if (history_service->BackendLoaded()) { |
| 1436 // HistoryService is already loaded. Continue with Initialization. | 1485 // HistoryService is already loaded. Continue with Initialization. |
| 1437 OnHistoryAndCacheLoaded(); | 1486 OnHistoryAndCacheLoaded(); |
| 1438 } | 1487 } |
| 1439 } | 1488 } |
| 1440 | 1489 |
| 1441 //////////////////////////////////////////////////////////////////////////////// | 1490 //////////////////////////////////////////////////////////////////////////////// |
| 1442 // TestObserver. | 1491 // TestObserver. |
| 1443 | 1492 |
| 1444 TestObserver::~TestObserver() { | 1493 TestObserver::~TestObserver() { |
| 1445 predictor_->SetObserverForTesting(nullptr); | 1494 predictor_->SetObserverForTesting(nullptr); |
| 1446 } | 1495 } |
| 1447 | 1496 |
| 1448 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1497 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1449 : predictor_(predictor) { | 1498 : predictor_(predictor) { |
| 1450 predictor_->SetObserverForTesting(this); | 1499 predictor_->SetObserverForTesting(this); |
| 1451 } | 1500 } |
| 1452 | 1501 |
| 1453 } // namespace predictors | 1502 } // namespace predictors |
| OLD | NEW |