| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "net/proxy/proxy_script_fetcher.h" | 5 #include "net/proxy/proxy_script_fetcher.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/ref_counted.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "net/base/io_buffer.h" |
| 10 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 11 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 12 | 14 |
| 13 // TODO(eroman): | 15 // TODO(eroman): |
| 14 // - Support auth-prompts. | 16 // - Support auth-prompts. |
| 15 | 17 |
| 16 namespace net { | 18 namespace net { |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 72 |
| 71 // Factory for creating the time-out task. This takes care of revoking | 73 // Factory for creating the time-out task. This takes care of revoking |
| 72 // outstanding tasks when |this| is deleted. | 74 // outstanding tasks when |this| is deleted. |
| 73 ScopedRunnableMethodFactory<ProxyScriptFetcherImpl> task_factory_; | 75 ScopedRunnableMethodFactory<ProxyScriptFetcherImpl> task_factory_; |
| 74 | 76 |
| 75 // The context used for making network requests. | 77 // The context used for making network requests. |
| 76 URLRequestContext* url_request_context_; | 78 URLRequestContext* url_request_context_; |
| 77 | 79 |
| 78 // Buffer that URLRequest writes into. | 80 // Buffer that URLRequest writes into. |
| 79 enum { kBufSize = 4096 }; | 81 enum { kBufSize = 4096 }; |
| 80 char buf_[kBufSize]; | 82 scoped_refptr<net::IOBuffer> buf_; |
| 81 | 83 |
| 82 // The next ID to use for |cur_request_| (monotonically increasing). | 84 // The next ID to use for |cur_request_| (monotonically increasing). |
| 83 int next_id_; | 85 int next_id_; |
| 84 | 86 |
| 85 // The current (in progress) request, or NULL. | 87 // The current (in progress) request, or NULL. |
| 86 scoped_ptr<URLRequest> cur_request_; | 88 scoped_ptr<URLRequest> cur_request_; |
| 87 | 89 |
| 88 // State for current request (only valid when |cur_request_| is not NULL): | 90 // State for current request (only valid when |cur_request_| is not NULL): |
| 89 | 91 |
| 90 // Unique ID for the current request. | 92 // Unique ID for the current request. |
| 91 int cur_request_id_; | 93 int cur_request_id_; |
| 92 | 94 |
| 93 // Callback to invoke on completion of the fetch. | 95 // Callback to invoke on completion of the fetch. |
| 94 CompletionCallback* callback_; | 96 CompletionCallback* callback_; |
| 95 | 97 |
| 96 // Holds the error condition that was hit on the current request, or OK. | 98 // Holds the error condition that was hit on the current request, or OK. |
| 97 int result_code_; | 99 int result_code_; |
| 98 | 100 |
| 99 // Holds the bytes read so far. Will not exceed |max_response_bytes|. This | 101 // Holds the bytes read so far. Will not exceed |max_response_bytes|. This |
| 100 // buffer is owned by the owner of |callback|. | 102 // buffer is owned by the owner of |callback|. |
| 101 std::string* result_bytes_; | 103 std::string* result_bytes_; |
| 102 }; | 104 }; |
| 103 | 105 |
| 104 ProxyScriptFetcherImpl::ProxyScriptFetcherImpl( | 106 ProxyScriptFetcherImpl::ProxyScriptFetcherImpl( |
| 105 URLRequestContext* url_request_context) | 107 URLRequestContext* url_request_context) |
| 106 : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), | 108 : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), |
| 107 url_request_context_(url_request_context), | 109 url_request_context_(url_request_context), |
| 110 buf_(new net::IOBuffer(kBufSize)), |
| 108 next_id_(0), | 111 next_id_(0), |
| 109 cur_request_(NULL), | 112 cur_request_(NULL), |
| 110 cur_request_id_(0), | 113 cur_request_id_(0), |
| 111 callback_(NULL), | 114 callback_(NULL), |
| 112 result_code_(OK), | 115 result_code_(OK), |
| 113 result_bytes_(NULL) { | 116 result_bytes_(NULL) { |
| 114 DCHECK(url_request_context); | 117 DCHECK(url_request_context); |
| 115 } | 118 } |
| 116 | 119 |
| 117 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() { | 120 ProxyScriptFetcherImpl::~ProxyScriptFetcherImpl() { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 int num_bytes) { | 213 int num_bytes) { |
| 211 DCHECK(request == cur_request_.get()); | 214 DCHECK(request == cur_request_.get()); |
| 212 if (num_bytes > 0) { | 215 if (num_bytes > 0) { |
| 213 // Enforce maximum size bound. | 216 // Enforce maximum size bound. |
| 214 if (num_bytes + result_bytes_->size() > | 217 if (num_bytes + result_bytes_->size() > |
| 215 static_cast<size_t>(max_response_bytes)) { | 218 static_cast<size_t>(max_response_bytes)) { |
| 216 result_code_ = ERR_FILE_TOO_BIG; | 219 result_code_ = ERR_FILE_TOO_BIG; |
| 217 request->Cancel(); | 220 request->Cancel(); |
| 218 return; | 221 return; |
| 219 } | 222 } |
| 220 result_bytes_->append(buf_, num_bytes); | 223 result_bytes_->append(buf_->data(), num_bytes); |
| 221 ReadBody(request); | 224 ReadBody(request); |
| 222 } else { // Error while reading, or EOF | 225 } else { // Error while reading, or EOF |
| 223 OnResponseCompleted(request); | 226 OnResponseCompleted(request); |
| 224 } | 227 } |
| 225 } | 228 } |
| 226 | 229 |
| 227 void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request) { | 230 void ProxyScriptFetcherImpl::OnResponseCompleted(URLRequest* request) { |
| 228 DCHECK(request == cur_request_.get()); | 231 DCHECK(request == cur_request_.get()); |
| 229 | 232 |
| 230 // Use |result_code_| as the request's error if we have already set it to | 233 // Use |result_code_| as the request's error if we have already set it to |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 } | 295 } |
| 293 | 296 |
| 294 // static | 297 // static |
| 295 size_t ProxyScriptFetcher::SetSizeConstraintForUnittest(size_t size_bytes) { | 298 size_t ProxyScriptFetcher::SetSizeConstraintForUnittest(size_t size_bytes) { |
| 296 size_t prev = max_response_bytes; | 299 size_t prev = max_response_bytes; |
| 297 max_response_bytes = size_bytes; | 300 max_response_bytes = size_bytes; |
| 298 return prev; | 301 return prev; |
| 299 } | 302 } |
| 300 | 303 |
| 301 } // namespace net | 304 } // namespace net |
| OLD | NEW |