| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "net/proxy/proxy_script_fetcher_impl.h" | 5 #include "net/proxy/proxy_script_fetcher_impl.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/i18n/icu_string_conversions.h" | 8 #include "base/i18n/icu_string_conversions.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 max_response_bytes_(kDefaultMaxResponseBytes), | 83 max_response_bytes_(kDefaultMaxResponseBytes), |
| 84 max_duration_(base::TimeDelta::FromMilliseconds(kDefaultMaxDurationMs)) { | 84 max_duration_(base::TimeDelta::FromMilliseconds(kDefaultMaxDurationMs)) { |
| 85 DCHECK(url_request_context); | 85 DCHECK(url_request_context); |
| 86 } | 86 } |
| 87 | 87 |
| 88 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() { | 88 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() { |
| 89 // The URLRequest's destructor will cancel the outstanding request, and | 89 // The URLRequest's destructor will cancel the outstanding request, and |
| 90 // ensure that the delegate (this) is not called again. | 90 // ensure that the delegate (this) is not called again. |
| 91 } | 91 } |
| 92 | 92 |
| 93 base::TimeDelta ProxyScriptFetcherImpl::SetTimeoutConstraint( |
| 94 base::TimeDelta timeout) { |
| 95 base::TimeDelta prev = max_duration_; |
| 96 max_duration_ = timeout; |
| 97 return prev; |
| 98 } |
| 99 |
| 100 size_t ProxyScriptFetcherImpl::SetSizeConstraint(size_t size_bytes) { |
| 101 size_t prev = max_response_bytes_; |
| 102 max_response_bytes_ = size_bytes; |
| 103 return prev; |
| 104 } |
| 105 |
| 106 void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request) { |
| 107 DCHECK_EQ(request, cur_request_.get()); |
| 108 |
| 109 // Use |result_code_| as the request's error if we have already set it to |
| 110 // something specific. |
| 111 if (result_code_ == OK && !request->status().is_success()) |
| 112 result_code_ = request->status().os_error(); |
| 113 |
| 114 FetchCompleted(); |
| 115 } |
| 116 |
| 93 int ProxyScriptFetcherImpl::Fetch(const GURL& url, | 117 int ProxyScriptFetcherImpl::Fetch(const GURL& url, |
| 94 string16* text, | 118 string16* text, |
| 95 CompletionCallback* callback) { | 119 CompletionCallback* callback) { |
| 96 // It is invalid to call Fetch() while a request is already in progress. | 120 // It is invalid to call Fetch() while a request is already in progress. |
| 97 DCHECK(!cur_request_.get()); | 121 DCHECK(!cur_request_.get()); |
| 98 | 122 |
| 99 DCHECK(callback); | 123 DCHECK(callback); |
| 100 DCHECK(text); | 124 DCHECK(text); |
| 101 | 125 |
| 102 cur_request_.reset(new URLRequest(url, this)); | 126 cur_request_.reset(new URLRequest(url, this)); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 217 |
| 194 void ProxyScriptFetcherImpl::OnReadCompleted(URLRequest* request, | 218 void ProxyScriptFetcherImpl::OnReadCompleted(URLRequest* request, |
| 195 int num_bytes) { | 219 int num_bytes) { |
| 196 DCHECK_EQ(request, cur_request_.get()); | 220 DCHECK_EQ(request, cur_request_.get()); |
| 197 if (ConsumeBytesRead(request, num_bytes)) { | 221 if (ConsumeBytesRead(request, num_bytes)) { |
| 198 // Keep reading. | 222 // Keep reading. |
| 199 ReadBody(request); | 223 ReadBody(request); |
| 200 } | 224 } |
| 201 } | 225 } |
| 202 | 226 |
| 203 void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request) { | |
| 204 DCHECK_EQ(request, cur_request_.get()); | |
| 205 | |
| 206 // Use |result_code_| as the request's error if we have already set it to | |
| 207 // something specific. | |
| 208 if (result_code_ == OK && !request->status().is_success()) | |
| 209 result_code_ = request->status().os_error(); | |
| 210 | |
| 211 FetchCompleted(); | |
| 212 } | |
| 213 | |
| 214 void ProxyScriptFetcherImpl::ReadBody(URLRequest* request) { | 227 void ProxyScriptFetcherImpl::ReadBody(URLRequest* request) { |
| 215 // Read as many bytes as are available synchronously. | 228 // Read as many bytes as are available synchronously. |
| 216 while (true) { | 229 while (true) { |
| 217 int num_bytes; | 230 int num_bytes; |
| 218 if (!request->Read(buf_, kBufSize, &num_bytes)) { | 231 if (!request->Read(buf_, kBufSize, &num_bytes)) { |
| 219 // Check whether the read failed synchronously. | 232 // Check whether the read failed synchronously. |
| 220 if (!request->status().is_io_pending()) | 233 if (!request->status().is_io_pending()) |
| 221 OnResponseCompleted(request); | 234 OnResponseCompleted(request); |
| 222 return; | 235 return; |
| 223 } | 236 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 // Timeout tasks may outlive the URLRequest they reference. Make sure it | 293 // Timeout tasks may outlive the URLRequest they reference. Make sure it |
| 281 // is still applicable. | 294 // is still applicable. |
| 282 if (cur_request_id_ != id) | 295 if (cur_request_id_ != id) |
| 283 return; | 296 return; |
| 284 | 297 |
| 285 DCHECK(cur_request_.get()); | 298 DCHECK(cur_request_.get()); |
| 286 result_code_ = ERR_TIMED_OUT; | 299 result_code_ = ERR_TIMED_OUT; |
| 287 cur_request_->Cancel(); | 300 cur_request_->Cancel(); |
| 288 } | 301 } |
| 289 | 302 |
| 290 base::TimeDelta ProxyScriptFetcherImpl::SetTimeoutConstraint( | |
| 291 base::TimeDelta timeout) { | |
| 292 base::TimeDelta prev = max_duration_; | |
| 293 max_duration_ = timeout; | |
| 294 return prev; | |
| 295 } | |
| 296 | |
| 297 size_t ProxyScriptFetcherImpl::SetSizeConstraint(size_t size_bytes) { | |
| 298 size_t prev = max_response_bytes_; | |
| 299 max_response_bytes_ = size_bytes; | |
| 300 return prev; | |
| 301 } | |
| 302 | |
| 303 } // namespace net | 303 } // namespace net |
| OLD | NEW |