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

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

Powered by Google App Engine
This is Rietveld 408576698