Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(827)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor.cc

Issue 2355273002: Redirect handling in the resource_prefetch_predictor. (Closed)
Patch Set: More nits. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
13 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
14 #include "base/metrics/sparse_histogram.h" 15 #include "base/metrics/sparse_histogram.h"
15 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
17 #include "base/time/time.h" 18 #include "base/time/time.h"
18 #include "chrome/browser/history/history_service_factory.h" 19 #include "chrome/browser/history/history_service_factory.h"
19 #include "chrome/browser/predictors/predictor_database.h" 20 #include "chrome/browser/predictors/predictor_database.h"
20 #include "chrome/browser/predictors/predictor_database_factory.h" 21 #include "chrome/browser/predictors/predictor_database_factory.h"
21 #include "chrome/browser/predictors/resource_prefetcher_manager.h" 22 #include "chrome/browser/predictors/resource_prefetcher_manager.h"
22 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
62 }; 63 };
63 64
64 } // namespace 65 } // namespace
65 66
66 namespace predictors { 67 namespace predictors {
67 68
69 const int kMaxRedirectsPerEntry = 20;
70
68 //////////////////////////////////////////////////////////////////////////////// 71 ////////////////////////////////////////////////////////////////////////////////
69 // History lookup task. 72 // History lookup task.
70 73
71 // Used to fetch the visit count for a URL from the History database. 74 // Used to fetch the visit count for a URL from the History database.
72 class GetUrlVisitCountTask : public history::HistoryDBTask { 75 class GetUrlVisitCountTask : public history::HistoryDBTask {
73 public: 76 public:
74 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; 77 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary;
75 typedef base::Callback<void( 78 typedef ResourcePrefetchPredictor::Navigation Navigation;
76 size_t, // Visit count. 79 typedef base::Callback<void(size_t, // Visit count.
77 const NavigationID&, 80 const NavigationID&,
78 const std::vector<URLRequestSummary>&)> VisitInfoCallback; 81 const Navigation&)>
82 VisitInfoCallback;
79 83
80 GetUrlVisitCountTask( 84 GetUrlVisitCountTask(const NavigationID& navigation_id,
81 const NavigationID& navigation_id, 85 std::unique_ptr<Navigation> navigation,
82 std::vector<URLRequestSummary>* requests, 86 VisitInfoCallback callback)
83 VisitInfoCallback callback)
84 : visit_count_(0), 87 : visit_count_(0),
85 navigation_id_(navigation_id), 88 navigation_id_(navigation_id),
86 requests_(requests), 89 navigation_(std::move(navigation)),
87 callback_(callback) { 90 callback_(callback) {
88 DCHECK(requests_.get()); 91 DCHECK(navigation_.get());
89 } 92 }
90 93
91 bool RunOnDBThread(history::HistoryBackend* backend, 94 bool RunOnDBThread(history::HistoryBackend* backend,
92 history::HistoryDatabase* db) override { 95 history::HistoryDatabase* db) override {
93 history::URLRow url_row; 96 history::URLRow url_row;
94 if (db->GetRowForURL(navigation_id_.main_frame_url, &url_row)) 97 if (db->GetRowForURL(navigation_id_.main_frame_url, &url_row))
95 visit_count_ = url_row.visit_count(); 98 visit_count_ = url_row.visit_count();
96 return true; 99 return true;
97 } 100 }
98 101
99 void DoneRunOnMainThread() override { 102 void DoneRunOnMainThread() override {
100 callback_.Run(visit_count_, navigation_id_, *requests_); 103 callback_.Run(visit_count_, navigation_id_, *navigation_);
101 } 104 }
102 105
103 private: 106 private:
104 ~GetUrlVisitCountTask() override {} 107 ~GetUrlVisitCountTask() override {}
105 108
106 int visit_count_; 109 int visit_count_;
107 NavigationID navigation_id_; 110 NavigationID navigation_id_;
108 std::unique_ptr<std::vector<URLRequestSummary>> requests_; 111 std::unique_ptr<Navigation> navigation_;
109 VisitInfoCallback callback_; 112 VisitInfoCallback callback_;
110 113
111 DISALLOW_COPY_AND_ASSIGN(GetUrlVisitCountTask); 114 DISALLOW_COPY_AND_ASSIGN(GetUrlVisitCountTask);
112 }; 115 };
113 116
114 //////////////////////////////////////////////////////////////////////////////// 117 ////////////////////////////////////////////////////////////////////////////////
115 // ResourcePrefetchPredictor static functions. 118 // ResourcePrefetchPredictor static functions.
116 119
117 // static 120 // static
118 bool ResourcePrefetchPredictor::ShouldRecordRequest( 121 bool ResourcePrefetchPredictor::ShouldRecordRequest(
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 priority(other.priority), 277 priority(other.priority),
275 mime_type(other.mime_type), 278 mime_type(other.mime_type),
276 was_cached(other.was_cached), 279 was_cached(other.was_cached),
277 redirect_url(other.redirect_url), 280 redirect_url(other.redirect_url),
278 has_validators(other.has_validators), 281 has_validators(other.has_validators),
279 always_revalidate(other.always_revalidate) {} 282 always_revalidate(other.always_revalidate) {}
280 283
281 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { 284 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() {
282 } 285 }
283 286
287 ResourcePrefetchPredictor::Navigation::Navigation(const GURL& i_initial_url)
288 : initial_url(i_initial_url) {}
289
290 ResourcePrefetchPredictor::Navigation::~Navigation() {}
291
284 // static 292 // static
285 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( 293 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse(
286 const net::URLRequest& request, 294 const net::URLRequest& request,
287 URLRequestSummary* summary) { 295 URLRequestSummary* summary) {
288 const content::ResourceRequestInfo* info = 296 const content::ResourceRequestInfo* info =
289 content::ResourceRequestInfo::ForRequest(&request); 297 content::ResourceRequestInfo::ForRequest(&request);
290 if (!info) 298 if (!info)
291 return false; 299 return false;
292 300
293 int render_process_id, render_frame_id; 301 int render_process_id, render_frame_id;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 const URLRequestSummary& request) { 415 const URLRequestSummary& request) {
408 DCHECK_CURRENTLY_ON(BrowserThread::UI); 416 DCHECK_CURRENTLY_ON(BrowserThread::UI);
409 DCHECK_EQ(INITIALIZED, initialization_state_); 417 DCHECK_EQ(INITIALIZED, initialization_state_);
410 418
411 StartPrefetching(request.navigation_id); 419 StartPrefetching(request.navigation_id);
412 420
413 // Cleanup older navigations. 421 // Cleanup older navigations.
414 CleanupAbandonedNavigations(request.navigation_id); 422 CleanupAbandonedNavigations(request.navigation_id);
415 423
416 // New empty navigation entry. 424 // New empty navigation entry.
425 const GURL& initial_url = request.navigation_id.main_frame_url;
417 inflight_navigations_.insert(std::make_pair( 426 inflight_navigations_.insert(std::make_pair(
418 request.navigation_id, 427 request.navigation_id, base::MakeUnique<Navigation>(initial_url)));
419 make_linked_ptr(new std::vector<URLRequestSummary>())));
420 } 428 }
421 429
422 void ResourcePrefetchPredictor::OnMainFrameResponse( 430 void ResourcePrefetchPredictor::OnMainFrameResponse(
423 const URLRequestSummary& response) { 431 const URLRequestSummary& response) {
424 DCHECK_CURRENTLY_ON(BrowserThread::UI); 432 DCHECK_CURRENTLY_ON(BrowserThread::UI);
425 if (initialization_state_ != INITIALIZED) 433 if (initialization_state_ != INITIALIZED)
426 return; 434 return;
427 435
428 StopPrefetching(response.navigation_id); 436 StopPrefetching(response.navigation_id);
429 } 437 }
430 438
431 void ResourcePrefetchPredictor::OnMainFrameRedirect( 439 void ResourcePrefetchPredictor::OnMainFrameRedirect(
432 const URLRequestSummary& response) { 440 const URLRequestSummary& response) {
433 DCHECK_CURRENTLY_ON(BrowserThread::UI); 441 DCHECK_CURRENTLY_ON(BrowserThread::UI);
434 442
435 // TODO(shishir): There are significant gains to be had here if we can use the
436 // start URL in a redirect chain as the key to start prefetching. We can save
437 // of redirect times considerably assuming that the redirect chains do not
438 // change.
439
440 // Stop any inflight prefetching. Remove the older navigation. 443 // Stop any inflight prefetching. Remove the older navigation.
441 StopPrefetching(response.navigation_id); 444 StopPrefetching(response.navigation_id);
442 inflight_navigations_.erase(response.navigation_id);
443 445
444 // A redirect will not lead to another OnMainFrameRequest call, so record the 446 std::unique_ptr<Navigation> navigation;
445 // redirect url as a new navigation. 447 NavigationMap::iterator nav_it =
448 inflight_navigations_.find(response.navigation_id);
449 if (nav_it != inflight_navigations_.end()) {
450 navigation.reset(nav_it->second.release());
451 inflight_navigations_.erase(nav_it);
452 }
446 453
447 // The redirect url may be empty if the URL was invalid. 454 // The redirect url may be empty if the URL was invalid.
448 if (response.redirect_url.is_empty()) 455 if (response.redirect_url.is_empty())
449 return; 456 return;
450 457
458 // If we lost information about first hop for some reason.
459 if (!navigation) {
460 navigation =
461 base::MakeUnique<Navigation>(response.navigation_id.main_frame_url);
462 }
463
464 // A redirect will not lead to another OnMainFrameRequest call, so record the
465 // redirect url as a new navigation id and save initial url in navigation.
Benoit L 2016/09/27 09:55:06 nit: and save the initial url.
alexilin 2016/09/27 14:52:46 Done.
451 NavigationID navigation_id(response.navigation_id); 466 NavigationID navigation_id(response.navigation_id);
452 navigation_id.main_frame_url = response.redirect_url; 467 navigation_id.main_frame_url = response.redirect_url;
453 inflight_navigations_.insert(std::make_pair( 468 inflight_navigations_.insert(
454 navigation_id, 469 std::make_pair(navigation_id, std::move(navigation)));
455 make_linked_ptr(new std::vector<URLRequestSummary>())));
456 } 470 }
457 471
458 void ResourcePrefetchPredictor::OnSubresourceResponse( 472 void ResourcePrefetchPredictor::OnSubresourceResponse(
459 const URLRequestSummary& response) { 473 const URLRequestSummary& response) {
460 DCHECK_CURRENTLY_ON(BrowserThread::UI); 474 DCHECK_CURRENTLY_ON(BrowserThread::UI);
461 475
462 NavigationMap::const_iterator nav_it = 476 NavigationMap::const_iterator nav_it =
463 inflight_navigations_.find(response.navigation_id); 477 inflight_navigations_.find(response.navigation_id);
464 if (nav_it == inflight_navigations_.end()) { 478 if (nav_it == inflight_navigations_.end()) {
465 return; 479 return;
466 } 480 }
467 481
468 nav_it->second->push_back(response); 482 nav_it->second->subresource_requests.push_back(response);
469 } 483 }
470 484
471 void ResourcePrefetchPredictor::OnNavigationComplete( 485 void ResourcePrefetchPredictor::OnNavigationComplete(
472 const NavigationID& nav_id_without_timing_info) { 486 const NavigationID& nav_id_without_timing_info) {
473 DCHECK_CURRENTLY_ON(BrowserThread::UI); 487 DCHECK_CURRENTLY_ON(BrowserThread::UI);
474 488
475 NavigationMap::iterator nav_it = 489 NavigationMap::iterator nav_it =
476 inflight_navigations_.find(nav_id_without_timing_info); 490 inflight_navigations_.find(nav_id_without_timing_info);
477 if (nav_it == inflight_navigations_.end()) 491 if (nav_it == inflight_navigations_.end())
478 return; 492 return;
479 493
480 const NavigationID navigation_id(nav_it->first); 494 const NavigationID navigation_id(nav_it->first);
481 495
482 // Remove the navigation from the inflight navigations. 496 // Remove the navigation from the inflight navigations.
483 std::vector<URLRequestSummary>* requests = (nav_it->second).release(); 497 std::unique_ptr<Navigation> navigation = std::move(nav_it->second);
484 inflight_navigations_.erase(nav_it); 498 inflight_navigations_.erase(nav_it);
485 499
486 // Kick off history lookup to determine if we should record the URL. 500 // Kick off history lookup to determine if we should record the URL.
487 history::HistoryService* history_service = 501 history::HistoryService* history_service =
488 HistoryServiceFactory::GetForProfile(profile_, 502 HistoryServiceFactory::GetForProfile(profile_,
489 ServiceAccessType::EXPLICIT_ACCESS); 503 ServiceAccessType::EXPLICIT_ACCESS);
490 DCHECK(history_service); 504 DCHECK(history_service);
491 history_service->ScheduleDBTask( 505 history_service->ScheduleDBTask(
492 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask( 506 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask(
493 navigation_id, requests, 507 navigation_id, std::move(navigation),
494 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, 508 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup,
495 AsWeakPtr()))), 509 AsWeakPtr()))),
496 &history_lookup_consumer_); 510 &history_lookup_consumer_);
497 } 511 }
498 512
499 bool ResourcePrefetchPredictor::GetPrefetchData( 513 bool ResourcePrefetchPredictor::GetPrefetchData(
500 const NavigationID& navigation_id, 514 const NavigationID& navigation_id,
501 std::vector<GURL>* urls, 515 std::vector<GURL>* urls,
502 PrefetchKeyType* key_type) { 516 PrefetchKeyType* key_type) {
503 DCHECK(urls); 517 DCHECK(urls);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 navigation_id)); 593 navigation_id));
580 } 594 }
581 595
582 void ResourcePrefetchPredictor::StartInitialization() { 596 void ResourcePrefetchPredictor::StartInitialization() {
583 DCHECK_CURRENTLY_ON(BrowserThread::UI); 597 DCHECK_CURRENTLY_ON(BrowserThread::UI);
584 598
585 DCHECK_EQ(NOT_INITIALIZED, initialization_state_); 599 DCHECK_EQ(NOT_INITIALIZED, initialization_state_);
586 initialization_state_ = INITIALIZING; 600 initialization_state_ = INITIALIZING;
587 601
588 // Create local caches using the database as loaded. 602 // Create local caches using the database as loaded.
589 std::unique_ptr<PrefetchDataMap> url_data_map(new PrefetchDataMap()); 603 auto url_data_map = base::MakeUnique<PrefetchDataMap>();
590 std::unique_ptr<PrefetchDataMap> host_data_map(new PrefetchDataMap()); 604 auto host_data_map = base::MakeUnique<PrefetchDataMap>();
591 PrefetchDataMap* url_data_ptr = url_data_map.get(); 605 auto url_redirect_data_map = base::MakeUnique<RedirectDataMap>();
592 PrefetchDataMap* host_data_ptr = host_data_map.get(); 606 auto host_redirect_data_map = base::MakeUnique<RedirectDataMap>();
593 607
594 BrowserThread::PostTaskAndReply( 608 BrowserThread::PostTaskAndReply(
595 BrowserThread::DB, FROM_HERE, 609 BrowserThread::DB, FROM_HERE,
596 base::Bind(&ResourcePrefetchPredictorTables::GetAllData, 610 base::Bind(&ResourcePrefetchPredictorTables::GetAllData, tables_,
597 tables_, url_data_ptr, host_data_ptr), 611 url_data_map.get(), host_data_map.get(),
612 url_redirect_data_map.get(), host_redirect_data_map.get()),
598 base::Bind(&ResourcePrefetchPredictor::CreateCaches, AsWeakPtr(), 613 base::Bind(&ResourcePrefetchPredictor::CreateCaches, AsWeakPtr(),
599 base::Passed(&url_data_map), base::Passed(&host_data_map))); 614 base::Passed(&url_data_map), base::Passed(&host_data_map),
615 base::Passed(&url_redirect_data_map),
616 base::Passed(&host_redirect_data_map)));
600 } 617 }
601 618
602 void ResourcePrefetchPredictor::CreateCaches( 619 void ResourcePrefetchPredictor::CreateCaches(
603 std::unique_ptr<PrefetchDataMap> url_data_map, 620 std::unique_ptr<PrefetchDataMap> url_data_map,
604 std::unique_ptr<PrefetchDataMap> host_data_map) { 621 std::unique_ptr<PrefetchDataMap> host_data_map,
622 std::unique_ptr<RedirectDataMap> url_redirect_data_map,
623 std::unique_ptr<RedirectDataMap> host_redirect_data_map) {
605 DCHECK_CURRENTLY_ON(BrowserThread::UI); 624 DCHECK_CURRENTLY_ON(BrowserThread::UI);
606 625
607 DCHECK_EQ(INITIALIZING, initialization_state_); 626 DCHECK_EQ(INITIALIZING, initialization_state_);
608 DCHECK(!url_table_cache_); 627 DCHECK(!url_table_cache_);
609 DCHECK(!host_table_cache_); 628 DCHECK(!host_table_cache_);
629 DCHECK(!url_redirect_table_cache_);
630 DCHECK(!host_redirect_table_cache_);
610 DCHECK(inflight_navigations_.empty()); 631 DCHECK(inflight_navigations_.empty());
611 632
612 url_table_cache_.reset(url_data_map.release()); 633 url_table_cache_.reset(url_data_map.release());
613 host_table_cache_.reset(host_data_map.release()); 634 host_table_cache_.reset(host_data_map.release());
635 url_redirect_table_cache_.reset(url_redirect_data_map.release());
636 host_redirect_table_cache_.reset(host_redirect_data_map.release());
614 637
615 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableMainFrameUrlCount", 638 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableMainFrameUrlCount",
616 url_table_cache_->size()); 639 url_table_cache_->size());
617 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount", 640 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount",
618 host_table_cache_->size()); 641 host_table_cache_->size());
619 642
620 ConnectToHistoryService(); 643 ConnectToHistoryService();
621 } 644 }
622 645
623 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { 646 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 } 694 }
672 695
673 const std::string& host = it.url().host(); 696 const std::string& host = it.url().host();
674 if (host_table_cache_->find(host) != host_table_cache_->end()) { 697 if (host_table_cache_->find(host) != host_table_cache_->end()) {
675 hosts_to_delete.push_back(host); 698 hosts_to_delete.push_back(host);
676 host_table_cache_->erase(host); 699 host_table_cache_->erase(host);
677 } 700 }
678 } 701 }
679 702
680 if (!urls_to_delete.empty() || !hosts_to_delete.empty()) { 703 if (!urls_to_delete.empty() || !hosts_to_delete.empty()) {
681 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 704 BrowserThread::PostTask(
682 base::Bind(&ResourcePrefetchPredictorTables::DeleteData, 705 BrowserThread::DB, FROM_HERE,
683 tables_, 706 base::Bind(&ResourcePrefetchPredictorTables::DeleteResourceData,
684 urls_to_delete, 707 tables_, urls_to_delete, hosts_to_delete));
685 hosts_to_delete));
686 } 708 }
687 } 709 }
688 710
689 void ResourcePrefetchPredictor::RemoveOldestEntryInPrefetchDataMap( 711 void ResourcePrefetchPredictor::RemoveOldestEntryInPrefetchDataMap(
690 PrefetchKeyType key_type, 712 PrefetchKeyType key_type,
691 PrefetchDataMap* data_map) { 713 PrefetchDataMap* data_map) {
692 if (data_map->empty()) 714 if (data_map->empty())
693 return; 715 return;
694 716
695 base::Time oldest_time; 717 base::Time oldest_time;
696 std::string key_to_delete; 718 std::string key_to_delete;
697 for (PrefetchDataMap::iterator it = data_map->begin(); 719 for (PrefetchDataMap::iterator it = data_map->begin();
698 it != data_map->end(); ++it) { 720 it != data_map->end(); ++it) {
699 if (key_to_delete.empty() || it->second.last_visit < oldest_time) { 721 if (key_to_delete.empty() || it->second.last_visit < oldest_time) {
700 key_to_delete = it->first; 722 key_to_delete = it->first;
701 oldest_time = it->second.last_visit; 723 oldest_time = it->second.last_visit;
702 } 724 }
703 } 725 }
704 726
705 data_map->erase(key_to_delete); 727 data_map->erase(key_to_delete);
706 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 728 BrowserThread::PostTask(
707 base::Bind(&ResourcePrefetchPredictorTables::DeleteSingleDataPoint, 729 BrowserThread::DB, FROM_HERE,
708 tables_, 730 base::Bind(
709 key_to_delete, 731 &ResourcePrefetchPredictorTables::DeleteSingleResourceDataPoint,
710 key_type)); 732 tables_, key_to_delete, key_type));
733 }
734
735 void ResourcePrefetchPredictor::RemoveOldestEntryInRedirectDataMap(
736 PrefetchKeyType key_type,
737 RedirectDataMap* data_map) {
738 if (data_map->empty())
739 return;
740
741 uint64_t oldest_time;
742 std::string key_to_delete;
743 for (const auto& kv : *data_map) {
744 const RedirectData& data = kv.second;
745 if (key_to_delete.empty() || data.last_visit_time() < oldest_time) {
746 key_to_delete = data.primary_key();
747 oldest_time = data.last_visit_time();
748 }
749 }
750
751 data_map->erase(key_to_delete);
752 BrowserThread::PostTask(
753 BrowserThread::DB, FROM_HERE,
754 base::Bind(
755 &ResourcePrefetchPredictorTables::DeleteSingleRedirectDataPoint,
756 tables_, key_to_delete, key_type));
711 } 757 }
712 758
713 void ResourcePrefetchPredictor::OnVisitCountLookup( 759 void ResourcePrefetchPredictor::OnVisitCountLookup(
714 size_t visit_count, 760 size_t visit_count,
715 const NavigationID& navigation_id, 761 const NavigationID& navigation_id,
716 const std::vector<URLRequestSummary>& requests) { 762 const Navigation& navigation) {
717 DCHECK_CURRENTLY_ON(BrowserThread::UI); 763 DCHECK_CURRENTLY_ON(BrowserThread::UI);
718 764
719 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HistoryVisitCountForUrl", 765 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HistoryVisitCountForUrl",
720 visit_count); 766 visit_count);
721 767
722 // URL level data - merge only if we are already saving the data, or we it 768 // TODO(alexilin) make only one request to DB thread.
769
770 // URL level data - merge only either we've already saved the data, or it
Benoit L 2016/09/27 09:55:06 nit: "merge only if we already saved the data, or
alexilin 2016/09/27 14:52:46 Done.
723 // meets the cutoff requirement. 771 // meets the cutoff requirement.
724 const std::string url_spec = navigation_id.main_frame_url.spec(); 772 const std::string url_spec = navigation_id.main_frame_url.spec();
725 bool already_tracking = url_table_cache_->find(url_spec) != 773 bool already_tracking = url_table_cache_->find(url_spec) !=
726 url_table_cache_->end(); 774 url_table_cache_->end();
727 bool should_track_url = already_tracking || 775 bool should_track_url = already_tracking ||
728 (visit_count >= config_.min_url_visit_count); 776 (visit_count >= config_.min_url_visit_count);
729 777
730 if (should_track_url && config_.IsURLLearningEnabled()) { 778 if (should_track_url && config_.IsURLLearningEnabled()) {
731 LearnNavigation(url_spec, PREFETCH_KEY_TYPE_URL, requests, 779 LearnNavigation(url_spec, PREFETCH_KEY_TYPE_URL,
732 config_.max_urls_to_track, url_table_cache_.get()); 780 navigation.subresource_requests, config_.max_urls_to_track,
781 url_table_cache_.get(), navigation.initial_url.spec(),
782 url_redirect_table_cache_.get());
733 } 783 }
734 784
735 // Host level data - no cutoff, always learn the navigation if enabled. 785 // Host level data - no cutoff, always learn the navigation if enabled.
736 if (config_.IsHostLearningEnabled()) { 786 if (config_.IsHostLearningEnabled()) {
737 LearnNavigation(navigation_id.main_frame_url.host(), 787 const std::string host = navigation_id.main_frame_url.host();
738 PREFETCH_KEY_TYPE_HOST, 788 LearnNavigation(host, PREFETCH_KEY_TYPE_HOST,
739 requests, 789 navigation.subresource_requests, config_.max_hosts_to_track,
740 config_.max_hosts_to_track, 790 host_table_cache_.get(), navigation.initial_url.host(),
741 host_table_cache_.get()); 791 host_redirect_table_cache_.get());
742 } 792 }
743 } 793 }
744 794
745 void ResourcePrefetchPredictor::LearnNavigation( 795 void ResourcePrefetchPredictor::LearnNavigation(
746 const std::string& key, 796 const std::string& key,
747 PrefetchKeyType key_type, 797 PrefetchKeyType key_type,
748 const std::vector<URLRequestSummary>& new_resources, 798 const std::vector<URLRequestSummary>& new_resources,
749 size_t max_data_map_size, 799 size_t max_data_map_size,
750 PrefetchDataMap* data_map) { 800 PrefetchDataMap* data_map,
801 const std::string& key_before_redirects,
802 RedirectDataMap* redirect_map) {
751 DCHECK_CURRENTLY_ON(BrowserThread::UI); 803 DCHECK_CURRENTLY_ON(BrowserThread::UI);
752 804
753 // If the primary key is too long reject it. 805 // If the primary key is too long reject it.
754 if (key.length() > ResourcePrefetchPredictorTables::kMaxStringLength) 806 if (key.length() > ResourcePrefetchPredictorTables::kMaxStringLength)
755 return; 807 return;
756 808
757 PrefetchDataMap::iterator cache_entry = data_map->find(key); 809 PrefetchDataMap::iterator cache_entry = data_map->find(key);
758 if (cache_entry == data_map->end()) { 810 if (cache_entry == data_map->end()) {
759 if (data_map->size() >= max_data_map_size) { 811 // If the table is full, delete an entry.
760 // The table is full, delete an entry. 812 if (data_map->size() >= max_data_map_size)
761 RemoveOldestEntryInPrefetchDataMap(key_type, data_map); 813 RemoveOldestEntryInPrefetchDataMap(key_type, data_map);
762 }
763 814
764 cache_entry = data_map->insert(std::make_pair( 815 cache_entry = data_map->insert(std::make_pair(
765 key, PrefetchData(key_type, key))).first; 816 key, PrefetchData(key_type, key))).first;
766 cache_entry->second.last_visit = base::Time::Now(); 817 cache_entry->second.last_visit = base::Time::Now();
767 size_t new_resources_size = new_resources.size(); 818 size_t new_resources_size = new_resources.size();
768 std::set<GURL> resources_seen; 819 std::set<GURL> resources_seen;
769 for (size_t i = 0; i < new_resources_size; ++i) { 820 for (size_t i = 0; i < new_resources_size; ++i) {
770 if (resources_seen.find(new_resources[i].resource_url) != 821 if (resources_seen.find(new_resources[i].resource_url) !=
771 resources_seen.end()) { 822 resources_seen.end()) {
772 continue; 823 continue;
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 ResourcePrefetchPredictorTables::SortResources(&resources); 923 ResourcePrefetchPredictorTables::SortResources(&resources);
873 if (resources.size() > config_.max_resources_per_entry) 924 if (resources.size() > config_.max_resources_per_entry)
874 resources.resize(config_.max_resources_per_entry); 925 resources.resize(config_.max_resources_per_entry);
875 926
876 // If the row has no resources, remove it from the cache and delete the 927 // If the row has no resources, remove it from the cache and delete the
877 // entry in the database. Else update the database. 928 // entry in the database. Else update the database.
878 if (resources.empty()) { 929 if (resources.empty()) {
879 data_map->erase(key); 930 data_map->erase(key);
880 BrowserThread::PostTask( 931 BrowserThread::PostTask(
881 BrowserThread::DB, FROM_HERE, 932 BrowserThread::DB, FROM_HERE,
882 base::Bind(&ResourcePrefetchPredictorTables::DeleteSingleDataPoint, 933 base::Bind(
883 tables_, 934 &ResourcePrefetchPredictorTables::DeleteSingleResourceDataPoint,
884 key, 935 tables_, key, key_type));
885 key_type));
886 } else { 936 } else {
887 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST; 937 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST;
888 PrefetchData empty_data( 938 PrefetchData empty_data(
889 !is_host ? PREFETCH_KEY_TYPE_HOST : PREFETCH_KEY_TYPE_URL, 939 !is_host ? PREFETCH_KEY_TYPE_HOST : PREFETCH_KEY_TYPE_URL,
890 std::string()); 940 std::string());
941 RedirectData empty_redirect_data;
891 const PrefetchData& host_data = is_host ? cache_entry->second : empty_data; 942 const PrefetchData& host_data = is_host ? cache_entry->second : empty_data;
892 const PrefetchData& url_data = is_host ? empty_data : cache_entry->second; 943 const PrefetchData& url_data = is_host ? empty_data : cache_entry->second;
893 BrowserThread::PostTask( 944 BrowserThread::PostTask(
894 BrowserThread::DB, FROM_HERE, 945 BrowserThread::DB, FROM_HERE,
895 base::Bind(&ResourcePrefetchPredictorTables::UpdateData, 946 base::Bind(&ResourcePrefetchPredictorTables::UpdateData, tables_,
896 tables_, 947 url_data, host_data, empty_redirect_data,
897 url_data, 948 empty_redirect_data));
898 host_data)); 949 }
950
951 if (key != key_before_redirects) {
952 LearnRedirect(key_before_redirects, key_type, key, max_data_map_size,
953 redirect_map);
899 } 954 }
900 } 955 }
901 956
957 void ResourcePrefetchPredictor::LearnRedirect(const std::string& key,
958 PrefetchKeyType key_type,
959 const std::string& final_redirect,
960 size_t max_redirect_map_size,
961 RedirectDataMap* redirect_map) {
962 DCHECK_CURRENTLY_ON(BrowserThread::UI);
963
964 RedirectDataMap::iterator cache_entry = redirect_map->find(key);
965 if (cache_entry == redirect_map->end()) {
966 if (redirect_map->size() >= max_redirect_map_size)
967 RemoveOldestEntryInRedirectDataMap(key_type, redirect_map);
968
969 RedirectData new_data;
970 new_data.set_primary_key(key);
971 cache_entry = redirect_map->insert(std::make_pair(key, new_data)).first;
972 cache_entry->second.set_last_visit_time(
973 base::Time::Now().ToInternalValue());
974 RedirectStat* redirect_to_add = cache_entry->second.add_redirects();
975 redirect_to_add->set_url(final_redirect);
976 redirect_to_add->set_number_of_hits(1);
977 } else {
978 bool need_to_add = true;
979 cache_entry->second.set_last_visit_time(
980 base::Time::Now().ToInternalValue());
981
982 for (RedirectStat& redirect : *(cache_entry->second.mutable_redirects())) {
983 if (redirect.url() == final_redirect) {
984 need_to_add = false;
985 redirect.set_number_of_hits(redirect.number_of_hits() + 1);
986 redirect.set_consecutive_misses(0);
987 } else {
988 redirect.set_number_of_misses(redirect.number_of_misses() + 1);
989 redirect.set_consecutive_misses(redirect.consecutive_misses() + 1);
990 }
991 }
992
993 if (need_to_add) {
994 RedirectStat* redirect_to_add = cache_entry->second.add_redirects();
995 redirect_to_add->set_url(final_redirect);
996 redirect_to_add->set_number_of_hits(1);
997 }
998 }
999
1000 // Trim and sort redirects after update.
1001 std::vector<RedirectStat> redirects;
1002 redirects.reserve(cache_entry->second.redirects_size());
1003 for (const RedirectStat& redirect : cache_entry->second.redirects()) {
1004 if (redirect.consecutive_misses() < config_.max_consecutive_misses)
1005 redirects.push_back(redirect);
1006 }
1007 ResourcePrefetchPredictorTables::SortRedirects(&redirects);
1008
1009 if (redirects.size() > kMaxRedirectsPerEntry)
1010 redirects.resize(kMaxRedirectsPerEntry);
1011
1012 cache_entry->second.clear_redirects();
1013 for (const RedirectStat& redirect : redirects)
1014 cache_entry->second.add_redirects()->CopyFrom(redirect);
1015
1016 if (redirects.empty()) {
1017 redirect_map->erase(cache_entry);
1018 BrowserThread::PostTask(
1019 BrowserThread::DB, FROM_HERE,
1020 base::Bind(
1021 &ResourcePrefetchPredictorTables::DeleteSingleRedirectDataPoint,
1022 tables_, key, key_type));
1023 } else {
1024 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST;
1025 RedirectData empty_redirect_data;
1026 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string());
1027 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string());
1028 const RedirectData& host_redirect_data =
1029 is_host ? cache_entry->second : empty_redirect_data;
1030 const RedirectData& url_redirect_data =
1031 is_host ? empty_redirect_data : cache_entry->second;
1032 BrowserThread::PostTask(
1033 BrowserThread::DB, FROM_HERE,
1034 base::Bind(&ResourcePrefetchPredictorTables::UpdateData, tables_,
1035 empty_url_data, empty_host_data, url_redirect_data,
1036 host_redirect_data));
1037 }
1038 }
1039
902 void ResourcePrefetchPredictor::OnURLsDeleted( 1040 void ResourcePrefetchPredictor::OnURLsDeleted(
903 history::HistoryService* history_service, 1041 history::HistoryService* history_service,
904 bool all_history, 1042 bool all_history,
905 bool expired, 1043 bool expired,
906 const history::URLRows& deleted_rows, 1044 const history::URLRows& deleted_rows,
907 const std::set<GURL>& favicon_urls) { 1045 const std::set<GURL>& favicon_urls) {
908 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1046 DCHECK_CURRENTLY_ON(BrowserThread::UI);
909 if (INITIALIZED != initialization_state_) 1047 if (INITIALIZED != initialization_state_)
910 return; 1048 return;
911 1049
(...skipping 27 matching lines...) Expand all
939 // HistoryService is already loaded. Continue with Initialization. 1077 // HistoryService is already loaded. Continue with Initialization.
940 OnHistoryAndCacheLoaded(); 1078 OnHistoryAndCacheLoaded();
941 return; 1079 return;
942 } 1080 }
943 DCHECK(!history_service_observer_.IsObserving(history_service)); 1081 DCHECK(!history_service_observer_.IsObserving(history_service));
944 history_service_observer_.Add(history_service); 1082 history_service_observer_.Add(history_service);
945 return; 1083 return;
946 } 1084 }
947 1085
948 } // namespace predictors 1086 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698