summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorukai@chromium.org <ukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-06 10:11:01 +0000
committerukai@chromium.org <ukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-06 10:11:01 +0000
commit1ccf985c8f79af3078efaad651582c7ba627fc7b (patch)
tree5a5ad81de12a1a6918d961bc2b26ea44c60d7833 /net
parenta766dd338d790b85b36d8432114b3a45b2ee1b82 (diff)
downloadchromium_src-1ccf985c8f79af3078efaad651582c7ba627fc7b.zip
chromium_src-1ccf985c8f79af3078efaad651582c7ba627fc7b.tar.gz
chromium_src-1ccf985c8f79af3078efaad651582c7ba627fc7b.tar.bz2
Add Websocket::DetachDelegate
It is required when websocket live experiment task finished with timed out and detach websocket immediately without waiting OnClose. BUG=none TEST=none Review URL: http://codereview.chromium.org/378010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31219 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/websockets/websocket.cc33
-rw-r--r--net/websockets/websocket.h5
-rw-r--r--net/websockets/websocket_unittest.cc4
3 files changed, 37 insertions, 5 deletions
diff --git a/net/websockets/websocket.cc b/net/websockets/websocket.cc
index 57db486..ddef4c0 100644
--- a/net/websockets/websocket.cc
+++ b/net/websockets/websocket.cc
@@ -50,6 +50,7 @@ WebSocket::WebSocket(Request* request, WebSocketDelegate* delegate)
WebSocket::~WebSocket() {
DCHECK(ready_state_ == INITIALIZED || !delegate_);
DCHECK(!socket_stream_);
+ DCHECK(!delegate_);
}
void WebSocket::Connect() {
@@ -67,6 +68,7 @@ void WebSocket::Connect() {
if (request_->client_socket_factory())
socket_stream_->SetClientSocketFactory(request_->client_socket_factory());
+ AddRef(); // Release in DoClose().
ready_state_ = CONNECTING;
socket_stream_->Connect();
}
@@ -99,6 +101,13 @@ void WebSocket::Close() {
}
}
+void WebSocket::DetachDelegate() {
+ if (!delegate_)
+ return;
+ delegate_ = NULL;
+ Close();
+}
+
void WebSocket::OnConnected(SocketStream* socket_stream,
int max_pending_send_allowed) {
DCHECK(socket_stream == socket_stream_);
@@ -181,7 +190,7 @@ IOBufferWithSize* WebSocket::CreateClientHandshakeMessage() const {
}
// TODO(ukai): Add cookie if necessary.
msg += "\r\n";
- DLOG(INFO) << "ClientHandshakeMsg=" << msg;
+ DLOG(INFO) << "ClientHandshake request=" << msg;
IOBufferWithSize* buf = new IOBufferWithSize(msg.size());
memcpy(buf->data(), msg.data(), msg.size());
return buf;
@@ -194,7 +203,7 @@ int WebSocket::CheckHandshake() {
const char *start = current_read_buf_->StartOfBuffer() + read_consumed_len_;
const char *p = start;
size_t len = current_read_buf_->offset() - read_consumed_len_;
- DLOG(INFO) << "CheckHandshake response=" << std::string(start, len);
+ DLOG(INFO) << "ClientHandshake response=" << std::string(start, len);
if (len < kServerHandshakeHeaderLength) {
return -1;
}
@@ -210,6 +219,9 @@ int WebSocket::CheckHandshake() {
mode_ = MODE_AUTHENTICATE;
// TODO(ukai): Implement authentication handlers.
}
+ DLOG(INFO) << "non-normal websocket connection. "
+ << "response_code=" << headers->response_code()
+ << " mode=" << mode_;
// Invalid response code.
ready_state_ = CLOSED;
return eoh;
@@ -222,6 +234,8 @@ int WebSocket::CheckHandshake() {
if (header_size < kUpgradeHeaderLength)
return -1;
if (memcmp(p, kUpgradeHeader, kUpgradeHeaderLength)) {
+ DLOG(INFO) << "Bad Upgrade Header "
+ << std::string(p, kUpgradeHeaderLength);
ready_state_ = CLOSED;
return p - start;
}
@@ -231,6 +245,8 @@ int WebSocket::CheckHandshake() {
if (header_size < kConnectionHeaderLength)
return -1;
if (memcmp(p, kConnectionHeader, kConnectionHeaderLength)) {
+ DLOG(INFO) << "Bad Upgrade Header "
+ << std::string(p, kConnectionHeaderLength);
ready_state_ = CLOSED;
return p - start;
}
@@ -242,6 +258,7 @@ int WebSocket::CheckHandshake() {
scoped_refptr<HttpResponseHeaders> headers(
new HttpResponseHeaders(HttpUtil::AssembleRawHeaders(start, eoh)));
if (!ProcessHeaders(*headers)) {
+ DLOG(INFO) << "Process Headers failed";
ready_state_ = CLOSED;
return eoh;
}
@@ -257,6 +274,9 @@ int WebSocket::CheckHandshake() {
ready_state_ = CLOSED;
break;
}
+ DLOG(INFO) << "CheckHandshake mode=" << mode_
+ << " ready_state=" << ready_state_
+ << " eoh=" << eoh;
return eoh;
}
@@ -340,7 +360,8 @@ void WebSocket::DoReceivedData() {
socket_stream_->Close();
return;
}
- delegate_->OnOpen(this);
+ if (delegate_)
+ delegate_->OnOpen(this);
if (current_read_buf_->offset() == read_consumed_len_) {
// No remaining data after handshake message.
break;
@@ -394,7 +415,7 @@ void WebSocket::ProcessFrameData() {
while (p < end && *p != '\xff')
++p;
if (p < end && *p == '\xff') {
- if (frame_byte == 0x00)
+ if (frame_byte == 0x00 && delegate_)
delegate_->OnMessage(this, std::string(msg_start, p - msg_start));
++p;
next_frame = p;
@@ -450,7 +471,9 @@ void WebSocket::DoClose() {
if (!socket_stream_)
return;
socket_stream_ = NULL;
- delegate->OnClose(this);
+ if (delegate)
+ delegate->OnClose(this);
+ Release();
}
void WebSocket::DoError(int error) {
diff --git a/net/websockets/websocket.h b/net/websockets/websocket.h
index 8ce5e83..566ee6f 100644
--- a/net/websockets/websocket.h
+++ b/net/websockets/websocket.h
@@ -128,6 +128,11 @@ class WebSocket : public base::RefCountedThreadSafe<WebSocket>,
// Closes the WebSocket connection.
void Close();
+ // Detach delegate. Call before delegate is deleted.
+ // Once delegate is detached, close the WebSocket connection and never call
+ // delegate back.
+ void DetachDelegate();
+
// SocketStream::Delegate methods.
// Called on IO thread.
virtual void OnConnected(SocketStream* socket_stream,
diff --git a/net/websockets/websocket_unittest.cc b/net/websockets/websocket_unittest.cc
index 6157271..b0b27df 100644
--- a/net/websockets/websocket_unittest.cc
+++ b/net/websockets/websocket_unittest.cc
@@ -274,6 +274,8 @@ TEST_F(WebSocketTest, ProcessFrameDataForLengthCalculation) {
// No onmessage event expected.
const std::vector<WebSocketEvent>& events = delegate->GetSeenEvents();
EXPECT_EQ(0U, events.size());
+
+ websocket->DetachDelegate();
}
TEST_F(WebSocketTest, ProcessFrameDataForUnterminatedString) {
@@ -320,6 +322,8 @@ TEST_F(WebSocketTest, ProcessFrameDataForUnterminatedString) {
EXPECT_EQ(WebSocketEvent::EVENT_MESSAGE, events[0].event_type);
EXPECT_EQ(kExpectedMsg, events[0].msg);
}
+
+ websocket->DetachDelegate();
}
} // namespace net