Index: net/socket_stream/socket_stream.cc |
=================================================================== |
--- net/socket_stream/socket_stream.cc (revision 30551) |
+++ net/socket_stream/socket_stream.cc (working copy) |
@@ -143,7 +143,6 @@ |
return; |
if (socket_->IsConnected()) |
socket_->Disconnect(); |
- next_state_ = STATE_NONE; |
// Close asynchronously, so that delegate won't be called |
// back before returning Close(). |
MessageLoop::current()->PostTask( |
@@ -183,22 +182,18 @@ |
Close(); |
} |
-void SocketStream::Finish(int result) { |
+void SocketStream::Finish() { |
DCHECK(MessageLoop::current()) << |
"The current MessageLoop must exist"; |
DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
"The current MessageLoop must be TYPE_IO"; |
- DCHECK_LT(result, 0); |
DLOG(INFO) << "Finish"; |
- if (delegate_) |
- delegate_->OnError(this, result); |
- |
Delegate* delegate = delegate_; |
delegate_ = NULL; |
if (delegate) { |
delegate->OnClose(this); |
+ Release(); |
} |
- Release(); |
} |
void SocketStream::SetHostResolver(HostResolver* host_resolver) { |
@@ -212,16 +207,17 @@ |
factory_ = factory; |
} |
-int SocketStream::DidEstablishConnection() { |
+void SocketStream::DidEstablishConnection() { |
if (!socket_.get() || !socket_->IsConnected()) { |
- return ERR_CONNECTION_FAILED; |
+ Finish(); |
+ return; |
} |
next_state_ = STATE_READ_WRITE; |
if (delegate_) |
delegate_->OnConnected(this, max_pending_send_allowed_); |
- return OK; |
+ return; |
} |
void SocketStream::DidReceiveData(int result) { |
@@ -259,9 +255,11 @@ |
void SocketStream::OnIOCompleted(int result) { |
DoLoop(result); |
+ // TODO(ukai): notify error. |
} |
void SocketStream::OnReadCompleted(int result) { |
+ // TODO(ukai): notify error. |
if (result == 0) { |
// 0 indicates end-of-file, so socket was closed. |
next_state_ = STATE_NONE; |
@@ -281,17 +279,16 @@ |
DoLoop(result); |
} |
-void SocketStream::DoLoop(int result) { |
+int SocketStream::DoLoop(int result) { |
+ if (next_state_ == STATE_NONE) { |
+ Finish(); |
+ return ERR_CONNECTION_CLOSED; |
+ } |
+ |
do { |
State state = next_state_; |
next_state_ = STATE_NONE; |
switch (state) { |
- case STATE_NONE: |
- DCHECK_LE(result, OK); |
- if (result == OK) |
- result = ERR_CONNECTION_CLOSED; |
- Finish(result); |
- return; |
case STATE_RESOLVE_PROXY: |
DCHECK_EQ(OK, result); |
result = DoResolveProxy(); |
@@ -349,7 +346,12 @@ |
result = ERR_UNEXPECTED; |
break; |
} |
- } while (result != ERR_IO_PENDING); |
+ } while (result != ERR_IO_PENDING && next_state_ != STATE_NONE); |
+ |
+ if (result != ERR_IO_PENDING) |
+ Finish(); |
+ |
+ return result; |
} |
int SocketStream::DoResolveProxy() { |
@@ -366,8 +368,6 @@ |
pac_request_ = NULL; |
if (result != OK) { |
LOG(ERROR) << "Failed to resolve proxy: " << result; |
- if (delegate_) |
- delegate_->OnError(this, result); |
proxy_info_.UseDirect(); |
} |
@@ -428,9 +428,9 @@ |
else if (is_secure()) { |
next_state_ = STATE_SSL_CONNECT; |
} else { |
- result = DidEstablishConnection(); |
+ DidEstablishConnection(); |
} |
- return result; |
+ return OK; |
} |
int SocketStream::DoWriteTunnelHeaders() { |
@@ -532,8 +532,8 @@ |
if (result == 0) { |
// 0 indicates end-of-file, so socket was closed. |
- DCHECK_EQ(next_state_, STATE_NONE); |
- return ERR_CONNECTION_CLOSED; |
+ Finish(); |
+ return result; |
} |
tunnel_response_headers_len_ += result; |
@@ -542,10 +542,8 @@ |
int eoh = HttpUtil::LocateEndOfHeaders( |
tunnel_response_headers_->headers(), tunnel_response_headers_len_, 0); |
if (eoh == -1) { |
- if (tunnel_response_headers_len_ >= kMaxTunnelResponseHeadersSize) { |
- DCHECK_EQ(next_state_, STATE_NONE); |
+ if (tunnel_response_headers_len_ >= kMaxTunnelResponseHeadersSize) |
return ERR_RESPONSE_HEADERS_TOO_BIG; |
- } |
next_state_ = STATE_READ_TUNNEL_HEADERS; |
return OK; |
@@ -556,7 +554,6 @@ |
HttpUtil::AssembleRawHeaders(tunnel_response_headers_->headers(), eoh)); |
if (headers->GetParsedHttpVersion() < HttpVersion(1, 0)) { |
// Require the "HTTP/1.x" status line. |
- DCHECK_EQ(next_state_, STATE_NONE); |
return ERR_TUNNEL_CONNECTION_FAILED; |
} |
switch (headers->response_code()) { |
@@ -565,11 +562,7 @@ |
DCHECK_EQ(eoh, tunnel_response_headers_len_); |
next_state_ = STATE_SSL_CONNECT; |
} else { |
- result = DidEstablishConnection(); |
- if (result < 0) { |
- DCHECK_EQ(next_state_, STATE_NONE); |
- return result; |
- } |
+ DidEstablishConnection(); |
if ((eoh < tunnel_response_headers_len_) && delegate_) |
delegate_->OnReceivedData( |
this, tunnel_response_headers_->headers() + eoh, |
@@ -590,7 +583,6 @@ |
MessageLoop::current()->PostTask( |
FROM_HERE, |
NewRunnableMethod(this, &SocketStream::DoAuthRequired)); |
- next_state_ = STATE_AUTH_REQUIRED; |
return ERR_IO_PENDING; |
} |
default: |
@@ -641,15 +633,17 @@ |
result = HandleCertificateError(result); |
if (result == OK) |
- result = DidEstablishConnection(); |
+ DidEstablishConnection(); |
return result; |
} |
int SocketStream::DoReadWrite(int result) { |
if (result < OK) { |
+ Finish(); |
return result; |
} |
if (!socket_.get() || !socket_->IsConnected()) { |
+ Finish(); |
return ERR_CONNECTION_CLOSED; |
} |
@@ -664,6 +658,7 @@ |
return OK; |
} else if (result == 0) { |
// 0 indicates end-of-file, so socket was closed. |
+ Finish(); |
return ERR_CONNECTION_CLOSED; |
} |
// If read is pending, try write as well. |
@@ -751,7 +746,6 @@ |
} |
void SocketStream::DoRestartWithAuth() { |
- DCHECK_EQ(next_state_, STATE_AUTH_REQUIRED); |
auth_cache_.Add(ProxyAuthOrigin(), auth_handler_, |
auth_identity_.username, auth_identity_.password, |
std::string()); |