| 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. |
| 42 void SendFrame(bool fin, |
| 43 WebSocketFrameHeader::OpCode op_code, |
| 44 const std::vector<char>& data); |
| 45 |
| 46 // Send "quota" units of flow control to the remote side. |
| 47 void SendFlowControl(int64 quota); |
| 48 |
| 49 // Send a "Drop Channel" message to the remote side. |
| 50 void SendDropChannel(unsigned short reason, const std::string& reason_text); |
| 51 |
| 52 // Start the connection process, using a different factory function from the |
| 53 // default. This is used for testing. |
| 54 void SendAddChannelRequestWithFactory( |
| 55 const std::vector<std::string>& requested_protocols, |
| 56 const GURL& origin, |
| 57 URLRequestContext* url_request_context, |
| 58 base::Callback<void( |
| 59 const GURL&, |
| 60 const std::vector<std::string>&, |
| 61 const GURL&, |
| 62 URLRequestContext*, |
| 63 const WebSocketStream::SuccessCallback&, |
| 64 const WebSocketStream::FailureCallback&)> factory) NET_EXPORT_PRIVATE; |
| 65 |
| 66 private: |
| 67 // We have a simple linear progression of states from CONSTRUCTED to CLOSED, |
| 68 // except that the SEND_CLOSED and RECV_CLOSED states may be skipped in case |
| 69 // of error. |
| 70 enum State { |
| 71 FRESHLY_CONSTRUCTED, |
| 72 CONNECTING, |
| 73 CONNECTED, |
| 74 SEND_CLOSED, |
| 75 RECV_CLOSED, |
| 76 CLOSED, // also used for FAILED states. |
| 77 }; |
| 78 |
| 79 // Success callback from WebSocketStream::CreateAndConnectStream() |
| 80 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream); |
| 81 |
| 82 // Failure callback from WebSocketStream::CreateAndConnectStream() |
| 83 void OnConnectFailure(unsigned short web_socket_error); |
| 84 |
| 85 // Call WebSocketStream::WriteFrames() with the appropriate arguments |
| 86 void WriteFrames(); |
| 87 |
| 88 // Callback from WebSocketStream::WriteFrames |
| 89 void OnWriteDone(int result); |
| 90 |
| 91 // Call WebSocketStream::ReadFrames() with the appropriate arguments |
| 92 void ReadFrames(); |
| 93 |
| 94 // Callback from WebSocketStream::ReadFrames |
| 95 void OnReadDone(int result); |
| 96 |
| 97 // Process a single chunk that has been read from the stream. |
| 98 void ProcessFrameChunk(scoped_ptr<WebSocketFrameChunk> chunk); |
| 99 |
| 100 // Internal Send implementation. |
| 101 void Send(bool fin, |
| 102 WebSocketFrameHeader::OpCode op_code, |
| 103 const std::vector<char>& data); |
| 104 |
| 105 // Internal Send implementation (IOBufferWithSize version; used for control |
| 106 // frames). |
| 107 void SendIOBufferWithSize(bool fin, |
| 108 WebSocketFrameHeader::OpCode op_code, |
| 109 const scoped_refptr<IOBufferWithSize>& buffer); |
| 110 |
| 111 // Internal method to fail a channel in a manner appropriate the current |
| 112 // state. The suppled code and reason are sent back to the renderer; the |
| 113 // server just gets a generic "going away" error (if it gets a close message |
| 114 // at all). |
| 115 void FailChannel(unsigned short code, const std::string& reason); |
| 116 |
| 117 // Send a close frame to Start the WebSocket Closing Handshake, or to respond |
| 118 // to a close frame from the server. |
| 119 void SendClose(unsigned short code, const std::string& reason); |
| 120 |
| 121 // Parse a Close frame. If no code is supplied, then "1005" is returned with |
| 122 // empty reason_text. If the reason code is outside the valid range, then |
| 123 // "1008" is returned. If the reason text is not valid UTF-8, then an empty |
| 124 // string is returned instead. |
| 125 void ParseClose(const IOBufferWithSize& buffer, |
| 126 unsigned short* reason, |
| 127 std::string* reason_text); |
| 128 |
| 129 // The URL to which we connect. |
| 130 const GURL socket_url_; |
| 131 |
| 132 // The event_interface_ object to call back into. |
| 133 const scoped_ptr<WebSocketEventInterface> event_interface_; |
| 134 |
| 135 // The WebSocketStream to which we are sending/receiving data. |
| 136 scoped_ptr<WebSocketStream> stream_; |
| 137 |
| 138 // A data structure containing a vector of frames to be sent and the total |
| 139 // number of bytes contained in the vector. |
| 140 struct SendBuffer; |
| 141 // Data that is currently pending write, or NULL if no write is pending. |
| 142 scoped_ptr<SendBuffer> currently_sending_; |
| 143 // Data that is queued up to write after the current write completes. |
| 144 // Only non-NULL when such data actually exists. |
| 145 scoped_ptr<SendBuffer> send_next_; |
| 146 // Destination for the current call to WebSocketStream::ReadFrames |
| 147 ScopedVector<WebSocketFrameChunk> read_frame_chunks_; |
| 148 // Frame header for the current frame. Only non-NULL if we have had to |
| 149 // fragment the frame to send to the renderer because not all the data is |
| 150 // available yet. |
| 151 scoped_ptr<WebSocketFrameHeader> current_frame_header_; |
| 152 // Although it will almost never happen in practice, we can be passed an |
| 153 // incomplete control frame, in which case we need to keep the data around |
| 154 // long enough to reassemble it. This vector will be empty the rest of the |
| 155 // time. |
| 156 scoped_refptr<IOBufferWithSize> incomplete_control_frame_body_; |
| 157 // The point at which we give the renderer a quota refresh (bytes). |
| 158 int send_quota_low_water_mark_; |
| 159 // The amount which we refresh the quota to when it reaches the |
| 160 // low_water_mark (bytes). |
| 161 int send_quota_high_water_mark_; |
| 162 // The current amount of quota that the renderer has available for sending |
| 163 // (bytes). |
| 164 int current_send_quota_; |
| 165 // The current state of the channel. Mainly used for sanity checking, but also |
| 166 // used to track the close state. |
| 167 State state_; |
| 168 // We may be destroyed while callbacks are pending, so we need weak pointers |
| 169 // for those callbacks. |
| 170 base::WeakPtrFactory<WebSocketChannel> weak_factory_; |
| 171 |
| 172 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); |
| 173 }; |
| 174 |
| 175 } // namespace net |
| 176 |
| 177 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
| OLD | NEW |