OLD | NEW |
(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", |
| 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 enum ResponseCode { |
| 69 RESPONSE_CODE_INVALID = -1 |
| 70 }; |
| 71 |
| 72 enum RequestType { |
| 73 GET, |
| 74 POST, |
| 75 HEAD, |
| 76 }; |
| 77 |
| 78 // |url| is the URL to send the request to. |
| 79 // |request_type| is the type of request to make. |
| 80 // |d| the object that will receive the callback on fetch completion. |
| 81 static URLFetcher* Create(const GURL& url, |
| 82 RequestType request_type, |
| 83 URLFetcherDelegate* d); |
| 84 |
| 85 // Cancels all existing URLFetchers. Will notify the URLFetcherDelegates. |
| 86 // Note that any new URLFetchers created while this is running will not be |
| 87 // cancelled. Typically, one would call this in the CleanUp() method of an IO |
| 88 // thread, so that no new URLRequests would be able to start on the IO thread |
| 89 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO |
| 90 // thread though, even though the task won't ever run. |
| 91 static void CancelAll(); |
| 92 |
| 93 // Normally interception is disabled for URLFetcher, but you can use this |
| 94 // to enable it for tests. Also see ScopedURLFetcherFactory for another way |
| 95 // of testing code that uses an URLFetcher. |
| 96 static void SetEnableInterceptionForTests(bool enabled); |
| 97 |
| 98 virtual ~URLFetcher() {} |
| 99 |
| 100 // Sets data only needed by POSTs. All callers making POST requests should |
| 101 // call this before the request is started. |upload_content_type| is the MIME |
| 102 // type of the content, while |upload_content| is the data to be sent (the |
| 103 // Content-Length header value will be set to the length of this data). |
| 104 virtual void SetUploadData(const std::string& upload_content_type, |
| 105 const std::string& upload_content) = 0; |
| 106 |
| 107 // Indicates that the POST data is sent via chunked transfer encoding. |
| 108 // This may only be called before calling Start(). |
| 109 // Use AppendChunkToUpload() to give the data chunks after calling Start(). |
| 110 virtual void SetChunkedUpload(const std::string& upload_content_type) = 0; |
| 111 |
| 112 // Adds the given bytes to a request's POST data transmitted using chunked |
| 113 // transfer encoding. |
| 114 // This method should be called ONLY after calling Start(). |
| 115 virtual void AppendChunkToUpload(const std::string& data, |
| 116 bool is_last_chunk) = 0; |
| 117 |
| 118 // Set one or more load flags as defined in net/base/load_flags.h. Must be |
| 119 // called before the request is started. |
| 120 virtual void SetLoadFlags(int load_flags) = 0; |
| 121 |
| 122 // Returns the current load flags. |
| 123 virtual int GetLoadFlags() const = 0; |
| 124 |
| 125 // The referrer URL for the request. Must be called before the request is |
| 126 // started. |
| 127 virtual void SetReferrer(const std::string& referrer) = 0; |
| 128 |
| 129 // Set extra headers on the request. Must be called before the request |
| 130 // is started. |
| 131 virtual void SetExtraRequestHeaders( |
| 132 const std::string& extra_request_headers) = 0; |
| 133 |
| 134 virtual void GetExtraRequestHeaders(net::HttpRequestHeaders* headers) = 0; |
| 135 |
| 136 // Set the net::URLRequestContext on the request. Must be called before the |
| 137 // request is started. |
| 138 virtual void SetRequestContext( |
| 139 net::URLRequestContextGetter* request_context_getter) = 0; |
| 140 |
| 141 // If |retry| is false, 5xx responses will be propagated to the observer, |
| 142 // if it is true URLFetcher will automatically re-execute the request, |
| 143 // after backoff_delay() elapses. URLFetcher has it set to true by default. |
| 144 virtual void SetAutomaticallyRetryOn5xx(bool retry) = 0; |
| 145 |
| 146 virtual void SetMaxRetries(int max_retries) = 0; |
| 147 virtual int GetMaxRetries() const = 0; |
| 148 |
| 149 // Returns the back-off delay before the request will be retried, |
| 150 // when a 5xx response was received. |
| 151 virtual base::TimeDelta GetBackoffDelay() const = 0; |
| 152 |
| 153 // Sets the back-off delay, allowing to mock 5xx requests in unit-tests. |
| 154 virtual void SetBackoffDelayForTesting(base::TimeDelta backoff_delay) = 0; |
| 155 |
| 156 // By default, the response is saved in a string. Call this method to save the |
| 157 // response to a temporary file instead. Must be called before Start(). |
| 158 // |file_message_loop_proxy| will be used for all file operations. |
| 159 virtual void SaveResponseToTemporaryFile( |
| 160 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) = 0; |
| 161 |
| 162 // Retrieve the response headers from the request. Must only be called after |
| 163 // the OnURLFetchComplete callback has run. |
| 164 virtual net::HttpResponseHeaders* GetResponseHeaders() const = 0; |
| 165 |
| 166 // Retrieve the remote socket address from the request. Must only |
| 167 // be called after the OnURLFetchComplete callback has run and if |
| 168 // the request has not failed. |
| 169 virtual net::HostPortPair GetSocketAddress() const = 0; |
| 170 |
| 171 // Returns true if the request was delivered through a proxy. Must only |
| 172 // be called after the OnURLFetchComplete callback has run and the request |
| 173 // has not failed. |
| 174 virtual bool WasFetchedViaProxy() const = 0; |
| 175 |
| 176 // Start the request. After this is called, you may not change any other |
| 177 // settings. |
| 178 virtual void Start() = 0; |
| 179 |
| 180 // Restarts the URLFetcher with a new URLRequestContextGetter. |
| 181 virtual void StartWithRequestContextGetter( |
| 182 net::URLRequestContextGetter* request_context_getter) = 0; |
| 183 |
| 184 // Return the URL that we were asked to fetch. |
| 185 virtual const GURL& GetOriginalUrl() const = 0; |
| 186 |
| 187 // Return the URL that this fetcher is processing. |
| 188 virtual const GURL& GetUrl() const = 0; |
| 189 |
| 190 // The status of the URL fetch. |
| 191 virtual const net::URLRequestStatus& GetStatus() const = 0; |
| 192 |
| 193 // The http response code received. Will return RESPONSE_CODE_INVALID |
| 194 // if an error prevented any response from being received. |
| 195 virtual int GetResponseCode() const = 0; |
| 196 |
| 197 // Cookies recieved. |
| 198 virtual const net::ResponseCookies& GetCookies() const = 0; |
| 199 |
| 200 // Return true if any file system operation failed. If so, set |error_code| |
| 201 // to the error code. File system errors are only possible if user called |
| 202 // SaveResponseToTemporaryFile(). |
| 203 virtual bool FileErrorOccurred( |
| 204 base::PlatformFileError* out_error_code) const = 0; |
| 205 |
| 206 // Reports that the received content was malformed. |
| 207 virtual void ReceivedContentWasMalformed() = 0; |
| 208 |
| 209 // Get the response as a string. Return false if the fetcher was not |
| 210 // set to store the response as a string. |
| 211 virtual bool GetResponseAsString(std::string* out_response_string) const = 0; |
| 212 |
| 213 // Get the path to the file containing the response body. Returns false |
| 214 // if the response body was not saved to a file. If take_ownership is |
| 215 // true, caller takes responsibility for the temp file, and it will not |
| 216 // be removed once the URLFetcher is destroyed. User should not take |
| 217 // ownership more than once, or call this method after taking ownership. |
| 218 virtual bool GetResponseAsFilePath(bool take_ownership, |
| 219 FilePath* out_response_path) const = 0; |
| 220 }; |
| 221 |
| 222 } // namespace content |
| 223 |
| 224 #endif // CONTENT_PUBLIC_COMMON_URL_FETCHER_H_ |
OLD | NEW |