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