| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 return false; | 116 return false; |
| 117 } | 117 } |
| 118 if (!message->ParseFromString(response_string)) { | 118 if (!message->ParseFromString(response_string)) { |
| 119 DLOG(WARNING) << "Unable to parse proto served from " | 119 DLOG(WARNING) << "Unable to parse proto served from " |
| 120 << source.GetOriginalURL().spec(); | 120 << source.GetOriginalURL().spec(); |
| 121 return false; | 121 return false; |
| 122 } | 122 } |
| 123 return true; | 123 return true; |
| 124 } | 124 } |
| 125 | 125 |
| 126 // Returns the resource selection bitset from the |manifest| for the given |
| 127 // |experiment_id|. By default all resource will be selected if the experiment |
| 128 // group is not found. |
| 129 uint64_t GetResourceBitset(const PrecacheManifest& manifest, |
| 130 uint32_t experiment_id) { |
| 131 if (manifest.has_experiments()) { |
| 132 const auto& resource_bitset_map = |
| 133 manifest.experiments().resources_by_experiment_group(); |
| 134 const auto& resource_bitset_it = resource_bitset_map.find(experiment_id); |
| 135 if (resource_bitset_it != resource_bitset_map.end()) |
| 136 return resource_bitset_it->second.bitset(); |
| 137 } |
| 138 return ~0ULL; |
| 139 } |
| 140 |
| 126 // URLFetcherResponseWriter that ignores the response body, in order to avoid | 141 // URLFetcherResponseWriter that ignores the response body, in order to avoid |
| 127 // the unnecessary memory usage. Use it rather than the default if you don't | 142 // the unnecessary memory usage. Use it rather than the default if you don't |
| 128 // care about parsing the response body. We use it below as a means to populate | 143 // care about parsing the response body. We use it below as a means to populate |
| 129 // the cache with requested resource URLs. | 144 // the cache with requested resource URLs. |
| 130 class URLFetcherNullWriter : public net::URLFetcherResponseWriter { | 145 class URLFetcherNullWriter : public net::URLFetcherResponseWriter { |
| 131 public: | 146 public: |
| 132 int Initialize(const net::CompletionCallback& callback) override { | 147 int Initialize(const net::CompletionCallback& callback) override { |
| 133 return net::OK; | 148 return net::OK; |
| 134 } | 149 } |
| 135 | 150 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 unfinished_work.network_bytes(), | 314 unfinished_work.network_bytes(), |
| 300 1, kMaxResponseBytes, | 315 1, kMaxResponseBytes, |
| 301 100); | 316 100); |
| 302 } | 317 } |
| 303 | 318 |
| 304 PrecacheFetcher::PrecacheFetcher( | 319 PrecacheFetcher::PrecacheFetcher( |
| 305 net::URLRequestContextGetter* request_context, | 320 net::URLRequestContextGetter* request_context, |
| 306 const GURL& config_url, | 321 const GURL& config_url, |
| 307 const std::string& manifest_url_prefix, | 322 const std::string& manifest_url_prefix, |
| 308 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, | 323 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, |
| 324 uint32_t experiment_id, |
| 309 PrecacheFetcher::PrecacheDelegate* precache_delegate) | 325 PrecacheFetcher::PrecacheDelegate* precache_delegate) |
| 310 : request_context_(request_context), | 326 : request_context_(request_context), |
| 311 config_url_(config_url), | 327 config_url_(config_url), |
| 312 manifest_url_prefix_(manifest_url_prefix), | 328 manifest_url_prefix_(manifest_url_prefix), |
| 313 precache_delegate_(precache_delegate), | 329 precache_delegate_(precache_delegate), |
| 314 pool_(kMaxParallelFetches) { | 330 pool_(kMaxParallelFetches), |
| 331 experiment_id_(experiment_id) { |
| 315 DCHECK(request_context_.get()); // Request context must be non-NULL. | 332 DCHECK(request_context_.get()); // Request context must be non-NULL. |
| 316 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. | 333 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. |
| 317 | 334 |
| 318 DCHECK_NE(GURL(), GetDefaultConfigURL()) | 335 DCHECK_NE(GURL(), GetDefaultConfigURL()) |
| 319 << "Could not determine the precache config settings URL."; | 336 << "Could not determine the precache config settings URL."; |
| 320 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) | 337 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) |
| 321 << "Could not determine the default precache manifest URL prefix."; | 338 << "Could not determine the default precache manifest URL prefix."; |
| 322 DCHECK(unfinished_work); | 339 DCHECK(unfinished_work); |
| 323 | 340 |
| 324 // Copy manifests and resources to member variables as a convenience. | 341 // 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()); | 531 UpdateStats(source.response_bytes(), source.network_response_bytes()); |
| 515 if (source.network_url_fetcher() == nullptr) { | 532 if (source.network_url_fetcher() == nullptr) { |
| 516 pool_.DeleteAll(); // Cancel any other ongoing request. | 533 pool_.DeleteAll(); // Cancel any other ongoing request. |
| 517 } else { | 534 } else { |
| 518 PrecacheManifest manifest; | 535 PrecacheManifest manifest; |
| 519 | 536 |
| 520 if (ParseProtoFromFetchResponse(*source.network_url_fetcher(), &manifest)) { | 537 if (ParseProtoFromFetchResponse(*source.network_url_fetcher(), &manifest)) { |
| 521 const int32_t len = | 538 const int32_t len = |
| 522 std::min(manifest.resource_size(), | 539 std::min(manifest.resource_size(), |
| 523 unfinished_work_->config_settings().top_resources_count()); | 540 unfinished_work_->config_settings().top_resources_count()); |
| 541 const uint64_t resource_bitset = |
| 542 GetResourceBitset(manifest, experiment_id_); |
| 524 for (int i = 0; i < len; ++i) { | 543 for (int i = 0; i < len; ++i) { |
| 525 if (manifest.resource(i).has_url()) | 544 if (((0x1ULL << i) & resource_bitset) && manifest.resource(i).has_url()) |
| 526 resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url())); | 545 resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url())); |
| 527 } | 546 } |
| 528 } | 547 } |
| 529 } | 548 } |
| 530 | 549 |
| 531 pool_.Delete(source); | 550 pool_.Delete(source); |
| 532 StartNextFetch(); | 551 StartNextFetch(); |
| 533 } | 552 } |
| 534 | 553 |
| 535 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { | 554 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { |
| 536 UpdateStats(source.response_bytes(), source.network_response_bytes()); | 555 UpdateStats(source.response_bytes(), source.network_response_bytes()); |
| 537 pool_.Delete(source); | 556 pool_.Delete(source); |
| 538 // The resource has already been put in the cache during the fetch process, so | 557 // The resource has already been put in the cache during the fetch process, so |
| 539 // nothing more needs to be done for the resource. | 558 // nothing more needs to be done for the resource. |
| 540 StartNextFetch(); | 559 StartNextFetch(); |
| 541 } | 560 } |
| 542 | 561 |
| 543 void PrecacheFetcher::UpdateStats(int64_t response_bytes, | 562 void PrecacheFetcher::UpdateStats(int64_t response_bytes, |
| 544 int64_t network_response_bytes) { | 563 int64_t network_response_bytes) { |
| 545 unfinished_work_->set_total_bytes( | 564 unfinished_work_->set_total_bytes( |
| 546 unfinished_work_->total_bytes() + response_bytes); | 565 unfinished_work_->total_bytes() + response_bytes); |
| 547 unfinished_work_->set_network_bytes( | 566 unfinished_work_->set_network_bytes( |
| 548 unfinished_work_->network_bytes() + network_response_bytes); | 567 unfinished_work_->network_bytes() + network_response_bytes); |
| 549 } | 568 } |
| 550 | 569 |
| 551 } // namespace precache | 570 } // namespace precache |
| OLD | NEW |