| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 NET_URL_REQUEST_URL_FETCHER_CORE_H_ | 5 #ifndef NET_URL_REQUEST_URL_FETCHER_CORE_H_ |
| 6 #define NET_URL_REQUEST_URL_FETCHER_CORE_H_ | 6 #define NET_URL_REQUEST_URL_FETCHER_CORE_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/debug/stack_trace.h" | 13 #include "base/debug/stack_trace.h" |
| 14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/platform_file.h" | 19 #include "base/platform_file.h" |
| 20 #include "base/timer.h" | 20 #include "base/timer.h" |
| 21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "net/base/host_port_pair.h" | 22 #include "net/base/host_port_pair.h" |
| 23 #include "net/base/network_change_notifier.h" |
| 23 #include "net/http/http_request_headers.h" | 24 #include "net/http/http_request_headers.h" |
| 24 #include "net/url_request/url_fetcher.h" | 25 #include "net/url_request/url_fetcher.h" |
| 25 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
| 26 #include "net/url_request/url_request_status.h" | 27 #include "net/url_request/url_request_status.h" |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 class SingleThreadTaskRunner; | 30 class SingleThreadTaskRunner; |
| 30 } // namespace base | 31 } // namespace base |
| 31 | 32 |
| 32 namespace net { | 33 namespace net { |
| 33 class HttpResponseHeaders; | 34 class HttpResponseHeaders; |
| 34 class IOBuffer; | 35 class IOBuffer; |
| 35 class URLFetcherDelegate; | 36 class URLFetcherDelegate; |
| 36 class URLRequestContextGetter; | 37 class URLRequestContextGetter; |
| 37 class URLRequestThrottlerEntryInterface; | 38 class URLRequestThrottlerEntryInterface; |
| 38 | 39 |
| 39 class URLFetcherCore | 40 class URLFetcherCore |
| 40 : public base::RefCountedThreadSafe<URLFetcherCore>, | 41 : public base::RefCountedThreadSafe<URLFetcherCore>, |
| 41 public URLRequest::Delegate { | 42 public URLRequest::Delegate, |
| 43 public NetworkChangeNotifier::ConnectionTypeObserver { |
| 42 public: | 44 public: |
| 43 URLFetcherCore(URLFetcher* fetcher, | 45 URLFetcherCore(URLFetcher* fetcher, |
| 44 const GURL& original_url, | 46 const GURL& original_url, |
| 45 URLFetcher::RequestType request_type, | 47 URLFetcher::RequestType request_type, |
| 46 URLFetcherDelegate* d); | 48 URLFetcherDelegate* d); |
| 47 | 49 |
| 48 // Starts the load. It's important that this not happen in the constructor | 50 // Starts the load. It's important that this not happen in the constructor |
| 49 // because it causes the IO thread to begin AddRef()ing and Release()ing | 51 // because it causes the IO thread to begin AddRef()ing and Release()ing |
| 50 // us. If our caller hasn't had time to fully construct us and take a | 52 // us. If our caller hasn't had time to fully construct us and take a |
| 51 // reference, the IO thread could interrupt things, run a task, Release() | 53 // reference, the IO thread could interrupt things, run a task, Release() |
| (...skipping 27 matching lines...) Expand all Loading... |
| 79 // Set the URL that should be consulted for the third-party cookie | 81 // Set the URL that should be consulted for the third-party cookie |
| 80 // blocking policy. | 82 // blocking policy. |
| 81 void SetFirstPartyForCookies(const GURL& first_party_for_cookies); | 83 void SetFirstPartyForCookies(const GURL& first_party_for_cookies); |
| 82 // Set the key and data callback that is used when setting the user | 84 // Set the key and data callback that is used when setting the user |
| 83 // data on any URLRequest objects this object creates. | 85 // data on any URLRequest objects this object creates. |
| 84 void SetURLRequestUserData( | 86 void SetURLRequestUserData( |
| 85 const void* key, | 87 const void* key, |
| 86 const URLFetcher::CreateDataCallback& create_data_callback); | 88 const URLFetcher::CreateDataCallback& create_data_callback); |
| 87 void SetStopOnRedirect(bool stop_on_redirect); | 89 void SetStopOnRedirect(bool stop_on_redirect); |
| 88 void SetAutomaticallyRetryOn5xx(bool retry); | 90 void SetAutomaticallyRetryOn5xx(bool retry); |
| 89 void SetMaxRetries(int max_retries); | 91 void SetMaxRetriesOn5xx(int max_retries); |
| 90 int GetMaxRetries() const; | 92 int GetMaxRetriesOn5xx() const; |
| 91 base::TimeDelta GetBackoffDelay() const; | 93 base::TimeDelta GetBackoffDelay() const; |
| 94 void SetAutomaticallyRetryOnNetworkChanges(int max_retries); |
| 92 void SaveResponseToFileAtPath( | 95 void SaveResponseToFileAtPath( |
| 93 const FilePath& file_path, | 96 const FilePath& file_path, |
| 94 scoped_refptr<base::TaskRunner> file_task_runner); | 97 scoped_refptr<base::TaskRunner> file_task_runner); |
| 95 void SaveResponseToTemporaryFile( | 98 void SaveResponseToTemporaryFile( |
| 96 scoped_refptr<base::TaskRunner> file_task_runner); | 99 scoped_refptr<base::TaskRunner> file_task_runner); |
| 97 HttpResponseHeaders* GetResponseHeaders() const; | 100 HttpResponseHeaders* GetResponseHeaders() const; |
| 98 HostPortPair GetSocketAddress() const; | 101 HostPortPair GetSocketAddress() const; |
| 99 bool WasFetchedViaProxy() const; | 102 bool WasFetchedViaProxy() const; |
| 100 const GURL& GetOriginalURL() const; | 103 const GURL& GetOriginalURL() const; |
| 101 const GURL& GetURL() const; | 104 const GURL& GetURL() const; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 FilePath* out_response_path); | 118 FilePath* out_response_path); |
| 116 | 119 |
| 117 // Overridden from URLRequest::Delegate: | 120 // Overridden from URLRequest::Delegate: |
| 118 virtual void OnReceivedRedirect(URLRequest* request, | 121 virtual void OnReceivedRedirect(URLRequest* request, |
| 119 const GURL& new_url, | 122 const GURL& new_url, |
| 120 bool* defer_redirect) OVERRIDE; | 123 bool* defer_redirect) OVERRIDE; |
| 121 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; | 124 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; |
| 122 virtual void OnReadCompleted(URLRequest* request, | 125 virtual void OnReadCompleted(URLRequest* request, |
| 123 int bytes_read) OVERRIDE; | 126 int bytes_read) OVERRIDE; |
| 124 | 127 |
| 128 // Overridden from NetworkChangeNotifier::ConnectionTypeObserver: |
| 129 virtual void OnConnectionTypeChanged( |
| 130 NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
| 131 |
| 125 URLFetcherDelegate* delegate() const { return delegate_; } | 132 URLFetcherDelegate* delegate() const { return delegate_; } |
| 126 static void CancelAll(); | 133 static void CancelAll(); |
| 127 static int GetNumFetcherCores(); | 134 static int GetNumFetcherCores(); |
| 128 static void SetEnableInterceptionForTests(bool enabled); | 135 static void SetEnableInterceptionForTests(bool enabled); |
| 129 | 136 |
| 130 private: | 137 private: |
| 131 friend class base::RefCountedThreadSafe<URLFetcherCore>; | 138 friend class base::RefCountedThreadSafe<URLFetcherCore>; |
| 132 | 139 |
| 133 // How should the response be stored? | 140 // How should the response be stored? |
| 134 enum ResponseDestinationType { | 141 enum ResponseDestinationType { |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 // URL A won't increase. If we retry without considering the | 352 // URL A won't increase. If we retry without considering the |
| 346 // back-off constraint of URL B, we may send out too many requests | 353 // back-off constraint of URL B, we may send out too many requests |
| 347 // for URL A in a short period of time. | 354 // for URL A in a short period of time. |
| 348 // | 355 // |
| 349 // Both of these will be NULL if | 356 // Both of these will be NULL if |
| 350 // URLRequestContext::throttler_manager() is NULL. | 357 // URLRequestContext::throttler_manager() is NULL. |
| 351 scoped_refptr<URLRequestThrottlerEntryInterface> | 358 scoped_refptr<URLRequestThrottlerEntryInterface> |
| 352 original_url_throttler_entry_; | 359 original_url_throttler_entry_; |
| 353 scoped_refptr<URLRequestThrottlerEntryInterface> url_throttler_entry_; | 360 scoped_refptr<URLRequestThrottlerEntryInterface> url_throttler_entry_; |
| 354 | 361 |
| 355 // |num_retries_| indicates how many times we've failed to successfully | |
| 356 // fetch this URL. Once this value exceeds the maximum number of retries | |
| 357 // specified by the owner URLFetcher instance, we'll give up. | |
| 358 int num_retries_; | |
| 359 | |
| 360 // True if the URLFetcher has been cancelled. | 362 // True if the URLFetcher has been cancelled. |
| 361 bool was_cancelled_; | 363 bool was_cancelled_; |
| 362 | 364 |
| 363 // If writing results to a file, |file_writer_| will manage creation, | 365 // If writing results to a file, |file_writer_| will manage creation, |
| 364 // writing, and destruction of that file. | 366 // writing, and destruction of that file. |
| 365 scoped_ptr<FileWriter> file_writer_; | 367 scoped_ptr<FileWriter> file_writer_; |
| 366 | 368 |
| 367 // Where should responses be saved? | 369 // Where should responses be saved? |
| 368 ResponseDestinationType response_destination_; | 370 ResponseDestinationType response_destination_; |
| 369 | 371 |
| 370 // Path to the file where the response is written. | 372 // Path to the file where the response is written. |
| 371 FilePath response_destination_file_path_; | 373 FilePath response_destination_file_path_; |
| 372 | 374 |
| 373 // By default any server-initiated redirects are automatically followed. If | 375 // By default any server-initiated redirects are automatically followed. If |
| 374 // this flag is set to true, however, a redirect will halt the fetch and call | 376 // this flag is set to true, however, a redirect will halt the fetch and call |
| 375 // back to to the delegate immediately. | 377 // back to to the delegate immediately. |
| 376 bool stop_on_redirect_; | 378 bool stop_on_redirect_; |
| 377 // True when we're actually stopped due to a redirect halted by the above. We | 379 // True when we're actually stopped due to a redirect halted by the above. We |
| 378 // use this to ensure that |url_| is set to the redirect destination rather | 380 // use this to ensure that |url_| is set to the redirect destination rather |
| 379 // than the originally-fetched URL. | 381 // than the originally-fetched URL. |
| 380 bool stopped_on_redirect_; | 382 bool stopped_on_redirect_; |
| 381 | 383 |
| 382 // If |automatically_retry_on_5xx_| is false, 5xx responses will be | 384 // If |automatically_retry_on_5xx_| is false, 5xx responses will be |
| 383 // propagated to the observer, if it is true URLFetcher will automatically | 385 // propagated to the observer, if it is true URLFetcher will automatically |
| 384 // re-execute the request, after the back-off delay has expired. | 386 // re-execute the request, after the back-off delay has expired. |
| 385 // true by default. | 387 // true by default. |
| 386 bool automatically_retry_on_5xx_; | 388 bool automatically_retry_on_5xx_; |
| 387 // Maximum retries allowed. | 389 // |num_retries_on_5xx_| indicates how many times we've failed to successfully |
| 388 int max_retries_; | 390 // fetch this URL due to 5xx responses. Once this value exceeds the maximum |
| 391 // number of retries specified by the owner URLFetcher instance, |
| 392 // we'll give up. |
| 393 int num_retries_on_5xx_; |
| 394 // Maximum retries allowed when 5xx responses are received. |
| 395 int max_retries_on_5xx_; |
| 389 // Back-off time delay. 0 by default. | 396 // Back-off time delay. 0 by default. |
| 390 base::TimeDelta backoff_delay_; | 397 base::TimeDelta backoff_delay_; |
| 391 | 398 |
| 399 // The number of retries that have been attempted due to ERR_NETWORK_CHANGED. |
| 400 int num_retries_on_network_changes_; |
| 401 // Maximum retries allowed when the request fails with ERR_NETWORK_CHANGED. |
| 402 // 0 by default. |
| 403 int max_retries_on_network_changes_; |
| 404 |
| 392 // Timer to poll the progress of uploading for POST and PUT requests. | 405 // Timer to poll the progress of uploading for POST and PUT requests. |
| 393 // When crbug.com/119629 is fixed, scoped_ptr is not necessary here. | 406 // When crbug.com/119629 is fixed, scoped_ptr is not necessary here. |
| 394 scoped_ptr<base::RepeatingTimer<URLFetcherCore> > | 407 scoped_ptr<base::RepeatingTimer<URLFetcherCore> > |
| 395 upload_progress_checker_timer_; | 408 upload_progress_checker_timer_; |
| 396 // Number of bytes sent so far. | 409 // Number of bytes sent so far. |
| 397 int64 current_upload_bytes_; | 410 int64 current_upload_bytes_; |
| 398 // Number of bytes received so far. | 411 // Number of bytes received so far. |
| 399 int64 current_response_bytes_; | 412 int64 current_response_bytes_; |
| 400 // Total expected bytes to receive (-1 if it cannot be determined). | 413 // Total expected bytes to receive (-1 if it cannot be determined). |
| 401 int64 total_response_bytes_; | 414 int64 total_response_bytes_; |
| 402 | 415 |
| 403 // TODO(willchan): Get rid of this after debugging crbug.com/90971. | 416 // TODO(willchan): Get rid of this after debugging crbug.com/90971. |
| 404 base::debug::StackTrace stack_trace_; | 417 base::debug::StackTrace stack_trace_; |
| 405 | 418 |
| 406 static base::LazyInstance<Registry> g_registry; | 419 static base::LazyInstance<Registry> g_registry; |
| 407 | 420 |
| 408 DISALLOW_COPY_AND_ASSIGN(URLFetcherCore); | 421 DISALLOW_COPY_AND_ASSIGN(URLFetcherCore); |
| 409 }; | 422 }; |
| 410 | 423 |
| 411 } // namespace net | 424 } // namespace net |
| 412 | 425 |
| 413 #endif // NET_URL_REQUEST_URL_FETCHER_CORE_H_ | 426 #endif // NET_URL_REQUEST_URL_FETCHER_CORE_H_ |
| OLD | NEW |