OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ |
6 #define NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ |
7 | 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
8 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback_forward.h" |
| 13 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
10 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 16 #include "net/base/net_export.h" |
11 #include "net/websockets/websocket_stream_base.h" | 17 #include "net/websockets/websocket_stream_base.h" |
12 | 18 |
| 19 class GURL; |
| 20 |
13 namespace net { | 21 namespace net { |
14 | 22 |
15 class BoundNetLog; | 23 class BoundNetLog; |
16 class HttpRequestHeaders; | 24 class HttpRequestHeaders; |
17 struct HttpRequestInfo; | |
18 class HttpResponseInfo; | 25 class HttpResponseInfo; |
| 26 class URLRequestContext; |
19 struct WebSocketFrameChunk; | 27 struct WebSocketFrameChunk; |
20 | 28 |
| 29 // WebSocketStreamRequest is the caller's handle to the process of creation of a |
| 30 // WebSocketStream. Deleting the object before the OnSuccess or OnFailure |
| 31 // callbacks are called will cancel the request (and neither callback will be |
| 32 // called). After OnSuccess or OnFailure have been called, this object may be |
| 33 // safely deleted without side-effects. |
| 34 class NET_EXPORT_PRIVATE WebSocketStreamRequest { |
| 35 public: |
| 36 virtual ~WebSocketStreamRequest(); |
| 37 }; |
| 38 |
21 // WebSocketStream is a transport-agnostic interface for reading and writing | 39 // WebSocketStream is a transport-agnostic interface for reading and writing |
22 // WebSocket frames. This class provides an abstraction for WebSocket streams | 40 // WebSocket frames. This class provides an abstraction for WebSocket streams |
23 // based on various transport layer, such as normal WebSocket connections | 41 // based on various transport layers, such as normal WebSocket connections |
24 // (WebSocket protocol upgraded from HTTP handshake), SPDY transports, or | 42 // (WebSocket protocol upgraded from HTTP handshake), SPDY transports, or |
25 // WebSocket connections with multiplexing extension. Subtypes of | 43 // WebSocket connections with multiplexing extension. Subtypes of |
26 // WebSocketStream are responsible for managing the underlying transport | 44 // WebSocketStream are responsible for managing the underlying transport |
27 // appropriately. | 45 // appropriately. |
28 // | 46 // |
29 // All functions except Close() can be asynchronous. If an operation cannot | 47 // All functions except Close() can be asynchronous. If an operation cannot |
30 // be finished synchronously, the function returns ERR_IO_PENDING, and | 48 // be finished synchronously, the function returns ERR_IO_PENDING, and |
31 // |callback| will be called when the operation is finished. Non-null |callback| | 49 // |callback| will be called when the operation is finished. Non-null |callback| |
32 // must be provided to these functions. | 50 // must be provided to these functions. |
33 | 51 |
34 class WebSocketStream : public WebSocketStreamBase { | 52 class NET_EXPORT_PRIVATE WebSocketStream : public WebSocketStreamBase { |
35 public: | 53 public: |
36 WebSocketStream() {} | 54 // A concrete object derived from ConnectDelegate is supplied by the caller to |
| 55 // CreateAndConnectStream() to receive the result of the connection. |
| 56 class ConnectDelegate { |
| 57 public: |
| 58 virtual ~ConnectDelegate(); |
| 59 // Called on successful connection. The parameter is an object derived from |
| 60 // WebSocketStream. |
| 61 virtual void OnSuccess(scoped_ptr<WebSocketStream> stream) = 0; |
| 62 |
| 63 // Called on failure to connect. The parameter is either one of the values |
| 64 // defined in net::WebSocketError, or an error defined by some WebSocket |
| 65 // extension protocol that we implement. |
| 66 virtual void OnFailure(unsigned short websocket_error) = 0; |
| 67 }; |
| 68 |
| 69 // Create and connect a WebSocketStream of an appropriate type. The actual |
| 70 // concrete type returned depends on whether multiplexing or SPDY are being |
| 71 // used to communicate with the remote server. If the handshake completed |
| 72 // successfully, then connect_delegate->OnSuccess() is called with a |
| 73 // WebSocketStream instance. If it failed, then connect_delegate->OnFailure() |
| 74 // is called with a WebSocket result code corresponding to the error. Deleting |
| 75 // the returned WebSocketStreamRequest object will cancel the connection, in |
| 76 // which case the |connect_delegate| object that the caller passed will be |
| 77 // deleted without any of its methods being called. Unless cancellation is |
| 78 // required, the caller should keep the WebSocketStreamRequest object alive |
| 79 // until connect_delegate->OnSuccess() or OnFailure() have been called, then |
| 80 // it is safe to delete. |
| 81 static scoped_ptr<WebSocketStreamRequest> CreateAndConnectStream( |
| 82 const GURL& socket_url, |
| 83 const std::vector<std::string>& requested_subprotocols, |
| 84 const GURL& origin, |
| 85 URLRequestContext* url_request_context, |
| 86 const BoundNetLog& net_log, |
| 87 scoped_ptr<ConnectDelegate> connect_delegate); |
37 | 88 |
38 // Derived classes must make sure Close() is called when the stream is not | 89 // Derived classes must make sure Close() is called when the stream is not |
39 // closed on destruction. | 90 // closed on destruction. |
40 virtual ~WebSocketStream() {} | 91 virtual ~WebSocketStream(); |
41 | |
42 // Initializes stream. Must be called before calling SendHandshakeRequest(). | |
43 // Returns a net error code, possibly ERR_IO_PENDING, as stated above. | |
44 // | |
45 // |request_info.url| must be a URL starting with "ws://" or "wss://". | |
46 // |request_info.method| must be "GET". |request_info.upload_data| is | |
47 // ignored. | |
48 virtual int InitializeStream(const HttpRequestInfo& request_info, | |
49 const BoundNetLog& net_log, | |
50 const CompletionCallback& callback) = 0; | |
51 | |
52 // Writes WebSocket handshake request to the underlying socket. Must be | |
53 // called after InitializeStream() completes and before | |
54 // ReadHandshakeResponse() is called. | |
55 // | |
56 // |response_info| must remain valid until the stream is destroyed. | |
57 virtual int SendHandshakeRequest(const HttpRequestHeaders& headers, | |
58 HttpResponseInfo* response_info, | |
59 const CompletionCallback& callback) = 0; | |
60 | |
61 // Reads WebSocket handshake response from the underlying socket. This | |
62 // function completes when the response headers have been completely | |
63 // received. Must be called after SendHandshakeRequest() completes. | |
64 virtual int ReadHandshakeResponse(const CompletionCallback& callback) = 0; | |
65 | 92 |
66 // Reads WebSocket frame data. This operation finishes when new frame data | 93 // Reads WebSocket frame data. This operation finishes when new frame data |
67 // becomes available. Each frame message might be chopped off in the middle | 94 // becomes available. Each frame message might be chopped off in the middle |
68 // as specified in the description of WebSocketFrameChunk struct. | 95 // as specified in the description of WebSocketFrameChunk struct. |
69 // |frame_chunks| must be valid until the operation completes or Close() | 96 // |frame_chunks| remains owned by the caller and must be valid until the |
70 // is called. | 97 // operation completes or Close() is called. |frame_chunks| must be empty on |
| 98 // calling. |
71 // | 99 // |
72 // This function can be called after ReadHandshakeResponse(). This function | 100 // This function should not be called while the previous call of ReadFrames() |
73 // should not be called while the previous call of ReadFrames() is still | 101 // is still pending. |
74 // pending. | 102 // |
| 103 // Returns net::OK or one of the net::ERR_* codes. |
| 104 // |
| 105 // frame_chunks->size() >= 1 if the result is OK. |
| 106 // |
| 107 // A frame with an incomplete header will never be inserted into |
| 108 // |frame_chunks|. If the currently available bytes of a new frame do not form |
| 109 // a complete frame header, then the implementation will buffer them until all |
| 110 // the fields in the WebSocketFrameHeader object can be filled. If |
| 111 // ReadFrames() is freshly called in this situation, it will return |
| 112 // ERR_IO_PENDING exactly as if no data was available. |
| 113 // |
| 114 // Every WebSocketFrameChunk in the vector except the first and last is |
| 115 // guaranteed to be a complete frame. The first chunk may be the final part |
| 116 // of the previous frame. The last chunk may be the first part of a new |
| 117 // frame. If there is only one chunk, then it may consist of data from the |
| 118 // middle part of a frame. |
| 119 // |
| 120 // When the socket is closed on the remote side, this method will return |
| 121 // ERR_CONNECTION_CLOSED. It will not return OK with an empty vector. |
| 122 // |
| 123 // If the connection is closed in the middle of receiving an incomplete frame, |
| 124 // ReadFrames may discard the incomplete frame. Since the renderer will |
| 125 // discard any incomplete messages when the connection is closed, this makes |
| 126 // no difference to the overall semantics. |
75 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | 127 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, |
76 const CompletionCallback& callback) = 0; | 128 const CompletionCallback& callback) = 0; |
77 | 129 |
78 // Writes WebSocket frame data. |frame_chunks| must obey the rule specified | 130 // Writes WebSocket frame data. |frame_chunks| must obey the rule specified |
79 // in the documentation of WebSocketFrameChunk struct: the first chunk of | 131 // in the documentation of WebSocketFrameChunk struct: the first chunk of |
80 // a WebSocket frame must contain non-NULL |header|, and the last chunk must | 132 // a WebSocket frame must contain non-NULL |header|, and the last chunk must |
81 // have |final_chunk| field set to true. Series of chunks representing a | 133 // have |final_chunk| field set to true. Series of chunks representing a |
82 // WebSocket frame must be consistent (the total length of |data| fields must | 134 // WebSocket frame must be consistent (the total length of |data| fields must |
83 // match |header->payload_length|). |frame_chunks| must be valid until the | 135 // match |header->payload_length|). |frame_chunks| must be valid until the |
84 // operation completes or Close() is called. | 136 // operation completes or Close() is called. |
85 // | 137 // |
86 // This function can be called after ReadHandshakeResponse(). This function | 138 // This function should not be called while previous call of WriteFrames() is |
87 // should not be called while previous call of WriteFrames() is still pending. | 139 // still pending. |
| 140 // |
| 141 // Support for incomplete frames is not guaranteed and may be removed from |
| 142 // future iterations of the API. |
| 143 // |
| 144 // This method will only return OK if all frames were written completely. |
| 145 // Otherwise it will return an appropriate net error code. |
88 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | 146 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, |
89 const CompletionCallback& callback) = 0; | 147 const CompletionCallback& callback) = 0; |
90 | 148 |
91 // Closes the stream. All pending I/O operations (if any) are canceled | 149 // Closes the stream. All pending I/O operations (if any) are cancelled |
92 // at this point, so |frame_chunks| can be freed. | 150 // at this point, so |frame_chunks| can be freed. |
93 virtual void Close() = 0; | 151 virtual void Close() = 0; |
94 | 152 |
| 153 // The subprotocol that was negotiated for the stream. If no protocol was |
| 154 // negotiated, then the empty string is returned. |
| 155 virtual std::string GetSubProtocol() const = 0; |
| 156 |
| 157 // The extensions that were negotiated for the stream. Since WebSocketStreams |
| 158 // can be layered, this may be different from what this particular |
| 159 // WebSocketStream implements. The primary purpose of this accessor is to make |
| 160 // the data available to Javascript. The format of the string is identical to |
| 161 // the contents of the Sec-WebSocket-Extensions header supplied by the server, |
| 162 // with some canonicalisations applied (leading and trailing whitespace |
| 163 // removed, multiple headers concatenated into one comma-separated list). See |
| 164 // RFC6455 section 9.1 for the exact format specification. If no |
| 165 // extensions were negotiated, the empty string is returned. |
| 166 virtual std::string GetExtensions() const = 0; |
| 167 |
95 // TODO(yutak): Add following interfaces: | 168 // TODO(yutak): Add following interfaces: |
96 // - RenewStreamForAuth for authentication (is this necessary?) | 169 // - RenewStreamForAuth for authentication (is this necessary?) |
97 // - GetSSLInfo, GetSSLCertRequsetInfo for SSL | 170 // - GetSSLInfo, GetSSLCertRequestInfo for SSL |
98 | 171 |
99 // WebSocketStreamBase derived functions | 172 // WebSocketStreamBase derived functions |
100 virtual WebSocketStream* AsWebSocketStream() { return this; } | 173 virtual WebSocketStream* AsWebSocketStream() OVERRIDE; |
| 174 |
| 175 //////////////////////////////////////////////////////////////////////////// |
| 176 // Methods used during the stream handshake. These must not be called once a |
| 177 // WebSocket protocol stream has been established (ie. after the |
| 178 // SuccessCallback or FailureCallback has been called.) |
| 179 |
| 180 // Writes WebSocket handshake request to the underlying socket. Must be called |
| 181 // before ReadHandshakeResponse(). |
| 182 // |
| 183 // |callback| will only be called if this method returns ERR_IO_PENDING. |
| 184 // |
| 185 // |response_info| must remain valid until the callback from |
| 186 // ReadHandshakeResponse has been called. |
| 187 // |
| 188 // TODO(ricea): This function is only used during the handshake and is |
| 189 // probably only applicable to certain subclasses of WebSocketStream. Move it |
| 190 // somewhere else? Also applies to ReadHandshakeResponse. |
| 191 virtual int SendHandshakeRequest(const GURL& url, |
| 192 const HttpRequestHeaders& headers, |
| 193 HttpResponseInfo* response_info, |
| 194 const CompletionCallback& callback) = 0; |
| 195 |
| 196 // Reads WebSocket handshake response from the underlying socket. Must be |
| 197 // called after SendHandshakeRequest() completes. |
| 198 // |
| 199 // |callback| will only be called if this method returns ERR_IO_PENDING. |
| 200 virtual int ReadHandshakeResponse(const CompletionCallback& callback) = 0; |
| 201 |
| 202 protected: |
| 203 WebSocketStream(); |
101 | 204 |
102 private: | 205 private: |
103 DISALLOW_COPY_AND_ASSIGN(WebSocketStream); | 206 DISALLOW_COPY_AND_ASSIGN(WebSocketStream); |
104 }; | 207 }; |
105 | 208 |
106 } // namespace net | 209 } // namespace net |
107 | 210 |
108 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ | 211 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ |
OLD | NEW |