| 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 21 matching lines...) Expand all Loading... |
| 32 #include "content/public/browser/resource_request_info.h" | 32 #include "content/public/browser/resource_request_info.h" |
| 33 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 34 #include "net/base/mime_util.h" | 34 #include "net/base/mime_util.h" |
| 35 #include "net/base/network_change_notifier.h" | 35 #include "net/base/network_change_notifier.h" |
| 36 #include "net/http/http_response_headers.h" | 36 #include "net/http/http_response_headers.h" |
| 37 #include "net/url_request/url_request.h" | 37 #include "net/url_request/url_request.h" |
| 38 #include "net/url_request/url_request_context_getter.h" | 38 #include "net/url_request/url_request_context_getter.h" |
| 39 | 39 |
| 40 using content::BrowserThread; | 40 using content::BrowserThread; |
| 41 | 41 |
| 42 namespace predictors { |
| 43 |
| 42 namespace { | 44 namespace { |
| 43 | 45 |
| 44 // Sorted by decreasing likelihood according to HTTP archive. | 46 // Sorted by decreasing likelihood according to HTTP archive. |
| 45 const char* kFontMimeTypes[] = {"font/woff2", | 47 const char* kFontMimeTypes[] = {"font/woff2", |
| 46 "application/x-font-woff", | 48 "application/x-font-woff", |
| 47 "application/font-woff", | 49 "application/font-woff", |
| 48 "application/font-woff2", | 50 "application/font-woff2", |
| 49 "font/x-woff", | 51 "font/x-woff", |
| 50 "application/x-font-ttf", | 52 "application/x-font-ttf", |
| 51 "font/woff", | 53 "font/woff", |
| 52 "font/ttf", | 54 "font/ttf", |
| 53 "application/x-font-otf", | 55 "application/x-font-otf", |
| 54 "x-font/woff", | 56 "x-font/woff", |
| 55 "application/font-sfnt", | 57 "application/font-sfnt", |
| 56 "application/font-ttf"}; | 58 "application/font-ttf"}; |
| 57 | 59 |
| 58 // For reporting events of interest that are not tied to any navigation. | 60 // For reporting events of interest that are not tied to any navigation. |
| 59 enum ReportingEvent { | 61 enum ReportingEvent { |
| 60 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, | 62 REPORTING_EVENT_ALL_HISTORY_CLEARED = 0, |
| 61 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, | 63 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED = 1, |
| 62 REPORTING_EVENT_COUNT = 2 | 64 REPORTING_EVENT_COUNT = 2 |
| 63 }; | 65 }; |
| 64 | 66 |
| 65 float ComputeRedirectConfidence(const predictors::RedirectStat& redirect) { | 67 float ComputeRedirectConfidence(const predictors::RedirectStat& redirect) { |
| 66 return (redirect.number_of_hits() + 0.0) / | 68 return (redirect.number_of_hits() + 0.0) / |
| 67 (redirect.number_of_hits() + redirect.number_of_misses()); | 69 (redirect.number_of_hits() + redirect.number_of_misses()); |
| 68 } | 70 } |
| 69 | 71 |
| 72 // Used to fetch the visit count for a URL from the History database. |
| 73 class GetUrlVisitCountTask : public history::HistoryDBTask { |
| 74 public: |
| 75 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 76 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 77 typedef base::Callback<void(size_t, // URL visit count. |
| 78 const PageRequestSummary&)> |
| 79 VisitInfoCallback; |
| 80 |
| 81 GetUrlVisitCountTask(std::unique_ptr<PageRequestSummary> summary, |
| 82 VisitInfoCallback callback); |
| 83 |
| 84 bool RunOnDBThread(history::HistoryBackend* backend, |
| 85 history::HistoryDatabase* db) override; |
| 86 |
| 87 void DoneRunOnMainThread() override; |
| 88 |
| 89 private: |
| 90 ~GetUrlVisitCountTask() override; |
| 91 |
| 92 int visit_count_; |
| 93 std::unique_ptr<PageRequestSummary> summary_; |
| 94 VisitInfoCallback callback_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(GetUrlVisitCountTask); |
| 97 }; |
| 98 |
| 99 GetUrlVisitCountTask::GetUrlVisitCountTask( |
| 100 std::unique_ptr<PageRequestSummary> summary, |
| 101 VisitInfoCallback callback) |
| 102 : visit_count_(0), summary_(std::move(summary)), callback_(callback) { |
| 103 DCHECK(summary_.get()); |
| 104 } |
| 105 |
| 106 bool GetUrlVisitCountTask::RunOnDBThread(history::HistoryBackend* backend, |
| 107 history::HistoryDatabase* db) { |
| 108 history::URLRow url_row; |
| 109 if (db->GetRowForURL(summary_->main_frame_url, &url_row)) |
| 110 visit_count_ = url_row.visit_count(); |
| 111 return true; |
| 112 } |
| 113 |
| 114 void GetUrlVisitCountTask::DoneRunOnMainThread() { |
| 115 callback_.Run(visit_count_, *summary_); |
| 116 } |
| 117 |
| 118 GetUrlVisitCountTask::~GetUrlVisitCountTask() {} |
| 119 |
| 70 } // namespace | 120 } // namespace |
| 71 | 121 |
| 72 namespace predictors { | |
| 73 | |
| 74 //////////////////////////////////////////////////////////////////////////////// | 122 //////////////////////////////////////////////////////////////////////////////// |
| 75 // ResourcePrefetchPredictor static functions. | 123 // ResourcePrefetchPredictor static functions. |
| 76 | 124 |
| 77 // static | 125 // static |
| 78 bool ResourcePrefetchPredictor::ShouldRecordRequest( | 126 bool ResourcePrefetchPredictor::ShouldRecordRequest( |
| 79 net::URLRequest* request, | 127 net::URLRequest* request, |
| 80 content::ResourceType resource_type) { | 128 content::ResourceType resource_type) { |
| 81 const content::ResourceRequestInfo* request_info = | 129 const content::ResourceRequestInfo* request_info = |
| 82 content::ResourceRequestInfo::ForRequest(request); | 130 content::ResourceRequestInfo::ForRequest(request); |
| 83 if (!request_info) | 131 if (!request_info) |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 summary->has_validators = headers->HasValidators(); | 351 summary->has_validators = headers->HasValidators(); |
| 304 // RFC 2616, section 14.9. | 352 // RFC 2616, section 14.9. |
| 305 summary->always_revalidate = | 353 summary->always_revalidate = |
| 306 headers->HasHeaderValue("cache-control", "no-cache") || | 354 headers->HasHeaderValue("cache-control", "no-cache") || |
| 307 headers->HasHeaderValue("pragma", "no-cache") || | 355 headers->HasHeaderValue("pragma", "no-cache") || |
| 308 headers->HasHeaderValue("vary", "*"); | 356 headers->HasHeaderValue("vary", "*"); |
| 309 } | 357 } |
| 310 return true; | 358 return true; |
| 311 } | 359 } |
| 312 | 360 |
| 313 ResourcePrefetchPredictor::GetUrlVisitCountTask::GetUrlVisitCountTask( | 361 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( |
| 314 const NavigationID& navigation_id, | 362 const GURL& i_main_frame_url) |
| 315 std::unique_ptr<PageRequestSummary> summary, | 363 : main_frame_url(i_main_frame_url), initial_url(i_main_frame_url) {} |
| 316 VisitInfoCallback callback) | |
| 317 : visit_count_(0), | |
| 318 navigation_id_(navigation_id), | |
| 319 summary_(std::move(summary)), | |
| 320 callback_(callback) { | |
| 321 DCHECK(summary_.get()); | |
| 322 } | |
| 323 | |
| 324 bool ResourcePrefetchPredictor::GetUrlVisitCountTask::RunOnDBThread( | |
| 325 history::HistoryBackend* backend, | |
| 326 history::HistoryDatabase* db) { | |
| 327 history::URLRow url_row; | |
| 328 if (db->GetRowForURL(navigation_id_.main_frame_url, &url_row)) | |
| 329 visit_count_ = url_row.visit_count(); | |
| 330 return true; | |
| 331 } | |
| 332 | |
| 333 void ResourcePrefetchPredictor::GetUrlVisitCountTask::DoneRunOnMainThread() { | |
| 334 callback_.Run(visit_count_, navigation_id_, *summary_); | |
| 335 } | |
| 336 | |
| 337 ResourcePrefetchPredictor::GetUrlVisitCountTask::~GetUrlVisitCountTask() {} | |
| 338 | 364 |
| 339 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( | 365 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( |
| 340 const GURL& i_initial_url) | 366 const PageRequestSummary& other) = default; |
| 341 : initial_url(i_initial_url) {} | |
| 342 | 367 |
| 343 ResourcePrefetchPredictor::PageRequestSummary::~PageRequestSummary() {} | 368 ResourcePrefetchPredictor::PageRequestSummary::~PageRequestSummary() {} |
| 344 | 369 |
| 345 //////////////////////////////////////////////////////////////////////////////// | 370 //////////////////////////////////////////////////////////////////////////////// |
| 346 // ResourcePrefetchPredictor. | 371 // ResourcePrefetchPredictor. |
| 347 | 372 |
| 348 ResourcePrefetchPredictor::ResourcePrefetchPredictor( | 373 ResourcePrefetchPredictor::ResourcePrefetchPredictor( |
| 349 const ResourcePrefetchPredictorConfig& config, | 374 const ResourcePrefetchPredictorConfig& config, |
| 350 Profile* profile) | 375 Profile* profile) |
| 351 : profile_(profile), | 376 : profile_(profile), |
| 377 observer_(nullptr), |
| 352 config_(config), | 378 config_(config), |
| 353 initialization_state_(NOT_INITIALIZED), | 379 initialization_state_(NOT_INITIALIZED), |
| 354 tables_(PredictorDatabaseFactory::GetForProfile(profile) | 380 tables_(PredictorDatabaseFactory::GetForProfile(profile) |
| 355 ->resource_prefetch_tables()), | 381 ->resource_prefetch_tables()), |
| 356 history_service_observer_(this) { | 382 history_service_observer_(this) { |
| 357 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 383 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 358 | 384 |
| 359 // Some form of learning has to be enabled. | 385 // Some form of learning has to be enabled. |
| 360 DCHECK(config_.IsLearningEnabled()); | 386 DCHECK(config_.IsLearningEnabled()); |
| 361 if (config_.IsURLPrefetchingEnabled(profile_)) | 387 if (config_.IsURLPrefetchingEnabled(profile_)) |
| 362 DCHECK(config_.IsURLLearningEnabled()); | 388 DCHECK(config_.IsURLLearningEnabled()); |
| 363 if (config_.IsHostPrefetchingEnabled(profile_)) | 389 if (config_.IsHostPrefetchingEnabled(profile_)) |
| 364 DCHECK(config_.IsHostLearningEnabled()); | 390 DCHECK(config_.IsHostLearningEnabled()); |
| 365 } | 391 } |
| 366 | 392 |
| 367 ResourcePrefetchPredictor::~ResourcePrefetchPredictor() { | 393 ResourcePrefetchPredictor::~ResourcePrefetchPredictor() {} |
| 368 } | |
| 369 | 394 |
| 370 void ResourcePrefetchPredictor::RecordURLRequest( | 395 void ResourcePrefetchPredictor::RecordURLRequest( |
| 371 const URLRequestSummary& request) { | 396 const URLRequestSummary& request) { |
| 372 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 397 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 373 if (initialization_state_ != INITIALIZED) | 398 if (initialization_state_ != INITIALIZED) |
| 374 return; | 399 return; |
| 375 | 400 |
| 376 CHECK_EQ(request.resource_type, content::RESOURCE_TYPE_MAIN_FRAME); | 401 CHECK_EQ(request.resource_type, content::RESOURCE_TYPE_MAIN_FRAME); |
| 377 OnMainFrameRequest(request); | 402 OnMainFrameRequest(request); |
| 378 } | 403 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 // corresponding to the navigation has not been created yet. | 437 // corresponding to the navigation has not been created yet. |
| 413 if (!navigation_id.main_frame_url.is_empty()) | 438 if (!navigation_id.main_frame_url.is_empty()) |
| 414 OnNavigationComplete(navigation_id); | 439 OnNavigationComplete(navigation_id); |
| 415 break; | 440 break; |
| 416 default: | 441 default: |
| 417 NOTREACHED() << "Unexpected initialization_state_: " | 442 NOTREACHED() << "Unexpected initialization_state_: " |
| 418 << initialization_state_; | 443 << initialization_state_; |
| 419 } | 444 } |
| 420 } | 445 } |
| 421 | 446 |
| 447 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url) { |
| 448 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", |
| 449 url.spec()); |
| 450 if (!prefetch_manager_.get()) // Prefetching not enabled. |
| 451 return; |
| 452 |
| 453 std::vector<GURL> subresource_urls; |
| 454 if (!GetPrefetchData(url, &subresource_urls)) { |
| 455 // No prefetching data at host or URL level. |
| 456 return; |
| 457 } |
| 458 |
| 459 BrowserThread::PostTask( |
| 460 BrowserThread::IO, FROM_HERE, |
| 461 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, |
| 462 prefetch_manager_, url, subresource_urls)); |
| 463 } |
| 464 |
| 465 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { |
| 466 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StopPrefetching", "url", |
| 467 url.spec()); |
| 468 if (!prefetch_manager_.get()) // Not enabled. |
| 469 return; |
| 470 |
| 471 BrowserThread::PostTask( |
| 472 BrowserThread::IO, FROM_HERE, |
| 473 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, |
| 474 prefetch_manager_, url)); |
| 475 } |
| 476 |
| 477 void ResourcePrefetchPredictor::SetObserverForTesting(TestObserver* observer) { |
| 478 observer_ = observer; |
| 479 } |
| 480 |
| 422 void ResourcePrefetchPredictor::Shutdown() { | 481 void ResourcePrefetchPredictor::Shutdown() { |
| 423 if (prefetch_manager_.get()) { | 482 if (prefetch_manager_.get()) { |
| 424 prefetch_manager_->ShutdownOnUIThread(); | 483 prefetch_manager_->ShutdownOnUIThread(); |
| 425 prefetch_manager_ = NULL; | 484 prefetch_manager_ = NULL; |
| 426 } | 485 } |
| 427 history_service_observer_.RemoveAll(); | 486 history_service_observer_.RemoveAll(); |
| 428 } | 487 } |
| 429 | 488 |
| 430 void ResourcePrefetchPredictor::OnMainFrameRequest( | 489 void ResourcePrefetchPredictor::OnMainFrameRequest( |
| 431 const URLRequestSummary& request) { | 490 const URLRequestSummary& request) { |
| 432 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 491 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 433 DCHECK_EQ(INITIALIZED, initialization_state_); | 492 DCHECK_EQ(INITIALIZED, initialization_state_); |
| 434 | 493 |
| 435 StartPrefetching(request.navigation_id.main_frame_url); | 494 const GURL& main_frame_url = request.navigation_id.main_frame_url; |
| 495 StartPrefetching(main_frame_url); |
| 436 | 496 |
| 437 // Cleanup older navigations. | 497 // Cleanup older navigations. |
| 438 CleanupAbandonedNavigations(request.navigation_id); | 498 CleanupAbandonedNavigations(request.navigation_id); |
| 439 | 499 |
| 440 // New empty navigation entry. | 500 // New empty navigation entry. |
| 441 const GURL& initial_url = request.navigation_id.main_frame_url; | |
| 442 inflight_navigations_.insert( | 501 inflight_navigations_.insert( |
| 443 std::make_pair(request.navigation_id, | 502 std::make_pair(request.navigation_id, |
| 444 base::MakeUnique<PageRequestSummary>(initial_url))); | 503 base::MakeUnique<PageRequestSummary>(main_frame_url))); |
| 445 } | 504 } |
| 446 | 505 |
| 447 void ResourcePrefetchPredictor::OnMainFrameResponse( | 506 void ResourcePrefetchPredictor::OnMainFrameResponse( |
| 448 const URLRequestSummary& response) { | 507 const URLRequestSummary& response) { |
| 449 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 508 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 450 if (initialization_state_ != INITIALIZED) | 509 if (initialization_state_ != INITIALIZED) |
| 451 return; | 510 return; |
| 452 | 511 |
| 453 StopPrefetching(response.navigation_id.main_frame_url); | 512 StopPrefetching(response.navigation_id.main_frame_url); |
| 454 } | 513 } |
| 455 | 514 |
| 456 void ResourcePrefetchPredictor::OnMainFrameRedirect( | 515 void ResourcePrefetchPredictor::OnMainFrameRedirect( |
| 457 const URLRequestSummary& response) { | 516 const URLRequestSummary& response) { |
| 458 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 517 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 459 | 518 |
| 519 const GURL& main_frame_url = response.navigation_id.main_frame_url; |
| 460 std::unique_ptr<PageRequestSummary> summary; | 520 std::unique_ptr<PageRequestSummary> summary; |
| 461 NavigationMap::iterator nav_it = | 521 NavigationMap::iterator nav_it = |
| 462 inflight_navigations_.find(response.navigation_id); | 522 inflight_navigations_.find(response.navigation_id); |
| 463 if (nav_it != inflight_navigations_.end()) { | 523 if (nav_it != inflight_navigations_.end()) { |
| 464 summary.reset(nav_it->second.release()); | 524 summary.reset(nav_it->second.release()); |
| 465 inflight_navigations_.erase(nav_it); | 525 inflight_navigations_.erase(nav_it); |
| 466 } | 526 } |
| 467 | 527 |
| 468 // The redirect url may be empty if the URL was invalid. | 528 // The redirect url may be empty if the URL was invalid. |
| 469 if (response.redirect_url.is_empty()) | 529 if (response.redirect_url.is_empty()) |
| 470 return; | 530 return; |
| 471 | 531 |
| 472 // If we lost the information about the first hop for some reason. | 532 // If we lost the information about the first hop for some reason. |
| 473 if (!summary) { | 533 if (!summary) { |
| 474 const GURL& initial_url = response.navigation_id.main_frame_url; | 534 summary = base::MakeUnique<PageRequestSummary>(main_frame_url); |
| 475 summary = base::MakeUnique<PageRequestSummary>(initial_url); | |
| 476 } | 535 } |
| 477 | 536 |
| 478 // A redirect will not lead to another OnMainFrameRequest call, so record the | 537 // A redirect will not lead to another OnMainFrameRequest call, so record the |
| 479 // redirect url as a new navigation id and save the initial url. | 538 // redirect url as a new navigation id and save the initial url. |
| 480 NavigationID navigation_id(response.navigation_id); | 539 NavigationID navigation_id(response.navigation_id); |
| 481 navigation_id.main_frame_url = response.redirect_url; | 540 navigation_id.main_frame_url = response.redirect_url; |
| 541 summary->main_frame_url = response.redirect_url; |
| 482 inflight_navigations_.insert( | 542 inflight_navigations_.insert( |
| 483 std::make_pair(navigation_id, std::move(summary))); | 543 std::make_pair(navigation_id, std::move(summary))); |
| 484 } | 544 } |
| 485 | 545 |
| 486 void ResourcePrefetchPredictor::OnSubresourceResponse( | 546 void ResourcePrefetchPredictor::OnSubresourceResponse( |
| 487 const URLRequestSummary& response) { | 547 const URLRequestSummary& response) { |
| 488 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 548 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 489 | 549 |
| 490 NavigationMap::const_iterator nav_it = | 550 NavigationMap::const_iterator nav_it = |
| 491 inflight_navigations_.find(response.navigation_id); | 551 inflight_navigations_.find(response.navigation_id); |
| 492 if (nav_it == inflight_navigations_.end()) { | 552 if (nav_it == inflight_navigations_.end()) { |
| 493 return; | 553 return; |
| 494 } | 554 } |
| 495 | 555 |
| 496 nav_it->second->subresource_requests.push_back(response); | 556 nav_it->second->subresource_requests.push_back(response); |
| 497 } | 557 } |
| 498 | 558 |
| 499 void ResourcePrefetchPredictor::OnNavigationComplete( | 559 void ResourcePrefetchPredictor::OnNavigationComplete( |
| 500 const NavigationID& nav_id_without_timing_info) { | 560 const NavigationID& nav_id_without_timing_info) { |
| 501 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 561 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 502 | 562 |
| 503 NavigationMap::iterator nav_it = | 563 NavigationMap::iterator nav_it = |
| 504 inflight_navigations_.find(nav_id_without_timing_info); | 564 inflight_navigations_.find(nav_id_without_timing_info); |
| 505 if (nav_it == inflight_navigations_.end()) | 565 if (nav_it == inflight_navigations_.end()) |
| 506 return; | 566 return; |
| 507 | 567 |
| 508 const NavigationID navigation_id(nav_it->first); | |
| 509 | |
| 510 // Remove the navigation from the inflight navigations. | 568 // Remove the navigation from the inflight navigations. |
| 511 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); | 569 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); |
| 512 inflight_navigations_.erase(nav_it); | 570 inflight_navigations_.erase(nav_it); |
| 513 | 571 |
| 514 // Kick off history lookup to determine if we should record the URL. | 572 // Kick off history lookup to determine if we should record the URL. |
| 515 history::HistoryService* history_service = | 573 history::HistoryService* history_service = |
| 516 HistoryServiceFactory::GetForProfile(profile_, | 574 HistoryServiceFactory::GetForProfile(profile_, |
| 517 ServiceAccessType::EXPLICIT_ACCESS); | 575 ServiceAccessType::EXPLICIT_ACCESS); |
| 518 DCHECK(history_service); | 576 DCHECK(history_service); |
| 519 history_service->ScheduleDBTask( | 577 history_service->ScheduleDBTask( |
| 520 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask( | 578 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask( |
| 521 navigation_id, std::move(summary), | 579 std::move(summary), |
| 522 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, | 580 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, |
| 523 AsWeakPtr()))), | 581 AsWeakPtr()))), |
| 524 &history_lookup_consumer_); | 582 &history_lookup_consumer_); |
| 525 } | 583 } |
| 526 | 584 |
| 527 bool ResourcePrefetchPredictor::GetPrefetchData(const GURL& main_frame_url, | 585 bool ResourcePrefetchPredictor::GetPrefetchData(const GURL& main_frame_url, |
| 528 std::vector<GURL>* urls) { | 586 std::vector<GURL>* urls) { |
| 529 DCHECK(urls); | 587 DCHECK(urls); |
| 530 DCHECK(urls->empty()); | 588 DCHECK(urls->empty()); |
| 531 bool use_url_data = config_.IsPrefetchingEnabled(profile_) ? | 589 bool use_url_data = config_.IsPrefetchingEnabled(profile_) ? |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 resource.number_of_hits() < | 637 resource.number_of_hits() < |
| 580 config_.min_resource_hits_to_trigger_prefetch) | 638 config_.min_resource_hits_to_trigger_prefetch) |
| 581 continue; | 639 continue; |
| 582 | 640 |
| 583 urls->push_back(GURL(resource.resource_url())); | 641 urls->push_back(GURL(resource.resource_url())); |
| 584 } | 642 } |
| 585 | 643 |
| 586 return urls->size() > initial_size; | 644 return urls->size() > initial_size; |
| 587 } | 645 } |
| 588 | 646 |
| 589 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url) { | |
| 590 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", | |
| 591 url.spec()); | |
| 592 if (!prefetch_manager_.get()) // Prefetching not enabled. | |
| 593 return; | |
| 594 | |
| 595 std::vector<GURL> subresource_urls; | |
| 596 if (!GetPrefetchData(url, &subresource_urls)) { | |
| 597 // No prefetching data at host or URL level. | |
| 598 return; | |
| 599 } | |
| 600 | |
| 601 BrowserThread::PostTask( | |
| 602 BrowserThread::IO, FROM_HERE, | |
| 603 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, | |
| 604 prefetch_manager_, url, subresource_urls)); | |
| 605 } | |
| 606 | |
| 607 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { | |
| 608 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StopPrefetching", "url", | |
| 609 url.spec()); | |
| 610 if (!prefetch_manager_.get()) // Not enabled. | |
| 611 return; | |
| 612 | |
| 613 BrowserThread::PostTask( | |
| 614 BrowserThread::IO, FROM_HERE, | |
| 615 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, | |
| 616 prefetch_manager_, url)); | |
| 617 } | |
| 618 | |
| 619 void ResourcePrefetchPredictor::StartInitialization() { | 647 void ResourcePrefetchPredictor::StartInitialization() { |
| 620 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 648 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 621 | 649 |
| 622 DCHECK_EQ(NOT_INITIALIZED, initialization_state_); | 650 DCHECK_EQ(NOT_INITIALIZED, initialization_state_); |
| 623 initialization_state_ = INITIALIZING; | 651 initialization_state_ = INITIALIZING; |
| 624 | 652 |
| 625 // Create local caches using the database as loaded. | 653 // Create local caches using the database as loaded. |
| 626 auto url_data_map = base::MakeUnique<PrefetchDataMap>(); | 654 auto url_data_map = base::MakeUnique<PrefetchDataMap>(); |
| 627 auto host_data_map = base::MakeUnique<PrefetchDataMap>(); | 655 auto host_data_map = base::MakeUnique<PrefetchDataMap>(); |
| 628 auto url_redirect_data_map = base::MakeUnique<RedirectDataMap>(); | 656 auto url_redirect_data_map = base::MakeUnique<RedirectDataMap>(); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 | 830 |
| 803 data_map->erase(key_to_delete); | 831 data_map->erase(key_to_delete); |
| 804 BrowserThread::PostTask( | 832 BrowserThread::PostTask( |
| 805 BrowserThread::DB, FROM_HERE, | 833 BrowserThread::DB, FROM_HERE, |
| 806 base::Bind( | 834 base::Bind( |
| 807 &ResourcePrefetchPredictorTables::DeleteSingleRedirectDataPoint, | 835 &ResourcePrefetchPredictorTables::DeleteSingleRedirectDataPoint, |
| 808 tables_, key_to_delete, key_type)); | 836 tables_, key_to_delete, key_type)); |
| 809 } | 837 } |
| 810 | 838 |
| 811 void ResourcePrefetchPredictor::OnVisitCountLookup( | 839 void ResourcePrefetchPredictor::OnVisitCountLookup( |
| 812 size_t visit_count, | 840 size_t url_visit_count, |
| 813 const NavigationID& navigation_id, | |
| 814 const PageRequestSummary& summary) { | 841 const PageRequestSummary& summary) { |
| 815 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 842 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 816 | 843 |
| 817 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HistoryVisitCountForUrl", | 844 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HistoryVisitCountForUrl", |
| 818 visit_count); | 845 url_visit_count); |
| 819 | 846 |
| 820 // TODO(alexilin): make only one request to DB thread. | 847 // TODO(alexilin): make only one request to DB thread. |
| 821 | 848 |
| 822 // URL level data - merge only if we already saved the data, or it | 849 // URL level data - merge only if we already saved the data, or it |
| 823 // meets the cutoff requirement. | 850 // meets the cutoff requirement. |
| 824 const std::string url_spec = navigation_id.main_frame_url.spec(); | 851 const std::string url_spec = summary.main_frame_url.spec(); |
| 825 bool already_tracking = url_table_cache_->find(url_spec) != | 852 bool already_tracking = url_table_cache_->find(url_spec) != |
| 826 url_table_cache_->end(); | 853 url_table_cache_->end(); |
| 827 bool should_track_url = already_tracking || | 854 bool should_track_url = |
| 828 (visit_count >= config_.min_url_visit_count); | 855 already_tracking || (url_visit_count >= config_.min_url_visit_count); |
| 829 | 856 |
| 830 if (should_track_url && config_.IsURLLearningEnabled()) { | 857 if (should_track_url && config_.IsURLLearningEnabled()) { |
| 831 LearnNavigation(url_spec, PREFETCH_KEY_TYPE_URL, | 858 LearnNavigation(url_spec, PREFETCH_KEY_TYPE_URL, |
| 832 summary.subresource_requests, config_.max_urls_to_track, | 859 summary.subresource_requests, config_.max_urls_to_track, |
| 833 url_table_cache_.get(), summary.initial_url.spec(), | 860 url_table_cache_.get(), summary.initial_url.spec(), |
| 834 url_redirect_table_cache_.get()); | 861 url_redirect_table_cache_.get()); |
| 835 } | 862 } |
| 836 | 863 |
| 837 // Host level data - no cutoff, always learn the navigation if enabled. | 864 // Host level data - no cutoff, always learn the navigation if enabled. |
| 838 if (config_.IsHostLearningEnabled()) { | 865 if (config_.IsHostLearningEnabled()) { |
| 839 const std::string host = navigation_id.main_frame_url.host(); | 866 const std::string host = summary.main_frame_url.host(); |
| 840 LearnNavigation(host, PREFETCH_KEY_TYPE_HOST, summary.subresource_requests, | 867 LearnNavigation(host, PREFETCH_KEY_TYPE_HOST, summary.subresource_requests, |
| 841 config_.max_hosts_to_track, host_table_cache_.get(), | 868 config_.max_hosts_to_track, host_table_cache_.get(), |
| 842 summary.initial_url.host(), | 869 summary.initial_url.host(), |
| 843 host_redirect_table_cache_.get()); | 870 host_redirect_table_cache_.get()); |
| 844 } | 871 } |
| 872 |
| 873 if (observer_) |
| 874 observer_->OnNavigationLearned(url_visit_count, summary); |
| 845 } | 875 } |
| 846 | 876 |
| 847 void ResourcePrefetchPredictor::LearnNavigation( | 877 void ResourcePrefetchPredictor::LearnNavigation( |
| 848 const std::string& key, | 878 const std::string& key, |
| 849 PrefetchKeyType key_type, | 879 PrefetchKeyType key_type, |
| 850 const std::vector<URLRequestSummary>& new_resources, | 880 const std::vector<URLRequestSummary>& new_resources, |
| 851 size_t max_data_map_size, | 881 size_t max_data_map_size, |
| 852 PrefetchDataMap* data_map, | 882 PrefetchDataMap* data_map, |
| 853 const std::string& key_before_redirects, | 883 const std::string& key_before_redirects, |
| 854 RedirectDataMap* redirect_map) { | 884 RedirectDataMap* redirect_map) { |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1123 if (history_service->BackendLoaded()) { | 1153 if (history_service->BackendLoaded()) { |
| 1124 // HistoryService is already loaded. Continue with Initialization. | 1154 // HistoryService is already loaded. Continue with Initialization. |
| 1125 OnHistoryAndCacheLoaded(); | 1155 OnHistoryAndCacheLoaded(); |
| 1126 return; | 1156 return; |
| 1127 } | 1157 } |
| 1128 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1158 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1129 history_service_observer_.Add(history_service); | 1159 history_service_observer_.Add(history_service); |
| 1130 return; | 1160 return; |
| 1131 } | 1161 } |
| 1132 | 1162 |
| 1163 //////////////////////////////////////////////////////////////////////////////// |
| 1164 // TestObserver. |
| 1165 |
| 1166 TestObserver::~TestObserver() { |
| 1167 predictor_->SetObserverForTesting(nullptr); |
| 1168 } |
| 1169 |
| 1170 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1171 : predictor_(predictor) { |
| 1172 predictor_->SetObserverForTesting(this); |
| 1173 } |
| 1174 |
| 1133 } // namespace predictors | 1175 } // namespace predictors |
| OLD | NEW |