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 <string> | 18 #include "base/compiler_specific.h" |
19 #include <vector> | |
20 | |
21 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
22 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
23 #include "base/platform_file.h" | |
24 #include "base/time.h" | 21 #include "base/time.h" |
25 #include "content/common/content_export.h" | 22 #include "content/public/common/url_fetcher.h" |
26 | 23 |
27 class FilePath; | 24 class CONTENT_EXPORT URLFetcher : public content::URLFetcher{ |
willchan no longer on Chromium
2011/10/25 16:59:49
This is still going to be named URLFetcher? I'd be
jam
2011/10/25 17:16:20
yep it'll be renamed to UrlFetcherImpl (in the nex
| |
28 class GURL; | |
29 | |
30 namespace base { | |
31 class MessageLoopProxy; | |
32 } // namespace base | |
33 | |
34 namespace content { | |
35 class URLFetcherDelegate; | |
36 } | |
37 | |
38 namespace net { | |
39 class HostPortPair; | |
40 class HttpResponseHeaders; | |
41 class HttpRequestHeaders; | |
42 class URLRequestContextGetter; | |
43 class URLRequestStatus; | |
44 typedef std::vector<std::string> ResponseCookies; | |
45 } // namespace net | |
46 | |
47 // To use this class, create an instance with the desired URL and a pointer to | |
48 // the object to be notified when the URL has been loaded: | |
49 // URLFetcher* fetcher = new URLFetcher("http://www.google.com", | |
50 // URLFetcher::GET, this); | |
51 // | |
52 // Then, optionally set properties on this object, like the request context or | |
53 // extra headers: | |
54 // fetcher->set_extra_request_headers("X-Foo: bar"); | |
55 // | |
56 // Finally, start the request: | |
57 // fetcher->Start(); | |
58 // | |
59 // | |
60 // The object you supply as a delegate must inherit from | |
61 // content::URLFetcherDelegate; when the fetch is completed, | |
62 // OnURLFetchComplete() will be called with a pointer to the URLFetcher. From | |
63 // that point until the original URLFetcher instance is destroyed, you may use | |
64 // accessor methods to see the result of the fetch. You should copy these | |
65 // objects if you need them to live longer than the URLFetcher instance. If the | |
66 // URLFetcher instance is destroyed before the callback happens, the fetch will | |
67 // be canceled and no callback will occur. | |
68 // | |
69 // You may create the URLFetcher instance on any thread; OnURLFetchComplete() | |
70 // will be called back on the same thread you use to create the instance. | |
71 // | |
72 // | |
73 // NOTE: By default URLFetcher requests are NOT intercepted, except when | |
74 // interception is explicitly enabled in tests. | |
75 | |
76 class CONTENT_EXPORT URLFetcher { | |
77 public: | 25 public: |
78 enum RequestType { | |
79 GET, | |
80 POST, | |
81 HEAD, | |
82 }; | |
83 | |
84 // Imposible http response code. Used to signal that no http response code | |
85 // was received. | |
86 static const int kInvalidHttpResponseCode; | |
87 | |
88 // URLFetcher::Create uses the currently registered Factory to create the | 26 // URLFetcher::Create uses the currently registered Factory to create the |
89 // URLFetcher. Factory is intended for testing. | 27 // URLFetcher. Factory is intended for testing. |
90 class Factory { | 28 class Factory { |
91 public: | 29 public: |
92 virtual URLFetcher* CreateURLFetcher(int id, | 30 virtual URLFetcher* CreateURLFetcher(int id, |
93 const GURL& url, | 31 const GURL& url, |
94 RequestType request_type, | 32 RequestType request_type, |
95 content::URLFetcherDelegate* d) = 0; | 33 content::URLFetcherDelegate* d) = 0; |
96 | 34 |
97 protected: | 35 protected: |
98 virtual ~Factory() {} | 36 virtual ~Factory() {} |
99 }; | 37 }; |
100 | 38 |
101 // |url| is the URL to send the request to. | 39 // |url| is the URL to send the request to. |
102 // |request_type| is the type of request to make. | 40 // |request_type| is the type of request to make. |
103 // |d| the object that will receive the callback on fetch completion. | 41 // |d| the object that will receive the callback on fetch completion. |
104 URLFetcher(const GURL& url, | 42 URLFetcher(const GURL& url, |
105 RequestType request_type, | 43 RequestType request_type, |
106 content::URLFetcherDelegate* d); | 44 content::URLFetcherDelegate* d); |
107 virtual ~URLFetcher(); | 45 virtual ~URLFetcher(); |
108 | 46 |
109 // Normally interception is disabled for URLFetcher, but you can use this | |
110 // to enable it for tests. Also see ScopedURLFetcherFactory for another way | |
111 // of testing code that uses an URLFetcher. | |
112 static void enable_interception_for_tests(bool enabled) { | |
113 g_interception_enabled = enabled; | |
114 } | |
115 | |
116 // Creates a URLFetcher, ownership returns to the caller. If there is no | 47 // Creates a URLFetcher, ownership returns to the caller. If there is no |
117 // Factory (the default) this creates and returns a new URLFetcher. See the | 48 // Factory (the default) this creates and returns a new URLFetcher. See the |
118 // constructor for a description of the args. |id| may be used during testing | 49 // constructor for a description of the args. |id| may be used during testing |
119 // to identify who is creating the URLFetcher. | 50 // to identify who is creating the URLFetcher. |
120 static URLFetcher* Create(int id, const GURL& url, RequestType request_type, | 51 static URLFetcher* Create(int id, const GURL& url, RequestType request_type, |
121 content::URLFetcherDelegate* d); | 52 content::URLFetcherDelegate* d); |
122 | 53 |
123 // Sets data only needed by POSTs. All callers making POST requests should | 54 // content::URLFetcher implementation: |
124 // call this before the request is started. |upload_content_type| is the MIME | 55 virtual void SetUploadData(const std::string& upload_content_type, |
125 // type of the content, while |upload_content| is the data to be sent (the | 56 const std::string& upload_content) OVERRIDE; |
126 // Content-Length header value will be set to the length of this data). | 57 virtual void SetChunkedUpload( |
127 void set_upload_data(const std::string& upload_content_type, | 58 const std::string& upload_content_type) OVERRIDE; |
128 const std::string& upload_content); | 59 virtual void AppendChunkToUpload(const std::string& data, |
60 bool is_last_chunk) OVERRIDE; | |
61 virtual void SetLoadFlags(int load_flags) OVERRIDE; | |
62 virtual int GetLoadFlags() const OVERRIDE; | |
63 virtual void SetReferrer(const std::string& referrer) OVERRIDE; | |
64 virtual void SetExtraRequestHeaders( | |
65 const std::string& extra_request_headers) OVERRIDE; | |
66 virtual void GetExtraRequestHeaders( | |
67 net::HttpRequestHeaders* headers) OVERRIDE; | |
68 virtual void SetRequestContext( | |
69 net::URLRequestContextGetter* request_context_getter) OVERRIDE; | |
70 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; | |
71 virtual void SetMaxRetries(int max_retries) OVERRIDE; | |
72 virtual int GetMaxRetries() const OVERRIDE; | |
73 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; | |
74 virtual void SetBackoffDelayForTesting( | |
75 base::TimeDelta backoff_delay) OVERRIDE; | |
76 virtual void SaveResponseToTemporaryFile( | |
77 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | |
78 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; | |
79 virtual net::HostPortPair GetSocketAddress() const OVERRIDE; | |
80 virtual bool WasFetchedViaProxy() const OVERRIDE; | |
81 virtual void Start() OVERRIDE; | |
82 virtual void StartWithRequestContextGetter( | |
83 net::URLRequestContextGetter* request_context_getter) OVERRIDE; | |
84 virtual const GURL& GetOriginalUrl() const OVERRIDE; | |
85 virtual const GURL& GetUrl() const OVERRIDE; | |
86 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE; | |
87 virtual int GetResponseCode() const OVERRIDE; | |
88 virtual const net::ResponseCookies& GetCookies() const OVERRIDE; | |
89 virtual bool FileErrorOccurred( | |
90 base::PlatformFileError* out_error_code) const OVERRIDE; | |
91 virtual void ReceivedContentWasMalformed() OVERRIDE; | |
92 virtual bool GetResponseAsString( | |
93 std::string* out_response_string) const OVERRIDE; | |
94 virtual bool GetResponseAsFilePath( | |
95 bool take_ownership, | |
96 FilePath* out_response_path) const OVERRIDE; | |
129 | 97 |
130 // Indicates that the POST data is sent via chunked transfer encoding. | |
131 // This may only be called before calling Start(). | |
132 // Use AppendChunkToUpload() to give the data chunks after calling Start(). | |
133 void set_chunked_upload(const std::string& upload_content_type); | |
134 | |
135 // Adds the given bytes to a request's POST data transmitted using chunked | |
136 // transfer encoding. | |
137 // This method should be called ONLY after calling Start(). | |
138 virtual void AppendChunkToUpload(const std::string& data, bool is_last_chunk); | |
139 | |
140 // Set one or more load flags as defined in net/base/load_flags.h. Must be | |
141 // called before the request is started. | |
142 void set_load_flags(int load_flags); | |
143 | |
144 // Returns the current load flags. | |
145 int load_flags() const; | |
146 | |
147 // The referrer URL for the request. Must be called before the request is | |
148 // started. | |
149 void set_referrer(const std::string& referrer); | |
150 | |
151 // Set extra headers on the request. Must be called before the request | |
152 // is started. | |
153 void set_extra_request_headers(const std::string& extra_request_headers); | |
154 | |
155 void GetExtraRequestHeaders(net::HttpRequestHeaders* headers); | |
156 | |
157 // Set the net::URLRequestContext on the request. Must be called before the | |
158 // request is started. | |
159 void set_request_context( | |
160 net::URLRequestContextGetter* request_context_getter); | |
161 | |
162 // If |retry| is false, 5xx responses will be propagated to the observer, | |
163 // if it is true URLFetcher will automatically re-execute the request, | |
164 // after backoff_delay() elapses. URLFetcher has it set to true by default. | |
165 void set_automatically_retry_on_5xx(bool retry); | |
166 | |
167 int max_retries() const; | |
168 | |
169 void set_max_retries(int max_retries); | |
170 | |
171 // Returns the back-off delay before the request will be retried, | |
172 // when a 5xx response was received. | |
173 base::TimeDelta backoff_delay() const; | |
174 | |
175 // Sets the back-off delay, allowing to mock 5xx requests in unit-tests. | |
176 void set_backoff_delay_for_testing(base::TimeDelta backoff_delay); | |
177 | |
178 // By default, the response is saved in a string. Call this method to save the | |
179 // response to a temporary file instead. Must be called before Start(). | |
180 // |file_message_loop_proxy| will be used for all file operations. | |
181 void SaveResponseToTemporaryFile( | |
182 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy); | |
183 | |
184 // Retrieve the response headers from the request. Must only be called after | |
185 // the OnURLFetchComplete callback has run. | |
186 virtual net::HttpResponseHeaders* response_headers() const; | |
187 | |
188 // Retrieve the remote socket address from the request. Must only | |
189 // be called after the OnURLFetchComplete callback has run and if | |
190 // the request has not failed. | |
191 net::HostPortPair socket_address() const; | |
192 | |
193 // Returns true if the request was delivered through a proxy. Must only | |
194 // be called after the OnURLFetchComplete callback has run and the request | |
195 // has not failed. | |
196 bool was_fetched_via_proxy() const; | |
197 | |
198 // Start the request. After this is called, you may not change any other | |
199 // settings. | |
200 virtual void Start(); | |
201 | |
202 // Restarts the URLFetcher with a new URLRequestContextGetter. | |
203 void StartWithRequestContextGetter( | |
204 net::URLRequestContextGetter* request_context_getter); | |
205 | |
206 // Return the URL that we were asked to fetch. | |
207 virtual const GURL& original_url() const; | |
208 | |
209 // Return the URL that this fetcher is processing. | |
210 virtual const GURL& url() const; | |
211 | |
212 // The status of the URL fetch. | |
213 virtual const net::URLRequestStatus& status() const; | |
214 | |
215 // The http response code received. Will return | |
216 // URLFetcher::kInvalidHttpResponseCode if an error prevented any response | |
217 // from being received. | |
218 virtual int response_code() const; | |
219 | |
220 // Cookies recieved. | |
221 virtual const net::ResponseCookies& cookies() const; | |
222 | |
223 // Return true if any file system operation failed. If so, set |error_code| | |
224 // to the error code. File system errors are only possible if user called | |
225 // SaveResponseToTemporaryFile(). | |
226 virtual bool FileErrorOccurred(base::PlatformFileError* out_error_code) const; | |
227 | |
228 // Reports that the received content was malformed. | |
229 void ReceivedContentWasMalformed(); | |
230 | |
231 // Get the response as a string. Return false if the fetcher was not | |
232 // set to store the response as a string. | |
233 virtual bool GetResponseAsString(std::string* out_response_string) const; | |
234 | |
235 // Get the path to the file containing the response body. Returns false | |
236 // if the response body was not saved to a file. If take_ownership is | |
237 // true, caller takes responsibility for the temp file, and it will not | |
238 // be removed once the URLFetcher is destroyed. User should not take | |
239 // ownership more than once, or call this method after taking ownership. | |
240 virtual bool GetResponseAsFilePath(bool take_ownership, | |
241 FilePath* out_response_path) const; | |
242 | |
243 // Cancels all existing URLFetchers. Will notify the URLFetcherDelegates. | |
244 // Note that any new URLFetchers created while this is running will not be | |
245 // cancelled. Typically, one would call this in the CleanUp() method of an IO | |
246 // thread, so that no new URLRequests would be able to start on the IO thread | |
247 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO | |
248 // thread though, even though the task won't ever run. | |
249 static void CancelAll(); | 98 static void CancelAll(); |
willchan no longer on Chromium
2011/10/25 16:59:49
CancelAll() is in both places?
jam
2011/10/25 17:16:20
the problem is that the static content::UrlFetcher
| |
250 | 99 |
251 protected: | 100 protected: |
252 // How should the response be stored? | 101 // How should the response be stored? |
253 enum ResponseDestinationType { | 102 enum ResponseDestinationType { |
254 STRING, // Default: In a std::string | 103 STRING, // Default: In a std::string |
255 TEMP_FILE // Write to a temp file | 104 TEMP_FILE // Write to a temp file |
256 }; | 105 }; |
257 | 106 |
258 // Returns the delegate. | 107 // Returns the delegate. |
259 content::URLFetcherDelegate* delegate() const; | 108 content::URLFetcherDelegate* delegate() const; |
(...skipping 28 matching lines...) Expand all Loading... | |
288 // NOTE: for safety, this should only be used through ScopedURLFetcherFactory! | 137 // NOTE: for safety, this should only be used through ScopedURLFetcherFactory! |
289 static void set_factory(Factory* factory) { | 138 static void set_factory(Factory* factory) { |
290 factory_ = factory; | 139 factory_ = factory; |
291 } | 140 } |
292 | 141 |
293 class Core; | 142 class Core; |
294 scoped_refptr<Core> core_; | 143 scoped_refptr<Core> core_; |
295 | 144 |
296 static Factory* factory_; | 145 static Factory* factory_; |
297 | 146 |
298 static bool g_interception_enabled; | |
299 | |
300 DISALLOW_COPY_AND_ASSIGN(URLFetcher); | 147 DISALLOW_COPY_AND_ASSIGN(URLFetcher); |
301 }; | 148 }; |
302 | 149 |
303 #endif // CONTENT_COMMON_NET_URL_FETCHER_H_ | 150 #endif // CONTENT_COMMON_NET_URL_FETCHER_H_ |
OLD | NEW |