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

Side by Side Diff: content/public/common/url_fetcher.h

Issue 8375039: Create a content::UrlFetcher interface that lives in content/public/common and convert users to i... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync Created 9 years, 1 month 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_PUBLIC_COMMON_URL_FETCHER_H_
6 #define CONTENT_PUBLIC_COMMON_URL_FETCHER_H_
7 #pragma once
8
9 #include <string>
10 #include <vector>
11
12 #include "base/platform_file.h"
13 #include "content/common/content_export.h"
14
15 class FilePath;
16 class GURL;
17
18 namespace base {
19 class MessageLoopProxy;
20 class TimeDelta;
21 }
22
23 namespace net {
24 class HostPortPair;
25 class HttpRequestHeaders;
26 class HttpResponseHeaders;
27 class URLRequestContextGetter;
28 class URLRequestStatus;
29 typedef std::vector<std::string> ResponseCookies;
30 }
31
32 namespace content {
33
34 class URLFetcherDelegate;
35
36 // To use this class, create an instance with the desired URL and a pointer to
37 // the object to be notified when the URL has been loaded:
38 // URLFetcher* fetcher = URLFetcher::create("http://www.google.com",
willchan no longer on Chromium 2011/10/25 16:59:49 URLFetcher::Create
jam 2011/10/25 17:16:20 Done.
39 // URLFetcher::GET, this);
40 //
41 // Then, optionally set properties on this object, like the request context or
42 // extra headers:
43 // fetcher->set_extra_request_headers("X-Foo: bar");
44 //
45 // Finally, start the request:
46 // fetcher->Start();
47 //
48 //
49 // The object you supply as a delegate must inherit from
50 // URLFetcherDelegate; when the fetch is completed,
51 // OnURLFetchComplete() will be called with a pointer to the URLFetcher. From
52 // that point until the original URLFetcher instance is destroyed, you may use
53 // accessor methods to see the result of the fetch. You should copy these
54 // objects if you need them to live longer than the URLFetcher instance. If the
55 // URLFetcher instance is destroyed before the callback happens, the fetch will
56 // be canceled and no callback will occur.
57 //
58 // You may create the URLFetcher instance on any thread; OnURLFetchComplete()
59 // will be called back on the same thread you use to create the instance.
60 //
61 //
62 // NOTE: By default URLFetcher requests are NOT intercepted, except when
63 // interception is explicitly enabled in tests.
64 class CONTENT_EXPORT URLFetcher {
65 public:
66 // Imposible http response code. Used to signal that no http response code
67 // was received.
68 static const int kInvalidHttpResponseCode;
willchan no longer on Chromium 2011/10/25 16:59:49 I don't think this actually needs to be public. I
jam 2011/10/25 17:16:20 it's referenced below in GetResponseCode
willchan no longer on Chromium 2011/10/25 17:22:58 Oh, good point. I just did a grep and no one's che
69
70 enum RequestType {
71 GET,
72 POST,
73 HEAD,
74 };
75
76 // |url| is the URL to send the request to.
77 // |request_type| is the type of request to make.
78 // |d| the object that will receive the callback on fetch completion.
79 static URLFetcher* Create(const GURL& url,
80 RequestType request_type,
81 content::URLFetcherDelegate* d);
willchan no longer on Chromium 2011/10/25 16:59:49 no need for content::
jam 2011/10/25 17:16:20 Done.
82
83 // Cancels all existing URLFetchers. Will notify the URLFetcherDelegates.
84 // Note that any new URLFetchers created while this is running will not be
85 // cancelled. Typically, one would call this in the CleanUp() method of an IO
86 // thread, so that no new URLRequests would be able to start on the IO thread
87 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO
88 // thread though, even though the task won't ever run.
89 static void CancelAll();
90
91 // Normally interception is disabled for URLFetcher, but you can use this
92 // to enable it for tests. Also see ScopedURLFetcherFactory for another way
93 // of testing code that uses an URLFetcher.
94 static void SetEnableInterceptionForTests(bool enabled);
95
96 virtual ~URLFetcher() {}
97
98 // Sets data only needed by POSTs. All callers making POST requests should
99 // call this before the request is started. |upload_content_type| is the MIME
100 // type of the content, while |upload_content| is the data to be sent (the
101 // Content-Length header value will be set to the length of this data).
102 virtual void SetUploadData(const std::string& upload_content_type,
103 const std::string& upload_content) = 0;
104
105 // Indicates that the POST data is sent via chunked transfer encoding.
106 // This may only be called before calling Start().
107 // Use AppendChunkToUpload() to give the data chunks after calling Start().
108 virtual void SetChunkedUpload(const std::string& upload_content_type) = 0;
109
110 // Adds the given bytes to a request's POST data transmitted using chunked
111 // transfer encoding.
112 // This method should be called ONLY after calling Start().
113 virtual void AppendChunkToUpload(const std::string& data,
114 bool is_last_chunk) = 0;
115
116 // Set one or more load flags as defined in net/base/load_flags.h. Must be
117 // called before the request is started.
118 virtual void SetLoadFlags(int load_flags) = 0;
119
120 // Returns the current load flags.
121 virtual int GetLoadFlags() const = 0;
122
123 // The referrer URL for the request. Must be called before the request is
124 // started.
125 virtual void SetReferrer(const std::string& referrer) = 0;
126
127 // Set extra headers on the request. Must be called before the request
128 // is started.
129 virtual void SetExtraRequestHeaders(
130 const std::string& extra_request_headers) = 0;
131
132 virtual void GetExtraRequestHeaders(net::HttpRequestHeaders* headers) = 0;
133
134 // Set the net::URLRequestContext on the request. Must be called before the
135 // request is started.
136 virtual void SetRequestContext(
137 net::URLRequestContextGetter* request_context_getter) = 0;
138
139 // If |retry| is false, 5xx responses will be propagated to the observer,
140 // if it is true URLFetcher will automatically re-execute the request,
141 // after backoff_delay() elapses. URLFetcher has it set to true by default.
142 virtual void SetAutomaticallyRetryOn5xx(bool retry) = 0;
143
144 virtual void SetMaxRetries(int max_retries) = 0;
145 virtual int GetMaxRetries() const = 0;
146
147 // Returns the back-off delay before the request will be retried,
148 // when a 5xx response was received.
149 virtual base::TimeDelta GetBackoffDelay() const = 0;
150
151 // Sets the back-off delay, allowing to mock 5xx requests in unit-tests.
152 virtual void SetBackoffDelayForTesting(base::TimeDelta backoff_delay) = 0;
153
154 // By default, the response is saved in a string. Call this method to save the
155 // response to a temporary file instead. Must be called before Start().
156 // |file_message_loop_proxy| will be used for all file operations.
157 virtual void SaveResponseToTemporaryFile(
158 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) = 0;
159
160 // Retrieve the response headers from the request. Must only be called after
161 // the OnURLFetchComplete callback has run.
162 virtual net::HttpResponseHeaders* GetResponseHeaders() const = 0;
163
164 // Retrieve the remote socket address from the request. Must only
165 // be called after the OnURLFetchComplete callback has run and if
166 // the request has not failed.
167 virtual net::HostPortPair GetSocketAddress() const = 0;
168
169 // Returns true if the request was delivered through a proxy. Must only
170 // be called after the OnURLFetchComplete callback has run and the request
171 // has not failed.
172 virtual bool WasFetchedViaProxy() const = 0;
173
174 // Start the request. After this is called, you may not change any other
175 // settings.
176 virtual void Start() = 0;
177
178 // Restarts the URLFetcher with a new URLRequestContextGetter.
179 virtual void StartWithRequestContextGetter(
180 net::URLRequestContextGetter* request_context_getter) = 0;
181
182 // Return the URL that we were asked to fetch.
183 virtual const GURL& GetOriginalUrl() const = 0;
184
185 // Return the URL that this fetcher is processing.
186 virtual const GURL& GetUrl() const = 0;
187
188 // The status of the URL fetch.
189 virtual const net::URLRequestStatus& GetStatus() const = 0;
190
191 // The http response code received. Will return
192 // URLFetcher::kInvalidHttpResponseCode if an error prevented any response
193 // from being received.
194 virtual int GetResponseCode() const = 0;
195
196 // Cookies recieved.
197 virtual const net::ResponseCookies& GetCookies() const = 0;
198
199 // Return true if any file system operation failed. If so, set |error_code|
200 // to the error code. File system errors are only possible if user called
201 // SaveResponseToTemporaryFile().
202 virtual bool FileErrorOccurred(
203 base::PlatformFileError* out_error_code) const = 0;
204
205 // Reports that the received content was malformed.
206 virtual void ReceivedContentWasMalformed() = 0;
207
208 // Get the response as a string. Return false if the fetcher was not
209 // set to store the response as a string.
210 virtual bool GetResponseAsString(std::string* out_response_string) const = 0;
211
212 // Get the path to the file containing the response body. Returns false
213 // if the response body was not saved to a file. If take_ownership is
214 // true, caller takes responsibility for the temp file, and it will not
215 // be removed once the URLFetcher is destroyed. User should not take
216 // ownership more than once, or call this method after taking ownership.
217 virtual bool GetResponseAsFilePath(bool take_ownership,
218 FilePath* out_response_path) const = 0;
219 };
220
221 } // namespace content
222
223 #endif // CONTENT_PUBLIC_COMMON_URL_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698