Web MIDI: enable receiving functionality in Linux and Chrome OS
[chromium-blink-merge.git] / net / websockets / websocket_stream.h
bloba35eefa4b603f35ddb9b48e3e58285304bbcafbd
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_
8 #include <string>
9 #include <vector>
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_handshake_request_info.h"
18 #include "net/websockets/websocket_handshake_response_info.h"
20 class GURL;
22 namespace net {
24 class BoundNetLog;
25 class URLRequestContext;
26 struct WebSocketFrame;
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();
38 // WebSocketStream is a transport-agnostic interface for reading and writing
39 // WebSocket frames. This class provides an abstraction for WebSocket streams
40 // based on various transport layers, such as normal WebSocket connections
41 // (WebSocket protocol upgraded from HTTP handshake), SPDY transports, or
42 // WebSocket connections with multiplexing extension. Subtypes of
43 // WebSocketStream are responsible for managing the underlying transport
44 // appropriately.
46 // All functions except Close() can be asynchronous. If an operation cannot
47 // be finished synchronously, the function returns ERR_IO_PENDING, and
48 // |callback| will be called when the operation is finished. Non-null |callback|
49 // must be provided to these functions.
51 class NET_EXPORT_PRIVATE WebSocketStream {
52 public:
53 // A concrete object derived from ConnectDelegate is supplied by the caller to
54 // CreateAndConnectStream() to receive the result of the connection.
55 class NET_EXPORT_PRIVATE ConnectDelegate {
56 public:
57 virtual ~ConnectDelegate();
58 // Called on successful connection. The parameter is an object derived from
59 // WebSocketStream.
60 virtual void OnSuccess(scoped_ptr<WebSocketStream> stream) = 0;
62 // Called on failure to connect.
63 // |message| contains defails of the failure.
64 virtual void OnFailure(const std::string& message) = 0;
66 // Called when the WebSocket Opening Handshake starts.
67 virtual void OnStartOpeningHandshake(
68 scoped_ptr<WebSocketHandshakeRequestInfo> request) = 0;
70 // Called when the WebSocket Opening Handshake ends.
71 virtual void OnFinishOpeningHandshake(
72 scoped_ptr<WebSocketHandshakeResponseInfo> response) = 0;
75 // Create and connect a WebSocketStream of an appropriate type. The actual
76 // concrete type returned depends on whether multiplexing or SPDY are being
77 // used to communicate with the remote server. If the handshake completed
78 // successfully, then connect_delegate->OnSuccess() is called with a
79 // WebSocketStream instance. If it failed, then connect_delegate->OnFailure()
80 // is called with a WebSocket result code corresponding to the error. Deleting
81 // the returned WebSocketStreamRequest object will cancel the connection, in
82 // which case the |connect_delegate| object that the caller passed will be
83 // deleted without any of its methods being called. Unless cancellation is
84 // required, the caller should keep the WebSocketStreamRequest object alive
85 // until connect_delegate->OnSuccess() or OnFailure() have been called, then
86 // it is safe to delete.
87 static scoped_ptr<WebSocketStreamRequest> CreateAndConnectStream(
88 const GURL& socket_url,
89 const std::vector<std::string>& requested_subprotocols,
90 const GURL& origin,
91 URLRequestContext* url_request_context,
92 const BoundNetLog& net_log,
93 scoped_ptr<ConnectDelegate> connect_delegate);
95 // Derived classes must make sure Close() is called when the stream is not
96 // closed on destruction.
97 virtual ~WebSocketStream();
99 // Reads WebSocket frame data. This operation finishes when new frame data
100 // becomes available.
102 // |frames| remains owned by the caller and must be valid until the
103 // operation completes or Close() is called. |frames| must be empty on
104 // calling.
106 // This function should not be called while the previous call of ReadFrames()
107 // is still pending.
109 // Returns net::OK or one of the net::ERR_* codes.
111 // frames->size() >= 1 if the result is OK.
113 // Only frames with complete header information are inserted into |frames|. If
114 // the currently available bytes of a new frame do not form a complete frame
115 // header, then the implementation will buffer them until all the fields in
116 // the WebSocketFrameHeader object can be filled. If ReadFrames() is freshly
117 // called in this situation, it will return ERR_IO_PENDING exactly as if no
118 // data was available.
120 // Original frame boundaries are not preserved. In particular, if only part of
121 // a frame is available, then the frame will be split, and the available data
122 // will be returned immediately.
124 // When the socket is closed on the remote side, this method will return
125 // ERR_CONNECTION_CLOSED. It will not return OK with an empty vector.
127 // If the connection is closed in the middle of receiving an incomplete frame,
128 // ReadFrames may discard the incomplete frame. Since the renderer will
129 // discard any incomplete messages when the connection is closed, this makes
130 // no difference to the overall semantics.
132 // Implementations of ReadFrames() must be able to handle being deleted during
133 // the execution of callback.Run(). In practice this means that the method
134 // calling callback.Run() (and any calling methods in the same object) must
135 // return immediately without any further method calls or access to member
136 // variables. Implementors should write test(s) for this case.
137 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames,
138 const CompletionCallback& callback) = 0;
140 // Writes WebSocket frame data.
142 // |frames| must be valid until the operation completes or Close() is called.
144 // This function must not be called while a previous call of WriteFrames() is
145 // still pending.
147 // This method will only return OK if all frames were written completely.
148 // Otherwise it will return an appropriate net error code.
150 // The callback implementation is permitted to delete this
151 // object. Implementations of WriteFrames() should be robust against
152 // this. This generally means returning to the event loop immediately after
153 // calling the callback.
154 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames,
155 const CompletionCallback& callback) = 0;
157 // Closes the stream. All pending I/O operations (if any) are cancelled
158 // at this point, so |frames| can be freed.
159 virtual void Close() = 0;
161 // The subprotocol that was negotiated for the stream. If no protocol was
162 // negotiated, then the empty string is returned.
163 virtual std::string GetSubProtocol() const = 0;
165 // The extensions that were negotiated for the stream. Since WebSocketStreams
166 // can be layered, this may be different from what this particular
167 // WebSocketStream implements. The primary purpose of this accessor is to make
168 // the data available to Javascript. The format of the string is identical to
169 // the contents of the Sec-WebSocket-Extensions header supplied by the server,
170 // with some canonicalisations applied (leading and trailing whitespace
171 // removed, multiple headers concatenated into one comma-separated list). See
172 // RFC6455 section 9.1 for the exact format specification. If no
173 // extensions were negotiated, the empty string is returned.
174 virtual std::string GetExtensions() const = 0;
176 protected:
177 WebSocketStream();
179 private:
180 DISALLOW_COPY_AND_ASSIGN(WebSocketStream);
183 } // namespace net
185 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_