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

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: Created 7 years, 6 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"
9 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
10 #include "net/base/completion_callback.h" 13 #include "net/base/completion_callback.h"
14 #include "net/base/net_export.h"
11 #include "net/websockets/websocket_stream_base.h" 15 #include "net/websockets/websocket_stream_base.h"
12 16
17 class GURL;
18
13 namespace net { 19 namespace net {
14 20
15 class BoundNetLog;
16 class HttpRequestHeaders; 21 class HttpRequestHeaders;
17 struct HttpRequestInfo;
18 class HttpResponseInfo; 22 class HttpResponseInfo;
23 class URLRequestContext;
19 struct WebSocketFrameChunk; 24 struct WebSocketFrameChunk;
20 25
21 // WebSocketStream is a transport-agnostic interface for reading and writing 26 // WebSocketStream is a transport-agnostic interface for reading and writing
22 // WebSocket frames. This class provides an abstraction for WebSocket streams 27 // WebSocket frames. This class provides an abstraction for WebSocket streams
23 // based on various transport layer, such as normal WebSocket connections 28 // based on various transport layer, such as normal WebSocket connections
24 // (WebSocket protocol upgraded from HTTP handshake), SPDY transports, or 29 // (WebSocket protocol upgraded from HTTP handshake), SPDY transports, or
25 // WebSocket connections with multiplexing extension. Subtypes of 30 // WebSocket connections with multiplexing extension. Subtypes of
26 // WebSocketStream are responsible for managing the underlying transport 31 // WebSocketStream are responsible for managing the underlying transport
27 // appropriately. 32 // appropriately.
28 // 33 //
29 // All functions except Close() can be asynchronous. If an operation cannot 34 // All functions except Close() can be asynchronous. If an operation cannot
30 // be finished synchronously, the function returns ERR_IO_PENDING, and 35 // be finished synchronously, the function returns ERR_IO_PENDING, and
31 // |callback| will be called when the operation is finished. Non-null |callback| 36 // |callback| will be called when the operation is finished. Non-null |callback|
32 // must be provided to these functions. 37 // must be provided to these functions.
33 38
34 class WebSocketStream : public WebSocketStreamBase { 39 class NET_EXPORT_PRIVATE WebSocketStream : public WebSocketStreamBase {
35 public: 40 public:
36 WebSocketStream() {} 41 // The type the callback that should be supplied to handle successful
42 // connection. This must be supplied by the caller. It must take a scoped_ptr
43 // to a WebSocketStream as a parameter.
44 typedef base::Callback<void(scoped_ptr<WebSocketStream>)> SuccessCallback;
45
46 // The type of the callback that should be supplied to handle failed
47 // connection. The parameter is a net::WebSocketError, but it is passed as
48 // unsigned short as it is not guaranteed to actually be defined in that enum.
49 typedef base::Callback<void(unsigned short)> FailureCallback;
50
51 // Create and connect a WebSocketStream of an appropriate type. If the
52 // handshake completed successfully, then the on_success callback is called
53 // with a subclass of WebSocketStream. If it failed, then the on_failure
54 // callback is called with a WebSocket result code corresponding to the
55 // error.
56 static void CreateAndConnectStream(
57 const GURL& socket_url,
58 const std::vector<std::string>& requested_protocols,
59 const GURL& origin,
60 URLRequestContext* url_request_context,
61 const SuccessCallback& on_success,
62 const FailureCallback& on_failure);
37 63
38 // Derived classes must make sure Close() is called when the stream is not 64 // Derived classes must make sure Close() is called when the stream is not
39 // closed on destruction. 65 // closed on destruction.
40 virtual ~WebSocketStream() {} 66 virtual ~WebSocketStream();
41 67
42 // Initializes stream. Must be called before calling SendHandshakeRequest(). 68 // Writes WebSocket handshake request to the underlying socket. Must be called
43 // Returns a net error code, possibly ERR_IO_PENDING, as stated above. 69 // before ReadHandshakeResponse().
44 // 70 //
45 // |request_info.url| must be a URL starting with "ws://" or "wss://". 71 // "callback" will only be called if this method returns ERR_IO_PENDING.
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 // 72 //
56 // |response_info| must remain valid until the stream is destroyed. 73 // |response_info| must remain valid until the callback from
57 virtual int SendHandshakeRequest(const HttpRequestHeaders& headers, 74 // ReadHandshakeResponse has been called.
75 //
76 // TODO(ricea): This function is only used during the handshake and is
77 // probably only applicable to certain subclasses of WebSocketStream. Move it
78 // somewhere else? Also applies to ReadHandshakeResponse.
79 virtual int SendHandshakeRequest(const GURL& url,
80 const HttpRequestHeaders& headers,
58 HttpResponseInfo* response_info, 81 HttpResponseInfo* response_info,
59 const CompletionCallback& callback) = 0; 82 const CompletionCallback& callback) = 0;
60 83
61 // Reads WebSocket handshake response from the underlying socket. This 84 // Reads WebSocket handshake response from the underlying socket. Must be
62 // function completes when the response headers have been completely 85 // called after SendHandshakeRequest() completes.
63 // received. Must be called after SendHandshakeRequest() completes. 86 //
87 // |callback| will only be called if this method returns ERR_IO_PENDING.
64 virtual int ReadHandshakeResponse(const CompletionCallback& callback) = 0; 88 virtual int ReadHandshakeResponse(const CompletionCallback& callback) = 0;
65 89
66 // Reads WebSocket frame data. This operation finishes when new frame data 90 // Reads WebSocket frame data. This operation finishes when new frame data
67 // becomes available. Each frame message might be chopped off in the middle 91 // becomes available. Each frame message might be chopped off in the middle
68 // as specified in the description of WebSocketFrameChunk struct. 92 // as specified in the description of WebSocketFrameChunk struct.
69 // |frame_chunks| must be valid until the operation completes or Close() 93 // |frame_chunks| remains owned by the caller and must be valid until the
70 // is called. 94 // operation completes or Close() is called. |frame_chunks| must be empty on
95 // calling.
71 // 96 //
72 // This function can be called after ReadHandshakeResponse(). This function 97 // This function should not be called while the previous call of ReadFrames()
yhirano 2013/06/25 07:51:40 Can this function be called before ReadHandshakeRe
Adam Rice 2013/06/25 09:48:10 No, I have moved ReadHandshakeResponse() and SendH
73 // should not be called while the previous call of ReadFrames() is still 98 // is still pending.
74 // pending. 99 //
100 // Returns net::OK or one of the net::ERR_* codes.
101 //
102 // frame_chunks->size() >= 1 if the result is OK.
103 // The callback will not be called until the entire frame header is
yhirano 2013/06/25 07:51:40 How about "This function will not complete until t
Adam Rice 2013/06/25 09:48:10 I have tried to make the comment clearer. Please t
104 // available. If only the first byte is available, the callback will not be
105 // called until the rest arrives. If ReadFrames() is freshly called while only
106 // the first byte is available, it will return ERR_IO_PENDING exactly as if no
107 // data was available.
108 //
109 // Every WebSocketFrameChunk in the vector except the last one is guaranteed
110 // to be a complete 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
yhirano 2013/06/25 07:51:40 Can this function be called before ReadHandshakeRe
Adam Rice 2013/06/25 09:48:10 No, but WebSocketFactory will not attempt to call
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.
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 canceled
92 // at this point, so |frame_chunks| can be freed. 142 // at this point, so |frame_chunks| can be freed.
143 // TODO(ricea): Is this actually needed?
yhirano 2013/06/25 07:51:40 Isn't it? I think it is necessary.
Adam Rice 2013/06/25 09:48:10 Yes, I wrote that comment before implementing WebS
93 virtual void Close() = 0; 144 virtual void Close() = 0;
94 145
146 // The protocol that was negotiated for the stream. If no protocol was
147 // negotiated, then the empty string is returned.
148 virtual std::string Protocol() = 0;
149
150 // The extensions that were negotiated for the stream. Since WebSocketStreams
151 // can be layered, this may be different from what this particular
152 // WebSocketStream implements.
153 virtual std::string Extensions() = 0;
154
95 // TODO(yutak): Add following interfaces: 155 // TODO(yutak): Add following interfaces:
96 // - RenewStreamForAuth for authentication (is this necessary?) 156 // - RenewStreamForAuth for authentication (is this necessary?)
97 // - GetSSLInfo, GetSSLCertRequsetInfo for SSL 157 // - GetSSLInfo, GetSSLCertRequsetInfo for SSL
98 158
99 // WebSocketStreamBase derived functions 159 // WebSocketStreamBase derived functions
100 virtual WebSocketStream* AsWebSocketStream() { return this; } 160 virtual WebSocketStream* AsWebSocketStream() { return this; }
101 161
162 protected:
163 WebSocketStream();
164
102 private: 165 private:
103 DISALLOW_COPY_AND_ASSIGN(WebSocketStream); 166 DISALLOW_COPY_AND_ASSIGN(WebSocketStream);
104 }; 167 };
105 168
106 } // namespace net 169 } // namespace net
107 170
108 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ 171 #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