Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: net/websockets/websocket_channel.h

Issue 12764006: WebSocketChannel implementation (Closed) Base URL: http://git.chromium.org/chromium/src.git@web_socket_dispatcher
Patch Set: Small fixes. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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);
yhirano 2013/06/28 10:52:33 nit: We decided that we would use code and reason
Adam Rice 2013/06/28 14:06:41 Done.
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<scoped_ptr<WebSocketStreamRequest>(
59 const GURL&,
60 const std::vector<std::string>&,
61 const GURL&,
62 URLRequestContext*,
63 const BoundNetLog&,
64 scoped_ptr<WebSocketStream::ConnectDelegate>)> factory)
65 NET_EXPORT_PRIVATE;
66
67 private:
68 // We have a simple linear progression of states from CONSTRUCTED to CLOSED,
69 // except that the SEND_CLOSED and RECV_CLOSED states may be skipped in case
70 // of error.
71 enum State {
72 FRESHLY_CONSTRUCTED,
73 CONNECTING,
74 CONNECTED,
75 SEND_CLOSED,
76 RECV_CLOSED,
77 CLOSED, // also used for FAILED states.
78 };
79
80 // When failing a channel, we may or may not want to send the real reason for
81 // failing to the remote server. This enum is used by FailChannel() to
82 // choose.
83 enum ExposeError {
84 SEND_REAL_ERROR,
85 SEND_INTERNAL_ERROR,
86 };
87
88 // Our implementation of WebSocketStream::ConnectDelegate.
89 class ConnectDelegate;
90
91 // Success callback from WebSocketStream::CreateAndConnectStream()
92 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream);
93
94 // Failure callback from WebSocketStream::CreateAndConnectStream()
95 void OnConnectFailure(unsigned short websocket_error);
96
97 // Call WebSocketStream::WriteFrames() with the appropriate arguments
98 void WriteFrames();
99
100 // Callback from WebSocketStream::WriteFrames
101 void OnWriteDone(int result);
102
103 // Call WebSocketStream::ReadFrames() with the appropriate arguments
104 void ReadFrames();
105
106 // Callback from WebSocketStream::ReadFrames
107 void OnReadDone(int result);
108
109 // Process a single chunk that has been read from the stream.
110 void ProcessFrameChunk(scoped_ptr<WebSocketFrameChunk> chunk);
111
112 // Internal Send implementation.
113 void Send(bool fin,
114 WebSocketFrameHeader::OpCode op_code,
115 const std::vector<char>& data);
116
117 // Internal Send implementation (IOBufferWithSize version; used for control
118 // frames).
119 void SendIOBufferWithSize(bool fin,
120 WebSocketFrameHeader::OpCode op_code,
121 const scoped_refptr<IOBufferWithSize>& buffer);
122
123 // Internal method to fail a channel in a manner appropriate the current
124 // state. The suppled code and reason are sent back to the renderer; the
125 // server just gets a generic "going away" error (if it gets a close message
126 // at all).
127 void FailChannel(ExposeError expose,
128 unsigned short code,
129 const std::string& reason);
130
131 // Send a close frame to Start the WebSocket Closing Handshake, or to respond
132 // to a close frame from the server.
133 void SendClose(unsigned short code, const std::string& reason);
134
135 // Parse a Close frame. If no code is supplied, then "1005" is returned with
136 // empty reason_text. If the reason code is outside the valid range, then
137 // "1008" is returned. If the reason text is not valid UTF-8, then an empty
138 // string is returned instead.
139 void ParseClose(const IOBufferWithSize& buffer,
yhirano 2013/06/28 10:52:33 Ditto
Adam Rice 2013/06/28 14:06:41 Done.
140 unsigned short* reason,
141 std::string* reason_text);
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_;
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_;
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
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_;
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698