| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 // U+FFFD rather than failing. | 66 // U+FFFD rather than failing. |
| 67 base::CodepageToUTF16(bytes, codepage, | 67 base::CodepageToUTF16(bytes, codepage, |
| 68 base::OnStringConversionError::SUBSTITUTE, | 68 base::OnStringConversionError::SUBSTITUTE, |
| 69 utf16); | 69 utf16); |
| 70 } | 70 } |
| 71 | 71 |
| 72 } // namespace | 72 } // namespace |
| 73 | 73 |
| 74 ProxyScriptFetcherImpl::ProxyScriptFetcherImpl( | 74 ProxyScriptFetcherImpl::ProxyScriptFetcherImpl( |
| 75 URLRequestContext* url_request_context) | 75 URLRequestContext* url_request_context) |
| 76 : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), | 76 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 77 url_request_context_(url_request_context), | 77 url_request_context_(url_request_context), |
| 78 buf_(new IOBuffer(kBufSize)), | 78 buf_(new IOBuffer(kBufSize)), |
| 79 next_id_(0), | 79 next_id_(0), |
| 80 cur_request_(NULL), | 80 cur_request_(NULL), |
| 81 cur_request_id_(0), | 81 cur_request_id_(0), |
| 82 callback_(NULL), | |
| 83 result_code_(OK), | 82 result_code_(OK), |
| 84 result_text_(NULL), | 83 result_text_(NULL), |
| 85 max_response_bytes_(kDefaultMaxResponseBytes), | 84 max_response_bytes_(kDefaultMaxResponseBytes), |
| 86 max_duration_(base::TimeDelta::FromMilliseconds(kDefaultMaxDurationMs)) { | 85 max_duration_(base::TimeDelta::FromMilliseconds(kDefaultMaxDurationMs)) { |
| 87 DCHECK(url_request_context); | 86 DCHECK(url_request_context); |
| 88 } | 87 } |
| 89 | 88 |
| 90 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() { | 89 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() { |
| 91 // The URLRequest's destructor will cancel the outstanding request, and | 90 // The URLRequest's destructor will cancel the outstanding request, and |
| 92 // ensure that the delegate (this) is not called again. | 91 // ensure that the delegate (this) is not called again. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 109 DCHECK_EQ(request, cur_request_.get()); | 108 DCHECK_EQ(request, cur_request_.get()); |
| 110 | 109 |
| 111 // Use |result_code_| as the request's error if we have already set it to | 110 // Use |result_code_| as the request's error if we have already set it to |
| 112 // something specific. | 111 // something specific. |
| 113 if (result_code_ == OK && !request->status().is_success()) | 112 if (result_code_ == OK && !request->status().is_success()) |
| 114 result_code_ = request->status().error(); | 113 result_code_ = request->status().error(); |
| 115 | 114 |
| 116 FetchCompleted(); | 115 FetchCompleted(); |
| 117 } | 116 } |
| 118 | 117 |
| 119 int ProxyScriptFetcherImpl::Fetch(const GURL& url, | 118 int ProxyScriptFetcherImpl::Fetch( |
| 120 string16* text, | 119 const GURL& url, string16* text, const CompletionCallback& callback) { |
| 121 OldCompletionCallback* callback) { | |
| 122 // 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. |
| 123 DCHECK(!cur_request_.get()); | 121 DCHECK(!cur_request_.get()); |
| 124 | 122 DCHECK(!callback.is_null()); |
| 125 DCHECK(callback); | |
| 126 DCHECK(text); | 123 DCHECK(text); |
| 127 | 124 |
| 128 // Handle base-64 encoded data-urls that contain custom PAC scripts. | 125 // Handle base-64 encoded data-urls that contain custom PAC scripts. |
| 129 if (url.SchemeIs("data")) { | 126 if (url.SchemeIs("data")) { |
| 130 std::string mime_type; | 127 std::string mime_type; |
| 131 std::string charset; | 128 std::string charset; |
| 132 std::string data; | 129 std::string data; |
| 133 if (!DataURL::Parse(url, &mime_type, &charset, &data)) | 130 if (!DataURL::Parse(url, &mime_type, &charset, &data)) |
| 134 return ERR_FAILED; | 131 return ERR_FAILED; |
| 135 | 132 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 154 LOAD_DISABLE_CERT_REVOCATION_CHECKING); | 151 LOAD_DISABLE_CERT_REVOCATION_CHECKING); |
| 155 | 152 |
| 156 // Save the caller's info for notification on completion. | 153 // Save the caller's info for notification on completion. |
| 157 callback_ = callback; | 154 callback_ = callback; |
| 158 result_text_ = text; | 155 result_text_ = text; |
| 159 | 156 |
| 160 bytes_read_so_far_.clear(); | 157 bytes_read_so_far_.clear(); |
| 161 | 158 |
| 162 // Post a task to timeout this request if it takes too long. | 159 // Post a task to timeout this request if it takes too long. |
| 163 cur_request_id_ = ++next_id_; | 160 cur_request_id_ = ++next_id_; |
| 164 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 161 MessageLoop::current()->PostDelayedTask( |
| 165 task_factory_.NewRunnableMethod(&ProxyScriptFetcherImpl::OnTimeout, | 162 FROM_HERE, |
| 166 cur_request_id_), | 163 base::Bind(&ProxyScriptFetcherImpl::OnTimeout, weak_factory_.GetWeakPtr(), |
| 164 cur_request_id_), |
| 167 static_cast<int>(max_duration_.InMilliseconds())); | 165 static_cast<int>(max_duration_.InMilliseconds())); |
| 168 | 166 |
| 169 // Start the request. | 167 // Start the request. |
| 170 cur_request_->Start(); | 168 cur_request_->Start(); |
| 171 return ERR_IO_PENDING; | 169 return ERR_IO_PENDING; |
| 172 } | 170 } |
| 173 | 171 |
| 174 void ProxyScriptFetcherImpl::Cancel() { | 172 void ProxyScriptFetcherImpl::Cancel() { |
| 175 // ResetCurRequestState will free the URLRequest, which will cause | 173 // ResetCurRequestState will free the URLRequest, which will cause |
| 176 // cancellation. | 174 // cancellation. |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 // The caller expects the response to be encoded as UTF16. | 286 // The caller expects the response to be encoded as UTF16. |
| 289 std::string charset; | 287 std::string charset; |
| 290 cur_request_->GetCharset(&charset); | 288 cur_request_->GetCharset(&charset); |
| 291 ConvertResponseToUTF16(charset, bytes_read_so_far_, result_text_); | 289 ConvertResponseToUTF16(charset, bytes_read_so_far_, result_text_); |
| 292 } else { | 290 } else { |
| 293 // On error, the caller expects empty string for bytes. | 291 // On error, the caller expects empty string for bytes. |
| 294 result_text_->clear(); | 292 result_text_->clear(); |
| 295 } | 293 } |
| 296 | 294 |
| 297 int result_code = result_code_; | 295 int result_code = result_code_; |
| 298 OldCompletionCallback* callback = callback_; | 296 CompletionCallback callback = callback_; |
| 299 | 297 |
| 300 // Hold a reference to the URLRequestContext to prevent re-entrancy from | 298 // Hold a reference to the URLRequestContext to prevent re-entrancy from |
| 301 // ~URLRequestContext. | 299 // ~URLRequestContext. |
| 302 scoped_refptr<const URLRequestContext> context(cur_request_->context()); | 300 scoped_refptr<const URLRequestContext> context(cur_request_->context()); |
| 303 ResetCurRequestState(); | 301 ResetCurRequestState(); |
| 304 | 302 |
| 305 callback->Run(result_code); | 303 callback.Run(result_code); |
| 306 } | 304 } |
| 307 | 305 |
| 308 void ProxyScriptFetcherImpl::ResetCurRequestState() { | 306 void ProxyScriptFetcherImpl::ResetCurRequestState() { |
| 309 cur_request_.reset(); | 307 cur_request_.reset(); |
| 310 cur_request_id_ = 0; | 308 cur_request_id_ = 0; |
| 311 callback_ = NULL; | 309 callback_.Reset(); |
| 312 result_code_ = OK; | 310 result_code_ = OK; |
| 313 result_text_ = NULL; | 311 result_text_ = NULL; |
| 314 } | 312 } |
| 315 | 313 |
| 316 void ProxyScriptFetcherImpl::OnTimeout(int id) { | 314 void ProxyScriptFetcherImpl::OnTimeout(int id) { |
| 317 // Timeout tasks may outlive the URLRequest they reference. Make sure it | 315 // Timeout tasks may outlive the URLRequest they reference. Make sure it |
| 318 // is still applicable. | 316 // is still applicable. |
| 319 if (cur_request_id_ != id) | 317 if (cur_request_id_ != id) |
| 320 return; | 318 return; |
| 321 | 319 |
| 322 DCHECK(cur_request_.get()); | 320 DCHECK(cur_request_.get()); |
| 323 result_code_ = ERR_TIMED_OUT; | 321 result_code_ = ERR_TIMED_OUT; |
| 324 cur_request_->Cancel(); | 322 cur_request_->Cancel(); |
| 325 } | 323 } |
| 326 | 324 |
| 327 } // namespace net | 325 } // namespace net |
| OLD | NEW |