Enables compositing support for webview.
[chromium-blink-merge.git] / net / spdy / spdy_websocket_stream.cc
blob6323b71cc8ba3640d98581374a6684b7ffcee915
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/spdy/spdy_websocket_stream.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "googleurl/src/gurl.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/io_buffer.h"
12 #include "net/spdy/spdy_framer.h"
13 #include "net/spdy/spdy_protocol.h"
14 #include "net/spdy/spdy_session.h"
15 #include "net/spdy/spdy_stream.h"
17 namespace net {
19 SpdyWebSocketStream::SpdyWebSocketStream(
20 SpdySession* spdy_session, Delegate* delegate)
21 : stream_(NULL),
22 spdy_session_(spdy_session),
23 delegate_(delegate) {
24 DCHECK(spdy_session_);
25 DCHECK(delegate_);
28 SpdyWebSocketStream::~SpdyWebSocketStream() {
29 if (stream_) {
30 // If Close() has not already been called, DetachDelegate() will send a
31 // SPDY RST_STREAM. Deleting SpdyWebSocketStream is good enough to initiate
32 // graceful shutdown, so we call Close() to avoid sending a RST_STREAM.
33 // For safe, we should eliminate |delegate_| for OnClose() calback.
34 delegate_ = NULL;
35 stream_->Close();
39 int SpdyWebSocketStream::InitializeStream(const GURL& url,
40 RequestPriority request_priority,
41 const BoundNetLog& net_log) {
42 if (spdy_session_->IsClosed())
43 return ERR_SOCKET_NOT_CONNECTED;
45 int result = spdy_session_->CreateStream(
46 url, request_priority, &stream_, net_log,
47 base::Bind(&SpdyWebSocketStream::OnSpdyStreamCreated,
48 base::Unretained(this)));
50 if (result == OK) {
51 DCHECK(stream_);
52 stream_->SetDelegate(this);
54 return result;
57 int SpdyWebSocketStream::SendRequest(scoped_ptr<SpdyHeaderBlock> headers) {
58 if (!stream_) {
59 NOTREACHED();
60 return ERR_UNEXPECTED;
62 stream_->set_spdy_headers(headers.Pass());
63 int result = stream_->SendRequest(true);
64 if (result < OK && result != ERR_IO_PENDING)
65 Close();
66 return result;
69 int SpdyWebSocketStream::SendData(const char* data, int length) {
70 if (!stream_) {
71 NOTREACHED();
72 return ERR_UNEXPECTED;
74 scoped_refptr<IOBuffer> buf(new IOBuffer(length));
75 memcpy(buf->data(), data, length);
76 return stream_->WriteStreamData(buf.get(), length, DATA_FLAG_NONE);
79 void SpdyWebSocketStream::Close() {
80 if (spdy_session_)
81 spdy_session_->CancelPendingCreateStreams(&stream_);
82 if (stream_)
83 stream_->Close();
86 bool SpdyWebSocketStream::OnSendHeadersComplete(int status) {
87 DCHECK(delegate_);
88 delegate_->OnSentSpdyHeaders(status);
89 return true;
92 int SpdyWebSocketStream::OnSendBody() {
93 NOTREACHED();
94 return ERR_UNEXPECTED;
97 int SpdyWebSocketStream::OnSendBodyComplete(int status, bool* eof) {
98 NOTREACHED();
99 *eof = true;
100 return ERR_UNEXPECTED;
103 int SpdyWebSocketStream::OnResponseReceived(
104 const SpdyHeaderBlock& response,
105 base::Time response_time, int status) {
106 DCHECK(delegate_);
107 return delegate_->OnReceivedSpdyResponseHeader(response, status);
110 void SpdyWebSocketStream::OnHeadersSent() {
111 // This will be called when WebSocket over SPDY supports new framing.
112 NOTREACHED();
115 int SpdyWebSocketStream::OnDataReceived(const char* data, int length) {
116 DCHECK(delegate_);
117 delegate_->OnReceivedSpdyData(data, length);
118 return OK;
121 void SpdyWebSocketStream::OnDataSent(int length) {
122 DCHECK(delegate_);
123 delegate_->OnSentSpdyData(length);
126 void SpdyWebSocketStream::OnClose(int status) {
127 stream_ = NULL;
129 // Destruction without Close() call OnClose() with delegate_ being NULL.
130 if (!delegate_)
131 return;
132 Delegate* delegate = delegate_;
133 delegate_ = NULL;
134 delegate->OnCloseSpdyStream();
137 void SpdyWebSocketStream::OnSpdyStreamCreated(int result) {
138 DCHECK_NE(ERR_IO_PENDING, result);
139 if (result == OK) {
140 DCHECK(stream_);
141 stream_->SetDelegate(this);
143 DCHECK(delegate_);
144 delegate_->OnCreatedSpdyStream(result);
147 } // namespace net