| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // TODO(ukai): code is similar with http_network_transaction.cc. We should | 5 // TODO(ukai): code is similar with http_network_transaction.cc. We should |
| 6 // think about ways to share code, if possible. | 6 // think about ways to share code, if possible. |
| 7 | 7 |
| 8 #include "net/socket_stream/socket_stream.h" | 8 #include "net/socket_stream/socket_stream.h" |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "net/base/auth.h" | 16 #include "net/base/auth.h" |
| 17 #include "net/base/host_resolver.h" | 17 #include "net/base/host_resolver.h" |
| 18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
| 21 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
| 22 #include "net/http/http_util.h" | 22 #include "net/http/http_util.h" |
| 23 #include "net/socket/client_socket_factory.h" | 23 #include "net/socket/client_socket_factory.h" |
| 24 #include "net/socket/ssl_client_socket.h" | 24 #include "net/socket/ssl_client_socket.h" |
| 25 #include "net/socket/socks5_client_socket.h" | 25 #include "net/socket/socks5_client_socket.h" |
| 26 #include "net/socket/socks_client_socket.h" | 26 #include "net/socket/socks_client_socket.h" |
| 27 #include "net/socket/tcp_client_socket.h" | 27 #include "net/socket/tcp_client_socket.h" |
| 28 #include "net/socket_stream/socket_stream_metrics.h" | 28 #include "net/socket_stream/socket_stream_metrics.h" |
| 29 #include "net/socket_stream/socket_stream_throttle.h" | |
| 30 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
| 31 | 30 |
| 32 static const int kMaxPendingSendAllowed = 32768; // 32 kilobytes. | 31 static const int kMaxPendingSendAllowed = 32768; // 32 kilobytes. |
| 33 static const int kReadBufferSize = 4096; | 32 static const int kReadBufferSize = 4096; |
| 34 | 33 |
| 35 namespace net { | 34 namespace net { |
| 36 | 35 |
| 37 void SocketStream::ResponseHeaders::Realloc(size_t new_size) { | 36 void SocketStream::ResponseHeaders::Realloc(size_t new_size) { |
| 38 headers_.reset(static_cast<char*>(realloc(headers_.release(), new_size))); | 37 headers_.reset(static_cast<char*>(realloc(headers_.release(), new_size))); |
| 39 } | 38 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 52 io_callback_(this, &SocketStream::OnIOCompleted)), | 51 io_callback_(this, &SocketStream::OnIOCompleted)), |
| 53 ALLOW_THIS_IN_INITIALIZER_LIST( | 52 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 54 read_callback_(this, &SocketStream::OnReadCompleted)), | 53 read_callback_(this, &SocketStream::OnReadCompleted)), |
| 55 ALLOW_THIS_IN_INITIALIZER_LIST( | 54 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 56 write_callback_(this, &SocketStream::OnWriteCompleted)), | 55 write_callback_(this, &SocketStream::OnWriteCompleted)), |
| 57 read_buf_(NULL), | 56 read_buf_(NULL), |
| 58 write_buf_(NULL), | 57 write_buf_(NULL), |
| 59 current_write_buf_(NULL), | 58 current_write_buf_(NULL), |
| 60 write_buf_offset_(0), | 59 write_buf_offset_(0), |
| 61 write_buf_size_(0), | 60 write_buf_size_(0), |
| 62 throttle_( | |
| 63 SocketStreamThrottle::GetSocketStreamThrottleForScheme( | |
| 64 url.scheme())), | |
| 65 metrics_(new SocketStreamMetrics(url)) { | 61 metrics_(new SocketStreamMetrics(url)) { |
| 66 DCHECK(MessageLoop::current()) << | 62 DCHECK(MessageLoop::current()) << |
| 67 "The current MessageLoop must exist"; | 63 "The current MessageLoop must exist"; |
| 68 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 64 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
| 69 "The current MessageLoop must be TYPE_IO"; | 65 "The current MessageLoop must be TYPE_IO"; |
| 70 DCHECK(delegate_); | 66 DCHECK(delegate_); |
| 71 DCHECK(throttle_); | |
| 72 } | 67 } |
| 73 | 68 |
| 74 SocketStream::~SocketStream() { | 69 SocketStream::~SocketStream() { |
| 75 set_context(NULL); | 70 set_context(NULL); |
| 76 DCHECK(!delegate_); | 71 DCHECK(!delegate_); |
| 77 } | 72 } |
| 78 | 73 |
| 79 SocketStream::UserData* SocketStream::GetUserData( | 74 SocketStream::UserData* SocketStream::GetUserData( |
| 80 const void* key) const { | 75 const void* key) const { |
| 81 UserDataMap::const_iterator found = user_data_.find(key); | 76 UserDataMap::const_iterator found = user_data_.find(key); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 DLOG(INFO) << "Finish result=" << net::ErrorToString(result); | 223 DLOG(INFO) << "Finish result=" << net::ErrorToString(result); |
| 229 if (delegate_) | 224 if (delegate_) |
| 230 delegate_->OnError(this, result); | 225 delegate_->OnError(this, result); |
| 231 | 226 |
| 232 metrics_->OnClose(); | 227 metrics_->OnClose(); |
| 233 Delegate* delegate = delegate_; | 228 Delegate* delegate = delegate_; |
| 234 delegate_ = NULL; | 229 delegate_ = NULL; |
| 235 if (delegate) { | 230 if (delegate) { |
| 236 delegate->OnClose(this); | 231 delegate->OnClose(this); |
| 237 } | 232 } |
| 238 throttle_->OnClose(this); | |
| 239 Release(); | 233 Release(); |
| 240 } | 234 } |
| 241 | 235 |
| 242 void SocketStream::SetHostResolver(HostResolver* host_resolver) { | 236 void SocketStream::SetHostResolver(HostResolver* host_resolver) { |
| 243 DCHECK(host_resolver); | 237 DCHECK(host_resolver); |
| 244 host_resolver_ = host_resolver; | 238 host_resolver_ = host_resolver; |
| 245 } | 239 } |
| 246 | 240 |
| 247 void SocketStream::SetClientSocketFactory( | 241 void SocketStream::SetClientSocketFactory( |
| 248 ClientSocketFactory* factory) { | 242 ClientSocketFactory* factory) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 268 | 262 |
| 269 return OK; | 263 return OK; |
| 270 } | 264 } |
| 271 | 265 |
| 272 int SocketStream::DidReceiveData(int result) { | 266 int SocketStream::DidReceiveData(int result) { |
| 273 DCHECK(read_buf_); | 267 DCHECK(read_buf_); |
| 274 DCHECK_GT(result, 0); | 268 DCHECK_GT(result, 0); |
| 275 net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_RECEIVED); | 269 net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_RECEIVED); |
| 276 int len = result; | 270 int len = result; |
| 277 metrics_->OnRead(len); | 271 metrics_->OnRead(len); |
| 278 result = throttle_->OnRead(this, read_buf_->data(), len, &io_callback_); | |
| 279 if (delegate_) { | 272 if (delegate_) { |
| 280 // Notify recevied data to delegate. | 273 // Notify recevied data to delegate. |
| 281 delegate_->OnReceivedData(this, read_buf_->data(), len); | 274 delegate_->OnReceivedData(this, read_buf_->data(), len); |
| 282 } | 275 } |
| 283 read_buf_ = NULL; | 276 read_buf_ = NULL; |
| 284 return result; | 277 return OK; |
| 285 } | 278 } |
| 286 | 279 |
| 287 int SocketStream::DidSendData(int result) { | 280 int SocketStream::DidSendData(int result) { |
| 288 DCHECK_GT(result, 0); | 281 DCHECK_GT(result, 0); |
| 289 net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_SENT); | 282 net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_SENT); |
| 290 int len = result; | 283 int len = result; |
| 291 metrics_->OnWrite(len); | 284 metrics_->OnWrite(len); |
| 292 result = throttle_->OnWrite(this, current_write_buf_->data(), len, | |
| 293 &io_callback_); | |
| 294 current_write_buf_ = NULL; | 285 current_write_buf_ = NULL; |
| 295 if (delegate_) | 286 if (delegate_) |
| 296 delegate_->OnSentData(this, len); | 287 delegate_->OnSentData(this, len); |
| 297 | 288 |
| 298 int remaining_size = write_buf_size_ - write_buf_offset_ - len; | 289 int remaining_size = write_buf_size_ - write_buf_offset_ - len; |
| 299 if (remaining_size == 0) { | 290 if (remaining_size == 0) { |
| 300 if (!pending_write_bufs_.empty()) { | 291 if (!pending_write_bufs_.empty()) { |
| 301 write_buf_size_ = pending_write_bufs_.front()->size(); | 292 write_buf_size_ = pending_write_bufs_.front()->size(); |
| 302 write_buf_ = pending_write_bufs_.front(); | 293 write_buf_ = pending_write_bufs_.front(); |
| 303 pending_write_bufs_.pop_front(); | 294 pending_write_bufs_.pop_front(); |
| 304 } else { | 295 } else { |
| 305 write_buf_size_ = 0; | 296 write_buf_size_ = 0; |
| 306 write_buf_ = NULL; | 297 write_buf_ = NULL; |
| 307 } | 298 } |
| 308 write_buf_offset_ = 0; | 299 write_buf_offset_ = 0; |
| 309 } else { | 300 } else { |
| 310 write_buf_offset_ += len; | 301 write_buf_offset_ += len; |
| 311 } | 302 } |
| 312 return result; | 303 return OK; |
| 313 } | 304 } |
| 314 | 305 |
| 315 void SocketStream::OnIOCompleted(int result) { | 306 void SocketStream::OnIOCompleted(int result) { |
| 316 DoLoop(result); | 307 DoLoop(result); |
| 317 } | 308 } |
| 318 | 309 |
| 319 void SocketStream::OnReadCompleted(int result) { | 310 void SocketStream::OnReadCompleted(int result) { |
| 320 if (result == 0) { | 311 if (result == 0) { |
| 321 // 0 indicates end-of-file, so socket was closed. | 312 // 0 indicates end-of-file, so socket was closed. |
| 322 next_state_ = STATE_CLOSE; | 313 next_state_ = STATE_CLOSE; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 | 474 |
| 484 DCHECK(host_resolver_.get()); | 475 DCHECK(host_resolver_.get()); |
| 485 resolver_.reset(new SingleRequestHostResolver(host_resolver_.get())); | 476 resolver_.reset(new SingleRequestHostResolver(host_resolver_.get())); |
| 486 return resolver_->Resolve(resolve_info, &addresses_, &io_callback_, | 477 return resolver_->Resolve(resolve_info, &addresses_, &io_callback_, |
| 487 net_log_); | 478 net_log_); |
| 488 } | 479 } |
| 489 | 480 |
| 490 int SocketStream::DoResolveHostComplete(int result) { | 481 int SocketStream::DoResolveHostComplete(int result) { |
| 491 if (result == OK) { | 482 if (result == OK) { |
| 492 next_state_ = STATE_TCP_CONNECT; | 483 next_state_ = STATE_TCP_CONNECT; |
| 493 result = throttle_->OnStartOpenConnection(this, &io_callback_); | 484 result = delegate_->OnStartOpenConnection(this, &io_callback_); |
| 494 if (result == net::ERR_IO_PENDING) | 485 if (result == net::ERR_IO_PENDING) |
| 495 metrics_->OnWaitConnection(); | 486 metrics_->OnWaitConnection(); |
| 496 } else { | 487 } else { |
| 497 next_state_ = STATE_CLOSE; | 488 next_state_ = STATE_CLOSE; |
| 498 } | 489 } |
| 499 // TODO(ukai): if error occured, reconsider proxy after error. | 490 // TODO(ukai): if error occured, reconsider proxy after error. |
| 500 return result; | 491 return result; |
| 501 } | 492 } |
| 502 | 493 |
| 503 int SocketStream::DoTcpConnect() { | 494 int SocketStream::DoTcpConnect() { |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 | 933 |
| 943 SSLConfigService* SocketStream::ssl_config_service() const { | 934 SSLConfigService* SocketStream::ssl_config_service() const { |
| 944 return context_->ssl_config_service(); | 935 return context_->ssl_config_service(); |
| 945 } | 936 } |
| 946 | 937 |
| 947 ProxyService* SocketStream::proxy_service() const { | 938 ProxyService* SocketStream::proxy_service() const { |
| 948 return context_->proxy_service(); | 939 return context_->proxy_service(); |
| 949 } | 940 } |
| 950 | 941 |
| 951 } // namespace net | 942 } // namespace net |
| OLD | NEW |