Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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. | |
| 4 | |
| 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | |
| 6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "net/websockets/websocket_frame_parser.h" | |
| 15 #include "net/websockets/websocket_stream.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 class ClientSocketHandle; | |
| 20 class DrainableIOBuffer; | |
| 21 class GrowableIOBuffer; | |
| 22 class HttpRequestHeaders; | |
| 23 class HttpResponseInfo; | |
| 24 class IOBufferWithSize; | |
| 25 struct WebSocketFrameChunk; | |
| 26 | |
| 27 // Implementation of WebSocketStream for non-multiplexed ws:// connections (or | |
| 28 // the physical side of a multiplexed ws:// connection). | |
| 29 class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream { | |
| 30 public: | |
| 31 // The type of a pointer to a function that generates a WebSocketMaskingKey. | |
|
szym
2013/08/27 22:54:04
Spurious comment.
Adam Rice
2013/08/28 12:01:11
Removed.
| |
| 32 typedef WebSocketMaskingKey (*WebSocketMaskingKeyGeneratorFunction)(); | |
| 33 | |
| 34 // This class should not normally be constructed directly; see | |
| 35 // WebSocketStream::CreateAndConnectStream. | |
| 36 explicit WebSocketBasicStream(scoped_ptr<ClientSocketHandle> connection); | |
| 37 | |
| 38 // The destructor has to make sure the connection is closed when we finish so | |
| 39 // that it does not get returned to the pool. | |
| 40 virtual ~WebSocketBasicStream(); | |
| 41 | |
| 42 // Reads one or more frame chunks. See WebSocketStream for the full | |
| 43 // specifications of the behaviour. | |
|
szym
2013/08/27 22:54:04
Remove all comments on the OVERRIDEN methods if th
Adam Rice
2013/08/28 12:01:11
Thanks. That helps.
| |
| 44 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | |
| 45 const CompletionCallback& callback) OVERRIDE; | |
| 46 | |
| 47 // Writes one or more frame chunks. Either writes everything or fails. | |
| 48 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | |
| 49 const CompletionCallback& callback) OVERRIDE; | |
| 50 | |
| 51 // Closes the stream, cancelling all pending IO operations without calling the | |
| 52 // callbacks. | |
| 53 virtual void Close() OVERRIDE; | |
| 54 | |
| 55 // Returns the optional sub-protocol that was negotiated with the server. | |
| 56 virtual std::string GetSubProtocol() const OVERRIDE; | |
| 57 | |
| 58 // Returns the extensions that were negotiated with the server. | |
| 59 virtual std::string GetExtensions() const OVERRIDE; | |
| 60 | |
| 61 // Writes WebSocket handshake request HTTP-style to the connection. Adds | |
| 62 // "Sec-WebSocket-Key" header; this should not be included in |headers|. | |
| 63 // | |
| 64 // "callback" will only be called if this method returns ERR_IO_PENDING. | |
| 65 // | |
| 66 // |response_info| must remain valid until the callback from | |
| 67 // ReadHandshakeResponse has been called. | |
| 68 virtual int SendHandshakeRequest(const GURL& url, | |
| 69 const HttpRequestHeaders& headers, | |
| 70 HttpResponseInfo* response_info, | |
| 71 const CompletionCallback& callback) OVERRIDE; | |
| 72 | |
| 73 // Reads WebSocket handshake response from the connection. The callback | |
| 74 // function completes when the response headers have been completely | |
| 75 // received. Must be called after SendHandshakeRequest() completes (including | |
| 76 // the callback, if relevant). If the result was not available synchronously, | |
| 77 // then it will be passed to the callback. Note that the actual response info | |
| 78 // is passed into the HttpResponseInfo object that was passed to | |
| 79 // SendHandshakeResponse. | |
| 80 virtual int ReadHandshakeResponse( | |
| 81 const CompletionCallback& callback) OVERRIDE; | |
| 82 | |
| 83 //////////////////////////////////////////////////////////////////////////// | |
| 84 // Methods for testing only. | |
| 85 | |
| 86 // Creates a WebSocketBasicStream with the specified |connection|, | |
| 87 // |http_read_buffer|, |sub_protocol|, |extensions| and | |
| 88 // |key_generator_function|. | |
|
szym
2013/08/27 22:54:04
Spurious comment.
Adam Rice
2013/08/28 12:01:11
Removed.
| |
| 89 static scoped_ptr<WebSocketBasicStream> CreateWebSocketBasicStreamForTesting( | |
| 90 scoped_ptr<ClientSocketHandle> connection, | |
| 91 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, | |
| 92 const std::string& sub_protocol, | |
| 93 const std::string& extensions, | |
| 94 WebSocketMaskingKeyGeneratorFunction key_generator_function); | |
| 95 | |
| 96 private: | |
| 97 // The semantics of WriteFrames() are such that it only returns OK or calls | |
|
szym
2013/08/27 22:54:04
Do not elaborate on the implementation in the head
Adam Rice
2013/08/28 12:01:11
Done.
| |
| 98 // the callback when everything is written or something has failed. But | |
| 99 // Socket::Write() can do partial writes, so this method wraps the call in a | |
| 100 // loop. | |
| 101 int WriteEverything(const scoped_refptr<DrainableIOBuffer>& buffer, | |
| 102 const CompletionCallback& callback); | |
| 103 | |
| 104 // Wraps the |callback| to continue writing until everything has been written. | |
| 105 void WriteDone(const scoped_refptr<DrainableIOBuffer>& buffer, | |
|
szym
2013/08/27 22:54:04
Suggest: OnWriteComplete or OnWriteDone
Adam Rice
2013/08/28 12:01:11
Done.
| |
| 106 const CompletionCallback& callback, | |
| 107 int result); | |
| 108 | |
| 109 // Called when a read completes. Parses the result and (unless no complete | |
| 110 // header has been received) calls |callback|. | |
| 111 void ReadDone(ScopedVector<WebSocketFrameChunk>* frame_chunks, | |
|
szym
2013/08/27 22:54:04
Suggest: OnReadComplete or OnReadDone
Adam Rice
2013/08/28 12:01:11
Done.
| |
| 112 const CompletionCallback& callback, | |
| 113 int result); | |
| 114 | |
| 115 // Storage for pending reads. All active WebSockets spend all the time with a | |
| 116 // call to ReadFrames() pending, so there is no benefit in trying to share | |
| 117 // this between sockets. | |
| 118 scoped_refptr<IOBufferWithSize> read_buffer_; | |
| 119 | |
| 120 // The connection, wrapped in a ClientSocketHandle so that we can prevent it | |
| 121 // from being returned to the pool. | |
|
szym
2013/08/27 22:54:04
connection_ is used only for its socket_. The sock
szym
2013/08/27 23:51:30
Disregard this comment. akalin@ explained to me in
Adam Rice
2013/08/28 12:01:11
Done.
| |
| 122 scoped_ptr<ClientSocketHandle> connection_; | |
| 123 | |
| 124 // Only used during handshake. Some data may be left in this buffer after the | |
| 125 // handshake, in which case it will be picked up during the first call to | |
| 126 // ReadFrames(). | |
| 127 scoped_refptr<GrowableIOBuffer> http_read_buffer_; | |
|
szym
2013/08/27 22:54:04
This appears to be used only in tests. Does it hav
Adam Rice
2013/08/28 12:01:11
Yes. I left it in this CL because the first call t
szym
2013/08/28 21:17:54
I understand that ReadFrames would use it if set,
Adam Rice
2013/08/29 13:03:23
I added a comment indicating why it is a GrowableI
| |
| 128 | |
| 129 // This keeps the current parse state (including any incomplete headers) and | |
| 130 // parses frames. | |
| 131 WebSocketFrameParser parser_; | |
| 132 | |
| 133 // The negotated sub-protocol, or empty for none. | |
| 134 std::string sub_protocol_; | |
| 135 | |
| 136 // The extensions negotiated with the remote server. | |
| 137 std::string extensions_; | |
| 138 | |
| 139 // This can be overridden in tests to make the output deterministic. We don't | |
| 140 // use a Callback here because a function pointer is faster and good enough | |
| 141 // for our purposes. | |
| 142 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_; | |
| 143 }; | |
| 144 | |
| 145 } // namespace net | |
| 146 | |
| 147 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | |
| OLD | NEW |