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 CONTENT_PUBLIC_COMMON_URL_FETCHER_H_ | 5 #ifndef NET_URL_REQUEST_URL_FETCHER_H_ |
6 #define CONTENT_PUBLIC_COMMON_URL_FETCHER_H_ | 6 #define NET_URL_REQUEST_URL_FETCHER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
14 #include "content/common/content_export.h" | 14 #include "net/base/net_export.h" |
15 | 15 |
16 class FilePath; | 16 class FilePath; |
17 class GURL; | 17 class GURL; |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 class MessageLoopProxy; | 20 class MessageLoopProxy; |
21 class TimeDelta; | 21 class TimeDelta; |
22 } | 22 } |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 class HostPortPair; | 25 class HostPortPair; |
26 class HttpRequestHeaders; | 26 class HttpRequestHeaders; |
27 class HttpResponseHeaders; | 27 class HttpResponseHeaders; |
| 28 class URLFetcherDelegate; |
28 class URLRequestContextGetter; | 29 class URLRequestContextGetter; |
29 class URLRequestStatus; | 30 class URLRequestStatus; |
30 typedef std::vector<std::string> ResponseCookies; | 31 typedef std::vector<std::string> ResponseCookies; |
31 } | |
32 | |
33 namespace content { | |
34 | |
35 class URLFetcherDelegate; | |
36 | 32 |
37 // To use this class, create an instance with the desired URL and a pointer to | 33 // To use this class, create an instance with the desired URL and a pointer to |
38 // the object to be notified when the URL has been loaded: | 34 // the object to be notified when the URL has been loaded: |
39 // URLFetcher* fetcher = URLFetcher::Create("http://www.google.com", | 35 // URLFetcher* fetcher = URLFetcher::Create("http://www.google.com", |
40 // URLFetcher::GET, this); | 36 // URLFetcher::GET, this); |
41 // | 37 // |
42 // Then, optionally set properties on this object, like the request context or | 38 // Then, optionally set properties on this object, like the request context or |
43 // extra headers: | 39 // extra headers: |
44 // fetcher->set_extra_request_headers("X-Foo: bar"); | 40 // fetcher->set_extra_request_headers("X-Foo: bar"); |
45 // | 41 // |
46 // Finally, start the request: | 42 // Finally, start the request: |
47 // fetcher->Start(); | 43 // fetcher->Start(); |
48 // | 44 // |
49 // | 45 // |
50 // The object you supply as a delegate must inherit from | 46 // The object you supply as a delegate must inherit from |
51 // URLFetcherDelegate; when the fetch is completed, | 47 // URLFetcherDelegate; when the fetch is completed, |
52 // OnURLFetchComplete() will be called with a pointer to the URLFetcher. From | 48 // OnURLFetchComplete() will be called with a pointer to the URLFetcher. From |
53 // that point until the original URLFetcher instance is destroyed, you may use | 49 // that point until the original URLFetcher instance is destroyed, you may use |
54 // accessor methods to see the result of the fetch. You should copy these | 50 // accessor methods to see the result of the fetch. You should copy these |
55 // objects if you need them to live longer than the URLFetcher instance. If the | 51 // objects if you need them to live longer than the URLFetcher instance. If the |
56 // URLFetcher instance is destroyed before the callback happens, the fetch will | 52 // URLFetcher instance is destroyed before the callback happens, the fetch will |
57 // be canceled and no callback will occur. | 53 // be canceled and no callback will occur. |
58 // | 54 // |
59 // You may create the URLFetcher instance on any thread; OnURLFetchComplete() | 55 // You may create the URLFetcher instance on any thread; OnURLFetchComplete() |
60 // will be called back on the same thread you use to create the instance. | 56 // will be called back on the same thread you use to create the instance. |
61 // | 57 // |
62 // | 58 // |
63 // NOTE: By default URLFetcher requests are NOT intercepted, except when | 59 // NOTE: By default URLFetcher requests are NOT intercepted, except when |
64 // interception is explicitly enabled in tests. | 60 // interception is explicitly enabled in tests. |
65 class CONTENT_EXPORT URLFetcher { | 61 class NET_EXPORT URLFetcher { |
66 public: | 62 public: |
67 // Imposible http response code. Used to signal that no http response code | 63 // Imposible http response code. Used to signal that no http response code |
68 // was received. | 64 // was received. |
69 enum ResponseCode { | 65 enum ResponseCode { |
70 RESPONSE_CODE_INVALID = -1 | 66 RESPONSE_CODE_INVALID = -1 |
71 }; | 67 }; |
72 | 68 |
73 enum RequestType { | 69 enum RequestType { |
74 GET, | 70 GET, |
75 POST, | 71 POST, |
76 HEAD, | 72 HEAD, |
77 DELETE_REQUEST, // DELETE is already taken on Windows. | 73 DELETE_REQUEST, // DELETE is already taken on Windows. |
78 // <winnt.h> defines a DELETE macro. | 74 // <winnt.h> defines a DELETE macro. |
79 PUT, | 75 PUT, |
80 }; | 76 }; |
81 | 77 |
82 // |url| is the URL to send the request to. | 78 virtual ~URLFetcher(); |
83 // |request_type| is the type of request to make. | |
84 // |d| the object that will receive the callback on fetch completion. | |
85 static URLFetcher* Create(const GURL& url, | |
86 RequestType request_type, | |
87 URLFetcherDelegate* d); | |
88 | |
89 // Like above, but if there's a URLFetcherFactory registered with the | |
90 // implementation it will be used. |id| may be used during testing to identify | |
91 // who is creating the URLFetcher. | |
92 static URLFetcher* Create(int id, | |
93 const GURL& url, | |
94 RequestType request_type, | |
95 URLFetcherDelegate* d); | |
96 | |
97 // Cancels all existing URLFetchers. Will notify the URLFetcherDelegates. | |
98 // Note that any new URLFetchers created while this is running will not be | |
99 // cancelled. Typically, one would call this in the CleanUp() method of an IO | |
100 // thread, so that no new URLRequests would be able to start on the IO thread | |
101 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO | |
102 // thread though, even though the task won't ever run. | |
103 static void CancelAll(); | |
104 | |
105 // Normally interception is disabled for URLFetcher, but you can use this | |
106 // to enable it for tests. Also see ScopedURLFetcherFactory for another way | |
107 // of testing code that uses an URLFetcher. | |
108 static void SetEnableInterceptionForTests(bool enabled); | |
109 | |
110 virtual ~URLFetcher() {} | |
111 | 79 |
112 // Sets data only needed by POSTs. All callers making POST requests should | 80 // Sets data only needed by POSTs. All callers making POST requests should |
113 // call this before the request is started. |upload_content_type| is the MIME | 81 // call this before the request is started. |upload_content_type| is the MIME |
114 // type of the content, while |upload_content| is the data to be sent (the | 82 // type of the content, while |upload_content| is the data to be sent (the |
115 // Content-Length header value will be set to the length of this data). | 83 // Content-Length header value will be set to the length of this data). |
116 virtual void SetUploadData(const std::string& upload_content_type, | 84 virtual void SetUploadData(const std::string& upload_content_type, |
117 const std::string& upload_content) = 0; | 85 const std::string& upload_content) = 0; |
118 | 86 |
119 // Indicates that the POST data is sent via chunked transfer encoding. | 87 // Indicates that the POST data is sent via chunked transfer encoding. |
120 // This may only be called before calling Start(). | 88 // This may only be called before calling Start(). |
(...skipping 22 matching lines...) Expand all Loading... |
143 // This replaces the entire extra request headers. | 111 // This replaces the entire extra request headers. |
144 virtual void SetExtraRequestHeaders( | 112 virtual void SetExtraRequestHeaders( |
145 const std::string& extra_request_headers) = 0; | 113 const std::string& extra_request_headers) = 0; |
146 | 114 |
147 // Add header (with format field-name ":" [ field-value ]) to the request | 115 // Add header (with format field-name ":" [ field-value ]) to the request |
148 // headers. Must be called before the request is started. | 116 // headers. Must be called before the request is started. |
149 // This appends the header to the current extra request headers. | 117 // This appends the header to the current extra request headers. |
150 virtual void AddExtraRequestHeader(const std::string& header_line) = 0; | 118 virtual void AddExtraRequestHeader(const std::string& header_line) = 0; |
151 | 119 |
152 virtual void GetExtraRequestHeaders( | 120 virtual void GetExtraRequestHeaders( |
153 net::HttpRequestHeaders* headers) const = 0; | 121 HttpRequestHeaders* headers) const = 0; |
154 | 122 |
155 // Set the net::URLRequestContext on the request. Must be called before the | 123 // Set the URLRequestContext on the request. Must be called before the |
156 // request is started. | 124 // request is started. |
157 virtual void SetRequestContext( | 125 virtual void SetRequestContext( |
158 net::URLRequestContextGetter* request_context_getter) = 0; | 126 URLRequestContextGetter* request_context_getter) = 0; |
159 | |
160 // Mark URLRequests started by the URLFetcher to stem from the given render | |
161 // view. | |
162 virtual void AssociateWithRenderView(const GURL& first_party_for_cookies, | |
163 int render_process_id, | |
164 int render_view_id) = 0; | |
165 | 127 |
166 // If |retry| is false, 5xx responses will be propagated to the observer, | 128 // If |retry| is false, 5xx responses will be propagated to the observer, |
167 // if it is true URLFetcher will automatically re-execute the request, | 129 // if it is true URLFetcher will automatically re-execute the request, |
168 // after backoff_delay() elapses. URLFetcher has it set to true by default. | 130 // after backoff_delay() elapses. URLFetcher has it set to true by default. |
169 virtual void SetAutomaticallyRetryOn5xx(bool retry) = 0; | 131 virtual void SetAutomaticallyRetryOn5xx(bool retry) = 0; |
170 | 132 |
171 virtual void SetMaxRetries(int max_retries) = 0; | 133 virtual void SetMaxRetries(int max_retries) = 0; |
172 virtual int GetMaxRetries() const = 0; | 134 virtual int GetMaxRetries() const = 0; |
173 | 135 |
174 // Returns the back-off delay before the request will be retried, | 136 // Returns the back-off delay before the request will be retried, |
(...skipping 13 matching lines...) Expand all Loading... |
188 // By default, the response is saved in a string. Call this method to save the | 150 // By default, the response is saved in a string. Call this method to save the |
189 // response to a temporary file instead. Must be called before Start(). | 151 // response to a temporary file instead. Must be called before Start(). |
190 // |file_message_loop_proxy| will be used for all file operations. | 152 // |file_message_loop_proxy| will be used for all file operations. |
191 // The created file is removed when the URLFetcher is deleted unless you | 153 // The created file is removed when the URLFetcher is deleted unless you |
192 // take ownership by calling GetResponseAsFilePath(). | 154 // take ownership by calling GetResponseAsFilePath(). |
193 virtual void SaveResponseToTemporaryFile( | 155 virtual void SaveResponseToTemporaryFile( |
194 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) = 0; | 156 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) = 0; |
195 | 157 |
196 // Retrieve the response headers from the request. Must only be called after | 158 // Retrieve the response headers from the request. Must only be called after |
197 // the OnURLFetchComplete callback has run. | 159 // the OnURLFetchComplete callback has run. |
198 virtual net::HttpResponseHeaders* GetResponseHeaders() const = 0; | 160 virtual HttpResponseHeaders* GetResponseHeaders() const = 0; |
199 | 161 |
200 // Retrieve the remote socket address from the request. Must only | 162 // Retrieve the remote socket address from the request. Must only |
201 // be called after the OnURLFetchComplete callback has run and if | 163 // be called after the OnURLFetchComplete callback has run and if |
202 // the request has not failed. | 164 // the request has not failed. |
203 virtual net::HostPortPair GetSocketAddress() const = 0; | 165 virtual HostPortPair GetSocketAddress() const = 0; |
204 | 166 |
205 // Returns true if the request was delivered through a proxy. Must only | 167 // Returns true if the request was delivered through a proxy. Must only |
206 // be called after the OnURLFetchComplete callback has run and the request | 168 // be called after the OnURLFetchComplete callback has run and the request |
207 // has not failed. | 169 // has not failed. |
208 virtual bool WasFetchedViaProxy() const = 0; | 170 virtual bool WasFetchedViaProxy() const = 0; |
209 | 171 |
210 // Start the request. After this is called, you may not change any other | 172 // Start the request. After this is called, you may not change any other |
211 // settings. | 173 // settings. |
212 virtual void Start() = 0; | 174 virtual void Start() = 0; |
213 | 175 |
214 // Return the URL that we were asked to fetch. | 176 // Return the URL that we were asked to fetch. |
215 virtual const GURL& GetOriginalURL() const = 0; | 177 virtual const GURL& GetOriginalURL() const = 0; |
216 | 178 |
217 // Return the URL that this fetcher is processing. | 179 // Return the URL that this fetcher is processing. |
218 virtual const GURL& GetURL() const = 0; | 180 virtual const GURL& GetURL() const = 0; |
219 | 181 |
220 // The status of the URL fetch. | 182 // The status of the URL fetch. |
221 virtual const net::URLRequestStatus& GetStatus() const = 0; | 183 virtual const URLRequestStatus& GetStatus() const = 0; |
222 | 184 |
223 // The http response code received. Will return RESPONSE_CODE_INVALID | 185 // The http response code received. Will return RESPONSE_CODE_INVALID |
224 // if an error prevented any response from being received. | 186 // if an error prevented any response from being received. |
225 virtual int GetResponseCode() const = 0; | 187 virtual int GetResponseCode() const = 0; |
226 | 188 |
227 // Cookies recieved. | 189 // Cookies recieved. |
228 virtual const net::ResponseCookies& GetCookies() const = 0; | 190 virtual const ResponseCookies& GetCookies() const = 0; |
229 | 191 |
230 // Return true if any file system operation failed. If so, set |error_code| | 192 // Return true if any file system operation failed. If so, set |error_code| |
231 // to the error code. File system errors are only possible if user called | 193 // to the error code. File system errors are only possible if user called |
232 // SaveResponseToTemporaryFile(). | 194 // SaveResponseToTemporaryFile(). |
233 virtual bool FileErrorOccurred( | 195 virtual bool FileErrorOccurred( |
234 base::PlatformFileError* out_error_code) const = 0; | 196 base::PlatformFileError* out_error_code) const = 0; |
235 | 197 |
236 // Reports that the received content was malformed. | 198 // Reports that the received content was malformed. |
237 virtual void ReceivedContentWasMalformed() = 0; | 199 virtual void ReceivedContentWasMalformed() = 0; |
238 | 200 |
239 // Get the response as a string. Return false if the fetcher was not | 201 // Get the response as a string. Return false if the fetcher was not |
240 // set to store the response as a string. | 202 // set to store the response as a string. |
241 virtual bool GetResponseAsString(std::string* out_response_string) const = 0; | 203 virtual bool GetResponseAsString(std::string* out_response_string) const = 0; |
242 | 204 |
243 // Get the path to the file containing the response body. Returns false | 205 // Get the path to the file containing the response body. Returns false |
244 // if the response body was not saved to a file. If take_ownership is | 206 // if the response body was not saved to a file. If take_ownership is |
245 // true, caller takes responsibility for the file, and it will not | 207 // true, caller takes responsibility for the file, and it will not |
246 // be removed once the URLFetcher is destroyed. User should not take | 208 // be removed once the URLFetcher is destroyed. User should not take |
247 // ownership more than once, or call this method after taking ownership. | 209 // ownership more than once, or call this method after taking ownership. |
248 virtual bool GetResponseAsFilePath(bool take_ownership, | 210 virtual bool GetResponseAsFilePath(bool take_ownership, |
249 FilePath* out_response_path) const = 0; | 211 FilePath* out_response_path) const = 0; |
250 }; | 212 }; |
251 | 213 |
252 } // namespace content | 214 } // namespace net |
253 | 215 |
254 #endif // CONTENT_PUBLIC_COMMON_URL_FETCHER_H_ | 216 #endif // NET_URL_REQUEST_URL_FETCHER_H_ |
OLD | NEW |