| 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" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // incrementally back off how rapidly we'll send requests to a particular | 88 // incrementally back off how rapidly we'll send requests to a particular |
| 89 // URL, to avoid placing too much demand on the remote resource. We update | 89 // URL, to avoid placing too much demand on the remote resource. We update |
| 90 // this with the status of all requests as they return, and in turn use it | 90 // this with the status of all requests as they return, and in turn use it |
| 91 // to determine how long to wait before making another request. | 91 // to determine how long to wait before making another request. |
| 92 URLFetcherProtectEntry* protect_entry_; | 92 URLFetcherProtectEntry* protect_entry_; |
| 93 // |num_retries_| indicates how many times we've failed to successfully | 93 // |num_retries_| indicates how many times we've failed to successfully |
| 94 // fetch this URL. Once this value exceeds the maximum number of retries | 94 // fetch this URL. Once this value exceeds the maximum number of retries |
| 95 // specified by the protection manager, we'll give up. | 95 // specified by the protection manager, we'll give up. |
| 96 int num_retries_; | 96 int num_retries_; |
| 97 | 97 |
| 98 // Temporary member variable to test whether requests are being started | |
| 99 // after they have already been cancelled. | |
| 100 // TODO(eroman): Remove this after done investigating 27074. | |
| 101 bool was_cancelled_; | |
| 102 | |
| 103 friend class URLFetcher; | 98 friend class URLFetcher; |
| 104 DISALLOW_COPY_AND_ASSIGN(Core); | 99 DISALLOW_COPY_AND_ASSIGN(Core); |
| 105 }; | 100 }; |
| 106 | 101 |
| 107 // static | 102 // static |
| 108 URLFetcher::Factory* URLFetcher::factory_ = NULL; | 103 URLFetcher::Factory* URLFetcher::factory_ = NULL; |
| 109 | 104 |
| 110 URLFetcher::URLFetcher(const GURL& url, | 105 URLFetcher::URLFetcher(const GURL& url, |
| 111 RequestType request_type, | 106 RequestType request_type, |
| 112 Delegate* d) | 107 Delegate* d) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 133 original_url_(original_url), | 128 original_url_(original_url), |
| 134 request_type_(request_type), | 129 request_type_(request_type), |
| 135 delegate_(d), | 130 delegate_(d), |
| 136 delegate_loop_(MessageLoop::current()), | 131 delegate_loop_(MessageLoop::current()), |
| 137 request_(NULL), | 132 request_(NULL), |
| 138 load_flags_(net::LOAD_NORMAL), | 133 load_flags_(net::LOAD_NORMAL), |
| 139 response_code_(-1), | 134 response_code_(-1), |
| 140 buffer_(new net::IOBuffer(kBufferSize)), | 135 buffer_(new net::IOBuffer(kBufferSize)), |
| 141 protect_entry_(URLFetcherProtectManager::GetInstance()->Register( | 136 protect_entry_(URLFetcherProtectManager::GetInstance()->Register( |
| 142 original_url_.host())), | 137 original_url_.host())), |
| 143 num_retries_(0), | 138 num_retries_(0) { |
| 144 was_cancelled_(false) { | |
| 145 } | 139 } |
| 146 | 140 |
| 147 void URLFetcher::Core::Start() { | 141 void URLFetcher::Core::Start() { |
| 148 DCHECK(delegate_loop_); | 142 DCHECK(delegate_loop_); |
| 149 CHECK(request_context_getter_) << "We need an URLRequestContext!"; | 143 CHECK(request_context_getter_) << "We need an URLRequestContext!"; |
| 150 ChromeThread::PostDelayedTask( | 144 ChromeThread::PostDelayedTask( |
| 151 ChromeThread::IO, FROM_HERE, | 145 ChromeThread::IO, FROM_HERE, |
| 152 NewRunnableMethod(this, &Core::StartURLRequest), | 146 NewRunnableMethod(this, &Core::StartURLRequest), |
| 153 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SEND)); | 147 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SEND)); |
| 154 } | 148 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 if (!request_->status().is_io_pending() || (request_type_ == HEAD)) { | 192 if (!request_->status().is_io_pending() || (request_type_ == HEAD)) { |
| 199 delegate_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 193 delegate_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 200 this, &Core::OnCompletedURLRequest, request_->status())); | 194 this, &Core::OnCompletedURLRequest, request_->status())); |
| 201 delete request_; | 195 delete request_; |
| 202 request_ = NULL; | 196 request_ = NULL; |
| 203 } | 197 } |
| 204 } | 198 } |
| 205 | 199 |
| 206 void URLFetcher::Core::StartURLRequest() { | 200 void URLFetcher::Core::StartURLRequest() { |
| 207 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 201 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 208 CHECK(!was_cancelled_); | |
| 209 CHECK(request_context_getter_); | 202 CHECK(request_context_getter_); |
| 210 CHECK(!request_); | 203 DCHECK(!request_); |
| 211 | 204 |
| 212 request_ = new URLRequest(original_url_, this); | 205 request_ = new URLRequest(original_url_, this); |
| 213 int flags = request_->load_flags() | load_flags_; | 206 int flags = request_->load_flags() | load_flags_; |
| 214 if (!g_interception_enabled) { | 207 if (!g_interception_enabled) { |
| 215 flags = flags | net::LOAD_DISABLE_INTERCEPT; | 208 flags = flags | net::LOAD_DISABLE_INTERCEPT; |
| 216 } | 209 } |
| 217 request_->set_load_flags(flags); | 210 request_->set_load_flags(flags); |
| 218 request_->set_context(request_context_getter_->GetURLRequestContext()); | 211 request_->set_context(request_context_getter_->GetURLRequestContext()); |
| 219 | 212 |
| 220 switch (request_type_) { | 213 switch (request_type_) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 if (request_) { | 246 if (request_) { |
| 254 request_->Cancel(); | 247 request_->Cancel(); |
| 255 delete request_; | 248 delete request_; |
| 256 request_ = NULL; | 249 request_ = NULL; |
| 257 } | 250 } |
| 258 // Release the reference to the request context. There could be multiple | 251 // Release the reference to the request context. There could be multiple |
| 259 // references to URLFetcher::Core at this point so it may take a while to | 252 // references to URLFetcher::Core at this point so it may take a while to |
| 260 // delete the object, but we cannot delay the destruction of the request | 253 // delete the object, but we cannot delay the destruction of the request |
| 261 // context. | 254 // context. |
| 262 request_context_getter_ = NULL; | 255 request_context_getter_ = NULL; |
| 263 was_cancelled_ = true; | |
| 264 } | 256 } |
| 265 | 257 |
| 266 void URLFetcher::Core::OnCompletedURLRequest(const URLRequestStatus& status) { | 258 void URLFetcher::Core::OnCompletedURLRequest(const URLRequestStatus& status) { |
| 267 DCHECK(MessageLoop::current() == delegate_loop_); | 259 DCHECK(MessageLoop::current() == delegate_loop_); |
| 268 | 260 |
| 269 // Checks the response from server. | 261 // Checks the response from server. |
| 270 if (response_code_ >= 500) { | 262 if (response_code_ >= 500) { |
| 271 // When encountering a server error, we will send the request again | 263 // When encountering a server error, we will send the request again |
| 272 // after backoff time. | 264 // after backoff time. |
| 273 const int64 wait = | 265 const int64 wait = |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 core_->Start(); | 316 core_->Start(); |
| 325 } | 317 } |
| 326 | 318 |
| 327 const GURL& URLFetcher::url() const { | 319 const GURL& URLFetcher::url() const { |
| 328 return core_->url_; | 320 return core_->url_; |
| 329 } | 321 } |
| 330 | 322 |
| 331 URLFetcher::Delegate* URLFetcher::delegate() const { | 323 URLFetcher::Delegate* URLFetcher::delegate() const { |
| 332 return core_->delegate(); | 324 return core_->delegate(); |
| 333 } | 325 } |
| OLD | NEW |