Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / spdy / spdy_websocket_stream.h
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.
4
5 #ifndef NET_SPDY_SPDY_WEBSOCKET_STREAM_H_
6 #define NET_SPDY_SPDY_WEBSOCKET_STREAM_H_
7
8 #include "base/basictypes.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/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_header_block.h"
18 #include "net/spdy/spdy_session.h"
19 #include "net/spdy/spdy_stream.h"
20
21 namespace net {
22
23 // The SpdyWebSocketStream is a WebSocket-specific type of stream known to a
24 // SpdySession. WebSocket's opening handshake is converted to SPDY's
25 // SYN_STREAM/SYN_REPLY. WebSocket frames are encapsulated as SPDY data frames.
26 class NET_EXPORT_PRIVATE SpdyWebSocketStream
27     : public SpdyStream::Delegate {
28  public:
29   // Delegate handles asynchronous events.
30   class NET_EXPORT_PRIVATE Delegate {
31    public:
32     // Called when InitializeStream() finishes asynchronously. This delegate is
33     // called if InitializeStream() returns ERR_IO_PENDING. |status| indicates
34     // network error.
35     virtual void OnCreatedSpdyStream(int status) = 0;
36
37     // Called on corresponding to OnSendHeadersComplete() or SPDY's SYN frame
38     // has been sent.
39     virtual void OnSentSpdyHeaders() = 0;
40
41     // Called on corresponding to OnResponseHeadersUpdated() or
42     // SPDY's SYN_STREAM, SYN_REPLY, or HEADERS frames are
43     // received. This callback may be called multiple times as SPDY's
44     // delegate does.
45     virtual void OnSpdyResponseHeadersUpdated(
46         const SpdyHeaderBlock& response_headers) = 0;
47
48     // Called when data is sent.
49     virtual void OnSentSpdyData(size_t bytes_sent) = 0;
50
51     // Called when data is received.
52     virtual void OnReceivedSpdyData(scoped_ptr<SpdyBuffer> buffer) = 0;
53
54     // Called when SpdyStream is closed.
55     virtual void OnCloseSpdyStream() = 0;
56
57    protected:
58     virtual ~Delegate() {}
59   };
60
61   SpdyWebSocketStream(const base::WeakPtr<SpdySession>& spdy_session,
62                       Delegate* delegate);
63   ~SpdyWebSocketStream() override;
64
65   // Initializes SPDY stream for the WebSocket.
66   // It might create SPDY stream asynchronously.  In this case, this method
67   // returns ERR_IO_PENDING and call OnCreatedSpdyStream delegate with result
68   // after completion. In other cases, delegate does not be called.
69   int InitializeStream(const GURL& url,
70                        RequestPriority request_priority,
71                        const BoundNetLog& stream_net_log);
72
73   int SendRequest(scoped_ptr<SpdyHeaderBlock> headers);
74   int SendData(const char* data, int length);
75   void Close();
76
77   // SpdyStream::Delegate
78   void OnRequestHeadersSent() override;
79   SpdyResponseHeadersStatus OnResponseHeadersUpdated(
80       const SpdyHeaderBlock& response_headers) override;
81   void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) override;
82   void OnDataSent() override;
83   void OnClose(int status) override;
84
85  private:
86   friend class SpdyWebSocketStreamTest;
87   FRIEND_TEST_ALL_PREFIXES(SpdyWebSocketStreamTest, Basic);
88
89   void OnSpdyStreamCreated(int status);
90
91   SpdyStreamRequest stream_request_;
92   base::WeakPtr<SpdyStream> stream_;
93   const base::WeakPtr<SpdySession> spdy_session_;
94   size_t pending_send_data_length_;
95   Delegate* delegate_;
96
97   base::WeakPtrFactory<SpdyWebSocketStream> weak_ptr_factory_;
98
99   DISALLOW_COPY_AND_ASSIGN(SpdyWebSocketStream);
100 };
101
102 }  // namespace net
103
104 #endif  // NET_SPDY_SPDY_WEBSOCKET_STREAM_H_