Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / url_request / url_fetcher.h
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
3 // found in the LICENSE file.
4
5 #ifndef NET_URL_REQUEST_URL_FETCHER_H_
6 #define NET_URL_REQUEST_URL_FETCHER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/callback_forward.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/supports_user_data.h"
15 #include "net/base/net_export.h"
16 #include "net/url_request/url_request.h"
17
18 class GURL;
19
20 namespace base {
21 class FilePath;
22 class MessageLoopProxy;
23 class SequencedTaskRunner;
24 class TaskRunner;
25 class TimeDelta;
26 }
27
28 namespace net {
29 class HostPortPair;
30 class HttpRequestHeaders;
31 class HttpResponseHeaders;
32 class URLFetcherDelegate;
33 class URLFetcherResponseWriter;
34 class URLRequestContextGetter;
35 class URLRequestStatus;
36 typedef std::vector<std::string> ResponseCookies;
37
38 // To use this class, create an instance with the desired URL and a pointer to
39 // the object to be notified when the URL has been loaded:
40 //   URLFetcher* fetcher = URLFetcher::Create("http://www.google.com",
41 //                                            URLFetcher::GET, this);
42 //
43 // You must also set a request context getter:
44 //
45 //   fetcher->SetRequestContext(&my_request_context_getter);
46 //
47 // Then, optionally set properties on this object, like the request context or
48 // extra headers:
49 //   fetcher->set_extra_request_headers("X-Foo: bar");
50 //
51 // Finally, start the request:
52 //   fetcher->Start();
53 //
54 //
55 // The object you supply as a delegate must inherit from
56 // URLFetcherDelegate; when the fetch is completed,
57 // OnURLFetchComplete() will be called with a pointer to the URLFetcher.  From
58 // that point until the original URLFetcher instance is destroyed, you may use
59 // accessor methods to see the result of the fetch. You should copy these
60 // objects if you need them to live longer than the URLFetcher instance. If the
61 // URLFetcher instance is destroyed before the callback happens, the fetch will
62 // be canceled and no callback will occur.
63 //
64 // You may create the URLFetcher instance on any thread; OnURLFetchComplete()
65 // will be called back on the same thread you use to create the instance.
66 //
67 //
68 // NOTE: By default URLFetcher requests are NOT intercepted, except when
69 // interception is explicitly enabled in tests.
70 class NET_EXPORT URLFetcher {
71  public:
72   // Imposible http response code. Used to signal that no http response code
73   // was received.
74   enum ResponseCode {
75     RESPONSE_CODE_INVALID = -1
76   };
77
78   enum RequestType {
79     GET,
80     POST,
81     HEAD,
82     DELETE_REQUEST,   // DELETE is already taken on Windows.
83                       // <winnt.h> defines a DELETE macro.
84     PUT,
85     PATCH,
86   };
87
88   // Used by SetURLRequestUserData.  The callback should make a fresh
89   // base::SupportsUserData::Data object every time it's called.
90   typedef base::Callback<base::SupportsUserData::Data*()> CreateDataCallback;
91
92   virtual ~URLFetcher();
93
94   // |url| is the URL to send the request to.
95   // |request_type| is the type of request to make.
96   // |d| the object that will receive the callback on fetch completion.
97   static URLFetcher* Create(const GURL& url,
98                             URLFetcher::RequestType request_type,
99                             URLFetcherDelegate* d);
100
101   // Like above, but if there's a URLFetcherFactory registered with the
102   // implementation it will be used. |id| may be used during testing to identify
103   // who is creating the URLFetcher.
104   static URLFetcher* Create(int id,
105                             const GURL& url,
106                             URLFetcher::RequestType request_type,
107                             URLFetcherDelegate* d);
108
109   // Cancels all existing URLFetchers.  Will notify the URLFetcherDelegates.
110   // Note that any new URLFetchers created while this is running will not be
111   // cancelled.  Typically, one would call this in the CleanUp() method of an IO
112   // thread, so that no new URLRequests would be able to start on the IO thread
113   // anyway.  This doesn't prevent new URLFetchers from trying to post to the IO
114   // thread though, even though the task won't ever run.
115   static void CancelAll();
116
117   // Normally interception is disabled for URLFetcher, but you can use this
118   // to enable it for tests. Also see ScopedURLFetcherFactory for another way
119   // of testing code that uses an URLFetcher.
120   static void SetEnableInterceptionForTests(bool enabled);
121
122   // Normally, URLFetcher will abort loads that request SSL client certificate
123   // authentication, but this method may be used to cause URLFetchers to ignore
124   // requests for client certificates and continue anonymously. Because such
125   // behaviour affects the URLRequestContext's shared network state and socket
126   // pools, it should only be used for testing.
127   static void SetIgnoreCertificateRequests(bool ignored);
128
129   // Sets data only needed by POSTs.  All callers making POST requests should
130   // call one of the SetUpload* methods before the request is started.
131   // |upload_content_type| is the MIME type of the content, while
132   // |upload_content| is the data to be sent (the Content-Length header value
133   // will be set to the length of this data).
134   virtual void SetUploadData(const std::string& upload_content_type,
135                              const std::string& upload_content) = 0;
136
137   // Sets data only needed by POSTs.  All callers making POST requests should
138   // call one of the SetUpload* methods before the request is started.
139   // |upload_content_type| is the MIME type of the content, while
140   // |file_path| is the path to the file containing the data to be sent (the
141   // Content-Length header value will be set to the length of this file).
142   // |range_offset| and |range_length| specify the range of the part
143   // to be uploaded. To upload the whole file, (0, kuint64max) can be used.
144   // |file_task_runner| will be used for all file operations.
145   virtual void SetUploadFilePath(
146       const std::string& upload_content_type,
147       const base::FilePath& file_path,
148       uint64 range_offset,
149       uint64 range_length,
150       scoped_refptr<base::TaskRunner> file_task_runner) = 0;
151
152   // Indicates that the POST data is sent via chunked transfer encoding.
153   // This may only be called before calling Start().
154   // Use AppendChunkToUpload() to give the data chunks after calling Start().
155   virtual void SetChunkedUpload(const std::string& upload_content_type) = 0;
156
157   // Adds the given bytes to a request's POST data transmitted using chunked
158   // transfer encoding.
159   // This method should be called ONLY after calling Start().
160   virtual void AppendChunkToUpload(const std::string& data,
161                                    bool is_last_chunk) = 0;
162
163   // Set one or more load flags as defined in net/base/load_flags.h.  Must be
164   // called before the request is started.
165   virtual void SetLoadFlags(int load_flags) = 0;
166
167   // Returns the current load flags.
168   virtual int GetLoadFlags() const = 0;
169
170   // The referrer URL for the request. Must be called before the request is
171   // started.
172   virtual void SetReferrer(const std::string& referrer) = 0;
173
174   // The referrer policy to apply when updating the referrer during redirects.
175   // The referrer policy may only be changed before Start() is called.
176   virtual void SetReferrerPolicy(
177       URLRequest::ReferrerPolicy referrer_policy) = 0;
178
179   // Set extra headers on the request.  Must be called before the request
180   // is started.
181   // This replaces the entire extra request headers.
182   virtual void SetExtraRequestHeaders(
183       const std::string& extra_request_headers) = 0;
184
185   // Add header (with format field-name ":" [ field-value ]) to the request
186   // headers.  Must be called before the request is started.
187   // This appends the header to the current extra request headers.
188   virtual void AddExtraRequestHeader(const std::string& header_line) = 0;
189
190   // Set the URLRequestContext on the request.  Must be called before the
191   // request is started.
192   virtual void SetRequestContext(
193       URLRequestContextGetter* request_context_getter) = 0;
194
195   // Set the URL that should be consulted for the third-party cookie
196   // blocking policy.
197   virtual void SetFirstPartyForCookies(
198       const GURL& first_party_for_cookies) = 0;
199
200   // Set the key and data callback that is used when setting the user
201   // data on any URLRequest objects this object creates.
202   virtual void SetURLRequestUserData(
203       const void* key,
204       const CreateDataCallback& create_data_callback) = 0;
205
206   // If |stop_on_redirect| is true, 3xx responses will cause the fetch to halt
207   // immediately rather than continue through the redirect.  OnURLFetchComplete
208   // will be called, with the URLFetcher's URL set to the redirect destination,
209   // its status set to CANCELED, and its response code set to the relevant 3xx
210   // server response code.
211   virtual void SetStopOnRedirect(bool stop_on_redirect) = 0;
212
213   // If |retry| is false, 5xx responses will be propagated to the observer,
214   // if it is true URLFetcher will automatically re-execute the request,
215   // after backoff_delay() elapses. URLFetcher has it set to true by default.
216   virtual void SetAutomaticallyRetryOn5xx(bool retry) = 0;
217
218   virtual void SetMaxRetriesOn5xx(int max_retries) = 0;
219   virtual int GetMaxRetriesOn5xx() const = 0;
220
221   // Returns the back-off delay before the request will be retried,
222   // when a 5xx response was received.
223   virtual base::TimeDelta GetBackoffDelay() const = 0;
224
225   // Retries up to |max_retries| times when requests fail with
226   // ERR_NETWORK_CHANGED. If ERR_NETWORK_CHANGED is received after having
227   // retried |max_retries| times then it is propagated to the observer.
228   virtual void SetAutomaticallyRetryOnNetworkChanges(int max_retries) = 0;
229
230   // By default, the response is saved in a string. Call this method to save the
231   // response to a file instead. Must be called before Start().
232   // |file_task_runner| will be used for all file operations.
233   // To save to a temporary file, use SaveResponseToTemporaryFile().
234   // The created file is removed when the URLFetcher is deleted unless you
235   // take ownership by calling GetResponseAsFilePath().
236   virtual void SaveResponseToFileAtPath(
237       const base::FilePath& file_path,
238       scoped_refptr<base::SequencedTaskRunner> file_task_runner) = 0;
239
240   // By default, the response is saved in a string. Call this method to save the
241   // response to a temporary file instead. Must be called before Start().
242   // |file_task_runner| will be used for all file operations.
243   // The created file is removed when the URLFetcher is deleted unless you
244   // take ownership by calling GetResponseAsFilePath().
245   virtual void SaveResponseToTemporaryFile(
246       scoped_refptr<base::SequencedTaskRunner> file_task_runner) = 0;
247
248   // By default, the response is saved in a string. Call this method to use the
249   // specified writer to save the response. Must be called before Start().
250   virtual void SaveResponseWithWriter(
251       scoped_ptr<URLFetcherResponseWriter> response_writer) = 0;
252
253   // Retrieve the response headers from the request.  Must only be called after
254   // the OnURLFetchComplete callback has run.
255   virtual HttpResponseHeaders* GetResponseHeaders() const = 0;
256
257   // Retrieve the remote socket address from the request.  Must only
258   // be called after the OnURLFetchComplete callback has run and if
259   // the request has not failed.
260   virtual HostPortPair GetSocketAddress() const = 0;
261
262   // Returns true if the request was delivered through a proxy.  Must only
263   // be called after the OnURLFetchComplete callback has run and the request
264   // has not failed.
265   virtual bool WasFetchedViaProxy() const = 0;
266
267   // Start the request.  After this is called, you may not change any other
268   // settings.
269   virtual void Start() = 0;
270
271   // Return the URL that we were asked to fetch.
272   virtual const GURL& GetOriginalURL() const = 0;
273
274   // Return the URL that this fetcher is processing.
275   virtual const GURL& GetURL() const = 0;
276
277   // The status of the URL fetch.
278   virtual const URLRequestStatus& GetStatus() const = 0;
279
280   // The http response code received. Will return RESPONSE_CODE_INVALID
281   // if an error prevented any response from being received.
282   virtual int GetResponseCode() const = 0;
283
284   // Cookies recieved.
285   virtual const ResponseCookies& GetCookies() const = 0;
286
287   // Reports that the received content was malformed.
288   virtual void ReceivedContentWasMalformed() = 0;
289
290   // Get the response as a string. Return false if the fetcher was not
291   // set to store the response as a string.
292   virtual bool GetResponseAsString(std::string* out_response_string) const = 0;
293
294   // Get the path to the file containing the response body. Returns false
295   // if the response body was not saved to a file. If take_ownership is
296   // true, caller takes responsibility for the file, and it will not
297   // be removed once the URLFetcher is destroyed.  User should not take
298   // ownership more than once, or call this method after taking ownership.
299   virtual bool GetResponseAsFilePath(
300       bool take_ownership,
301       base::FilePath* out_response_path) const = 0;
302 };
303
304 }  // namespace net
305
306 #endif  // NET_URL_REQUEST_URL_FETCHER_H_