| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/net/url_request_context_getter.h" |
| 12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 13 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
| 14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 15 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 16 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 18 | 19 |
| 19 static const int kBufferSize = 4096; | 20 static const int kBufferSize = 4096; |
| 20 | 21 |
| 21 bool URLFetcher::g_interception_enabled = false; | 22 bool URLFetcher::g_interception_enabled = false; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 RequestType request_type_; // What type of request is this? | 65 RequestType request_type_; // What type of request is this? |
| 65 URLFetcher::Delegate* delegate_; // Object to notify on completion | 66 URLFetcher::Delegate* delegate_; // Object to notify on completion |
| 66 MessageLoop* delegate_loop_; // Message loop of the creating thread | 67 MessageLoop* delegate_loop_; // Message loop of the creating thread |
| 67 MessageLoop* io_loop_; // Message loop of the IO thread | 68 MessageLoop* io_loop_; // Message loop of the IO thread |
| 68 URLRequest* request_; // The actual request this wraps | 69 URLRequest* request_; // The actual request this wraps |
| 69 int load_flags_; // Flags for the load operation | 70 int load_flags_; // Flags for the load operation |
| 70 int response_code_; // HTTP status code for the request | 71 int response_code_; // HTTP status code for the request |
| 71 std::string data_; // Results of the request | 72 std::string data_; // Results of the request |
| 72 scoped_refptr<net::IOBuffer> buffer_; | 73 scoped_refptr<net::IOBuffer> buffer_; |
| 73 // Read buffer | 74 // Read buffer |
| 74 scoped_refptr<URLRequestContext> request_context_; | 75 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
| 75 // Cookie/cache info for the request | 76 // Cookie/cache info for the request |
| 76 ResponseCookies cookies_; // Response cookies | 77 ResponseCookies cookies_; // Response cookies |
| 77 std::string extra_request_headers_;// Extra headers for the request, if any | 78 std::string extra_request_headers_;// Extra headers for the request, if any |
| 78 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 79 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
| 79 | 80 |
| 80 std::string upload_content_; // HTTP POST payload | 81 std::string upload_content_; // HTTP POST payload |
| 81 std::string upload_content_type_; // MIME type of POST payload | 82 std::string upload_content_type_; // MIME type of POST payload |
| 82 | 83 |
| 83 // The overload protection entry for this URL. This is used to | 84 // The overload protection entry for this URL. This is used to |
| 84 // incrementally back off how rapidly we'll send requests to a particular | 85 // incrementally back off how rapidly we'll send requests to a particular |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 response_code_(-1), | 132 response_code_(-1), |
| 132 buffer_(new net::IOBuffer(kBufferSize)), | 133 buffer_(new net::IOBuffer(kBufferSize)), |
| 133 protect_entry_(URLFetcherProtectManager::GetInstance()->Register( | 134 protect_entry_(URLFetcherProtectManager::GetInstance()->Register( |
| 134 original_url_.host())), | 135 original_url_.host())), |
| 135 num_retries_(0) { | 136 num_retries_(0) { |
| 136 } | 137 } |
| 137 | 138 |
| 138 void URLFetcher::Core::Start() { | 139 void URLFetcher::Core::Start() { |
| 139 DCHECK(delegate_loop_); | 140 DCHECK(delegate_loop_); |
| 140 DCHECK(io_loop_); | 141 DCHECK(io_loop_); |
| 141 DCHECK(request_context_) << "We need an URLRequestContext!"; | 142 DCHECK(request_context_getter_) << "We need an URLRequestContextGetter!"; |
| 142 io_loop_->PostDelayedTask(FROM_HERE, NewRunnableMethod( | 143 io_loop_->PostDelayedTask(FROM_HERE, NewRunnableMethod( |
| 143 this, &Core::StartURLRequest), | 144 this, &Core::StartURLRequest), |
| 144 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SEND)); | 145 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SEND)); |
| 145 } | 146 } |
| 146 | 147 |
| 147 void URLFetcher::Core::Stop() { | 148 void URLFetcher::Core::Stop() { |
| 148 DCHECK_EQ(MessageLoop::current(), delegate_loop_); | 149 DCHECK_EQ(MessageLoop::current(), delegate_loop_); |
| 149 delegate_ = NULL; | 150 delegate_ = NULL; |
| 150 io_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 151 io_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 151 this, &Core::CancelURLRequest)); | 152 this, &Core::CancelURLRequest)); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 void URLFetcher::Core::StartURLRequest() { | 197 void URLFetcher::Core::StartURLRequest() { |
| 197 DCHECK(MessageLoop::current() == io_loop_); | 198 DCHECK(MessageLoop::current() == io_loop_); |
| 198 DCHECK(!request_); | 199 DCHECK(!request_); |
| 199 | 200 |
| 200 request_ = new URLRequest(original_url_, this); | 201 request_ = new URLRequest(original_url_, this); |
| 201 int flags = request_->load_flags() | load_flags_; | 202 int flags = request_->load_flags() | load_flags_; |
| 202 if (!g_interception_enabled) { | 203 if (!g_interception_enabled) { |
| 203 flags = flags | net::LOAD_DISABLE_INTERCEPT; | 204 flags = flags | net::LOAD_DISABLE_INTERCEPT; |
| 204 } | 205 } |
| 205 request_->set_load_flags(flags); | 206 request_->set_load_flags(flags); |
| 206 request_->set_context(request_context_.get()); | 207 request_->set_context(request_context_getter_->GetURLRequestContext()); |
| 207 | 208 |
| 208 switch (request_type_) { | 209 switch (request_type_) { |
| 209 case GET: | 210 case GET: |
| 210 break; | 211 break; |
| 211 | 212 |
| 212 case POST: | 213 case POST: |
| 213 DCHECK(!upload_content_.empty()); | 214 DCHECK(!upload_content_.empty()); |
| 214 DCHECK(!upload_content_type_.empty()); | 215 DCHECK(!upload_content_type_.empty()); |
| 215 | 216 |
| 216 request_->set_method("POST"); | 217 request_->set_method("POST"); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 240 DCHECK(MessageLoop::current() == io_loop_); | 241 DCHECK(MessageLoop::current() == io_loop_); |
| 241 if (request_) { | 242 if (request_) { |
| 242 request_->Cancel(); | 243 request_->Cancel(); |
| 243 delete request_; | 244 delete request_; |
| 244 request_ = NULL; | 245 request_ = NULL; |
| 245 } | 246 } |
| 246 // Release the reference to the request context. There could be multiple | 247 // Release the reference to the request context. There could be multiple |
| 247 // references to URLFetcher::Core at this point so it may take a while to | 248 // references to URLFetcher::Core at this point so it may take a while to |
| 248 // delete the object, but we cannot delay the destruction of the request | 249 // delete the object, but we cannot delay the destruction of the request |
| 249 // context. | 250 // context. |
| 250 request_context_ = NULL; | 251 request_context_getter_ = NULL; |
| 251 } | 252 } |
| 252 | 253 |
| 253 void URLFetcher::Core::OnCompletedURLRequest(const URLRequestStatus& status) { | 254 void URLFetcher::Core::OnCompletedURLRequest(const URLRequestStatus& status) { |
| 254 DCHECK(MessageLoop::current() == delegate_loop_); | 255 DCHECK(MessageLoop::current() == delegate_loop_); |
| 255 | 256 |
| 256 // Checks the response from server. | 257 // Checks the response from server. |
| 257 if (response_code_ >= 500) { | 258 if (response_code_ >= 500) { |
| 258 // When encountering a server error, we will send the request again | 259 // When encountering a server error, we will send the request again |
| 259 // after backoff time. | 260 // after backoff time. |
| 260 const int64 wait = | 261 const int64 wait = |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 | 295 |
| 295 int URLFetcher::load_flags() const { | 296 int URLFetcher::load_flags() const { |
| 296 return core_->load_flags_; | 297 return core_->load_flags_; |
| 297 } | 298 } |
| 298 | 299 |
| 299 void URLFetcher::set_extra_request_headers( | 300 void URLFetcher::set_extra_request_headers( |
| 300 const std::string& extra_request_headers) { | 301 const std::string& extra_request_headers) { |
| 301 core_->extra_request_headers_ = extra_request_headers; | 302 core_->extra_request_headers_ = extra_request_headers; |
| 302 } | 303 } |
| 303 | 304 |
| 304 void URLFetcher::set_request_context(URLRequestContext* request_context) { | 305 void URLFetcher::set_request_context( |
| 305 core_->request_context_ = request_context; | 306 URLRequestContextGetter* request_context_getter) { |
| 307 core_->request_context_getter_ = request_context_getter; |
| 306 } | 308 } |
| 307 | 309 |
| 308 net::HttpResponseHeaders* URLFetcher::response_headers() const { | 310 net::HttpResponseHeaders* URLFetcher::response_headers() const { |
| 309 return core_->response_headers_; | 311 return core_->response_headers_; |
| 310 } | 312 } |
| 311 | 313 |
| 312 void URLFetcher::Start() { | 314 void URLFetcher::Start() { |
| 313 core_->Start(); | 315 core_->Start(); |
| 314 } | 316 } |
| 315 | 317 |
| 316 const GURL& URLFetcher::url() const { | 318 const GURL& URLFetcher::url() const { |
| 317 return core_->url_; | 319 return core_->url_; |
| 318 } | 320 } |
| 319 | 321 |
| 320 URLFetcher::Delegate* URLFetcher::delegate() const { | 322 URLFetcher::Delegate* URLFetcher::delegate() const { |
| 321 return core_->delegate(); | 323 return core_->delegate(); |
| 322 } | 324 } |
| OLD | NEW |