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