| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 num_total_resources)); | 433 num_total_resources)); |
| 434 | 434 |
| 435 UMA_HISTOGRAM_PERCENTAGE("Precache.Fetch.PercentCompleted", | 435 UMA_HISTOGRAM_PERCENTAGE("Precache.Fetch.PercentCompleted", |
| 436 percent_completed); | 436 percent_completed); |
| 437 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Total", | 437 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Total", |
| 438 unfinished_work.total_bytes(), 1, | 438 unfinished_work.total_bytes(), 1, |
| 439 kMaxResponseBytes, 100); | 439 kMaxResponseBytes, 100); |
| 440 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Network", | 440 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Network", |
| 441 unfinished_work.network_bytes(), 1, | 441 unfinished_work.network_bytes(), 1, |
| 442 kMaxResponseBytes, 100); | 442 kMaxResponseBytes, 100); |
| 443 |
| 444 if (unfinished_work.has_min_weight_fetched()) { |
| 445 UMA_HISTOGRAM_COUNTS_1000("Precache.Fetch.MinWeight", |
| 446 unfinished_work.min_weight_fetched() * 1000); |
| 447 } |
| 443 } | 448 } |
| 444 | 449 |
| 445 // static | 450 // static |
| 446 std::string PrecacheFetcher::GetResourceURLBase64HashForTesting( | 451 std::string PrecacheFetcher::GetResourceURLBase64HashForTesting( |
| 447 const std::vector<GURL>& urls) { | 452 const std::vector<GURL>& urls) { |
| 448 return GetResourceURLBase64Hash(urls); | 453 return GetResourceURLBase64Hash(urls); |
| 449 } | 454 } |
| 450 | 455 |
| 451 PrecacheFetcher::PrecacheFetcher( | 456 PrecacheFetcher::PrecacheFetcher( |
| 452 net::URLRequestContextGetter* request_context, | 457 net::URLRequestContextGetter* request_context, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 472 << "Could not determine the precache config settings URL."; | 477 << "Could not determine the precache config settings URL."; |
| 473 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) | 478 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) |
| 474 << "Could not determine the default precache manifest URL prefix."; | 479 << "Could not determine the default precache manifest URL prefix."; |
| 475 DCHECK(unfinished_work); | 480 DCHECK(unfinished_work); |
| 476 | 481 |
| 477 // Copy resources to member variable as a convenience. | 482 // Copy resources to member variable as a convenience. |
| 478 // TODO(rajendrant): Consider accessing these directly from the proto, by | 483 // TODO(rajendrant): Consider accessing these directly from the proto, by |
| 479 // keeping track of the current resource index. | 484 // keeping track of the current resource index. |
| 480 for (const auto& resource : unfinished_work->resource()) { | 485 for (const auto& resource : unfinished_work->resource()) { |
| 481 if (resource.has_url() && resource.has_top_host_name()) { | 486 if (resource.has_url() && resource.has_top_host_name()) { |
| 482 // Weight doesn't matter, as the resources have already been sorted by | 487 resources_to_fetch_.emplace_back( |
| 483 // this point. | 488 GURL(resource.url()), resource.top_host_name(), resource.weight()); |
| 484 resources_to_fetch_.emplace_back(GURL(resource.url()), | |
| 485 resource.top_host_name(), 0); | |
| 486 } | 489 } |
| 487 } | 490 } |
| 488 unfinished_work_ = std::move(unfinished_work); | 491 unfinished_work_ = std::move(unfinished_work); |
| 489 } | 492 } |
| 490 | 493 |
| 491 PrecacheFetcher::~PrecacheFetcher() { | 494 PrecacheFetcher::~PrecacheFetcher() { |
| 492 } | 495 } |
| 493 | 496 |
| 494 std::unique_ptr<PrecacheUnfinishedWork> PrecacheFetcher::CancelPrecaching() { | 497 std::unique_ptr<PrecacheUnfinishedWork> PrecacheFetcher::CancelPrecaching() { |
| 495 // This could get called multiple times, and it should be handled gracefully. | 498 // This could get called multiple times, and it should be handled gracefully. |
| 496 if (!unfinished_work_) | 499 if (!unfinished_work_) |
| 497 return nullptr; | 500 return nullptr; |
| 498 | 501 |
| 499 unfinished_work_->clear_resource(); | 502 unfinished_work_->clear_resource(); |
| 500 if (unfinished_work_->has_config_settings()) { | 503 if (unfinished_work_->has_config_settings()) { |
| 501 // If config fetch is incomplete, |top_hosts_to_fetch_| will be empty and | 504 // If config fetch is incomplete, |top_hosts_to_fetch_| will be empty and |
| 502 // top hosts should be left as is in |unfinished_work_|. | 505 // top hosts should be left as is in |unfinished_work_|. |
| 503 unfinished_work_->clear_top_host(); | 506 unfinished_work_->clear_top_host(); |
| 504 for (const auto& top_host : top_hosts_fetching_) | 507 for (const auto& top_host : top_hosts_fetching_) |
| 505 unfinished_work_->add_top_host()->set_hostname(top_host.hostname); | 508 unfinished_work_->add_top_host()->set_hostname(top_host.hostname); |
| 506 for (const auto& top_host : top_hosts_to_fetch_) | 509 for (const auto& top_host : top_hosts_to_fetch_) |
| 507 unfinished_work_->add_top_host()->set_hostname(top_host.hostname); | 510 unfinished_work_->add_top_host()->set_hostname(top_host.hostname); |
| 508 } | 511 } |
| 509 for (const auto& resource : resources_fetching_) { | 512 for (const auto& resource : resources_fetching_) { |
| 510 auto* new_resource = unfinished_work_->add_resource(); | 513 auto* new_resource = unfinished_work_->add_resource(); |
| 511 new_resource->set_url(resource.url.spec()); | 514 new_resource->set_url(resource.url.spec()); |
| 512 new_resource->set_top_host_name(resource.referrer); | 515 new_resource->set_top_host_name(resource.referrer); |
| 516 new_resource->set_weight(resource.weight); |
| 513 } | 517 } |
| 514 for (const auto& resource : resources_to_fetch_) { | 518 for (const auto& resource : resources_to_fetch_) { |
| 515 auto* new_resource = unfinished_work_->add_resource(); | 519 auto* new_resource = unfinished_work_->add_resource(); |
| 516 new_resource->set_url(resource.url.spec()); | 520 new_resource->set_url(resource.url.spec()); |
| 517 new_resource->set_top_host_name(resource.referrer); | 521 new_resource->set_top_host_name(resource.referrer); |
| 522 new_resource->set_weight(resource.weight); |
| 518 } | 523 } |
| 519 top_hosts_fetching_.clear(); | 524 top_hosts_fetching_.clear(); |
| 520 top_hosts_to_fetch_.clear(); | 525 top_hosts_to_fetch_.clear(); |
| 521 resources_fetching_.clear(); | 526 resources_fetching_.clear(); |
| 522 resources_to_fetch_.clear(); | 527 resources_to_fetch_.clear(); |
| 523 pool_.DeleteAll(); | 528 pool_.DeleteAll(); |
| 524 return std::move(unfinished_work_); | 529 return std::move(unfinished_work_); |
| 525 } | 530 } |
| 526 | 531 |
| 527 void PrecacheFetcher::Start() { | 532 void PrecacheFetcher::Start() { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 request_context_.get(), top_host.manifest_url, top_host.hostname, | 580 request_context_.get(), top_host.manifest_url, top_host.hostname, |
| 576 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, AsWeakPtr(), | 581 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, AsWeakPtr(), |
| 577 top_host.visits), | 582 top_host.visits), |
| 578 false /* is_resource_request */, std::numeric_limits<int32_t>::max(), | 583 false /* is_resource_request */, std::numeric_limits<int32_t>::max(), |
| 579 false /* revalidation_only */)); | 584 false /* revalidation_only */)); |
| 580 top_hosts_fetching_.push_back(std::move(top_host)); | 585 top_hosts_fetching_.push_back(std::move(top_host)); |
| 581 top_hosts_to_fetch_.pop_front(); | 586 top_hosts_to_fetch_.pop_front(); |
| 582 } | 587 } |
| 583 } | 588 } |
| 584 | 589 |
| 585 void PrecacheFetcher::NotifyDone( | 590 void PrecacheFetcher::NotifyDone(size_t remaining_manifest_urls_to_fetch, |
| 586 size_t remaining_manifest_urls_to_fetch, | 591 size_t remaining_resource_urls_to_fetch) { |
| 587 size_t remaining_resource_urls_to_fetch) { | |
| 588 RecordCompletionStatistics(*unfinished_work_, | 592 RecordCompletionStatistics(*unfinished_work_, |
| 589 remaining_manifest_urls_to_fetch, | 593 remaining_manifest_urls_to_fetch, |
| 590 remaining_resource_urls_to_fetch); | 594 remaining_resource_urls_to_fetch); |
| 591 precache_delegate_->OnDone(); | 595 precache_delegate_->OnDone(); |
| 592 } | 596 } |
| 593 | 597 |
| 594 void PrecacheFetcher::StartNextFetch() { | 598 void PrecacheFetcher::StartNextFetch() { |
| 595 DCHECK(unfinished_work_->has_config_settings()); | 599 DCHECK(unfinished_work_->has_config_settings()); |
| 596 | 600 |
| 597 // If over the precache total size cap or daily quota, then stop prefetching. | 601 // If over the precache total size cap or daily quota, then stop prefetching. |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 | 836 |
| 833 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { | 837 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { |
| 834 UpdateStats(source.response_bytes(), source.network_response_bytes()); | 838 UpdateStats(source.response_bytes(), source.network_response_bytes()); |
| 835 | 839 |
| 836 db_task_runner_->PostTask( | 840 db_task_runner_->PostTask( |
| 837 FROM_HERE, | 841 FROM_HERE, |
| 838 base::Bind(&PrecacheDatabase::RecordURLPrefetch, precache_database_, | 842 base::Bind(&PrecacheDatabase::RecordURLPrefetch, precache_database_, |
| 839 source.url(), source.referrer(), base::Time::Now(), | 843 source.url(), source.referrer(), base::Time::Now(), |
| 840 source.was_cached(), source.response_bytes())); | 844 source.was_cached(), source.response_bytes())); |
| 841 | 845 |
| 842 resources_fetching_.remove_if([&source](const ResourceInfo& resource) { | 846 auto resource = |
| 843 return resource.url == source.url(); | 847 std::find_if(resources_fetching_.begin(), resources_fetching_.end(), |
| 844 }); | 848 [&source](const ResourceInfo& resource) { |
| 849 return resource.url == source.url(); |
| 850 }); |
| 851 if (resource != resources_fetching_.end()) { |
| 852 if (unfinished_work_->config_settings().global_ranking() && |
| 853 (!unfinished_work_->has_min_weight_fetched() || |
| 854 resource->weight < unfinished_work_->min_weight_fetched())) |
| 855 unfinished_work_->set_min_weight_fetched(resource->weight); |
| 856 |
| 857 resources_fetching_.erase(resource); |
| 858 } |
| 845 | 859 |
| 846 pool_.Delete(source); | 860 pool_.Delete(source); |
| 847 | 861 |
| 848 // The resource has already been put in the cache during the fetch process, so | 862 // The resource has already been put in the cache during the fetch process, so |
| 849 // nothing more needs to be done for the resource. | 863 // nothing more needs to be done for the resource. |
| 850 StartNextFetch(); | 864 StartNextFetch(); |
| 851 } | 865 } |
| 852 | 866 |
| 853 void PrecacheFetcher::UpdateStats(int64_t response_bytes, | 867 void PrecacheFetcher::UpdateStats(int64_t response_bytes, |
| 854 int64_t network_response_bytes) { | 868 int64_t network_response_bytes) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 868 remaining = 0; | 882 remaining = 0; |
| 869 quota_.set_remaining( | 883 quota_.set_remaining( |
| 870 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); | 884 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); |
| 871 db_task_runner_->PostTask( | 885 db_task_runner_->PostTask( |
| 872 FROM_HERE, | 886 FROM_HERE, |
| 873 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); | 887 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); |
| 874 } | 888 } |
| 875 } | 889 } |
| 876 | 890 |
| 877 } // namespace precache | 891 } // namespace precache |
| OLD | NEW |