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_CHANNEL_H_ | |
| 6 #define NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/memory/scoped_vector.h" | |
| 15 #include "googleurl/src/gurl.h" | |
| 16 #include "net/base/net_export.h" | |
| 17 #include "net/websockets/websocket_frame.h" | |
| 18 #include "net/websockets/websocket_stream.h" | |
| 19 | |
| 20 namespace net { | |
| 21 | |
| 22 class URLRequestContext; | |
| 23 class WebSocketEventInterface; | |
| 24 | |
| 25 // Transport-independent implementation of WebSockets. Implements protocol | |
| 26 // semantics that do not depend on the underlying transport. Provides the | |
| 27 // interface to the content layer. | |
| 28 class NET_EXPORT WebSocketChannel { | |
| 29 public: | |
| 30 // Creates a new WebSocketChannel with the specified parameters. | |
| 31 // SendAddChannelRequest() must be called immediately afterwards to start the | |
| 32 // connection process. | |
| 33 WebSocketChannel(const GURL& socket_url, | |
| 34 scoped_ptr<WebSocketEventInterface> event_interface); | |
| 35 virtual ~WebSocketChannel(); | |
| 36 | |
| 37 // Starts the connection process. | |
| 38 void SendAddChannelRequest( | |
| 39 const std::vector<std::string>& requested_protocols, | |
| 40 const GURL& origin, | |
| 41 URLRequestContext* url_request_context); | |
| 42 | |
| 43 // Sends a data frame to the remote side. The frame should usually be no | |
| 44 // larger than 32KB to avoid blocking the IO thread. This method has a hard | |
| 45 // limit of 2GB. It is the responsibility of the caller to ensure that they | |
| 46 // have sufficient send quota to send this data, otherwise the connection will | |
| 47 // be closed without sending. | |
| 48 void SendFrame(bool fin, | |
| 49 WebSocketFrameHeader::OpCode op_code, | |
| 50 const std::vector<char>& data); | |
| 51 | |
| 52 // Sends "quota" units of flow control to the remote side. If the underlying | |
| 53 // transport has a concept of "quota", then it permits the remote server to | |
| 54 // send up to "quota" units of data. | |
| 55 void SendFlowControl(int64 quota); | |
| 56 | |
| 57 // Sends a "Drop Channel" message to the remote side. | |
| 58 void SendDropChannel(uint16 code, const std::string& reason); | |
| 59 | |
| 60 // Starts the connection process, using a different factory function from the | |
| 61 // default. This is used for testing. | |
| 62 void SendAddChannelRequestWithFactory( | |
| 63 const std::vector<std::string>& requested_protocols, | |
| 64 const GURL& origin, | |
| 65 URLRequestContext* url_request_context, | |
| 66 base::Callback<scoped_ptr<WebSocketStreamRequest>( | |
| 67 const GURL&, | |
| 68 const std::vector<std::string>&, | |
| 69 const GURL&, | |
| 70 URLRequestContext*, | |
| 71 const BoundNetLog&, | |
| 72 scoped_ptr<WebSocketStream::ConnectDelegate>)> factory); | |
| 73 | |
| 74 private: | |
| 75 // We have a simple linear progression of states from CONSTRUCTED to CLOSED, | |
|
tyoshino (SeeGerritForStatus)
2013/07/02 08:23:01
FRESHLY_
Adam Rice
2013/07/02 13:49:29
Done.
| |
| 76 // except that the SEND_CLOSED and RECV_CLOSED states may be skipped in case | |
| 77 // of error. | |
| 78 enum State { | |
| 79 FRESHLY_CONSTRUCTED, | |
| 80 CONNECTING, | |
| 81 CONNECTED, | |
| 82 SEND_CLOSED, | |
| 83 RECV_CLOSED, | |
| 84 CLOSED, // also used for FAILED states. | |
| 85 }; | |
| 86 | |
| 87 // When failing a channel, we may or may not want to send the real reason for | |
| 88 // failing to the remote server. This enum is used by FailChannel() to | |
| 89 // choose. | |
| 90 enum ExposeError { | |
| 91 SEND_REAL_ERROR, | |
| 92 SEND_INTERNAL_ERROR, | |
| 93 }; | |
| 94 | |
| 95 // Our implementation of WebSocketStream::ConnectDelegate. We do not inherit | |
| 96 // from WebSocketStream::ConnectDelegate directly to avoid cluttering our | |
| 97 // public interface with the implementation of those methods, and because the | |
| 98 // lifetime of a WebSocketChannel is longer than the lifetime of the | |
| 99 // connection process. | |
| 100 class ConnectDelegate; | |
| 101 | |
| 102 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports | |
| 103 // success to the event interface. | |
| 104 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream); | |
| 105 | |
| 106 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports | |
| 107 // failure to the event interface. | |
| 108 void OnConnectFailure(uint16 websocket_error); | |
| 109 | |
| 110 // Calls WebSocketStream::WriteFrames() with the appropriate arguments | |
| 111 void WriteFrames(); | |
| 112 | |
| 113 // Callback from WebSocketStream::WriteFrames. Sends pending data or | |
| 114 // adjusts the send quota of the renderer channel as appropriate. | |
| 115 void OnWriteDone(int result); | |
| 116 | |
| 117 // Calls WebSocketStream::ReadFrames() with the appropriate arguments. | |
| 118 void ReadFrames(); | |
| 119 | |
| 120 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes | |
| 121 // the returned chunks appropriately to their type. | |
| 122 void OnReadDone(int result); | |
| 123 | |
| 124 // Processes a single chunk that has been read from the stream. | |
| 125 void ProcessFrameChunk(scoped_ptr<WebSocketFrameChunk> chunk); | |
| 126 | |
| 127 // Low-level method to send a single frame. Used for both data and control | |
| 128 // frames. Either sends the frame immediately or buffers it to be scheduled | |
| 129 // when the current write finishes. | |
| 130 void SendIOBufferWithSize(bool fin, | |
| 131 WebSocketFrameHeader::OpCode op_code, | |
| 132 const scoped_refptr<IOBufferWithSize>& buffer); | |
| 133 | |
| 134 // Internal method to fail a channel in a manner appropriate the current | |
| 135 // state. The suppled code and reason are sent back to the renderer; the | |
|
tyoshino (SeeGerritForStatus)
2013/07/02 08:23:01
supplied
Adam Rice
2013/07/02 13:49:29
Done.
| |
| 136 // server just gets a generic "going away" error (if the current state is | |
| 137 // CONNECTED and |expose| is SendInternalError). Resets current_frame_header_ | |
|
tyoshino (SeeGerritForStatus)
2013/07/02 08:23:01
SEND_INTERNAL_ERROR
Adam Rice
2013/07/02 13:49:29
Done.
| |
| 138 // as a side-effect. | |
| 139 void FailChannel(ExposeError expose, | |
| 140 uint16 code, | |
| 141 const std::string& reason); | |
| 142 | |
| 143 // Sends a close frame to Start the WebSocket Closing Handshake, or to respond | |
| 144 // to a close frame from the server. | |
| 145 void SendClose(uint16 code, const std::string& reason); | |
| 146 | |
| 147 // Parses a Close frame. If no status code is supplied, then |code| is set to | |
| 148 // 1005 (No status code) with empty |reason|. If the supplied code is | |
| 149 // outside the valid range, then 1002 (Protocol error) is set instead. If the | |
| 150 // reason text is not valid UTF-8, then |reason| is set to an empty string | |
| 151 // instead. | |
| 152 void ParseClose(const scoped_refptr<IOBufferWithSize>& buffer, | |
| 153 uint16* code, | |
| 154 std::string* reason); | |
| 155 | |
| 156 // The URL to which we connect. | |
| 157 const GURL socket_url_; | |
| 158 | |
| 159 // The event_interface_ object to call back into. | |
| 160 const scoped_ptr<WebSocketEventInterface> event_interface_; | |
| 161 | |
| 162 // The WebSocketStream to which we are sending/receiving data. | |
| 163 scoped_ptr<WebSocketStream> stream_; | |
| 164 | |
| 165 // A data structure containing a vector of frames to be sent and the total | |
| 166 // number of bytes contained in the vector. | |
| 167 struct SendBuffer; | |
| 168 // Data that is currently pending write, or NULL if no write is pending. | |
| 169 scoped_ptr<SendBuffer> data_being_sent_; | |
| 170 // Data that is queued up to write after the current write completes. | |
| 171 // Only non-NULL when such data actually exists. | |
| 172 scoped_ptr<SendBuffer> data_to_send_next_; | |
| 173 | |
| 174 // Destination for the current call to WebSocketStream::ReadFrames | |
| 175 ScopedVector<WebSocketFrameChunk> read_frame_chunks_; | |
| 176 // Frame header for the frame currently being received. Only non-NULL if the | |
| 177 // current frame has been broken into multiple chunks. | |
| 178 scoped_ptr<WebSocketFrameHeader> current_frame_header_; | |
| 179 // Handle to an in-progress WebSocketStream creation request. Only non-NULL | |
| 180 // during the connection process. | |
| 181 scoped_ptr<WebSocketStreamRequest> stream_request_; | |
| 182 // Although it will almost never happen in practice, we can be passed an | |
| 183 // incomplete control frame, in which case we need to keep the data around | |
| 184 // long enough to reassemble it. This variable will be NULL the rest of the | |
| 185 // time. | |
| 186 scoped_refptr<IOBufferWithSize> incomplete_control_frame_body_; | |
| 187 // The point at which we give the renderer a quota refresh (bytes). | |
| 188 int send_quota_low_water_mark_; | |
| 189 // The amount which we refresh the quota to when it reaches the | |
| 190 // low_water_mark (bytes). | |
| 191 int send_quota_high_water_mark_; | |
| 192 // The current amount of quota that the renderer has available for sending | |
| 193 // on this logical channel (bytes). | |
| 194 int current_send_quota_; | |
| 195 | |
| 196 // The current state of the channel. Mainly used for sanity checking, but also | |
| 197 // used to track the close state. | |
| 198 State state_; | |
| 199 | |
| 200 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); | |
| 201 }; | |
| 202 | |
| 203 } // namespace net | |
| 204 | |
| 205 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ | |
| OLD | NEW |