Chromium Code Reviews| 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" | |
| 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 layers, 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. The actual | |
| 52 // concrete type returned depends on whether multiplexing or SPDY are being | |
| 53 // used to communicate with the remote server. If the handshake completed | |
| 54 // successfully, then the on_success callback is called with a subclass of | |
| 55 // WebSocketStream. If it failed, then the on_failure callback is called with | |
| 56 // a WebSocket result code corresponding to the error. | |
| 57 static void CreateAndConnectStream( | |
| 58 const GURL& socket_url, | |
| 59 const std::vector<std::string>& requested_protocols, | |
| 60 const GURL& origin, | |
| 61 URLRequestContext* url_request_context, | |
| 62 const SuccessCallback& on_success, | |
| 63 const FailureCallback& on_failure); | |
| 37 | 64 |
| 38 // Derived classes must make sure Close() is called when the stream is not | 65 // Derived classes must make sure Close() is called when the stream is not |
| 39 // closed on destruction. | 66 // closed on destruction. |
| 40 virtual ~WebSocketStream() {} | 67 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 | 68 |
| 66 // Reads WebSocket frame data. This operation finishes when new frame data | 69 // Reads WebSocket frame data. This operation finishes when new frame data |
| 67 // becomes available. Each frame message might be chopped off in the middle | 70 // becomes available. Each frame message might be chopped off in the middle |
| 68 // as specified in the description of WebSocketFrameChunk struct. | 71 // as specified in the description of WebSocketFrameChunk struct. |
| 69 // |frame_chunks| must be valid until the operation completes or Close() | 72 // |frame_chunks| remains owned by the caller and must be valid until the |
| 70 // is called. | 73 // operation completes or Close() is called. |frame_chunks| must be empty on |
| 74 // calling. | |
| 71 // | 75 // |
| 72 // This function can be called after ReadHandshakeResponse(). This function | 76 // 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 | 77 // is still pending. |
| 74 // pending. | 78 // |
| 79 // Returns net::OK or one of the net::ERR_* codes. | |
| 80 // | |
| 81 // frame_chunks->size() >= 1 if the result is OK. | |
| 82 // | |
| 83 // A frame with an incomplete header will never be inserted into | |
| 84 // |frame_chunks|. If the currently available bytes of a new frame do not form | |
| 85 // a complete frame header, then the implementation will buffer them until all | |
| 86 // the fields in the WebSocketFrameHeader object can be filled. If | |
| 87 // ReadFrames() is freshly called in this situation, it will return | |
| 88 // ERR_IO_PENDING exactly as if no data was available. | |
| 89 // | |
| 90 // Every WebSocketFrameChunk in the vector except the first and last is | |
| 91 // guaranteed to be a complete frame. The first chunk may be the final part | |
| 92 // of the previous frame. The last chunk may be the first part of a new | |
| 93 // frame. If there is only one chunk, then it may consist of data from the | |
| 94 // middle part of a frame. | |
| 95 // | |
| 96 // When the socket is closed on the remote side, this method will return | |
| 97 // ERR_CONNECTION_CLOSED. It will not return OK with an empty vector. | |
| 98 // | |
| 99 // If the connection is closed in the middle of receiving an incomplete frame, | |
| 100 // ReadFrames may discard the incomplete frame. Since the renderer will | |
| 101 // discard any incomplete messages when the connection is closed, this makes | |
| 102 // no difference to the overall semantics. | |
| 75 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | 103 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, |
| 76 const CompletionCallback& callback) = 0; | 104 const CompletionCallback& callback) = 0; |
| 77 | 105 |
| 78 // Writes WebSocket frame data. |frame_chunks| must obey the rule specified | 106 // Writes WebSocket frame data. |frame_chunks| must obey the rule specified |
| 79 // in the documentation of WebSocketFrameChunk struct: the first chunk of | 107 // in the documentation of WebSocketFrameChunk struct: the first chunk of |
| 80 // a WebSocket frame must contain non-NULL |header|, and the last chunk must | 108 // 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 | 109 // 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 | 110 // WebSocket frame must be consistent (the total length of |data| fields must |
| 83 // match |header->payload_length|). |frame_chunks| must be valid until the | 111 // match |header->payload_length|). |frame_chunks| must be valid until the |
| 84 // operation completes or Close() is called. | 112 // operation completes or Close() is called. |
| 85 // | 113 // |
| 86 // This function can be called after ReadHandshakeResponse(). This function | 114 // 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. | 115 // still pending. |
| 116 // | |
| 117 // Support for incomplete frames is not guaranteed and may be removed from | |
| 118 // future iterations of the API. | |
| 119 // | |
| 120 // This method will only return OK if all frames were written completely. | |
| 121 // Otherwise it will return an appropriate ERR_ code. | |
| 88 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | 122 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, |
| 89 const CompletionCallback& callback) = 0; | 123 const CompletionCallback& callback) = 0; |
| 90 | 124 |
| 91 // Closes the stream. All pending I/O operations (if any) are canceled | 125 // Closes the stream. All pending I/O operations (if any) are cancelled |
| 92 // at this point, so |frame_chunks| can be freed. | 126 // at this point, so |frame_chunks| can be freed. |
| 93 virtual void Close() = 0; | 127 virtual void Close() = 0; |
| 94 | 128 |
| 129 // The protocol that was negotiated for the stream. If no protocol was | |
| 130 // negotiated, then the empty string is returned. | |
| 131 virtual std::string Protocol() = 0; | |
|
tyoshino (SeeGerritForStatus)
2013/06/26 04:05:10
GetProtocol?
Adam Rice
2013/06/26 05:57:00
Done.
| |
| 132 | |
| 133 // The extensions that were negotiated for the stream. Since WebSocketStreams | |
| 134 // can be layered, this may be different from what this particular | |
| 135 // WebSocketStream implements. | |
| 136 virtual std::string Extensions() = 0; | |
|
tyoshino (SeeGerritForStatus)
2013/06/26 04:05:10
GetExtensions?
Adam Rice
2013/06/26 05:57:00
Done.
| |
| 137 | |
| 95 // TODO(yutak): Add following interfaces: | 138 // TODO(yutak): Add following interfaces: |
| 96 // - RenewStreamForAuth for authentication (is this necessary?) | 139 // - RenewStreamForAuth for authentication (is this necessary?) |
| 97 // - GetSSLInfo, GetSSLCertRequsetInfo for SSL | 140 // - GetSSLInfo, GetSSLCertRequestInfo for SSL |
| 98 | 141 |
| 99 // WebSocketStreamBase derived functions | 142 // WebSocketStreamBase derived functions |
| 100 virtual WebSocketStream* AsWebSocketStream() { return this; } | 143 virtual WebSocketStream* AsWebSocketStream() { return this; } |
| 101 | 144 |
| 145 //////////////////////////////////////////////////////////////////////////// | |
| 146 // Methods used during the stream handshake. These must not be called once a | |
| 147 // WebSocket protocol stream has been established (ie. after the | |
| 148 // SuccessCallback or FailureCallback has been called.) | |
| 149 | |
| 150 // Writes WebSocket handshake request to the underlying socket. Must be called | |
| 151 // before ReadHandshakeResponse(). | |
| 152 // | |
| 153 // |callback| will only be called if this method returns ERR_IO_PENDING. | |
| 154 // | |
| 155 // |response_info| must remain valid until the callback from | |
| 156 // ReadHandshakeResponse has been called. | |
| 157 // | |
| 158 // TODO(ricea): This function is only used during the handshake and is | |
| 159 // probably only applicable to certain subclasses of WebSocketStream. Move it | |
| 160 // somewhere else? Also applies to ReadHandshakeResponse. | |
| 161 virtual int SendHandshakeRequest(const GURL& url, | |
| 162 const HttpRequestHeaders& headers, | |
| 163 HttpResponseInfo* response_info, | |
| 164 const CompletionCallback& callback) = 0; | |
| 165 | |
| 166 // Reads WebSocket handshake response from the underlying socket. Must be | |
| 167 // called after SendHandshakeRequest() completes. | |
| 168 // | |
| 169 // |callback| will only be called if this method returns ERR_IO_PENDING. | |
| 170 virtual int ReadHandshakeResponse(const CompletionCallback& callback) = 0; | |
| 171 | |
| 172 protected: | |
| 173 WebSocketStream(); | |
| 174 | |
| 102 private: | 175 private: |
| 103 DISALLOW_COPY_AND_ASSIGN(WebSocketStream); | 176 DISALLOW_COPY_AND_ASSIGN(WebSocketStream); |
| 104 }; | 177 }; |
| 105 | 178 |
| 106 } // namespace net | 179 } // namespace net |
| 107 | 180 |
| 108 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ | 181 #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_H_ |
| OLD | NEW |