1 // Copyright (c) 2011 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 #ifndef NET_SPDY_SPDY_WEBSOCKET_STREAM_H_
6 #define NET_SPDY_SPDY_WEBSOCKET_STREAM_H_
9 #include "base/basictypes.h"
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/time.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/request_priority.h"
16 #include "net/spdy/spdy_framer.h"
17 #include "net/spdy/spdy_stream.h"
21 // The SpdyWebSocketStream is a WebSocket-specific type of stream known to a
22 // SpdySession. WebSocket's opening handshake is converted to SPDY's
23 // SYN_STREAM/SYN_REPLY. WebSocket frames are encapsulated as SPDY data frames.
24 class NET_EXPORT_PRIVATE SpdyWebSocketStream
25 : public SpdyStream::Delegate
{
27 // Delegate handles asynchronous events.
28 class NET_EXPORT_PRIVATE Delegate
{
30 // Called when InitializeStream() finishes asynchronously. This delegate is
31 // called if InitializeStream() returns ERR_IO_PENDING. |status| indicates
33 virtual void OnCreatedSpdyStream(int status
) = 0;
35 // Called on corresponding to OnSendHeadersComplete() or SPDY's SYN frame
37 virtual void OnSentSpdyHeaders(int status
) = 0;
39 // Called on corresponding to OnResponseReceived() or SPDY's SYN_STREAM,
40 // SYN_REPLY, or HEADERS frames are received. This callback may be called
41 // multiple times as SPDY's delegate does.
42 virtual int OnReceivedSpdyResponseHeader(
43 const spdy::SpdyHeaderBlock
& headers
,
46 // Called when data is sent.
47 virtual void OnSentSpdyData(int amount_sent
) = 0;
49 // Called when data is received.
50 virtual void OnReceivedSpdyData(const char* data
, int length
) = 0;
52 // Called when SpdyStream is closed.
53 virtual void OnCloseSpdyStream() = 0;
56 SpdyWebSocketStream(SpdySession
* spdy_session
, Delegate
* delegate
);
57 virtual ~SpdyWebSocketStream();
59 // Initializes SPDY stream for the WebSocket.
60 // It might create SPDY stream asynchronously. In this case, this method
61 // returns ERR_IO_PENDING and call OnCreatedSpdyStream delegate with result
62 // after completion. In other cases, delegate does not be called.
63 int InitializeStream(const GURL
& url
,
64 RequestPriority request_priority
,
65 const BoundNetLog
& stream_net_log
);
67 int SendRequest(const linked_ptr
<spdy::SpdyHeaderBlock
>& headers
);
68 int SendData(const char* data
, int length
);
71 // SpdyStream::Delegate
72 virtual bool OnSendHeadersComplete(int status
) OVERRIDE
;
73 virtual int OnSendBody() OVERRIDE
;
74 virtual int OnSendBodyComplete(int status
, bool* eof
) OVERRIDE
;
75 virtual int OnResponseReceived(const spdy::SpdyHeaderBlock
& response
,
76 base::Time response_time
,
78 virtual void OnDataReceived(const char* data
, int length
) OVERRIDE
;
79 virtual void OnDataSent(int length
) OVERRIDE
;
80 virtual void OnClose(int status
) OVERRIDE
;
81 virtual void set_chunk_callback(ChunkCallback
* callback
) OVERRIDE
;
84 FRIEND_TEST_ALL_PREFIXES(SpdyWebSocketStreamTest
, Basic
);
86 void OnSpdyStreamCreated(int status
);
88 scoped_refptr
<SpdyStream
> stream_
;
89 scoped_refptr
<SpdySession
> spdy_session_
;
92 DISALLOW_COPY_AND_ASSIGN(SpdyWebSocketStream
);
97 #endif // NET_SPDY_SPDY_WEBSOCKET_STREAM_H_