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