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 #include "chrome/browser/net/url_fetcher.h" | 5 #include "chrome/browser/net/url_fetcher.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/thread.h" | 9 #include "base/thread.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/chrome_thread.h" | 11 #include "chrome/browser/chrome_thread.h" |
12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/http/http_response_headers.h" |
| 16 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" |
15 | 18 |
16 static const int kBufferSize = 4096; | 19 static const int kBufferSize = 4096; |
17 | 20 |
| 21 class URLFetcher::Core |
| 22 : public base::RefCountedThreadSafe<URLFetcher::Core>, |
| 23 public URLRequest::Delegate { |
| 24 public: |
| 25 // For POST requests, set |content_type| to the MIME type of the content |
| 26 // and set |content| to the data to upload. |flags| are flags to apply to |
| 27 // the load operation--these should be one or more of the LOAD_* flags |
| 28 // defined in url_request.h. |
| 29 Core(URLFetcher* fetcher, |
| 30 const GURL& original_url, |
| 31 RequestType request_type, |
| 32 URLFetcher::Delegate* d); |
| 33 |
| 34 // Starts the load. It's important that this not happen in the constructor |
| 35 // because it causes the IO thread to begin AddRef()ing and Release()ing |
| 36 // us. If our caller hasn't had time to fully construct us and take a |
| 37 // reference, the IO thread could interrupt things, run a task, Release() |
| 38 // us, and destroy us, leaving the caller with an already-destroyed object |
| 39 // when construction finishes. |
| 40 void Start(); |
| 41 |
| 42 // Stops any in-progress load and ensures no callback will happen. It is |
| 43 // safe to call this multiple times. |
| 44 void Stop(); |
| 45 |
| 46 // URLRequest::Delegate implementations |
| 47 virtual void OnReceivedRedirect(URLRequest* request, |
| 48 const GURL& new_url) { } |
| 49 virtual void OnResponseStarted(URLRequest* request); |
| 50 virtual void OnReadCompleted(URLRequest* request, int bytes_read); |
| 51 |
| 52 private: |
| 53 // Wrapper functions that allow us to ensure actions happen on the right |
| 54 // thread. |
| 55 void StartURLRequest(); |
| 56 void CancelURLRequest(); |
| 57 void OnCompletedURLRequest(const URLRequestStatus& status); |
| 58 |
| 59 URLFetcher* fetcher_; // Corresponding fetcher object |
| 60 GURL original_url_; // The URL we were asked to fetch |
| 61 GURL url_; // The URL we eventually wound up at |
| 62 RequestType request_type_; // What type of request is this? |
| 63 URLFetcher::Delegate* delegate_; // Object to notify on completion |
| 64 MessageLoop* delegate_loop_; // Message loop of the creating thread |
| 65 MessageLoop* io_loop_; // Message loop of the IO thread |
| 66 URLRequest* request_; // The actual request this wraps |
| 67 int load_flags_; // Flags for the load operation |
| 68 int response_code_; // HTTP status code for the request |
| 69 std::string data_; // Results of the request |
| 70 scoped_refptr<net::IOBuffer> buffer_; |
| 71 // Read buffer |
| 72 scoped_refptr<URLRequestContext> request_context_; |
| 73 // Cookie/cache info for the request |
| 74 ResponseCookies cookies_; // Response cookies |
| 75 std::string extra_request_headers_;// Extra headers for the request, if any |
| 76 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
| 77 |
| 78 std::string upload_content_; // HTTP POST payload |
| 79 std::string upload_content_type_; // MIME type of POST payload |
| 80 |
| 81 // The overload protection entry for this URL. This is used to |
| 82 // incrementally back off how rapidly we'll send requests to a particular |
| 83 // URL, to avoid placing too much demand on the remote resource. We update |
| 84 // this with the status of all requests as they return, and in turn use it |
| 85 // to determine how long to wait before making another request. |
| 86 URLFetcherProtectEntry* protect_entry_; |
| 87 // |num_retries_| indicates how many times we've failed to successfully |
| 88 // fetch this URL. Once this value exceeds the maximum number of retries |
| 89 // specified by the protection manager, we'll give up. |
| 90 int num_retries_; |
| 91 |
| 92 friend class URLFetcher; |
| 93 DISALLOW_COPY_AND_ASSIGN(Core); |
| 94 }; |
| 95 |
18 URLFetcher::URLFetcher(const GURL& url, | 96 URLFetcher::URLFetcher(const GURL& url, |
19 RequestType request_type, | 97 RequestType request_type, |
20 Delegate* d) | 98 Delegate* d) |
21 : ALLOW_THIS_IN_INITIALIZER_LIST( | 99 : ALLOW_THIS_IN_INITIALIZER_LIST( |
22 core_(new Core(this, url, request_type, d))) { | 100 core_(new Core(this, url, request_type, d))) { |
23 } | 101 } |
24 | 102 |
25 URLFetcher::~URLFetcher() { | 103 URLFetcher::~URLFetcher() { |
26 core_->Stop(); | 104 core_->Stop(); |
27 } | 105 } |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 cookies_, data_); | 255 cookies_, data_); |
178 } | 256 } |
179 } | 257 } |
180 } else { | 258 } else { |
181 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SUCCESS); | 259 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SUCCESS); |
182 if (delegate_) | 260 if (delegate_) |
183 delegate_->OnURLFetchComplete(fetcher_, url_, status, response_code_, | 261 delegate_->OnURLFetchComplete(fetcher_, url_, status, response_code_, |
184 cookies_, data_); | 262 cookies_, data_); |
185 } | 263 } |
186 } | 264 } |
| 265 |
| 266 void URLFetcher::set_io_loop(MessageLoop* io_loop) { |
| 267 core_->io_loop_ = io_loop; |
| 268 } |
| 269 |
| 270 void URLFetcher::set_upload_data(const std::string& upload_content_type, |
| 271 const std::string& upload_content) { |
| 272 core_->upload_content_type_ = upload_content_type; |
| 273 core_->upload_content_ = upload_content; |
| 274 } |
| 275 |
| 276 void URLFetcher::set_load_flags(int load_flags) { |
| 277 core_->load_flags_ = load_flags; |
| 278 } |
| 279 |
| 280 void URLFetcher::set_extra_request_headers( |
| 281 const std::string& extra_request_headers) { |
| 282 core_->extra_request_headers_ = extra_request_headers; |
| 283 } |
| 284 |
| 285 void URLFetcher::set_request_context(URLRequestContext* request_context) { |
| 286 core_->request_context_ = request_context; |
| 287 } |
| 288 |
| 289 net::HttpResponseHeaders* URLFetcher::response_headers() const { |
| 290 return core_->response_headers_; |
| 291 } |
| 292 |
| 293 void URLFetcher::Start() { |
| 294 core_->Start(); |
| 295 } |
| 296 |
| 297 const GURL& URLFetcher::url() const { |
| 298 return core_->url_; |
| 299 } |
OLD | NEW |