| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 URLRequest that handles | 5 // This file contains URLFetcher, a wrapper around 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 #ifndef CHROME_BROWSER_URL_FETCHER_H__ | 10 #ifndef CHROME_BROWSER_URL_FETCHER_H_ |
| 11 #define CHROME_BROWSER_URL_FETCHER_H__ | 11 #define CHROME_BROWSER_URL_FETCHER_H_ |
| 12 | 12 |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
| 15 #include "chrome/browser/net/url_fetcher_protect.h" | 15 #include "chrome/browser/net/url_fetcher_protect.h" |
| 16 #include "net/url_request/url_request.h" | |
| 17 | 16 |
| 17 class GURL; |
| 18 typedef std::vector<std::string> ResponseCookies; |
| 19 class URLFetcher; |
| 18 class URLRequestContext; | 20 class URLRequestContext; |
| 21 class URLRequestStatus; |
| 22 |
| 23 namespace net { |
| 24 class HttpResponseHeaders; |
| 25 } |
| 19 | 26 |
| 20 // To use this class, create an instance with the desired URL and a pointer to | 27 // To use this class, create an instance with the desired URL and a pointer to |
| 21 // the object to be notified when the URL has been loaded: | 28 // the object to be notified when the URL has been loaded: |
| 22 // URLFetcher* fetcher = new URLFetcher("http://www.google.com", this); | 29 // URLFetcher* fetcher = new URLFetcher("http://www.google.com", this); |
| 23 // | 30 // |
| 24 // Then, optionally set properties on this object, like the request context or | 31 // Then, optionally set properties on this object, like the request context or |
| 25 // extra headers: | 32 // extra headers: |
| 26 // fetcher->SetExtraRequestHeaders("X-Foo: bar"); | 33 // fetcher->SetExtraRequestHeaders("X-Foo: bar"); |
| 27 // | 34 // |
| 28 // Finally, start the request: | 35 // Finally, start the request: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 int response_code, | 74 int response_code, |
| 68 const ResponseCookies& cookies, | 75 const ResponseCookies& cookies, |
| 69 const std::string& data) = 0; | 76 const std::string& data) = 0; |
| 70 }; | 77 }; |
| 71 | 78 |
| 72 // |url| is the URL to send the request to. | 79 // |url| is the URL to send the request to. |
| 73 // |request_type| is the type of request to make. | 80 // |request_type| is the type of request to make. |
| 74 // |d| the object that will receive the callback on fetch completion. | 81 // |d| the object that will receive the callback on fetch completion. |
| 75 URLFetcher(const GURL& url, RequestType request_type, Delegate* d); | 82 URLFetcher(const GURL& url, RequestType request_type, Delegate* d); |
| 76 | 83 |
| 84 ~URLFetcher(); |
| 85 |
| 77 // This should only be used by unittests, where g_browser_process->io_thread() | 86 // This should only be used by unittests, where g_browser_process->io_thread() |
| 78 // does not exist and we must specify an alternate loop. Unfortunately, we | 87 // does not exist and we must specify an alternate loop. Unfortunately, we |
| 79 // can't put it under #ifdef UNIT_TEST since some callers (which themselves | 88 // can't put it under #ifdef UNIT_TEST since some callers (which themselves |
| 80 // should only be reached in unit tests) use this. See | 89 // should only be reached in unit tests) use this. See |
| 81 // chrome/browser/feeds/feed_manager.cc. | 90 // chrome/browser/feeds/feed_manager.cc. |
| 82 void set_io_loop(MessageLoop* io_loop) { | 91 void set_io_loop(MessageLoop* io_loop); |
| 83 core_->io_loop_ = io_loop; | |
| 84 } | |
| 85 | 92 |
| 86 // Sets data only needed by POSTs. All callers making POST requests should | 93 // Sets data only needed by POSTs. All callers making POST requests should |
| 87 // call this before the request is started. |upload_content_type| is the MIME | 94 // call this before the request is started. |upload_content_type| is the MIME |
| 88 // type of the content, while |upload_content| is the data to be sent (the | 95 // type of the content, while |upload_content| is the data to be sent (the |
| 89 // Content-Length header value will be set to the length of this data). | 96 // Content-Length header value will be set to the length of this data). |
| 90 void set_upload_data(const std::string& upload_content_type, | 97 void set_upload_data(const std::string& upload_content_type, |
| 91 const std::string& upload_content) { | 98 const std::string& upload_content); |
| 92 core_->upload_content_type_ = upload_content_type; | |
| 93 core_->upload_content_ = upload_content; | |
| 94 } | |
| 95 | 99 |
| 96 // Set one or more load flags as defined in net/base/load_flags.h. Must be | 100 // Set one or more load flags as defined in net/base/load_flags.h. Must be |
| 97 // called before the request is started. | 101 // called before the request is started. |
| 98 void set_load_flags(int load_flags) { | 102 void set_load_flags(int load_flags); |
| 99 core_->load_flags_ = load_flags; | |
| 100 } | |
| 101 | 103 |
| 102 // Set extra headers on the request. Must be called before the request | 104 // Set extra headers on the request. Must be called before the request |
| 103 // is started. | 105 // is started. |
| 104 void set_extra_request_headers(const std::string& extra_request_headers) { | 106 void set_extra_request_headers(const std::string& extra_request_headers); |
| 105 core_->extra_request_headers_ = extra_request_headers; | |
| 106 } | |
| 107 | 107 |
| 108 // Set the URLRequestContext on the request. Must be called before the | 108 // Set the URLRequestContext on the request. Must be called before the |
| 109 // request is started. | 109 // request is started. |
| 110 void set_request_context(URLRequestContext* request_context) { | 110 void set_request_context(URLRequestContext* request_context); |
| 111 core_->request_context_ = request_context; | |
| 112 } | |
| 113 | 111 |
| 114 // Retrieve the response headers from the request. Must only be called after | 112 // Retrieve the response headers from the request. Must only be called after |
| 115 // the OnURLFetchComplete callback has run. | 113 // the OnURLFetchComplete callback has run. |
| 116 net::HttpResponseHeaders* response_headers() const { | 114 net::HttpResponseHeaders* response_headers() const; |
| 117 return core_->response_headers_; | |
| 118 } | |
| 119 | 115 |
| 120 // Start the request. After this is called, you may not change any other | 116 // Start the request. After this is called, you may not change any other |
| 121 // settings. | 117 // settings. |
| 122 void Start() { core_->Start(); } | 118 void Start(); |
| 123 | 119 |
| 124 // Return the URL that this fetcher is processing. | 120 // Return the URL that this fetcher is processing. |
| 125 const GURL& url() const { | 121 const GURL& url() const; |
| 126 return core_->url_; | |
| 127 } | |
| 128 | |
| 129 ~URLFetcher(); | |
| 130 | 122 |
| 131 private: | 123 private: |
| 132 // This class is the real guts of URLFetcher. | 124 // This class is the real guts of URLFetcher. |
| 133 // | 125 // |
| 134 // When created, delegate_loop_ is set to the message loop of the current | 126 // When created, delegate_loop_ is set to the message loop of the current |
| 135 // thread, while io_loop_ is set to the message loop of the IO thread. These | 127 // thread, while io_loop_ is set to the message loop of the IO thread. These |
| 136 // are used to ensure that all handling of URLRequests happens on the IO | 128 // are used to ensure that all handling of URLRequests happens on the IO |
| 137 // thread (since that class is not currently threadsafe and relies on | 129 // thread (since that class is not currently threadsafe and relies on |
| 138 // underlying Microsoft APIs that we don't know to be threadsafe), while | 130 // underlying Microsoft APIs that we don't know to be threadsafe), while |
| 139 // keeping the delegate callback on the delegate's thread. | 131 // keeping the delegate callback on the delegate's thread. |
| 140 class Core : public base::RefCountedThreadSafe<URLFetcher::Core>, | 132 class Core; |
| 141 public URLRequest::Delegate { | |
| 142 public: | |
| 143 // For POST requests, set |content_type| to the MIME type of the content | |
| 144 // and set |content| to the data to upload. |flags| are flags to apply to | |
| 145 // the load operation--these should be one or more of the LOAD_* flags | |
| 146 // defined in url_request.h. | |
| 147 Core(URLFetcher* fetcher, | |
| 148 const GURL& original_url, | |
| 149 RequestType request_type, | |
| 150 URLFetcher::Delegate* d); | |
| 151 | |
| 152 // Starts the load. It's important that this not happen in the constructor | |
| 153 // because it causes the IO thread to begin AddRef()ing and Release()ing | |
| 154 // us. If our caller hasn't had time to fully construct us and take a | |
| 155 // reference, the IO thread could interrupt things, run a task, Release() | |
| 156 // us, and destroy us, leaving the caller with an already-destroyed object | |
| 157 // when construction finishes. | |
| 158 void Start(); | |
| 159 | |
| 160 // Stops any in-progress load and ensures no callback will happen. It is | |
| 161 // safe to call this multiple times. | |
| 162 void Stop(); | |
| 163 | |
| 164 // URLRequest::Delegate implementations | |
| 165 virtual void OnReceivedRedirect(URLRequest* request, | |
| 166 const GURL& new_url) { } | |
| 167 virtual void OnResponseStarted(URLRequest* request); | |
| 168 virtual void OnReadCompleted(URLRequest* request, int bytes_read); | |
| 169 | |
| 170 private: | |
| 171 // Wrapper functions that allow us to ensure actions happen on the right | |
| 172 // thread. | |
| 173 void StartURLRequest(); | |
| 174 void CancelURLRequest(); | |
| 175 void OnCompletedURLRequest(const URLRequestStatus& status); | |
| 176 | |
| 177 URLFetcher* fetcher_; // Corresponding fetcher object | |
| 178 GURL original_url_; // The URL we were asked to fetch | |
| 179 GURL url_; // The URL we eventually wound up at | |
| 180 RequestType request_type_; // What type of request is this? | |
| 181 URLFetcher::Delegate* delegate_; // Object to notify on completion | |
| 182 MessageLoop* delegate_loop_; // Message loop of the creating thread | |
| 183 MessageLoop* io_loop_; // Message loop of the IO thread | |
| 184 URLRequest* request_; // The actual request this wraps | |
| 185 int load_flags_; // Flags for the load operation | |
| 186 int response_code_; // HTTP status code for the request | |
| 187 std::string data_; // Results of the request | |
| 188 scoped_refptr<net::IOBuffer> buffer_; | |
| 189 // Read buffer | |
| 190 scoped_refptr<URLRequestContext> request_context_; | |
| 191 // Cookie/cache info for the request | |
| 192 ResponseCookies cookies_; // Response cookies | |
| 193 std::string extra_request_headers_;// Extra headers for the request, if any | |
| 194 scoped_refptr<net::HttpResponseHeaders> response_headers_; | |
| 195 | |
| 196 std::string upload_content_; // HTTP POST payload | |
| 197 std::string upload_content_type_; // MIME type of POST payload | |
| 198 | |
| 199 // The overload protection entry for this URL. This is used to | |
| 200 // incrementally back off how rapidly we'll send requests to a particular | |
| 201 // URL, to avoid placing too much demand on the remote resource. We update | |
| 202 // this with the status of all requests as they return, and in turn use it | |
| 203 // to determine how long to wait before making another request. | |
| 204 URLFetcherProtectEntry* protect_entry_; | |
| 205 // |num_retries_| indicates how many times we've failed to successfully | |
| 206 // fetch this URL. Once this value exceeds the maximum number of retries | |
| 207 // specified by the protection manager, we'll give up. | |
| 208 int num_retries_; | |
| 209 | |
| 210 friend class URLFetcher; | |
| 211 DISALLOW_EVIL_CONSTRUCTORS(Core); | |
| 212 }; | |
| 213 | 133 |
| 214 scoped_refptr<Core> core_; | 134 scoped_refptr<Core> core_; |
| 215 | 135 |
| 216 DISALLOW_EVIL_CONSTRUCTORS(URLFetcher); | 136 DISALLOW_EVIL_CONSTRUCTORS(URLFetcher); |
| 217 }; | 137 }; |
| 218 | 138 |
| 219 #endif // CHROME_BROWSER_URL_FETCHER_H__ | 139 #endif // CHROME_BROWSER_URL_FETCHER_H_ |
| 220 | |
| OLD | NEW |