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 #ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 5 #ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <deque> | 10 #include <deque> |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 // | 285 // |
286 // This allows the precache to "refresh" cache entries by increasing their | 286 // This allows the precache to "refresh" cache entries by increasing their |
287 // expiration date, but minimizes the network impact of doing so, by performing | 287 // expiration date, but minimizes the network impact of doing so, by performing |
288 // only conditional GETs. | 288 // only conditional GETs. |
289 // | 289 // |
290 // On completion it calls the given callback. This class cancels requests whose | 290 // On completion it calls the given callback. This class cancels requests whose |
291 // responses are or will be larger than max_bytes. In such cases, | 291 // responses are or will be larger than max_bytes. In such cases, |
292 // network_url_fetcher() will return nullptr. | 292 // network_url_fetcher() will return nullptr. |
293 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { | 293 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { |
294 public: | 294 public: |
295 // Construct a new Fetcher. This will create and start a new URLFetcher for | 295 // Construct a new Fetcher. This will create and start a new URLFetcher |
296 // the specified URL using the specified request context. | 296 // immediately. Parameters: |
| 297 // request_context: The request context to pass to the URLFetcher. |
| 298 // url: The URL to fetch. |
| 299 // referrer: The hostname of the manifest requesting this resource. Empty |
| 300 // for config fetches. |
| 301 // callback: Called when the fetch is finished or cancelled. |
| 302 // is_resource_request: If true, the URL may be refreshed using |
| 303 // LOAD_VALIDATE_CACHE. |
| 304 // max_bytes: The number of bytes to download before cancelling. |
| 305 // revalidation_only: If true, the URL is fetched only if it has an existing |
| 306 // cache entry with conditional headers. |
297 Fetcher(net::URLRequestContextGetter* request_context, | 307 Fetcher(net::URLRequestContextGetter* request_context, |
298 const GURL& url, | 308 const GURL& url, |
299 const std::string& referrer, | 309 const std::string& referrer, |
300 const base::Callback<void(const Fetcher&)>& callback, | 310 const base::Callback<void(const Fetcher&)>& callback, |
301 bool is_resource_request, | 311 bool is_resource_request, |
302 size_t max_bytes); | 312 size_t max_bytes, |
| 313 bool revalidation_only); |
303 ~Fetcher() override; | 314 ~Fetcher() override; |
304 void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 315 void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
305 int64_t current, | 316 int64_t current, |
306 int64_t total, | 317 int64_t total, |
307 int64_t current_network_bytes) override; | 318 int64_t current_network_bytes) override; |
308 void OnURLFetchComplete(const net::URLFetcher* source) override; | 319 void OnURLFetchComplete(const net::URLFetcher* source) override; |
309 int64_t response_bytes() const { return response_bytes_; } | 320 int64_t response_bytes() const { return response_bytes_; } |
310 int64_t network_response_bytes() const { return network_response_bytes_; } | 321 int64_t network_response_bytes() const { return network_response_bytes_; } |
311 const net::URLFetcher* network_url_fetcher() const { | 322 const net::URLFetcher* network_url_fetcher() const { |
312 return network_url_fetcher_.get(); | 323 return network_url_fetcher_.get(); |
313 } | 324 } |
314 const GURL& url() const { return url_; } | 325 const GURL& url() const { return url_; } |
315 const std::string& referrer() const { return referrer_; } | 326 const std::string& referrer() const { return referrer_; } |
316 bool is_resource_request() const { return is_resource_request_; } | 327 bool is_resource_request() const { return is_resource_request_; } |
317 bool was_cached() const { return was_cached_; } | 328 bool was_cached() const { return was_cached_; } |
318 | 329 |
319 private: | 330 private: |
320 enum class FetchStage { CACHE, NETWORK }; | 331 enum class FetchStage { CACHE, NETWORK }; |
321 | 332 |
322 void LoadFromCache(); | 333 void LoadFromCache(); |
323 void LoadFromNetwork(); | 334 void LoadFromNetwork(); |
324 | 335 |
| 336 // The arguments to this Fetcher's constructor. |
325 net::URLRequestContextGetter* const request_context_; | 337 net::URLRequestContextGetter* const request_context_; |
326 const GURL url_; | 338 const GURL url_; |
327 const std::string referrer_; | 339 const std::string referrer_; |
328 const base::Callback<void(const Fetcher&)> callback_; | 340 const base::Callback<void(const Fetcher&)> callback_; |
329 const bool is_resource_request_; | 341 const bool is_resource_request_; |
330 const size_t max_bytes_; | 342 const size_t max_bytes_; |
| 343 const bool revalidation_only_; |
331 | 344 |
332 FetchStage fetch_stage_; | 345 FetchStage fetch_stage_; |
333 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. | 346 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. |
334 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; | 347 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; |
335 std::unique_ptr<net::URLFetcher> network_url_fetcher_; | 348 std::unique_ptr<net::URLFetcher> network_url_fetcher_; |
336 int64_t response_bytes_; | 349 int64_t response_bytes_; |
337 int64_t network_response_bytes_; | 350 int64_t network_response_bytes_; |
338 bool was_cached_; | 351 bool was_cached_; |
339 | 352 |
340 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 353 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
341 }; | 354 }; |
342 | 355 |
343 } // namespace precache | 356 } // namespace precache |
344 | 357 |
345 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 358 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
OLD | NEW |