Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Side by Side Diff: chrome/common/net/url_fetcher.h

Issue 6904057: Schetch changes required to support URLFetcher saving response to a file (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Allow URLFetcher to save the response as a file. Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/common/net/url_fetcher.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 CHROME_COMMON_NET_URL_FETCHER_H_ 14 #ifndef CHROME_COMMON_NET_URL_FETCHER_H_
15 #define CHROME_COMMON_NET_URL_FETCHER_H_ 15 #define CHROME_COMMON_NET_URL_FETCHER_H_
16 #pragma once 16 #pragma once
17 17
18 #include <string> 18 #include <string>
19 #include <vector> 19 #include <vector>
20 20
21 #include "base/memory/ref_counted.h" 21 #include "base/memory/ref_counted.h"
22 #include "base/message_loop.h" 22 #include "base/message_loop.h"
23 #include "base/platform_file.h"
23 #include "base/time.h" 24 #include "base/time.h"
24 25
26 class FilePath;
25 class GURL; 27 class GURL;
26 28
29 namespace base {
30 class MessageLoopProxy;
31 } // namespace base
32
27 namespace net { 33 namespace net {
28 class HostPortPair; 34 class HostPortPair;
29 class HttpResponseHeaders; 35 class HttpResponseHeaders;
30 class URLRequestContextGetter; 36 class URLRequestContextGetter;
31 class URLRequestStatus; 37 class URLRequestStatus;
32 typedef std::vector<std::string> ResponseCookies; 38 typedef std::vector<std::string> ResponseCookies;
33 } // namespace net 39 } // namespace net
34 40
35 // To use this class, create an instance with the desired URL and a pointer to 41 // To use this class, create an instance with the desired URL and a pointer to
36 // the object to be notified when the URL has been loaded: 42 // the object to be notified when the URL has been loaded:
(...skipping 24 matching lines...) Expand all
61 // interception is explicitly enabled in tests. 67 // interception is explicitly enabled in tests.
62 68
63 class URLFetcher { 69 class URLFetcher {
64 public: 70 public:
65 enum RequestType { 71 enum RequestType {
66 GET, 72 GET,
67 POST, 73 POST,
68 HEAD, 74 HEAD,
69 }; 75 };
70 76
77 // Where should the response be saved? Use set_response_destination()
78 // to choose.
79 enum ResponseDestinationType {
80 STRING, // Default
81 TEMP_FILE
82 };
83
71 class Delegate { 84 class Delegate {
72 public: 85 public:
73 // This will be called when the URL has been fetched, successfully or not. 86 // TODO(skerner): This will be removed in favor of the |data|-free
74 // |response_code| is the HTTP response code (200, 404, etc.) if 87 // version below. Leaving this for now to make the initial code review
75 // applicable. |url|, |status| and |data| are all valid until the 88 // easy to read.
76 // URLFetcher instance is destroyed.
77 virtual void OnURLFetchComplete(const URLFetcher* source, 89 virtual void OnURLFetchComplete(const URLFetcher* source,
78 const GURL& url, 90 const GURL& url,
79 const net::URLRequestStatus& status, 91 const net::URLRequestStatus& status,
80 int response_code, 92 int response_code,
81 const net::ResponseCookies& cookies, 93 const net::ResponseCookies& cookies,
82 const std::string& data) = 0; 94 const std::string& data);
95
96 // This will be called when the URL has been fetched, successfully or not.
97 // |response_code| is the HTTP response code (200, 404, etc.) if
98 // applicable. |url| and |status| are all valid until the URLFetcher
99 // instance is destroyed.
100 virtual void OnURLFetchComplete(const URLFetcher* source,
101 const GURL& url,
102 const net::URLRequestStatus& status,
103 int response_code,
104 const net::ResponseCookies& cookies);
105
106 // Called on a file writing error. Only called when writing the response
107 // to a file.
108 // REVIEWER PLEASE NOTE: This method would be unnecessary if the failure
109 // could be encoded in a net::URLRequestStatus. However, it seems odd
110 // to put a file system error in that object. Is it worth having this
111 // method?
112 virtual void OnFileWriteError(const URLFetcher* source,
113 const GURL& url,
114 base::PlatformFileError error);
83 115
84 protected: 116 protected:
85 virtual ~Delegate() {} 117 virtual ~Delegate() {}
86 }; 118 };
87 119
88 // URLFetcher::Create uses the currently registered Factory to create the 120 // URLFetcher::Create uses the currently registered Factory to create the
89 // URLFetcher. Factory is intended for testing. 121 // URLFetcher. Factory is intended for testing.
90 class Factory { 122 class Factory {
91 public: 123 public:
92 virtual URLFetcher* CreateURLFetcher(int id, 124 virtual URLFetcher* CreateURLFetcher(int id,
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 // when a 5xx response was received. 208 // when a 5xx response was received.
177 base::TimeDelta backoff_delay() const { return backoff_delay_; } 209 base::TimeDelta backoff_delay() const { return backoff_delay_; }
178 210
179 // Sets the back-off delay, allowing to mock 5xx requests in unit-tests. 211 // Sets the back-off delay, allowing to mock 5xx requests in unit-tests.
180 #if defined(UNIT_TEST) 212 #if defined(UNIT_TEST)
181 void set_backoff_delay(base::TimeDelta backoff_delay) { 213 void set_backoff_delay(base::TimeDelta backoff_delay) {
182 backoff_delay_ = backoff_delay; 214 backoff_delay_ = backoff_delay;
183 } 215 }
184 #endif // defined(UNIT_TEST) 216 #endif // defined(UNIT_TEST)
185 217
218 ResponseDestinationType response_destination() {
219 return response_destination_;
220 }
221
222 // Where should the response be saved? This should only be called before
223 // calling Start(). If the response should be saved to a file, you need
224 // to call set_file_message_loop_proxy() to give the thread on which file
225 // operations may be done.
226 void set_response_destination(ResponseDestinationType response_destination) {
227 response_destination_ = response_destination;
228 }
229
230 // Set the message loop proxy for file operations. Used to write the
231 // response to a file.
232 void set_file_message_loop_proxy(
233 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy);
234
186 // Retrieve the response headers from the request. Must only be called after 235 // Retrieve the response headers from the request. Must only be called after
187 // the OnURLFetchComplete callback has run. 236 // the OnURLFetchComplete callback has run.
188 virtual net::HttpResponseHeaders* response_headers() const; 237 virtual net::HttpResponseHeaders* response_headers() const;
189 238
190 // Retrieve the remote socket address from the request. Must only 239 // Retrieve the remote socket address from the request. Must only
191 // be called after the OnURLFetchComplete callback has run and if 240 // be called after the OnURLFetchComplete callback has run and if
192 // the request has not failed. 241 // the request has not failed.
193 net::HostPortPair socket_address() const; 242 net::HostPortPair socket_address() const;
194 243
195 // Returns true if the request was delivered through a proxy. Must only 244 // Returns true if the request was delivered through a proxy. Must only
196 // be called after the OnURLFetchComplete callback has run and the request 245 // be called after the OnURLFetchComplete callback has run and the request
197 // has not failed. 246 // has not failed.
198 bool was_fetched_via_proxy() const; 247 bool was_fetched_via_proxy() const;
199 248
200 // Start the request. After this is called, you may not change any other 249 // Start the request. After this is called, you may not change any other
201 // settings. 250 // settings.
202 virtual void Start(); 251 virtual void Start();
203 252
204 // Return the URL that this fetcher is processing. 253 // Return the URL that this fetcher is processing.
205 const GURL& url() const; 254 const GURL& url() const;
206 255
207 // Reports that the received content was malformed. 256 // Reports that the received content was malformed.
208 void ReceivedContentWasMalformed(); 257 void ReceivedContentWasMalformed();
209 258
259 // Get the response as a string. Return false if the fetcher was not
260 // set to store the response as a string.
261 bool GetResponseAsString(std::string* response_string) const;
262
263 // Get the path to the file containing the response body. Returns false
264 // if the response body was not saved to a file.
265 bool GetResponseAsFilePath(FilePath* response_path) const;
266
210 // Cancels all existing URLFetchers. Will notify the URLFetcher::Delegates. 267 // Cancels all existing URLFetchers. Will notify the URLFetcher::Delegates.
211 // Note that any new URLFetchers created while this is running will not be 268 // Note that any new URLFetchers created while this is running will not be
212 // cancelled. Typically, one would call this in the CleanUp() method of an IO 269 // cancelled. Typically, one would call this in the CleanUp() method of an IO
213 // thread, so that no new URLRequests would be able to start on the IO thread 270 // thread, so that no new URLRequests would be able to start on the IO thread
214 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO 271 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO
215 // thread though, even though the task won't ever run. 272 // thread though, even though the task won't ever run.
216 static void CancelAll(); 273 static void CancelAll();
217 274
218 protected: 275 protected:
219 // Returns the delegate. 276 // Returns the delegate.
(...skipping 18 matching lines...) Expand all
238 // If |automatically_retry_on_5xx_| is false, 5xx responses will be 295 // If |automatically_retry_on_5xx_| is false, 5xx responses will be
239 // propagated to the observer, if it is true URLFetcher will automatically 296 // propagated to the observer, if it is true URLFetcher will automatically
240 // re-execute the request, after the back-off delay has expired. 297 // re-execute the request, after the back-off delay has expired.
241 // true by default. 298 // true by default.
242 bool automatically_retry_on_5xx_; 299 bool automatically_retry_on_5xx_;
243 // Back-off time delay. 0 by default. 300 // Back-off time delay. 0 by default.
244 base::TimeDelta backoff_delay_; 301 base::TimeDelta backoff_delay_;
245 // Maximum retries allowed. 302 // Maximum retries allowed.
246 int max_retries_; 303 int max_retries_;
247 304
305 // Where should responses be saved?
306 ResponseDestinationType response_destination_;
307
248 static bool g_interception_enabled; 308 static bool g_interception_enabled;
249 309
250 DISALLOW_COPY_AND_ASSIGN(URLFetcher); 310 DISALLOW_COPY_AND_ASSIGN(URLFetcher);
251 }; 311 };
252 312
253 #endif // CHROME_COMMON_NET_URL_FETCHER_H_ 313 #endif // CHROME_COMMON_NET_URL_FETCHER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/common/net/url_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698