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> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
17 #include "base/containers/hash_tables.h" | 17 #include "base/containers/hash_tables.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/memory/ptr_util.h" |
19 #include "base/metrics/histogram_macros.h" | 20 #include "base/metrics/histogram_macros.h" |
20 #include "components/precache/core/precache_switches.h" | 21 #include "components/precache/core/precache_switches.h" |
21 #include "components/precache/core/proto/precache.pb.h" | 22 #include "components/precache/core/proto/precache.pb.h" |
22 #include "net/base/completion_callback.h" | 23 #include "net/base/completion_callback.h" |
23 #include "net/base/escape.h" | 24 #include "net/base/escape.h" |
24 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
25 #include "net/base/load_flags.h" | 26 #include "net/base/load_flags.h" |
26 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
27 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
28 #include "net/url_request/url_fetcher_response_writer.h" | 29 #include "net/url_request/url_fetcher_response_writer.h" |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } | 172 } |
172 | 173 |
173 PrecacheFetcher::Fetcher::~Fetcher() {} | 174 PrecacheFetcher::Fetcher::~Fetcher() {} |
174 | 175 |
175 void PrecacheFetcher::Fetcher::LoadFromCache() { | 176 void PrecacheFetcher::Fetcher::LoadFromCache() { |
176 fetch_stage_ = FetchStage::CACHE; | 177 fetch_stage_ = FetchStage::CACHE; |
177 cache_url_fetcher_ = | 178 cache_url_fetcher_ = |
178 net::URLFetcher::Create(url_, net::URLFetcher::GET, this); | 179 net::URLFetcher::Create(url_, net::URLFetcher::GET, this); |
179 cache_url_fetcher_->SetRequestContext(request_context_); | 180 cache_url_fetcher_->SetRequestContext(request_context_); |
180 cache_url_fetcher_->SetLoadFlags(net::LOAD_ONLY_FROM_CACHE | kNoTracking); | 181 cache_url_fetcher_->SetLoadFlags(net::LOAD_ONLY_FROM_CACHE | kNoTracking); |
181 scoped_ptr<URLFetcherNullWriter> null_writer(new URLFetcherNullWriter); | 182 std::unique_ptr<URLFetcherNullWriter> null_writer(new URLFetcherNullWriter); |
182 cache_url_fetcher_->SaveResponseWithWriter(std::move(null_writer)); | 183 cache_url_fetcher_->SaveResponseWithWriter(std::move(null_writer)); |
183 cache_url_fetcher_->Start(); | 184 cache_url_fetcher_->Start(); |
184 } | 185 } |
185 | 186 |
186 void PrecacheFetcher::Fetcher::LoadFromNetwork() { | 187 void PrecacheFetcher::Fetcher::LoadFromNetwork() { |
187 fetch_stage_ = FetchStage::NETWORK; | 188 fetch_stage_ = FetchStage::NETWORK; |
188 network_url_fetcher_ = | 189 network_url_fetcher_ = |
189 net::URLFetcher::Create(url_, net::URLFetcher::GET, this); | 190 net::URLFetcher::Create(url_, net::URLFetcher::GET, this); |
190 network_url_fetcher_->SetRequestContext(request_context_); | 191 network_url_fetcher_->SetRequestContext(request_context_); |
191 if (is_resource_request_) { | 192 if (is_resource_request_) { |
192 // LOAD_VALIDATE_CACHE allows us to refresh Date headers for resources | 193 // LOAD_VALIDATE_CACHE allows us to refresh Date headers for resources |
193 // already in the cache. The Date headers are updated from 304s as well as | 194 // already in the cache. The Date headers are updated from 304s as well as |
194 // 200s. | 195 // 200s. |
195 network_url_fetcher_->SetLoadFlags(net::LOAD_VALIDATE_CACHE | kNoTracking); | 196 network_url_fetcher_->SetLoadFlags(net::LOAD_VALIDATE_CACHE | kNoTracking); |
196 // We don't need a copy of the response body for resource requests. The | 197 // We don't need a copy of the response body for resource requests. The |
197 // request is issued only to populate the browser cache. | 198 // request is issued only to populate the browser cache. |
198 scoped_ptr<URLFetcherNullWriter> null_writer(new URLFetcherNullWriter); | 199 std::unique_ptr<URLFetcherNullWriter> null_writer(new URLFetcherNullWriter); |
199 network_url_fetcher_->SaveResponseWithWriter(std::move(null_writer)); | 200 network_url_fetcher_->SaveResponseWithWriter(std::move(null_writer)); |
200 } else { | 201 } else { |
201 // Config and manifest requests do not need to be revalidated. It's okay if | 202 // Config and manifest requests do not need to be revalidated. It's okay if |
202 // they expire from the cache minutes after we request them. | 203 // they expire from the cache minutes after we request them. |
203 network_url_fetcher_->SetLoadFlags(kNoTracking); | 204 network_url_fetcher_->SetLoadFlags(kNoTracking); |
204 } | 205 } |
205 network_url_fetcher_->Start(); | 206 network_url_fetcher_->Start(); |
206 } | 207 } |
207 | 208 |
208 void PrecacheFetcher::Fetcher::OnURLFetchDownloadProgress( | 209 void PrecacheFetcher::Fetcher::OnURLFetchDownloadProgress( |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 config_url_.is_empty() ? GetDefaultConfigURL() : config_url_; | 315 config_url_.is_empty() ? GetDefaultConfigURL() : config_url_; |
315 | 316 |
316 DCHECK(config_url.is_valid()) << "Config URL not valid: " | 317 DCHECK(config_url.is_valid()) << "Config URL not valid: " |
317 << config_url.possibly_invalid_spec(); | 318 << config_url.possibly_invalid_spec(); |
318 | 319 |
319 start_time_ = base::TimeTicks::Now(); | 320 start_time_ = base::TimeTicks::Now(); |
320 | 321 |
321 // Fetch the precache configuration settings from the server. | 322 // Fetch the precache configuration settings from the server. |
322 DCHECK(pool_.IsEmpty()) << "All parallel requests should be available"; | 323 DCHECK(pool_.IsEmpty()) << "All parallel requests should be available"; |
323 VLOG(3) << "Fetching " << config_url; | 324 VLOG(3) << "Fetching " << config_url; |
324 pool_.Add(scoped_ptr<Fetcher>(new Fetcher( | 325 pool_.Add(base::WrapUnique(new Fetcher( |
325 request_context_.get(), config_url, | 326 request_context_.get(), config_url, |
326 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, | 327 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, |
327 base::Unretained(this)), | 328 base::Unretained(this)), |
328 false /* is_resource_request */, std::numeric_limits<int32_t>::max()))); | 329 false /* is_resource_request */, std::numeric_limits<int32_t>::max()))); |
329 } | 330 } |
330 | 331 |
331 void PrecacheFetcher::StartNextResourceFetch() { | 332 void PrecacheFetcher::StartNextResourceFetch() { |
332 while (!resource_urls_to_fetch_.empty() && pool_.IsAvailable()) { | 333 while (!resource_urls_to_fetch_.empty() && pool_.IsAvailable()) { |
333 const size_t max_bytes = | 334 const size_t max_bytes = |
334 std::min(config_->max_bytes_per_resource(), | 335 std::min(config_->max_bytes_per_resource(), |
335 config_->max_bytes_total() - total_response_bytes_); | 336 config_->max_bytes_total() - total_response_bytes_); |
336 VLOG(3) << "Fetching " << resource_urls_to_fetch_.front(); | 337 VLOG(3) << "Fetching " << resource_urls_to_fetch_.front(); |
337 pool_.Add(scoped_ptr<Fetcher>( | 338 pool_.Add(base::WrapUnique( |
338 new Fetcher(request_context_.get(), resource_urls_to_fetch_.front(), | 339 new Fetcher(request_context_.get(), resource_urls_to_fetch_.front(), |
339 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, | 340 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, |
340 base::Unretained(this)), | 341 base::Unretained(this)), |
341 true /* is_resource_request */, max_bytes))); | 342 true /* is_resource_request */, max_bytes))); |
342 | 343 |
343 resource_urls_to_fetch_.pop_front(); | 344 resource_urls_to_fetch_.pop_front(); |
344 } | 345 } |
345 } | 346 } |
346 | 347 |
347 void PrecacheFetcher::StartNextManifestFetch() { | 348 void PrecacheFetcher::StartNextManifestFetch() { |
348 if (manifest_urls_to_fetch_.empty()) | 349 if (manifest_urls_to_fetch_.empty()) |
349 return; | 350 return; |
350 | 351 |
351 // We only fetch one manifest at a time to keep the size of | 352 // We only fetch one manifest at a time to keep the size of |
352 // resource_urls_to_fetch_ as small as possible. | 353 // resource_urls_to_fetch_ as small as possible. |
353 DCHECK(pool_.IsAvailable()) | 354 DCHECK(pool_.IsAvailable()) |
354 << "There are no available parallel requests to fetch the next manifest. " | 355 << "There are no available parallel requests to fetch the next manifest. " |
355 "Did you forget to call Delete?"; | 356 "Did you forget to call Delete?"; |
356 VLOG(3) << "Fetching " << manifest_urls_to_fetch_.front(); | 357 VLOG(3) << "Fetching " << manifest_urls_to_fetch_.front(); |
357 pool_.Add(scoped_ptr<Fetcher>(new Fetcher( | 358 pool_.Add(base::WrapUnique(new Fetcher( |
358 request_context_.get(), manifest_urls_to_fetch_.front(), | 359 request_context_.get(), manifest_urls_to_fetch_.front(), |
359 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, | 360 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, |
360 base::Unretained(this)), | 361 base::Unretained(this)), |
361 false /* is_resource_request */, std::numeric_limits<int32_t>::max()))); | 362 false /* is_resource_request */, std::numeric_limits<int32_t>::max()))); |
362 | 363 |
363 manifest_urls_to_fetch_.pop_front(); | 364 manifest_urls_to_fetch_.pop_front(); |
364 } | 365 } |
365 | 366 |
366 void PrecacheFetcher::StartNextFetch() { | 367 void PrecacheFetcher::StartNextFetch() { |
367 // If over the precache total size cap, then stop prefetching. | 368 // If over the precache total size cap, then stop prefetching. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 StartNextFetch(); | 454 StartNextFetch(); |
454 } | 455 } |
455 | 456 |
456 void PrecacheFetcher::UpdateStats(int64_t response_bytes, | 457 void PrecacheFetcher::UpdateStats(int64_t response_bytes, |
457 int64_t network_response_bytes) { | 458 int64_t network_response_bytes) { |
458 total_response_bytes_ += response_bytes; | 459 total_response_bytes_ += response_bytes; |
459 network_response_bytes_ += network_response_bytes; | 460 network_response_bytes_ += network_response_bytes; |
460 } | 461 } |
461 | 462 |
462 } // namespace precache | 463 } // namespace precache |
OLD | NEW |