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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 } | 234 } |
235 | 235 |
236 } // namespace | 236 } // namespace |
237 | 237 |
238 PrecacheFetcher::Fetcher::Fetcher( | 238 PrecacheFetcher::Fetcher::Fetcher( |
239 net::URLRequestContextGetter* request_context, | 239 net::URLRequestContextGetter* request_context, |
240 const GURL& url, | 240 const GURL& url, |
241 const std::string& referrer, | 241 const std::string& referrer, |
242 const base::Callback<void(const Fetcher&)>& callback, | 242 const base::Callback<void(const Fetcher&)>& callback, |
243 bool is_resource_request, | 243 bool is_resource_request, |
244 size_t max_bytes) | 244 size_t max_bytes, |
| 245 bool revalidation_only) |
245 : request_context_(request_context), | 246 : request_context_(request_context), |
246 url_(url), | 247 url_(url), |
247 referrer_(referrer), | 248 referrer_(referrer), |
248 callback_(callback), | 249 callback_(callback), |
249 is_resource_request_(is_resource_request), | 250 is_resource_request_(is_resource_request), |
250 max_bytes_(max_bytes), | 251 max_bytes_(max_bytes), |
| 252 revalidation_only_(revalidation_only), |
251 response_bytes_(0), | 253 response_bytes_(0), |
252 network_response_bytes_(0), | 254 network_response_bytes_(0), |
253 was_cached_(false) { | 255 was_cached_(false) { |
254 DCHECK(url.is_valid()); | 256 DCHECK(url.is_valid()); |
255 if (is_resource_request_) | 257 if (is_resource_request_) |
256 LoadFromCache(); | 258 LoadFromCache(); |
257 else | 259 else |
258 LoadFromNetwork(); | 260 LoadFromNetwork(); |
259 } | 261 } |
260 | 262 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 // Cancel the download. | 325 // Cancel the download. |
324 network_url_fetcher_.reset(); | 326 network_url_fetcher_.reset(); |
325 callback_.Run(*this); | 327 callback_.Run(*this); |
326 } | 328 } |
327 } | 329 } |
328 | 330 |
329 void PrecacheFetcher::Fetcher::OnURLFetchComplete( | 331 void PrecacheFetcher::Fetcher::OnURLFetchComplete( |
330 const net::URLFetcher* source) { | 332 const net::URLFetcher* source) { |
331 CHECK(source); | 333 CHECK(source); |
332 if (fetch_stage_ == FetchStage::CACHE && | 334 if (fetch_stage_ == FetchStage::CACHE && |
333 (source->GetStatus().error() == net::ERR_CACHE_MISS || | 335 ((source->GetStatus().error() == net::ERR_CACHE_MISS && |
| 336 !revalidation_only_) || |
334 (source->GetResponseHeaders() && | 337 (source->GetResponseHeaders() && |
335 source->GetResponseHeaders()->HasValidators()))) { | 338 source->GetResponseHeaders()->HasValidators()))) { |
336 // If the resource was not found in the cache, request it from the | 339 // If the resource was not found in the cache, request it from the |
337 // network. | 340 // network. |
338 // | 341 // |
339 // If the resource was found in the cache, but contains validators, | 342 // If the resource was found in the cache, but contains validators, |
340 // request a refresh. The presence of validators increases the chance that | 343 // request a refresh. The presence of validators increases the chance that |
341 // we get a 304 response rather than a full one, thus allowing us to | 344 // we get a 304 response rather than a full one, thus allowing us to |
342 // refresh the cache with minimal network load. | 345 // refresh the cache with minimal network load. |
343 LoadFromNetwork(); | 346 LoadFromNetwork(); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 config_url_.is_empty() ? GetDefaultConfigURL() : config_url_; | 484 config_url_.is_empty() ? GetDefaultConfigURL() : config_url_; |
482 | 485 |
483 DCHECK(config_url.is_valid()) << "Config URL not valid: " | 486 DCHECK(config_url.is_valid()) << "Config URL not valid: " |
484 << config_url.possibly_invalid_spec(); | 487 << config_url.possibly_invalid_spec(); |
485 | 488 |
486 // Fetch the precache configuration settings from the server. | 489 // Fetch the precache configuration settings from the server. |
487 DCHECK(pool_.IsEmpty()) << "All parallel requests should be available"; | 490 DCHECK(pool_.IsEmpty()) << "All parallel requests should be available"; |
488 pool_.Add(base::MakeUnique<Fetcher>( | 491 pool_.Add(base::MakeUnique<Fetcher>( |
489 request_context_.get(), config_url, std::string(), | 492 request_context_.get(), config_url, std::string(), |
490 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, AsWeakPtr()), | 493 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, AsWeakPtr()), |
491 false /* is_resource_request */, std::numeric_limits<int32_t>::max())); | 494 false /* is_resource_request */, std::numeric_limits<int32_t>::max(), |
| 495 false /* revalidation_only */)); |
492 } | 496 } |
493 | 497 |
494 void PrecacheFetcher::StartNextResourceFetch() { | 498 void PrecacheFetcher::StartNextResourceFetch() { |
495 DCHECK(unfinished_work_->has_config_settings()); | 499 DCHECK(unfinished_work_->has_config_settings()); |
496 while (!resources_to_fetch_.empty() && pool_.IsAvailable()) { | 500 while (!resources_to_fetch_.empty() && pool_.IsAvailable()) { |
497 ResourceInfo& resource = resources_to_fetch_.front(); | 501 ResourceInfo& resource = resources_to_fetch_.front(); |
498 const size_t max_bytes = std::min( | 502 const size_t max_bytes = std::min( |
499 quota_.remaining(), | 503 quota_.remaining(), |
500 std::min(unfinished_work_->config_settings().max_bytes_per_resource(), | 504 std::min(unfinished_work_->config_settings().max_bytes_per_resource(), |
501 unfinished_work_->config_settings().max_bytes_total() - | 505 unfinished_work_->config_settings().max_bytes_total() - |
502 unfinished_work_->total_bytes())); | 506 unfinished_work_->total_bytes())); |
503 pool_.Add(base::MakeUnique<Fetcher>( | 507 pool_.Add(base::MakeUnique<Fetcher>( |
504 request_context_.get(), resource.url, resource.referrer, | 508 request_context_.get(), resource.url, resource.referrer, |
505 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, AsWeakPtr()), | 509 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, AsWeakPtr()), |
506 true /* is_resource_request */, max_bytes)); | 510 true /* is_resource_request */, max_bytes, |
| 511 unfinished_work_->config_settings().revalidation_only())); |
507 | 512 |
508 resources_fetching_.push_back(std::move(resource)); | 513 resources_fetching_.push_back(std::move(resource)); |
509 resources_to_fetch_.pop_front(); | 514 resources_to_fetch_.pop_front(); |
510 } | 515 } |
511 } | 516 } |
512 | 517 |
513 void PrecacheFetcher::StartNextManifestFetches() { | 518 void PrecacheFetcher::StartNextManifestFetches() { |
514 // We fetch as many manifests at a time as possible, as we need all resource | 519 // We fetch as many manifests at a time as possible, as we need all resource |
515 // URLs in memory in order to rank them. | 520 // URLs in memory in order to rank them. |
516 while (!top_hosts_to_fetch_.empty() && pool_.IsAvailable()) { | 521 while (!top_hosts_to_fetch_.empty() && pool_.IsAvailable()) { |
517 ManifestHostInfo& top_host = top_hosts_to_fetch_.front(); | 522 ManifestHostInfo& top_host = top_hosts_to_fetch_.front(); |
518 pool_.Add(base::MakeUnique<Fetcher>( | 523 pool_.Add(base::MakeUnique<Fetcher>( |
519 request_context_.get(), top_host.manifest_url, top_host.hostname, | 524 request_context_.get(), top_host.manifest_url, top_host.hostname, |
520 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, AsWeakPtr(), | 525 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, AsWeakPtr(), |
521 top_host.visits), | 526 top_host.visits), |
522 false /* is_resource_request */, std::numeric_limits<int32_t>::max())); | 527 false /* is_resource_request */, std::numeric_limits<int32_t>::max(), |
| 528 false /* revalidation_only */)); |
523 top_hosts_fetching_.push_back(std::move(top_host)); | 529 top_hosts_fetching_.push_back(std::move(top_host)); |
524 top_hosts_to_fetch_.pop_front(); | 530 top_hosts_to_fetch_.pop_front(); |
525 } | 531 } |
526 } | 532 } |
527 | 533 |
528 void PrecacheFetcher::NotifyDone( | 534 void PrecacheFetcher::NotifyDone( |
529 size_t remaining_manifest_urls_to_fetch, | 535 size_t remaining_manifest_urls_to_fetch, |
530 size_t remaining_resource_urls_to_fetch) { | 536 size_t remaining_resource_urls_to_fetch) { |
531 RecordCompletionStatistics(*unfinished_work_, | 537 RecordCompletionStatistics(*unfinished_work_, |
532 remaining_manifest_urls_to_fetch, | 538 remaining_manifest_urls_to_fetch, |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 remaining = 0; | 811 remaining = 0; |
806 quota_.set_remaining( | 812 quota_.set_remaining( |
807 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); | 813 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); |
808 db_task_runner_->PostTask( | 814 db_task_runner_->PostTask( |
809 FROM_HERE, | 815 FROM_HERE, |
810 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); | 816 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); |
811 } | 817 } |
812 } | 818 } |
813 | 819 |
814 } // namespace precache | 820 } // namespace precache |
OLD | NEW |