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

Side by Side Diff: mojo/services/network/web_socket_impl.cc

Issue 1873463003: Remove mojo network service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
« no previous file with comments | « mojo/services/network/web_socket_impl.h ('k') | testing/buildbot/chromium.linux.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 #include "mojo/services/network/web_socket_impl.h"
6
7 #include <stdint.h>
8
9 #include <utility>
10
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/message_loop/message_loop.h"
14 #include "mojo/message_pump/handle_watcher.h"
15 #include "mojo/services/network/network_context.h"
16 #include "mojo/services/network/public/cpp/web_socket_read_queue.h"
17 #include "mojo/services/network/public/cpp/web_socket_write_queue.h"
18 #include "net/websockets/websocket_channel.h"
19 #include "net/websockets/websocket_errors.h"
20 #include "net/websockets/websocket_event_interface.h"
21 #include "net/websockets/websocket_frame.h" // for WebSocketFrameHeader::OpCode
22 #include "net/websockets/websocket_handshake_request_info.h"
23 #include "net/websockets/websocket_handshake_response_info.h"
24 #include "url/origin.h"
25
26 namespace mojo {
27
28 template <>
29 struct TypeConverter<net::WebSocketFrameHeader::OpCode,
30 WebSocket::MessageType> {
31 static net::WebSocketFrameHeader::OpCode Convert(
32 WebSocket::MessageType type) {
33 DCHECK(type == WebSocket::MessageType::CONTINUATION ||
34 type == WebSocket::MessageType::TEXT ||
35 type == WebSocket::MessageType::BINARY);
36 typedef net::WebSocketFrameHeader::OpCode OpCode;
37 // These compile asserts verify that the same underlying values are used for
38 // both types, so we can simply cast between them.
39 static_assert(static_cast<OpCode>(WebSocket::MessageType::CONTINUATION) ==
40 net::WebSocketFrameHeader::kOpCodeContinuation,
41 "enum values must match for opcode continuation");
42 static_assert(static_cast<OpCode>(WebSocket::MessageType::TEXT) ==
43 net::WebSocketFrameHeader::kOpCodeText,
44 "enum values must match for opcode text");
45 static_assert(static_cast<OpCode>(WebSocket::MessageType::BINARY) ==
46 net::WebSocketFrameHeader::kOpCodeBinary,
47 "enum values must match for opcode binary");
48 return static_cast<OpCode>(type);
49 }
50 };
51
52 template <>
53 struct TypeConverter<WebSocket::MessageType,
54 net::WebSocketFrameHeader::OpCode> {
55 static WebSocket::MessageType Convert(
56 net::WebSocketFrameHeader::OpCode type) {
57 DCHECK(type == net::WebSocketFrameHeader::kOpCodeContinuation ||
58 type == net::WebSocketFrameHeader::kOpCodeText ||
59 type == net::WebSocketFrameHeader::kOpCodeBinary);
60 return static_cast<WebSocket::MessageType>(type);
61 }
62 };
63
64 namespace {
65
66 typedef net::WebSocketEventInterface::ChannelState ChannelState;
67
68 struct WebSocketEventHandler : public net::WebSocketEventInterface {
69 public:
70 WebSocketEventHandler(WebSocketClientPtr client)
71 : client_(std::move(client)) {}
72 ~WebSocketEventHandler() override {}
73
74 private:
75 // net::WebSocketEventInterface methods:
76 ChannelState OnAddChannelResponse(const std::string& selected_subprotocol,
77 const std::string& extensions) override;
78 ChannelState OnDataFrame(bool fin,
79 WebSocketMessageType type,
80 const std::vector<char>& data) override;
81 ChannelState OnClosingHandshake() override;
82 ChannelState OnFlowControl(int64_t quota) override;
83 ChannelState OnDropChannel(bool was_clean,
84 uint16_t code,
85 const std::string& reason) override;
86 ChannelState OnFailChannel(const std::string& message) override;
87 ChannelState OnStartOpeningHandshake(
88 scoped_ptr<net::WebSocketHandshakeRequestInfo> request) override;
89 ChannelState OnFinishOpeningHandshake(
90 scoped_ptr<net::WebSocketHandshakeResponseInfo> response) override;
91 ChannelState OnSSLCertificateError(
92 scoped_ptr<net::WebSocketEventInterface::SSLErrorCallbacks> callbacks,
93 const GURL& url,
94 const net::SSLInfo& ssl_info,
95 bool fatal) override;
96
97 // Called once we've written to |receive_stream_|.
98 void DidWriteToReceiveStream(bool fin,
99 net::WebSocketFrameHeader::OpCode type,
100 uint32_t num_bytes,
101 const char* buffer);
102 WebSocketClientPtr client_;
103 ScopedDataPipeProducerHandle receive_stream_;
104 scoped_ptr<WebSocketWriteQueue> write_queue_;
105
106 DISALLOW_COPY_AND_ASSIGN(WebSocketEventHandler);
107 };
108
109 ChannelState WebSocketEventHandler::OnAddChannelResponse(
110 const std::string& selected_protocol,
111 const std::string& extensions) {
112 DataPipe data_pipe;
113 receive_stream_ = std::move(data_pipe.producer_handle);
114 write_queue_.reset(new WebSocketWriteQueue(receive_stream_.get()));
115 client_->DidConnect(selected_protocol, extensions,
116 std::move(data_pipe.consumer_handle));
117 return WebSocketEventInterface::CHANNEL_ALIVE;
118 }
119
120 ChannelState WebSocketEventHandler::OnDataFrame(
121 bool fin,
122 net::WebSocketFrameHeader::OpCode type,
123 const std::vector<char>& data) {
124 uint32_t size = static_cast<uint32_t>(data.size());
125 write_queue_->Write(
126 &data[0], size,
127 base::Bind(&WebSocketEventHandler::DidWriteToReceiveStream,
128 base::Unretained(this),
129 fin, type, size));
130 return WebSocketEventInterface::CHANNEL_ALIVE;
131 }
132
133 ChannelState WebSocketEventHandler::OnClosingHandshake() {
134 return WebSocketEventInterface::CHANNEL_ALIVE;
135 }
136
137 ChannelState WebSocketEventHandler::OnFlowControl(int64_t quota) {
138 client_->DidReceiveFlowControl(quota);
139 return WebSocketEventInterface::CHANNEL_ALIVE;
140 }
141
142 ChannelState WebSocketEventHandler::OnDropChannel(bool was_clean,
143 uint16_t code,
144 const std::string& reason) {
145 client_->DidClose(was_clean, code, reason);
146 return WebSocketEventInterface::CHANNEL_DELETED;
147 }
148
149 ChannelState WebSocketEventHandler::OnFailChannel(const std::string& message) {
150 client_->DidFail(message);
151 return WebSocketEventInterface::CHANNEL_DELETED;
152 }
153
154 ChannelState WebSocketEventHandler::OnStartOpeningHandshake(
155 scoped_ptr<net::WebSocketHandshakeRequestInfo> request) {
156 return WebSocketEventInterface::CHANNEL_ALIVE;
157 }
158
159 ChannelState WebSocketEventHandler::OnFinishOpeningHandshake(
160 scoped_ptr<net::WebSocketHandshakeResponseInfo> response) {
161 return WebSocketEventInterface::CHANNEL_ALIVE;
162 }
163
164 ChannelState WebSocketEventHandler::OnSSLCertificateError(
165 scoped_ptr<net::WebSocketEventInterface::SSLErrorCallbacks> callbacks,
166 const GURL& url,
167 const net::SSLInfo& ssl_info,
168 bool fatal) {
169 client_->DidFail("SSL Error");
170 return WebSocketEventInterface::CHANNEL_DELETED;
171 }
172
173 void WebSocketEventHandler::DidWriteToReceiveStream(
174 bool fin,
175 net::WebSocketFrameHeader::OpCode type,
176 uint32_t num_bytes,
177 const char* buffer) {
178 client_->DidReceiveData(
179 fin, ConvertTo<WebSocket::MessageType>(type), num_bytes);
180 }
181
182 } // namespace mojo
183
184 WebSocketImpl::WebSocketImpl(NetworkContext* context,
185 scoped_ptr<mojo::MessageLoopRef> app_refcount,
186 InterfaceRequest<WebSocket> request)
187 : context_(context),
188 app_refcount_(std::move(app_refcount)),
189 binding_(this, std::move(request)) {}
190
191 WebSocketImpl::~WebSocketImpl() {
192 }
193
194 void WebSocketImpl::Connect(const String& url,
195 Array<String> protocols,
196 const String& origin,
197 ScopedDataPipeConsumerHandle send_stream,
198 WebSocketClientPtr client) {
199 DCHECK(!channel_);
200 send_stream_ = std::move(send_stream);
201 read_queue_.reset(new WebSocketReadQueue(send_stream_.get()));
202 scoped_ptr<net::WebSocketEventInterface> event_interface(
203 new WebSocketEventHandler(std::move(client)));
204 channel_.reset(new net::WebSocketChannel(std::move(event_interface),
205 context_->url_request_context()));
206 channel_->SendAddChannelRequest(GURL(url.get()),
207 protocols.To<std::vector<std::string>>(),
208 url::Origin(GURL(origin.get())));
209 }
210
211 void WebSocketImpl::Send(bool fin,
212 WebSocket::MessageType type,
213 uint32_t num_bytes) {
214 DCHECK(channel_);
215 read_queue_->Read(num_bytes,
216 base::Bind(&WebSocketImpl::DidReadFromSendStream,
217 base::Unretained(this),
218 fin, type, num_bytes));
219 }
220
221 void WebSocketImpl::FlowControl(int64_t quota) {
222 DCHECK(channel_);
223 channel_->SendFlowControl(quota);
224 }
225
226 void WebSocketImpl::Close(uint16_t code, const String& reason) {
227 DCHECK(channel_);
228 channel_->StartClosingHandshake(code, reason);
229 }
230
231 void WebSocketImpl::DidReadFromSendStream(bool fin,
232 WebSocket::MessageType type,
233 uint32_t num_bytes,
234 const char* data) {
235 std::vector<char> buffer(num_bytes);
236 memcpy(&buffer[0], data, num_bytes);
237 DCHECK(channel_);
238 channel_->SendFrame(
239 fin, ConvertTo<net::WebSocketFrameHeader::OpCode>(type), buffer);
240 }
241
242 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/network/web_socket_impl.h ('k') | testing/buildbot/chromium.linux.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698