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

Side by Side Diff: content/renderer/websockethandle_impl.cc

Issue 2119973002: Port WebSockets to Mojo IPC (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix shared worker support Created 4 years, 4 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 #include "content/renderer/websockethandle_impl.h"
6
7 #include <stdint.h>
8 #include <string.h>
9
10 #include <string>
11 #include <utility>
12
13 #include "base/logging.h"
14 #include "base/strings/string_util.h"
15 #include "third_party/WebKit/public/platform/InterfaceProvider.h"
16 #include "third_party/WebKit/public/platform/Platform.h"
17 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/platform/WebURL.h"
20 #include "third_party/WebKit/public/platform/WebVector.h"
21 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandle. h"
22 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandleC lient.h"
23 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandsha keRequestInfo.h"
24 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandsha keResponseInfo.h"
25 #include "url/gurl.h"
26 #include "url/origin.h"
27
28 using blink::WebSecurityOrigin;
29 using blink::WebSocketHandle;
30 using blink::WebSocketHandleClient;
31 using blink::WebString;
32 using blink::WebURL;
33 using blink::WebVector;
34
35 namespace content {
36 namespace {
37
38 const uint16_t kAbnormalShutdownOpCode = 1006;
39
40 } // namespace
41
42 WebSocketHandleImpl::WebSocketHandleImpl(
43 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
44 : client_(nullptr),
45 client_binding_(this),
46 task_runner_(std::move(task_runner)),
47 did_initialize_(false) {
48 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
49 << " created";
50 }
51
52 void WebSocketHandleImpl::Initialize(
53 blink::InterfaceProvider* interface_provider) {
54 DCHECK(!websocket_);
55 DCHECK(!did_initialize_);
56
57 interface_provider->getInterface(mojo::GetProxy(&websocket_));
58
59 websocket_.set_connection_error_handler(
60 base::Bind(&WebSocketHandleImpl::OnConnectionError,
61 base::Unretained(this)));
62 did_initialize_ = true;
63 }
64
65 void WebSocketHandleImpl::connect(const WebURL& url,
66 const WebVector<WebString>& protocols,
67 const WebSecurityOrigin& origin,
68 const WebURL& first_party_for_cookies,
69 const WebString& user_agent_override,
70 WebSocketHandleClient* client) {
71 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
72 << " Connect(" << url << ", " << origin.toString().utf8() << ")";
73
74 if (!did_initialize_)
jam 2016/08/04 18:10:43 is did_initialize_ redundant, e.g. you can check w
75 Initialize(blink::Platform::current()->interfaceProvider());
76
77 DCHECK(websocket_);
78
79 DCHECK(!client_);
80 DCHECK(client);
81 client_ = client;
82
83 mojo::Array<mojo::String> protocols_to_pass(protocols.size());
84 for (size_t i = 0; i < protocols.size(); ++i)
85 protocols_to_pass[i] = protocols[i].utf8();
86
87 websocket_->AddChannelRequest(
88 url,
89 std::move(protocols_to_pass),
90 origin,
91 first_party_for_cookies,
92 user_agent_override.latin1(),
93 client_binding_.CreateInterfacePtrAndBind(task_runner_));
94 }
95
96 void WebSocketHandleImpl::send(bool fin,
97 WebSocketHandle::MessageType type,
98 const char* data,
99 size_t size) {
100 DCHECK(websocket_);
101
102 mojom::WebSocketMessageType type_to_pass;
103 switch (type) {
104 case WebSocketHandle::MessageTypeContinuation:
105 type_to_pass = mojom::WebSocketMessageType::CONTINUATION;
106 break;
107 case WebSocketHandle::MessageTypeText:
108 type_to_pass = mojom::WebSocketMessageType::TEXT;
109 break;
110 case WebSocketHandle::MessageTypeBinary:
111 type_to_pass = mojom::WebSocketMessageType::BINARY;
112 break;
113 default:
114 NOTREACHED();
115 return;
116 }
117
118 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
119 << " Send(" << fin << ", " << type_to_pass << ", "
120 << "(data size = " << size << "))";
121
122 mojo::Array<uint8_t> data_to_pass(size);
123 std::copy(data, data + size, data_to_pass.begin());
124
125 websocket_->SendFrame(fin, type_to_pass, std::move(data_to_pass));
126 }
127
128 void WebSocketHandleImpl::flowControl(int64_t quota) {
129 DCHECK(websocket_);
130
131 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
132 << " FlowControl(" << quota << ")";
133
134 websocket_->SendFlowControl(quota);
135 }
136
137 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) {
138 DCHECK(websocket_);
139
140 std::string reason_to_pass = reason.utf8();
141
142 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
143 << " Close(" << code << ", " << reason_to_pass << ")";
144
145 websocket_->StartClosingHandshake(code, reason_to_pass);
146 }
147
148 WebSocketHandleImpl::~WebSocketHandleImpl() {
149 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
150 << " deleted";
151
152 if (websocket_)
153 websocket_->StartClosingHandshake(kAbnormalShutdownOpCode, std::string());
154 }
155
156 void WebSocketHandleImpl::Disconnect() {
157 websocket_.reset();
158 client_ = nullptr;
159 }
160
161 void WebSocketHandleImpl::OnConnectionError() {
162 // Our connection to the WebSocket was dropped. This could be due to
163 // exceeding the maximum number of concurrent websockets from this process.
164
165 // TODO(darin): This error message is overly specific. We don't know for sure
166 // that this is the only reason we'd get here. This should be more generic or
167 // we should figure out how to make it more specific.
168 OnFailChannel("Error in connection establishment: "
169 "net::ERR_INSUFFICIENT_RESOURCES");
170 }
171
172 void WebSocketHandleImpl::OnFailChannel(const mojo::String& message) {
173 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
174 << " OnFailChannel(" << message << ")";
175
176 WebSocketHandleClient* client = client_;
177 Disconnect();
178 if (!client)
179 return;
180
181 client->didFail(this, WebString::fromUTF8(message));
182 // |this| can be deleted here.
183 }
184
185 void WebSocketHandleImpl::OnStartOpeningHandshake(
186 mojom::WebSocketHandshakeRequestPtr request) {
187 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
188 << " OnStartOpeningHandshake(" << request->url << ")";
189 // All strings are already encoded to ASCII in the browser.
190 blink::WebSocketHandshakeRequestInfo request_to_pass;
191 request_to_pass.setURL(WebURL(request->url));
192 for (size_t i = 0; i < request->headers.size(); ++i) {
193 const mojom::HttpHeaderPtr& header = request->headers[i];
194 request_to_pass.addHeaderField(WebString::fromLatin1(header->name),
195 WebString::fromLatin1(header->value));
196 }
197 request_to_pass.setHeadersText(WebString::fromLatin1(request->headers_text));
198 client_->didStartOpeningHandshake(this, request_to_pass);
199 }
200
201 void WebSocketHandleImpl::OnFinishOpeningHandshake(
202 mojom::WebSocketHandshakeResponsePtr response) {
203 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
204 << " OnFinishOpeningHandshake(" << response->url << ")";
205
206 // All strings are already encoded to ASCII in the browser.
207 blink::WebSocketHandshakeResponseInfo response_to_pass;
208 response_to_pass.setStatusCode(response->status_code);
209 response_to_pass.setStatusText(WebString::fromLatin1(response->status_text));
210 for (size_t i = 0; i < response->headers.size(); ++i) {
211 const mojom::HttpHeaderPtr& header = response->headers[i];
212 response_to_pass.addHeaderField(WebString::fromLatin1(header->name),
213 WebString::fromLatin1(header->value));
214 }
215 response_to_pass.setHeadersText(
216 WebString::fromLatin1(response->headers_text));
217 client_->didFinishOpeningHandshake(this, response_to_pass);
218 }
219
220 void WebSocketHandleImpl::OnAddChannelResponse(
221 const mojo::String& protocol,
222 const mojo::String& extensions) {
223 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
224 << " OnAddChannelResponse("
225 << protocol << ", " << extensions << ")";
226
227 if (!client_)
228 return;
229
230 client_->didConnect(this,
231 WebString::fromUTF8(protocol),
232 WebString::fromUTF8(extensions));
233 // |this| can be deleted here.
234 }
235
236 void WebSocketHandleImpl::OnDataFrame(
237 bool fin, mojom::WebSocketMessageType type,
238 mojo::Array<uint8_t> data) {
239 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
240 << " OnDataFrame(" << fin << ", " << type << ", "
241 << "(data size = " << data.size() << "))";
242 if (!client_)
243 return;
244
245 WebSocketHandle::MessageType type_to_pass =
246 WebSocketHandle::MessageTypeContinuation;
247 switch (type) {
248 case mojom::WebSocketMessageType::CONTINUATION:
249 type_to_pass = WebSocketHandle::MessageTypeContinuation;
250 break;
251 case mojom::WebSocketMessageType::TEXT:
252 type_to_pass = WebSocketHandle::MessageTypeText;
253 break;
254 case mojom::WebSocketMessageType::BINARY:
255 type_to_pass = WebSocketHandle::MessageTypeBinary;
256 break;
257 }
258 const char* data_to_pass =
259 reinterpret_cast<const char*>(data.empty() ? nullptr : &data[0]);
260 client_->didReceiveData(this, fin, type_to_pass, data_to_pass, data.size());
261 // |this| can be deleted here.
262 }
263
264 void WebSocketHandleImpl::OnFlowControl(int64_t quota) {
265 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
266 << " OnFlowControl(" << quota << ")";
267 if (!client_)
268 return;
269
270 client_->didReceiveFlowControl(this, quota);
271 // |this| can be deleted here.
272 }
273
274 void WebSocketHandleImpl::OnDropChannel(bool was_clean, uint16_t code,
275 const mojo::String& reason) {
276 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
277 << " OnDropChannel(" << was_clean << ", " << code << ", "
278 << reason << ")";
279
280 WebSocketHandleClient* client = client_;
281 Disconnect();
282 if (!client)
283 return;
284
285 client->didClose(this, was_clean, code, WebString::fromUTF8(reason));
286 // |this| can be deleted here.
287 }
288
289 void WebSocketHandleImpl::OnClosingHandshake() {
290 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this)
291 << " OnClosingHandshake()";
292 if (!client_)
293 return;
294
295 client_->didStartClosingHandshake(this);
296 // |this| can be deleted here.
297 }
298
299 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698