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.
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_STREAM_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_STREAM_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_export.h"
17 #include "net/websockets/websocket_event_interface.h"
18 #include "net/websockets/websocket_handshake_request_info.h"
19 #include "net/websockets/websocket_handshake_response_info.h"
30 class URLRequestContext
;
31 struct WebSocketFrame
;
33 // WebSocketStreamRequest is the caller's handle to the process of creation of a
34 // WebSocketStream. Deleting the object before the OnSuccess or OnFailure
35 // callbacks are called will cancel the request (and neither callback will be
36 // called). After OnSuccess or OnFailure have been called, this object may be
37 // safely deleted without side-effects.
38 class NET_EXPORT_PRIVATE WebSocketStreamRequest
{
40 virtual ~WebSocketStreamRequest();
43 // WebSocketStream is a transport-agnostic interface for reading and writing
44 // WebSocket frames. This class provides an abstraction for WebSocket streams
45 // based on various transport layers, such as normal WebSocket connections
46 // (WebSocket protocol upgraded from HTTP handshake), SPDY transports, or
47 // WebSocket connections with multiplexing extension. Subtypes of
48 // WebSocketStream are responsible for managing the underlying transport
51 // All functions except Close() can be asynchronous. If an operation cannot
52 // be finished synchronously, the function returns ERR_IO_PENDING, and
53 // |callback| will be called when the operation is finished. Non-null |callback|
54 // must be provided to these functions.
56 class NET_EXPORT_PRIVATE WebSocketStream
{
58 // A concrete object derived from ConnectDelegate is supplied by the caller to
59 // CreateAndConnectStream() to receive the result of the connection.
60 class NET_EXPORT_PRIVATE ConnectDelegate
{
62 virtual ~ConnectDelegate();
63 // Called on successful connection. The parameter is an object derived from
65 virtual void OnSuccess(scoped_ptr
<WebSocketStream
> stream
) = 0;
67 // Called on failure to connect.
68 // |message| contains defails of the failure.
69 virtual void OnFailure(const std::string
& message
) = 0;
71 // Called when the WebSocket Opening Handshake starts.
72 virtual void OnStartOpeningHandshake(
73 scoped_ptr
<WebSocketHandshakeRequestInfo
> request
) = 0;
75 // Called when the WebSocket Opening Handshake ends.
76 virtual void OnFinishOpeningHandshake(
77 scoped_ptr
<WebSocketHandshakeResponseInfo
> response
) = 0;
79 // Called when there is an SSL certificate error. Should call
80 // ssl_error_callbacks->ContinueSSLRequest() or
81 // ssl_error_callbacks->CancelSSLRequest().
82 virtual void OnSSLCertificateError(
83 scoped_ptr
<WebSocketEventInterface::SSLErrorCallbacks
>
85 const SSLInfo
& ssl_info
,
89 // Create and connect a WebSocketStream of an appropriate type. The actual
90 // concrete type returned depends on whether multiplexing or SPDY are being
91 // used to communicate with the remote server. If the handshake completed
92 // successfully, then connect_delegate->OnSuccess() is called with a
93 // WebSocketStream instance. If it failed, then connect_delegate->OnFailure()
94 // is called with a WebSocket result code corresponding to the error. Deleting
95 // the returned WebSocketStreamRequest object will cancel the connection, in
96 // which case the |connect_delegate| object that the caller passed will be
97 // deleted without any of its methods being called. Unless cancellation is
98 // required, the caller should keep the WebSocketStreamRequest object alive
99 // until connect_delegate->OnSuccess() or OnFailure() have been called, then
100 // it is safe to delete.
101 static scoped_ptr
<WebSocketStreamRequest
> CreateAndConnectStream(
102 const GURL
& socket_url
,
103 const std::vector
<std::string
>& requested_subprotocols
,
104 const url::Origin
& origin
,
105 URLRequestContext
* url_request_context
,
106 const BoundNetLog
& net_log
,
107 scoped_ptr
<ConnectDelegate
> connect_delegate
);
109 // Derived classes must make sure Close() is called when the stream is not
110 // closed on destruction.
111 virtual ~WebSocketStream();
113 // Reads WebSocket frame data. This operation finishes when new frame data
114 // becomes available.
116 // |frames| remains owned by the caller and must be valid until the
117 // operation completes or Close() is called. |frames| must be empty on
120 // This function should not be called while the previous call of ReadFrames()
123 // Returns net::OK or one of the net::ERR_* codes.
125 // frames->size() >= 1 if the result is OK.
127 // Only frames with complete header information are inserted into |frames|. If
128 // the currently available bytes of a new frame do not form a complete frame
129 // header, then the implementation will buffer them until all the fields in
130 // the WebSocketFrameHeader object can be filled. If ReadFrames() is freshly
131 // called in this situation, it will return ERR_IO_PENDING exactly as if no
132 // data was available.
134 // Original frame boundaries are not preserved. In particular, if only part of
135 // a frame is available, then the frame will be split, and the available data
136 // will be returned immediately.
138 // When the socket is closed on the remote side, this method will return
139 // ERR_CONNECTION_CLOSED. It will not return OK with an empty vector.
141 // If the connection is closed in the middle of receiving an incomplete frame,
142 // ReadFrames may discard the incomplete frame. Since the renderer will
143 // discard any incomplete messages when the connection is closed, this makes
144 // no difference to the overall semantics.
146 // Implementations of ReadFrames() must be able to handle being deleted during
147 // the execution of callback.Run(). In practice this means that the method
148 // calling callback.Run() (and any calling methods in the same object) must
149 // return immediately without any further method calls or access to member
150 // variables. Implementors should write test(s) for this case.
152 // Extensions which use reserved header bits should clear them when they are
153 // set correctly. If the reserved header bits are set incorrectly, it is okay
154 // to leave it to the caller to report the error.
155 virtual int ReadFrames(ScopedVector
<WebSocketFrame
>* frames
,
156 const CompletionCallback
& callback
) = 0;
158 // Writes WebSocket frame data.
160 // |frames| must be valid until the operation completes or Close() is called.
162 // This function must not be called while a previous call of WriteFrames() is
165 // This method will only return OK if all frames were written completely.
166 // Otherwise it will return an appropriate net error code.
168 // The callback implementation is permitted to delete this
169 // object. Implementations of WriteFrames() should be robust against
170 // this. This generally means returning to the event loop immediately after
171 // calling the callback.
172 virtual int WriteFrames(ScopedVector
<WebSocketFrame
>* frames
,
173 const CompletionCallback
& callback
) = 0;
175 // Closes the stream. All pending I/O operations (if any) are cancelled
176 // at this point, so |frames| can be freed.
177 virtual void Close() = 0;
179 // The subprotocol that was negotiated for the stream. If no protocol was
180 // negotiated, then the empty string is returned.
181 virtual std::string
GetSubProtocol() const = 0;
183 // The extensions that were negotiated for the stream. Since WebSocketStreams
184 // can be layered, this may be different from what this particular
185 // WebSocketStream implements. The primary purpose of this accessor is to make
186 // the data available to Javascript. The format of the string is identical to
187 // the contents of the Sec-WebSocket-Extensions header supplied by the server,
188 // with some canonicalisations applied (leading and trailing whitespace
189 // removed, multiple headers concatenated into one comma-separated list). See
190 // RFC6455 section 9.1 for the exact format specification. If no
191 // extensions were negotiated, the empty string is returned.
192 virtual std::string
GetExtensions() const = 0;
198 DISALLOW_COPY_AND_ASSIGN(WebSocketStream
);
203 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_