summaryrefslogtreecommitdiffstats
path: root/net/socket_stream/socket_stream.cc
diff options
context:
space:
mode:
Diffstat (limited to 'net/socket_stream/socket_stream.cc')
-rw-r--r--net/socket_stream/socket_stream.cc62
1 files changed, 48 insertions, 14 deletions
diff --git a/net/socket_stream/socket_stream.cc b/net/socket_stream/socket_stream.cc
index 8ca8489..6a5285c 100644
--- a/net/socket_stream/socket_stream.cc
+++ b/net/socket_stream/socket_stream.cc
@@ -29,6 +29,7 @@
#include "net/socket_stream/socket_stream_throttle.h"
#include "net/url_request/url_request.h"
+static const int kMaxNumLoadLogEntries = 50;
static const int kMaxPendingSendAllowed = 32768; // 32 kilobytes.
static const int kReadBufferSize = 4096;
@@ -39,7 +40,8 @@ void SocketStream::ResponseHeaders::Realloc(size_t new_size) {
}
SocketStream::SocketStream(const GURL& url, Delegate* delegate)
- : url_(url),
+ : load_log_(new net::LoadLog(kMaxNumLoadLogEntries)),
+ url_(url),
delegate_(delegate),
max_pending_send_allowed_(kMaxPendingSendAllowed),
next_state_(STATE_NONE),
@@ -60,7 +62,9 @@ SocketStream::SocketStream(const GURL& url, Delegate* delegate)
throttle_(
SocketStreamThrottle::GetSocketStreamThrottleForScheme(
url.scheme())),
- metrics_(new SocketStreamMetrics(url)) {
+ metrics_(new SocketStreamMetrics(url)),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ request_tracker_node_(this)) {
DCHECK(MessageLoop::current()) <<
"The current MessageLoop must exist";
DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) <<
@@ -70,6 +74,7 @@ SocketStream::SocketStream(const GURL& url, Delegate* delegate)
}
SocketStream::~SocketStream() {
+ set_context(NULL);
DCHECK(!delegate_);
}
@@ -86,8 +91,20 @@ void SocketStream::SetUserData(const void* key, UserData* data) {
}
void SocketStream::set_context(URLRequestContext* context) {
+ scoped_refptr<URLRequestContext> prev_context = context_;
+
context_ = context;
- host_resolver_ = context_->host_resolver();
+
+ if (prev_context != context) {
+ if (prev_context)
+ prev_context->socket_stream_tracker()->Remove(this);
+ if (context)
+ context->socket_stream_tracker()->Add(this);
+ }
+
+ if (context_)
+ host_resolver_ = context_->host_resolver();
+
}
void SocketStream::Connect() {
@@ -102,6 +119,7 @@ void SocketStream::Connect() {
// Open a connection asynchronously, so that delegate won't be called
// back before returning Connect().
next_state_ = STATE_RESOLVE_PROXY;
+ LoadLog::BeginEvent(load_log_, LoadLog::TYPE_SOCKET_STREAM_CONNECT);
MessageLoop::current()->PostTask(
FROM_HERE,
NewRunnableMethod(this, &SocketStream::DoLoop, OK));
@@ -187,6 +205,7 @@ void SocketStream::DetachDelegate() {
if (!delegate_)
return;
delegate_ = NULL;
+ LoadLog::AddEvent(load_log_, LoadLog::TYPE_CANCELLED);
Close();
}
@@ -195,7 +214,9 @@ void SocketStream::Finish(int result) {
"The current MessageLoop must exist";
DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) <<
"The current MessageLoop must be TYPE_IO";
- DCHECK_LT(result, 0);
+ DCHECK_LE(result, OK);
+ if (result == OK)
+ result = ERR_CONNECTION_CLOSED;
DCHECK_EQ(next_state_, STATE_NONE);
DLOG(INFO) << "Finish result=" << net::ErrorToString(result);
if (delegate_)
@@ -234,6 +255,7 @@ int SocketStream::DidEstablishConnection() {
next_state_ = STATE_READ_WRITE;
metrics_->OnConnected();
+ LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_STREAM_CONNECT);
if (delegate_)
delegate_->OnConnected(this, max_pending_send_allowed_);
@@ -243,6 +265,7 @@ int SocketStream::DidEstablishConnection() {
int SocketStream::DidReceiveData(int result) {
DCHECK(read_buf_);
DCHECK_GT(result, 0);
+ LoadLog::AddEvent(load_log_, LoadLog::TYPE_SOCKET_STREAM_RECEIVED);
int len = result;
metrics_->OnRead(len);
result = throttle_->OnRead(this, read_buf_->data(), len, &io_callback_);
@@ -256,6 +279,7 @@ int SocketStream::DidReceiveData(int result) {
int SocketStream::DidSendData(int result) {
DCHECK_GT(result, 0);
+ LoadLog::AddEvent(load_log_, LoadLog::TYPE_SOCKET_STREAM_SENT);
int len = result;
metrics_->OnWrite(len);
result = throttle_->OnWrite(this, current_write_buf_->data(), len,
@@ -364,8 +388,6 @@ void SocketStream::DoLoop(int result) {
break;
case STATE_CLOSE:
DCHECK_LE(result, OK);
- if (result == OK)
- result = ERR_CONNECTION_CLOSED;
Finish(result);
return;
default:
@@ -373,6 +395,12 @@ void SocketStream::DoLoop(int result) {
Finish(result);
return;
}
+ // If the connection is not established yet and had actual errors,
+ // close the connection.
+ if (state != STATE_READ_WRITE && result < ERR_IO_PENDING) {
+ DCHECK_EQ(next_state_, STATE_CLOSE);
+ LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_STREAM_CONNECT);
+ }
} while (result != ERR_IO_PENDING);
}
@@ -381,7 +409,7 @@ int SocketStream::DoResolveProxy() {
next_state_ = STATE_RESOLVE_PROXY_COMPLETE;
return proxy_service()->ResolveProxy(
- url_, &proxy_info_, &io_callback_, &pac_request_, NULL);
+ url_, &proxy_info_, &io_callback_, &pac_request_, load_log_);
}
int SocketStream::DoResolveProxyComplete(int result) {
@@ -424,7 +452,8 @@ int SocketStream::DoResolveHost() {
DCHECK(host_resolver_.get());
resolver_.reset(new SingleRequestHostResolver(host_resolver_.get()));
- return resolver_->Resolve(resolve_info, &addresses_, &io_callback_, NULL);
+ return resolver_->Resolve(resolve_info, &addresses_, &io_callback_,
+ load_log_);
}
int SocketStream::DoResolveHostComplete(int result) {
@@ -444,9 +473,8 @@ int SocketStream::DoTcpConnect() {
next_state_ = STATE_TCP_CONNECT_COMPLETE;
DCHECK(factory_);
socket_.reset(factory_->CreateTCPClientSocket(addresses_));
- // TODO(willchan): Plumb LoadLog into SocketStream.
metrics_->OnStartConnection();
- return socket_->Connect(&io_callback_, NULL);
+ return socket_->Connect(&io_callback_, load_log_);
}
int SocketStream::DoTcpConnectComplete(int result) {
@@ -654,9 +682,8 @@ int SocketStream::DoSOCKSConnect() {
else
s = new SOCKSClientSocket(s, req_info, host_resolver_.get());
socket_.reset(s);
- // TODO(willchan): Plumb LoadLog into SocketStream.
metrics_->OnSOCKSProxy();
- return socket_->Connect(&io_callback_, NULL);
+ return socket_->Connect(&io_callback_, load_log_);
}
int SocketStream::DoSOCKSConnectComplete(int result) {
@@ -667,6 +694,8 @@ int SocketStream::DoSOCKSConnectComplete(int result) {
next_state_ = STATE_SSL_CONNECT;
else
result = DidEstablishConnection();
+ } else {
+ next_state_ = STATE_CLOSE;
}
return result;
}
@@ -676,9 +705,8 @@ int SocketStream::DoSSLConnect() {
socket_.reset(factory_->CreateSSLClientSocket(
socket_.release(), url_.HostNoBrackets(), ssl_config_));
next_state_ = STATE_SSL_CONNECT_COMPLETE;
- // TODO(willchan): Plumb LoadLog into SocketStream.
metrics_->OnSSLConnection();
- return socket_->Connect(&io_callback_, NULL);
+ return socket_->Connect(&io_callback_, load_log_);
}
int SocketStream::DoSSLConnectComplete(int result) {
@@ -848,4 +876,10 @@ ProxyService* SocketStream::proxy_service() const {
return context_->proxy_service();
}
+void SocketStream::GetInfoForTracker(
+ RequestTracker<SocketStream>::RecentRequestInfo* info) const {
+ info->original_url = url_;
+ info->load_log = load_log_;
+}
+
} // namespace net