Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: net/websockets/websocket_stream.h

Issue 17610006: Add WebSocketStream factory function (Closed) Base URL: http://git.chromium.org/chromium/src.git@web_socket_net
Patch Set: Changes inspired by mmenke's review Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/net.gyp ('k') | net/websockets/websocket_stream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:
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 };
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 connect_delegate->OnSuccess() is called with a
71 // WebSocketStream implementation. If it failed, then
mmenke1 2013/06/27 18:09:01 nit: "implementation" is generally used to mean a
Adam Rice 2013/06/28 04:32:45 Thanks. Done.
72 // connect_delegate->OnFailure() is called with a WebSocket result code
73 // corresponding to the error. Deleting the returned WebSocketStreamRequest
74 // object will cancel the connection, in which case the |connect_delegate|
75 // object that the caller passed will be deleted without any of its methods
76 // being called. Unless cancellation is required, the caller should keep the
mmenke1 2013/06/27 18:09:01 Wait...So the WebSocketStreamRequest owns the dele
Adam Rice 2013/06/28 04:32:45 I'm not really comfortable clogging up my public i
77 // WebSocketStreamRequest object alive until connect_delegate->OnSuccess() or
78 // OnFailure() have been called, then it is safe to delete.
79 static scoped_ptr<WebSocketStreamRequest> CreateAndConnectStream(
80 const GURL& socket_url,
81 const std::vector<std::string>& requested_subprotocols,
82 const GURL& origin,
83 URLRequestContext* url_request_context,
84 const BoundNetLog& net_log,
85 scoped_ptr<ConnectDelegate> connect_delegate);
37 86
38 // Derived classes must make sure Close() is called when the stream is not 87 // Derived classes must make sure Close() is called when the stream is not
39 // closed on destruction. 88 // closed on destruction.
40 virtual ~WebSocketStream() {} 89 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 90
66 // Reads WebSocket frame data. This operation finishes when new frame data 91 // Reads WebSocket frame data. This operation finishes when new frame data
67 // becomes available. Each frame message might be chopped off in the middle 92 // becomes available. Each frame message might be chopped off in the middle
68 // as specified in the description of WebSocketFrameChunk struct. 93 // as specified in the description of WebSocketFrameChunk struct.
69 // |frame_chunks| must be valid until the operation completes or Close() 94 // |frame_chunks| remains owned by the caller and must be valid until the
70 // is called. 95 // operation completes or Close() is called. |frame_chunks| must be empty on
96 // calling.
71 // 97 //
72 // This function can be called after ReadHandshakeResponse(). This function 98 // 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 99 // is still pending.
74 // pending. 100 //
101 // Returns net::OK or one of the net::ERR_* codes.
102 //
103 // frame_chunks->size() >= 1 if the result is OK.
104 //
105 // A frame with an incomplete header will never be inserted into
106 // |frame_chunks|. If the currently available bytes of a new frame do not form
107 // a complete frame header, then the implementation will buffer them until all
108 // the fields in the WebSocketFrameHeader object can be filled. If
109 // ReadFrames() is freshly called in this situation, it will return
110 // ERR_IO_PENDING exactly as if no data was available.
111 //
112 // Every WebSocketFrameChunk in the vector except the first and last is
113 // guaranteed to be a complete frame. The first chunk may be the final part
114 // of the previous frame. The last chunk may be the first part of a new
115 // frame. If there is only one chunk, then it may consist of data from the
116 // middle part of a frame.
117 //
118 // When the socket is closed on the remote side, this method will return
119 // ERR_CONNECTION_CLOSED. It will not return OK with an empty vector.
120 //
121 // If the connection is closed in the middle of receiving an incomplete frame,
122 // ReadFrames may discard the incomplete frame. Since the renderer will
123 // discard any incomplete messages when the connection is closed, this makes
124 // no difference to the overall semantics.
75 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, 125 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks,
76 const CompletionCallback& callback) = 0; 126 const CompletionCallback& callback) = 0;
77 127
78 // Writes WebSocket frame data. |frame_chunks| must obey the rule specified 128 // Writes WebSocket frame data. |frame_chunks| must obey the rule specified
79 // in the documentation of WebSocketFrameChunk struct: the first chunk of 129 // in the documentation of WebSocketFrameChunk struct: the first chunk of
80 // a WebSocket frame must contain non-NULL |header|, and the last chunk must 130 // 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 131 // 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 132 // WebSocket frame must be consistent (the total length of |data| fields must
83 // match |header->payload_length|). |frame_chunks| must be valid until the 133 // match |header->payload_length|). |frame_chunks| must be valid until the
84 // operation completes or Close() is called. 134 // operation completes or Close() is called.
85 // 135 //
86 // This function can be called after ReadHandshakeResponse(). This function 136 // 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. 137 // still pending.
138 //
139 // Support for incomplete frames is not guaranteed and may be removed from
140 // future iterations of the API.
141 //
142 // This method will only return OK if all frames were written completely.
143 // Otherwise it will return an appropriate ERR_ code.
88 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, 144 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks,
89 const CompletionCallback& callback) = 0; 145 const CompletionCallback& callback) = 0;
90 146
91 // Closes the stream. All pending I/O operations (if any) are canceled 147 // Closes the stream. All pending I/O operations (if any) are cancelled
92 // at this point, so |frame_chunks| can be freed. 148 // at this point, so |frame_chunks| can be freed.
93 virtual void Close() = 0; 149 virtual void Close() = 0;
94 150
151 // The subprotocol that was negotiated for the stream. If no protocol was
152 // negotiated, then the empty string is returned.
153 virtual std::string GetSubProtocol() const = 0;
154
155 // The extensions that were negotiated for the stream. Since WebSocketStreams
156 // can be layered, this may be different from what this particular
157 // WebSocketStream implements. The primary purpose of this accessor is to make
158 // the data available to Javascript. The format of the string is identical to
159 // the contents of the Sec-WebSocket-Extensions header supplied by the server,
160 // with some canonicalisations applied (leading and trailing whitespace
161 // removed, multiple headers concatenated into one comma-separated list). See
162 // RFC6455 section 9.1 for the exact format specification. If no
163 // extensions were negotiated, the empty string is returned.
164 virtual std::string GetExtensions() const = 0;
165
95 // TODO(yutak): Add following interfaces: 166 // TODO(yutak): Add following interfaces:
96 // - RenewStreamForAuth for authentication (is this necessary?) 167 // - RenewStreamForAuth for authentication (is this necessary?)
97 // - GetSSLInfo, GetSSLCertRequsetInfo for SSL 168 // - GetSSLInfo, GetSSLCertRequestInfo for SSL
98 169
99 // WebSocketStreamBase derived functions 170 // WebSocketStreamBase derived functions
100 virtual WebSocketStream* AsWebSocketStream() { return this; } 171 virtual WebSocketStream* AsWebSocketStream() OVERRIDE;
172
173 ////////////////////////////////////////////////////////////////////////////
174 // Methods used during the stream handshake. These must not be called once a
175 // WebSocket protocol stream has been established (ie. after the
176 // SuccessCallback or FailureCallback has been called.)
177
178 // Writes WebSocket handshake request to the underlying socket. Must be called
179 // before ReadHandshakeResponse().
180 //
181 // |callback| will only be called if this method returns ERR_IO_PENDING.
182 //
183 // |response_info| must remain valid until the callback from
184 // ReadHandshakeResponse has been called.
185 //
186 // TODO(ricea): This function is only used during the handshake and is
187 // probably only applicable to certain subclasses of WebSocketStream. Move it
188 // somewhere else? Also applies to ReadHandshakeResponse.
189 virtual int SendHandshakeRequest(const GURL& url,
190 const HttpRequestHeaders& headers,
191 HttpResponseInfo* response_info,
192 const CompletionCallback& callback) = 0;
193
194 // Reads WebSocket handshake response from the underlying socket. Must be
195 // called after SendHandshakeRequest() completes.
196 //
197 // |callback| will only be called if this method returns ERR_IO_PENDING.
198 virtual int ReadHandshakeResponse(const CompletionCallback& callback) = 0;
199
200 protected:
201 WebSocketStream();
101 202
102 private: 203 private:
103 DISALLOW_COPY_AND_ASSIGN(WebSocketStream); 204 DISALLOW_COPY_AND_ASSIGN(WebSocketStream);
104 }; 205 };
105 206
106 } // namespace net 207 } // namespace net
107 208
108 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ 209 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_
OLDNEW
« no previous file with comments | « net/net.gyp ('k') | net/websockets/websocket_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698