| 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 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 num_total_resources)); | 412 num_total_resources)); |
| 413 | 413 |
| 414 UMA_HISTOGRAM_PERCENTAGE("Precache.Fetch.PercentCompleted", | 414 UMA_HISTOGRAM_PERCENTAGE("Precache.Fetch.PercentCompleted", |
| 415 percent_completed); | 415 percent_completed); |
| 416 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Total", | 416 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Total", |
| 417 unfinished_work.total_bytes(), 1, | 417 unfinished_work.total_bytes(), 1, |
| 418 kMaxResponseBytes, 100); | 418 kMaxResponseBytes, 100); |
| 419 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Network", | 419 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Network", |
| 420 unfinished_work.network_bytes(), 1, | 420 unfinished_work.network_bytes(), 1, |
| 421 kMaxResponseBytes, 100); | 421 kMaxResponseBytes, 100); |
| 422 |
| 423 if (unfinished_work.has_min_weight_fetched()) { |
| 424 UMA_HISTOGRAM_COUNTS_1000("Precache.Fetch.MinWeight", |
| 425 unfinished_work.min_weight_fetched() * 1000); |
| 426 } |
| 422 } | 427 } |
| 423 | 428 |
| 424 // static | 429 // static |
| 425 std::string PrecacheFetcher::GetResourceURLBase64HashForTesting( | 430 std::string PrecacheFetcher::GetResourceURLBase64HashForTesting( |
| 426 const std::vector<GURL>& urls) { | 431 const std::vector<GURL>& urls) { |
| 427 return GetResourceURLBase64Hash(urls); | 432 return GetResourceURLBase64Hash(urls); |
| 428 } | 433 } |
| 429 | 434 |
| 430 PrecacheFetcher::PrecacheFetcher( | 435 PrecacheFetcher::PrecacheFetcher( |
| 431 net::URLRequestContextGetter* request_context, | 436 net::URLRequestContextGetter* request_context, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 451 << "Could not determine the precache config settings URL."; | 456 << "Could not determine the precache config settings URL."; |
| 452 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) | 457 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) |
| 453 << "Could not determine the default precache manifest URL prefix."; | 458 << "Could not determine the default precache manifest URL prefix."; |
| 454 DCHECK(unfinished_work); | 459 DCHECK(unfinished_work); |
| 455 | 460 |
| 456 // Copy resources to member variable as a convenience. | 461 // Copy resources to member variable as a convenience. |
| 457 // TODO(rajendrant): Consider accessing these directly from the proto, by | 462 // TODO(rajendrant): Consider accessing these directly from the proto, by |
| 458 // keeping track of the current resource index. | 463 // keeping track of the current resource index. |
| 459 for (const auto& resource : unfinished_work->resource()) { | 464 for (const auto& resource : unfinished_work->resource()) { |
| 460 if (resource.has_url() && resource.has_top_host_name()) { | 465 if (resource.has_url() && resource.has_top_host_name()) { |
| 461 // Weight doesn't matter, as the resources have already been sorted by | 466 resources_to_fetch_.emplace_back( |
| 462 // this point. | 467 GURL(resource.url()), resource.top_host_name(), resource.weight()); |
| 463 resources_to_fetch_.emplace_back(GURL(resource.url()), | |
| 464 resource.top_host_name(), 0); | |
| 465 } | 468 } |
| 466 } | 469 } |
| 467 unfinished_work_ = std::move(unfinished_work); | 470 unfinished_work_ = std::move(unfinished_work); |
| 468 } | 471 } |
| 469 | 472 |
| 470 PrecacheFetcher::~PrecacheFetcher() { | 473 PrecacheFetcher::~PrecacheFetcher() { |
| 471 } | 474 } |
| 472 | 475 |
| 473 std::unique_ptr<PrecacheUnfinishedWork> PrecacheFetcher::CancelPrecaching() { | 476 std::unique_ptr<PrecacheUnfinishedWork> PrecacheFetcher::CancelPrecaching() { |
| 474 // This could get called multiple times, and it should be handled gracefully. | 477 // This could get called multiple times, and it should be handled gracefully. |
| 475 if (!unfinished_work_) | 478 if (!unfinished_work_) |
| 476 return nullptr; | 479 return nullptr; |
| 477 | 480 |
| 478 unfinished_work_->clear_resource(); | 481 unfinished_work_->clear_resource(); |
| 479 if (unfinished_work_->has_config_settings()) { | 482 if (unfinished_work_->has_config_settings()) { |
| 480 // If config fetch is incomplete, |top_hosts_to_fetch_| will be empty and | 483 // If config fetch is incomplete, |top_hosts_to_fetch_| will be empty and |
| 481 // top hosts should be left as is in |unfinished_work_|. | 484 // top hosts should be left as is in |unfinished_work_|. |
| 482 unfinished_work_->clear_top_host(); | 485 unfinished_work_->clear_top_host(); |
| 483 for (const auto& top_host : top_hosts_fetching_) | 486 for (const auto& top_host : top_hosts_fetching_) |
| 484 unfinished_work_->add_top_host()->set_hostname(top_host.hostname); | 487 unfinished_work_->add_top_host()->set_hostname(top_host.hostname); |
| 485 for (const auto& top_host : top_hosts_to_fetch_) | 488 for (const auto& top_host : top_hosts_to_fetch_) |
| 486 unfinished_work_->add_top_host()->set_hostname(top_host.hostname); | 489 unfinished_work_->add_top_host()->set_hostname(top_host.hostname); |
| 487 } | 490 } |
| 488 for (const auto& resource : resources_fetching_) { | 491 for (const auto& resource : resources_fetching_) { |
| 489 auto new_resource = unfinished_work_->add_resource(); | 492 auto new_resource = unfinished_work_->add_resource(); |
| 490 new_resource->set_url(resource.url.spec()); | 493 new_resource->set_url(resource.url.spec()); |
| 491 new_resource->set_top_host_name(resource.referrer); | 494 new_resource->set_top_host_name(resource.referrer); |
| 495 new_resource->set_weight(resource.weight); |
| 492 } | 496 } |
| 493 for (const auto& resource : resources_to_fetch_) { | 497 for (const auto& resource : resources_to_fetch_) { |
| 494 auto new_resource = unfinished_work_->add_resource(); | 498 auto new_resource = unfinished_work_->add_resource(); |
| 495 new_resource->set_url(resource.url.spec()); | 499 new_resource->set_url(resource.url.spec()); |
| 496 new_resource->set_top_host_name(resource.referrer); | 500 new_resource->set_top_host_name(resource.referrer); |
| 501 new_resource->set_weight(resource.weight); |
| 497 } | 502 } |
| 498 top_hosts_fetching_.clear(); | 503 top_hosts_fetching_.clear(); |
| 499 top_hosts_to_fetch_.clear(); | 504 top_hosts_to_fetch_.clear(); |
| 500 resources_fetching_.clear(); | 505 resources_fetching_.clear(); |
| 501 resources_to_fetch_.clear(); | 506 resources_to_fetch_.clear(); |
| 502 pool_.DeleteAll(); | 507 pool_.DeleteAll(); |
| 503 return std::move(unfinished_work_); | 508 return std::move(unfinished_work_); |
| 504 } | 509 } |
| 505 | 510 |
| 506 void PrecacheFetcher::Start() { | 511 void PrecacheFetcher::Start() { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 request_context_.get(), top_host.manifest_url, top_host.hostname, | 559 request_context_.get(), top_host.manifest_url, top_host.hostname, |
| 555 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, AsWeakPtr(), | 560 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, AsWeakPtr(), |
| 556 top_host.visits), | 561 top_host.visits), |
| 557 false /* is_resource_request */, std::numeric_limits<int32_t>::max(), | 562 false /* is_resource_request */, std::numeric_limits<int32_t>::max(), |
| 558 false /* revalidation_only */)); | 563 false /* revalidation_only */)); |
| 559 top_hosts_fetching_.push_back(std::move(top_host)); | 564 top_hosts_fetching_.push_back(std::move(top_host)); |
| 560 top_hosts_to_fetch_.pop_front(); | 565 top_hosts_to_fetch_.pop_front(); |
| 561 } | 566 } |
| 562 } | 567 } |
| 563 | 568 |
| 564 void PrecacheFetcher::NotifyDone( | 569 void PrecacheFetcher::NotifyDone(size_t remaining_manifest_urls_to_fetch, |
| 565 size_t remaining_manifest_urls_to_fetch, | 570 size_t remaining_resource_urls_to_fetch) { |
| 566 size_t remaining_resource_urls_to_fetch) { | |
| 567 RecordCompletionStatistics(*unfinished_work_, | 571 RecordCompletionStatistics(*unfinished_work_, |
| 568 remaining_manifest_urls_to_fetch, | 572 remaining_manifest_urls_to_fetch, |
| 569 remaining_resource_urls_to_fetch); | 573 remaining_resource_urls_to_fetch); |
| 570 precache_delegate_->OnDone(); | 574 precache_delegate_->OnDone(); |
| 571 } | 575 } |
| 572 | 576 |
| 573 void PrecacheFetcher::StartNextFetch() { | 577 void PrecacheFetcher::StartNextFetch() { |
| 574 DCHECK(unfinished_work_->has_config_settings()); | 578 DCHECK(unfinished_work_->has_config_settings()); |
| 575 | 579 |
| 576 // If over the precache total size cap or daily quota, then stop prefetching. | 580 // If over the precache total size cap or daily quota, then stop prefetching. |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 807 | 811 |
| 808 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { | 812 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { |
| 809 UpdateStats(source.response_bytes(), source.network_response_bytes()); | 813 UpdateStats(source.response_bytes(), source.network_response_bytes()); |
| 810 | 814 |
| 811 db_task_runner_->PostTask( | 815 db_task_runner_->PostTask( |
| 812 FROM_HERE, | 816 FROM_HERE, |
| 813 base::Bind(&PrecacheDatabase::RecordURLPrefetch, precache_database_, | 817 base::Bind(&PrecacheDatabase::RecordURLPrefetch, precache_database_, |
| 814 source.url(), source.referrer(), base::Time::Now(), | 818 source.url(), source.referrer(), base::Time::Now(), |
| 815 source.was_cached(), source.response_bytes())); | 819 source.was_cached(), source.response_bytes())); |
| 816 | 820 |
| 817 resources_fetching_.remove_if([&source](const ResourceInfo& resource) { | 821 auto resource = |
| 818 return resource.url == source.url(); | 822 std::find_if(resources_fetching_.begin(), resources_fetching_.end(), |
| 819 }); | 823 [&source](const ResourceInfo& resource) { |
| 824 return resource.url == source.url(); |
| 825 }); |
| 826 if (resource != resources_fetching_.end()) { |
| 827 if (unfinished_work_->config_settings().global_ranking() && |
| 828 (!unfinished_work_->has_min_weight_fetched() || |
| 829 resource->weight < unfinished_work_->min_weight_fetched())) |
| 830 unfinished_work_->set_min_weight_fetched(resource->weight); |
| 831 |
| 832 resources_fetching_.erase(resource); |
| 833 } |
| 820 | 834 |
| 821 pool_.Delete(source); | 835 pool_.Delete(source); |
| 822 | 836 |
| 823 // The resource has already been put in the cache during the fetch process, so | 837 // The resource has already been put in the cache during the fetch process, so |
| 824 // nothing more needs to be done for the resource. | 838 // nothing more needs to be done for the resource. |
| 825 StartNextFetch(); | 839 StartNextFetch(); |
| 826 } | 840 } |
| 827 | 841 |
| 828 void PrecacheFetcher::UpdateStats(int64_t response_bytes, | 842 void PrecacheFetcher::UpdateStats(int64_t response_bytes, |
| 829 int64_t network_response_bytes) { | 843 int64_t network_response_bytes) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 843 remaining = 0; | 857 remaining = 0; |
| 844 quota_.set_remaining( | 858 quota_.set_remaining( |
| 845 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); | 859 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); |
| 846 db_task_runner_->PostTask( | 860 db_task_runner_->PostTask( |
| 847 FROM_HERE, | 861 FROM_HERE, |
| 848 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); | 862 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); |
| 849 } | 863 } |
| 850 } | 864 } |
| 851 | 865 |
| 852 } // namespace precache | 866 } // namespace precache |
| OLD | NEW |