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 |
(...skipping 13 matching lines...) Expand all Loading... | |
24 #include "base/time.h" | 24 #include "base/time.h" |
25 #include "content/common/content_export.h" | 25 #include "content/common/content_export.h" |
26 | 26 |
27 class FilePath; | 27 class FilePath; |
28 class GURL; | 28 class GURL; |
29 | 29 |
30 namespace base { | 30 namespace base { |
31 class MessageLoopProxy; | 31 class MessageLoopProxy; |
32 } // namespace base | 32 } // namespace base |
33 | 33 |
34 namespace content { | |
35 class URLFetcherDelegate; | |
36 } | |
37 | |
34 namespace net { | 38 namespace net { |
35 class HostPortPair; | 39 class HostPortPair; |
36 class HttpResponseHeaders; | 40 class HttpResponseHeaders; |
37 class HttpRequestHeaders; | 41 class HttpRequestHeaders; |
38 class URLRequestContextGetter; | 42 class URLRequestContextGetter; |
39 class URLRequestStatus; | 43 class URLRequestStatus; |
40 typedef std::vector<std::string> ResponseCookies; | 44 typedef std::vector<std::string> ResponseCookies; |
41 } // namespace net | 45 } // namespace net |
42 | 46 |
43 // To use this class, create an instance with the desired URL and a pointer to | 47 // To use this class, create an instance with the desired URL and a pointer to |
44 // the object to be notified when the URL has been loaded: | 48 // the object to be notified when the URL has been loaded: |
45 // URLFetcher* fetcher = new URLFetcher("http://www.google.com", | 49 // URLFetcher* fetcher = new URLFetcher("http://www.google.com", |
46 // URLFetcher::GET, this); | 50 // URLFetcher::GET, this); |
47 // | 51 // |
48 // Then, optionally set properties on this object, like the request context or | 52 // Then, optionally set properties on this object, like the request context or |
49 // extra headers: | 53 // extra headers: |
50 // fetcher->set_extra_request_headers("X-Foo: bar"); | 54 // fetcher->set_extra_request_headers("X-Foo: bar"); |
51 // | 55 // |
52 // Finally, start the request: | 56 // Finally, start the request: |
53 // fetcher->Start(); | 57 // fetcher->Start(); |
54 // | 58 // |
55 // | 59 // |
56 // The object you supply as a delegate must inherit from URLFetcher::Delegate; | 60 // The object you supply as a delegate must inherit from |
57 // when the fetch is completed, OnURLFetchComplete() will be called with a | 61 // content::URLFetcherDelegate; when the fetch is completed, |
58 // pointer to the URLFetcher. From that point until the original URLFetcher | 62 // OnURLFetchComplete() will be called with a pointer to the URLFetcher. From |
59 // instance is destroyed, you may use accessor methods to see the result of | 63 // that point until the original URLFetcher instance is destroyed, you may use |
60 // the fetch. You should copy these objects if you need them to live longer | 64 // accessor methods to see the result of the fetch. You should copy these |
61 // than the URLFetcher instance. If the URLFetcher instance is destroyed | 65 // objects if you need them to live longer than the URLFetcher instance. If the |
62 // before the callback happens, the fetch will be canceled and no callback | 66 // URLFetcher instance is destroyed before the callback happens, the fetch will |
63 // will occur. | 67 // be canceled and no callback will occur. |
64 // | 68 // |
65 // You may create the URLFetcher instance on any thread; OnURLFetchComplete() | 69 // You may create the URLFetcher instance on any thread; OnURLFetchComplete() |
66 // will be called back on the same thread you use to create the instance. | 70 // will be called back on the same thread you use to create the instance. |
67 // | 71 // |
68 // | 72 // |
69 // NOTE: By default URLFetcher requests are NOT intercepted, except when | 73 // NOTE: By default URLFetcher requests are NOT intercepted, except when |
70 // interception is explicitly enabled in tests. | 74 // interception is explicitly enabled in tests. |
71 | 75 |
72 class CONTENT_EXPORT URLFetcher { | 76 class CONTENT_EXPORT URLFetcher { |
73 public: | 77 public: |
74 enum RequestType { | 78 enum RequestType { |
75 GET, | 79 GET, |
76 POST, | 80 POST, |
77 HEAD, | 81 HEAD, |
78 }; | 82 }; |
79 | 83 |
80 // Imposible http response code. Used to signal that no http response code | 84 // Imposible http response code. Used to signal that no http response code |
81 // was received. | 85 // was received. |
82 static const int kInvalidHttpResponseCode; | 86 static const int kInvalidHttpResponseCode; |
83 | 87 |
84 class CONTENT_EXPORT Delegate { | |
85 public: | |
86 // TODO(skerner): This will be removed in favor of the |source|-only | |
87 // version below. Leaving this for now to make the initial code review | |
88 // easy to read. | |
89 virtual void OnURLFetchComplete(const URLFetcher* source, | |
90 const GURL& url, | |
91 const net::URLRequestStatus& status, | |
92 int response_code, | |
93 const net::ResponseCookies& cookies, | |
94 const std::string& data); | |
95 | |
96 // This will be called when the URL has been fetched, successfully or not. | |
97 // Use accessor methods on |source| to get the results. | |
98 virtual void OnURLFetchComplete(const URLFetcher* source); | |
99 | |
100 protected: | |
101 virtual ~Delegate() {} | |
102 }; | |
103 | |
104 // URLFetcher::Create uses the currently registered Factory to create the | 88 // URLFetcher::Create uses the currently registered Factory to create the |
105 // URLFetcher. Factory is intended for testing. | 89 // URLFetcher. Factory is intended for testing. |
106 class Factory { | 90 class Factory { |
107 public: | 91 public: |
108 virtual URLFetcher* CreateURLFetcher(int id, | 92 virtual URLFetcher* CreateURLFetcher(int id, |
109 const GURL& url, | 93 const GURL& url, |
110 RequestType request_type, | 94 RequestType request_type, |
111 Delegate* d) = 0; | 95 content::URLFetcherDelegate* d) = 0; |
112 | 96 |
113 protected: | 97 protected: |
114 virtual ~Factory() {} | 98 virtual ~Factory() {} |
115 }; | 99 }; |
116 | 100 |
117 // |url| is the URL to send the request to. | 101 // |url| is the URL to send the request to. |
118 // |request_type| is the type of request to make. | 102 // |request_type| is the type of request to make. |
119 // |d| the object that will receive the callback on fetch completion. | 103 // |d| the object that will receive the callback on fetch completion. |
120 URLFetcher(const GURL& url, RequestType request_type, Delegate* d); | 104 URLFetcher(const GURL& url, |
105 RequestType request_type, | |
106 content::URLFetcherDelegate* d); | |
121 virtual ~URLFetcher(); | 107 virtual ~URLFetcher(); |
122 | 108 |
123 // Normally interception is disabled for URLFetcher, but you can use this | 109 // Normally interception is disabled for URLFetcher, but you can use this |
124 // to enable it for tests. Also see ScopedURLFetcherFactory for another way | 110 // to enable it for tests. Also see ScopedURLFetcherFactory for another way |
125 // of testing code that uses an URLFetcher. | 111 // of testing code that uses an URLFetcher. |
126 static void enable_interception_for_tests(bool enabled) { | 112 static void enable_interception_for_tests(bool enabled) { |
127 g_interception_enabled = enabled; | 113 g_interception_enabled = enabled; |
128 } | 114 } |
129 | 115 |
130 // Creates a URLFetcher, ownership returns to the caller. If there is no | 116 // Creates a URLFetcher, ownership returns to the caller. If there is no |
131 // Factory (the default) this creates and returns a new URLFetcher. See the | 117 // Factory (the default) this creates and returns a new URLFetcher. See the |
132 // constructor for a description of the args. |id| may be used during testing | 118 // constructor for a description of the args. |id| may be used during testing |
133 // to identify who is creating the URLFetcher. | 119 // to identify who is creating the URLFetcher. |
134 static URLFetcher* Create(int id, const GURL& url, RequestType request_type, | 120 static URLFetcher* Create(int id, const GURL& url, RequestType request_type, |
135 Delegate* d); | 121 content::URLFetcherDelegate* d); |
136 | 122 |
137 // Sets data only needed by POSTs. All callers making POST requests should | 123 // Sets data only needed by POSTs. All callers making POST requests should |
138 // call this before the request is started. |upload_content_type| is the MIME | 124 // call this before the request is started. |upload_content_type| is the MIME |
139 // type of the content, while |upload_content| is the data to be sent (the | 125 // type of the content, while |upload_content| is the data to be sent (the |
140 // Content-Length header value will be set to the length of this data). | 126 // Content-Length header value will be set to the length of this data). |
141 void set_upload_data(const std::string& upload_content_type, | 127 void set_upload_data(const std::string& upload_content_type, |
142 const std::string& upload_content); | 128 const std::string& upload_content); |
143 | 129 |
144 // Indicates that the POST data is sent via chunked transfer encoding. | 130 // Indicates that the POST data is sent via chunked transfer encoding. |
145 // This may only be called before calling Start(). | 131 // This may only be called before calling Start(). |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 // SaveResponseToTemporaryFile(). | 225 // SaveResponseToTemporaryFile(). |
240 virtual bool FileErrorOccurred(base::PlatformFileError* out_error_code) const; | 226 virtual bool FileErrorOccurred(base::PlatformFileError* out_error_code) const; |
241 | 227 |
242 // Reports that the received content was malformed. | 228 // Reports that the received content was malformed. |
243 void ReceivedContentWasMalformed(); | 229 void ReceivedContentWasMalformed(); |
244 | 230 |
245 // Get the response as a string. Return false if the fetcher was not | 231 // Get the response as a string. Return false if the fetcher was not |
246 // set to store the response as a string. | 232 // set to store the response as a string. |
247 virtual bool GetResponseAsString(std::string* out_response_string) const; | 233 virtual bool GetResponseAsString(std::string* out_response_string) const; |
248 | 234 |
249 // Return a const reference to the string data fetched. Response type | |
250 // must be STRING, or this will CHECK. | |
251 virtual const std::string& GetResponseStringRef() const; | |
Sam Kerner (Chrome)
2011/10/24 18:36:25
Does GetResponseAsString() cause a copy of the str
willchan no longer on Chromium
2011/10/24 18:42:12
I don't think using URLFetcher for fetching large
| |
252 | |
253 // Get the path to the file containing the response body. Returns false | 235 // Get the path to the file containing the response body. Returns false |
254 // if the response body was not saved to a file. If take_ownership is | 236 // if the response body was not saved to a file. If take_ownership is |
255 // true, caller takes responsibility for the temp file, and it will not | 237 // true, caller takes responsibility for the temp file, and it will not |
256 // be removed once the URLFetcher is destroyed. User should not take | 238 // be removed once the URLFetcher is destroyed. User should not take |
257 // ownership more than once, or call this method after taking ownership. | 239 // ownership more than once, or call this method after taking ownership. |
258 virtual bool GetResponseAsFilePath(bool take_ownership, | 240 virtual bool GetResponseAsFilePath(bool take_ownership, |
259 FilePath* out_response_path) const; | 241 FilePath* out_response_path) const; |
260 | 242 |
261 // Cancels all existing URLFetchers. Will notify the URLFetcher::Delegates. | 243 // Cancels all existing URLFetchers. Will notify the URLFetcherDelegates. |
262 // Note that any new URLFetchers created while this is running will not be | 244 // Note that any new URLFetchers created while this is running will not be |
263 // cancelled. Typically, one would call this in the CleanUp() method of an IO | 245 // cancelled. Typically, one would call this in the CleanUp() method of an IO |
264 // thread, so that no new URLRequests would be able to start on the IO thread | 246 // thread, so that no new URLRequests would be able to start on the IO thread |
265 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO | 247 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO |
266 // thread though, even though the task won't ever run. | 248 // thread though, even though the task won't ever run. |
267 static void CancelAll(); | 249 static void CancelAll(); |
268 | 250 |
269 protected: | 251 protected: |
270 // How should the response be stored? | 252 // How should the response be stored? |
271 enum ResponseDestinationType { | 253 enum ResponseDestinationType { |
272 STRING, // Default: In a std::string | 254 STRING, // Default: In a std::string |
273 TEMP_FILE // Write to a temp file | 255 TEMP_FILE // Write to a temp file |
274 }; | 256 }; |
275 | 257 |
276 // Returns the delegate. | 258 // Returns the delegate. |
277 Delegate* delegate() const; | 259 content::URLFetcherDelegate* delegate() const; |
278 | 260 |
279 // Used by tests. | 261 // Used by tests. |
280 const std::string& upload_data() const; | 262 const std::string& upload_data() const; |
281 | 263 |
282 // Used by tests. | 264 // Used by tests. |
283 void set_was_fetched_via_proxy(bool flag); | 265 void set_was_fetched_via_proxy(bool flag); |
284 | 266 |
285 // Used by tests. | 267 // Used by tests. |
286 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); | 268 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); |
287 | 269 |
(...skipping 24 matching lines...) Expand all Loading... | |
312 scoped_refptr<Core> core_; | 294 scoped_refptr<Core> core_; |
313 | 295 |
314 static Factory* factory_; | 296 static Factory* factory_; |
315 | 297 |
316 static bool g_interception_enabled; | 298 static bool g_interception_enabled; |
317 | 299 |
318 DISALLOW_COPY_AND_ASSIGN(URLFetcher); | 300 DISALLOW_COPY_AND_ASSIGN(URLFetcher); |
319 }; | 301 }; |
320 | 302 |
321 #endif // CONTENT_COMMON_NET_URL_FETCHER_H_ | 303 #endif // CONTENT_COMMON_NET_URL_FETCHER_H_ |
OLD | NEW |