| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This file contains URLFetcher, a wrapper around net::URLRequest that handles | 5 // This file contains URLFetcher, a wrapper around net::URLRequest that handles |
| 6 // low-level details like thread safety, ref counting, and incremental buffer | 6 // low-level details like thread safety, ref counting, and incremental buffer |
| 7 // reading. This is useful for callers who simply want to get the data from a | 7 // reading. This is useful for callers who simply want to get the data from a |
| 8 // URL and don't care about all the nitty-gritty details. | 8 // URL and don't care about all the nitty-gritty details. |
| 9 // | 9 // |
| 10 // NOTE(willchan): Only one "IO" thread is supported for URLFetcher. This is a | 10 // NOTE(willchan): Only one "IO" thread is supported for URLFetcher. This is a |
| 11 // temporary situation. We will work on allowing support for multiple "io" | 11 // temporary situation. We will work on allowing support for multiple "io" |
| 12 // threads per process. | 12 // threads per process. |
| 13 | 13 |
| 14 #ifndef CONTENT_COMMON_NET_URL_FETCHER_H_ | 14 #ifndef CONTENT_COMMON_NET_URL_FETCHER_H_ |
| 15 #define CONTENT_COMMON_NET_URL_FETCHER_H_ | 15 #define CONTENT_COMMON_NET_URL_FETCHER_H_ |
| 16 #pragma once | 16 #pragma once |
| 17 | 17 |
| 18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
| 21 #include "base/time.h" | 21 #include "base/time.h" |
| 22 #include "content/public/common/url_fetcher.h" | 22 #include "content/public/common/url_fetcher.h" |
| 23 | 23 |
| 24 namespace content { |
| 25 class URLFetcherFactory; |
| 26 } |
| 27 |
| 24 class CONTENT_EXPORT URLFetcher : public content::URLFetcher{ | 28 class CONTENT_EXPORT URLFetcher : public content::URLFetcher{ |
| 25 public: | 29 public: |
| 26 // URLFetcher::Create uses the currently registered Factory to create the | |
| 27 // URLFetcher. Factory is intended for testing. | |
| 28 class Factory { | |
| 29 public: | |
| 30 virtual URLFetcher* CreateURLFetcher(int id, | |
| 31 const GURL& url, | |
| 32 RequestType request_type, | |
| 33 content::URLFetcherDelegate* d) = 0; | |
| 34 | |
| 35 protected: | |
| 36 virtual ~Factory() {} | |
| 37 }; | |
| 38 | |
| 39 // |url| is the URL to send the request to. | 30 // |url| is the URL to send the request to. |
| 40 // |request_type| is the type of request to make. | 31 // |request_type| is the type of request to make. |
| 41 // |d| the object that will receive the callback on fetch completion. | 32 // |d| the object that will receive the callback on fetch completion. |
| 42 URLFetcher(const GURL& url, | 33 URLFetcher(const GURL& url, |
| 43 RequestType request_type, | 34 RequestType request_type, |
| 44 content::URLFetcherDelegate* d); | 35 content::URLFetcherDelegate* d); |
| 45 virtual ~URLFetcher(); | 36 virtual ~URLFetcher(); |
| 46 | 37 |
| 47 // Creates a URLFetcher, ownership returns to the caller. If there is no | |
| 48 // Factory (the default) this creates and returns a new URLFetcher. See the | |
| 49 // constructor for a description of the args. |id| may be used during testing | |
| 50 // to identify who is creating the URLFetcher. | |
| 51 static URLFetcher* Create(int id, const GURL& url, RequestType request_type, | |
| 52 content::URLFetcherDelegate* d); | |
| 53 | |
| 54 // content::URLFetcher implementation: | 38 // content::URLFetcher implementation: |
| 55 virtual void SetUploadData(const std::string& upload_content_type, | 39 virtual void SetUploadData(const std::string& upload_content_type, |
| 56 const std::string& upload_content) OVERRIDE; | 40 const std::string& upload_content) OVERRIDE; |
| 57 virtual void SetChunkedUpload( | 41 virtual void SetChunkedUpload( |
| 58 const std::string& upload_content_type) OVERRIDE; | 42 const std::string& upload_content_type) OVERRIDE; |
| 59 virtual void AppendChunkToUpload(const std::string& data, | 43 virtual void AppendChunkToUpload(const std::string& data, |
| 60 bool is_last_chunk) OVERRIDE; | 44 bool is_last_chunk) OVERRIDE; |
| 61 virtual void SetLoadFlags(int load_flags) OVERRIDE; | 45 virtual void SetLoadFlags(int load_flags) OVERRIDE; |
| 62 virtual int GetLoadFlags() const OVERRIDE; | 46 virtual int GetLoadFlags() const OVERRIDE; |
| 63 virtual void SetReferrer(const std::string& referrer) OVERRIDE; | 47 virtual void SetReferrer(const std::string& referrer) OVERRIDE; |
| 64 virtual void SetExtraRequestHeaders( | 48 virtual void SetExtraRequestHeaders( |
| 65 const std::string& extra_request_headers) OVERRIDE; | 49 const std::string& extra_request_headers) OVERRIDE; |
| 66 virtual void GetExtraRequestHeaders( | 50 virtual void GetExtraRequestHeaders( |
| 67 net::HttpRequestHeaders* headers) OVERRIDE; | 51 net::HttpRequestHeaders* headers) OVERRIDE; |
| 68 virtual void SetRequestContext( | 52 virtual void SetRequestContext( |
| 69 net::URLRequestContextGetter* request_context_getter) OVERRIDE; | 53 net::URLRequestContextGetter* request_context_getter) OVERRIDE; |
| 70 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; | 54 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; |
| 71 virtual void SetMaxRetries(int max_retries) OVERRIDE; | 55 virtual void SetMaxRetries(int max_retries) OVERRIDE; |
| 72 virtual int GetMaxRetries() const OVERRIDE; | 56 virtual int GetMaxRetries() const OVERRIDE; |
| 73 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; | 57 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; |
| 74 virtual void SetBackoffDelayForTesting( | |
| 75 base::TimeDelta backoff_delay) OVERRIDE; | |
| 76 virtual void SaveResponseToTemporaryFile( | 58 virtual void SaveResponseToTemporaryFile( |
| 77 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | 59 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; |
| 78 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; | 60 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; |
| 79 virtual net::HostPortPair GetSocketAddress() const OVERRIDE; | 61 virtual net::HostPortPair GetSocketAddress() const OVERRIDE; |
| 80 virtual bool WasFetchedViaProxy() const OVERRIDE; | 62 virtual bool WasFetchedViaProxy() const OVERRIDE; |
| 81 virtual void Start() OVERRIDE; | 63 virtual void Start() OVERRIDE; |
| 82 virtual void StartWithRequestContextGetter( | 64 virtual void StartWithRequestContextGetter( |
| 83 net::URLRequestContextGetter* request_context_getter) OVERRIDE; | 65 net::URLRequestContextGetter* request_context_getter) OVERRIDE; |
| 84 virtual const GURL& GetOriginalUrl() const OVERRIDE; | 66 virtual const GURL& GetOriginalUrl() const OVERRIDE; |
| 85 virtual const GURL& GetUrl() const OVERRIDE; | 67 virtual const GURL& GetUrl() const OVERRIDE; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 109 | 91 |
| 110 // Used by tests. | 92 // Used by tests. |
| 111 const std::string& upload_data() const; | 93 const std::string& upload_data() const; |
| 112 | 94 |
| 113 // Used by tests. | 95 // Used by tests. |
| 114 void set_was_fetched_via_proxy(bool flag); | 96 void set_was_fetched_via_proxy(bool flag); |
| 115 | 97 |
| 116 // Used by tests. | 98 // Used by tests. |
| 117 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); | 99 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); |
| 118 | 100 |
| 119 virtual void SetResponseDestinationForTesting(ResponseDestinationType); | |
| 120 virtual ResponseDestinationType GetResponseDestinationForTesting() const; | |
| 121 | |
| 122 private: | 101 private: |
| 123 friend class ScopedURLFetcherFactory; | 102 friend class ScopedURLFetcherFactory; |
| 124 friend class TestURLFetcher; | 103 friend class TestURLFetcher; |
| 125 friend class URLFetcherTest; | 104 friend class URLFetcherTest; |
| 126 | 105 |
| 127 // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects | 106 // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects |
| 128 // actively running. | 107 // actively running. |
| 129 static int GetNumFetcherCores(); | 108 static int GetNumFetcherCores(); |
| 130 | 109 |
| 131 static Factory* factory() { return factory_; } | 110 static content::URLFetcherFactory* factory(); |
| 132 | 111 |
| 133 // Sets the factory used by the static method Create to create a URLFetcher. | 112 // Sets the factory used by the static method Create to create a URLFetcher. |
| 134 // URLFetcher does not take ownership of |factory|. A value of NULL results | 113 // URLFetcher does not take ownership of |factory|. A value of NULL results |
| 135 // in a URLFetcher being created directly. | 114 // in a URLFetcher being created directly. |
| 136 // | 115 // |
| 137 // NOTE: for safety, this should only be used through ScopedURLFetcherFactory! | 116 // NOTE: for safety, this should only be used through ScopedURLFetcherFactory! |
| 138 static void set_factory(Factory* factory) { | 117 static void set_factory(content::URLFetcherFactory* factory); |
| 139 factory_ = factory; | |
| 140 } | |
| 141 | 118 |
| 142 class Core; | 119 class Core; |
| 143 scoped_refptr<Core> core_; | 120 scoped_refptr<Core> core_; |
| 144 | 121 |
| 145 static Factory* factory_; | |
| 146 | |
| 147 DISALLOW_COPY_AND_ASSIGN(URLFetcher); | 122 DISALLOW_COPY_AND_ASSIGN(URLFetcher); |
| 148 }; | 123 }; |
| 149 | 124 |
| 150 #endif // CONTENT_COMMON_NET_URL_FETCHER_H_ | 125 #endif // CONTENT_COMMON_NET_URL_FETCHER_H_ |
| OLD | NEW |