| 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 <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 unfinished_work.network_bytes(), | 299 unfinished_work.network_bytes(), |
| 300 1, kMaxResponseBytes, | 300 1, kMaxResponseBytes, |
| 301 100); | 301 100); |
| 302 } | 302 } |
| 303 | 303 |
| 304 PrecacheFetcher::PrecacheFetcher( | 304 PrecacheFetcher::PrecacheFetcher( |
| 305 net::URLRequestContextGetter* request_context, | 305 net::URLRequestContextGetter* request_context, |
| 306 const GURL& config_url, | 306 const GURL& config_url, |
| 307 const std::string& manifest_url_prefix, | 307 const std::string& manifest_url_prefix, |
| 308 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, | 308 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, |
| 309 uint32_t experiment_id, |
| 309 PrecacheFetcher::PrecacheDelegate* precache_delegate) | 310 PrecacheFetcher::PrecacheDelegate* precache_delegate) |
| 310 : request_context_(request_context), | 311 : request_context_(request_context), |
| 311 config_url_(config_url), | 312 config_url_(config_url), |
| 312 manifest_url_prefix_(manifest_url_prefix), | 313 manifest_url_prefix_(manifest_url_prefix), |
| 313 precache_delegate_(precache_delegate), | 314 precache_delegate_(precache_delegate), |
| 314 pool_(kMaxParallelFetches) { | 315 pool_(kMaxParallelFetches), |
| 316 experiment_id_(experiment_id) { |
| 315 DCHECK(request_context_.get()); // Request context must be non-NULL. | 317 DCHECK(request_context_.get()); // Request context must be non-NULL. |
| 316 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. | 318 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. |
| 317 | 319 |
| 318 DCHECK_NE(GURL(), GetDefaultConfigURL()) | 320 DCHECK_NE(GURL(), GetDefaultConfigURL()) |
| 319 << "Could not determine the precache config settings URL."; | 321 << "Could not determine the precache config settings URL."; |
| 320 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) | 322 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) |
| 321 << "Could not determine the default precache manifest URL prefix."; | 323 << "Could not determine the default precache manifest URL prefix."; |
| 322 DCHECK(unfinished_work); | 324 DCHECK(unfinished_work); |
| 323 | 325 |
| 324 // Copy manifests and resources to member variables as a convenience. | 326 // Copy manifests and resources to member variables as a convenience. |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 UpdateStats(source.response_bytes(), source.network_response_bytes()); | 516 UpdateStats(source.response_bytes(), source.network_response_bytes()); |
| 515 if (source.network_url_fetcher() == nullptr) { | 517 if (source.network_url_fetcher() == nullptr) { |
| 516 pool_.DeleteAll(); // Cancel any other ongoing request. | 518 pool_.DeleteAll(); // Cancel any other ongoing request. |
| 517 } else { | 519 } else { |
| 518 PrecacheManifest manifest; | 520 PrecacheManifest manifest; |
| 519 | 521 |
| 520 if (ParseProtoFromFetchResponse(*source.network_url_fetcher(), &manifest)) { | 522 if (ParseProtoFromFetchResponse(*source.network_url_fetcher(), &manifest)) { |
| 521 const int32_t len = | 523 const int32_t len = |
| 522 std::min(manifest.resource_size(), | 524 std::min(manifest.resource_size(), |
| 523 unfinished_work_->config_settings().top_resources_count()); | 525 unfinished_work_->config_settings().top_resources_count()); |
| 526 const uint64_t resource_bitset = manifest.experiments() |
| 527 .resources_by_experiment_group() |
| 528 .at(experiment_id_) |
| 529 .bitset(); |
| 524 for (int i = 0; i < len; ++i) { | 530 for (int i = 0; i < len; ++i) { |
| 525 if (manifest.resource(i).has_url()) | 531 if (((0x1 << i) & resource_bitset) && manifest.resource(i).has_url()) |
| 526 resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url())); | 532 resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url())); |
| 527 } | 533 } |
| 528 } | 534 } |
| 529 } | 535 } |
| 530 | 536 |
| 531 pool_.Delete(source); | 537 pool_.Delete(source); |
| 532 StartNextFetch(); | 538 StartNextFetch(); |
| 533 } | 539 } |
| 534 | 540 |
| 535 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { | 541 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { |
| 536 UpdateStats(source.response_bytes(), source.network_response_bytes()); | 542 UpdateStats(source.response_bytes(), source.network_response_bytes()); |
| 537 pool_.Delete(source); | 543 pool_.Delete(source); |
| 538 // The resource has already been put in the cache during the fetch process, so | 544 // The resource has already been put in the cache during the fetch process, so |
| 539 // nothing more needs to be done for the resource. | 545 // nothing more needs to be done for the resource. |
| 540 StartNextFetch(); | 546 StartNextFetch(); |
| 541 } | 547 } |
| 542 | 548 |
| 543 void PrecacheFetcher::UpdateStats(int64_t response_bytes, | 549 void PrecacheFetcher::UpdateStats(int64_t response_bytes, |
| 544 int64_t network_response_bytes) { | 550 int64_t network_response_bytes) { |
| 545 unfinished_work_->set_total_bytes( | 551 unfinished_work_->set_total_bytes( |
| 546 unfinished_work_->total_bytes() + response_bytes); | 552 unfinished_work_->total_bytes() + response_bytes); |
| 547 unfinished_work_->set_network_bytes( | 553 unfinished_work_->set_network_bytes( |
| 548 unfinished_work_->network_bytes() + network_response_bytes); | 554 unfinished_work_->network_bytes() + network_response_bytes); |
| 549 } | 555 } |
| 550 | 556 |
| 551 } // namespace precache | 557 } // namespace precache |
| OLD | NEW |