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

Side by Side Diff: third_party/WebKit/Source/modules/websockets/WebSocketHandleImpl.cpp

Issue 2284473002: Move WebSocketHandleImpl into Blink (take 2) (Closed)
Patch Set: Extract WebSocketHandle as pure interface Created 4 years, 3 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 "modules/websockets/WebSocketHandleImpl.h"
6
7 #include "modules/websockets/WebSocketHandleClient.h"
8 #include "platform/network/NetworkLog.h"
9 #include "platform/network/WebSocketHandshakeRequest.h"
10 #include "platform/network/WebSocketHandshakeResponse.h"
11 #include "platform/weborigin/KURL.h"
12 #include "platform/weborigin/SecurityOrigin.h"
13 #include "public/platform/InterfaceProvider.h"
14 #include "public/platform/Platform.h"
15 #include "public/platform/WebScheduler.h"
16 #include "wtf/Functional.h"
17 #include "wtf/text/WTFString.h"
18
19 namespace blink {
20 namespace {
21
22 const uint16_t kAbnormalShutdownOpCode = 1006;
23
24 } // namespace
25
26 WebSocketHandleImpl::WebSocketHandleImpl()
27 : m_client(nullptr)
28 , m_clientBinding(this)
29 {
30 NETWORK_DVLOG(1) << this << " created";
31 }
32
33 WebSocketHandleImpl::~WebSocketHandleImpl()
34 {
35 NETWORK_DVLOG(1) << this << " deleted";
36
37 if (m_websocket)
38 m_websocket->StartClosingHandshake(kAbnormalShutdownOpCode, emptyString( ));
39 }
40
41 void WebSocketHandleImpl::initialize(InterfaceProvider* interfaceProvider)
42 {
43 NETWORK_DVLOG(1) << this << " initialize(...)";
44
45 DCHECK(!m_websocket);
46 interfaceProvider->getInterface(mojo::GetProxy(&m_websocket));
47
48 m_websocket.set_connection_error_handler(
49 convertToBaseCallback(bind(&WebSocketHandleImpl::onConnectionError, unre tained(this))));
50 }
51
52 void WebSocketHandleImpl::connect(const KURL& url, const Vector<String>& protoco ls, SecurityOrigin* origin, const KURL& firstPartyForCookies, const String& user AgentOverride, WebSocketHandleClient* client)
53 {
54 DCHECK(m_websocket);
55
56 NETWORK_DVLOG(1) << this << " connect(" << url.getString() << ", " << origin ->toString() << ")";
57
58 DCHECK(!m_client);
59 DCHECK(client);
60 m_client = client;
61
62 m_websocket->AddChannelRequest(
63 url,
64 protocols,
65 origin,
66 firstPartyForCookies,
67 userAgentOverride.isNull() ? emptyString() : userAgentOverride,
68 m_clientBinding.CreateInterfacePtrAndBind(
69 Platform::current()->currentThread()->scheduler()->loadingTaskRunner ()->toSingleThreadTaskRunner()));
70 }
71
72 void WebSocketHandleImpl::send(bool fin, WebSocketHandle::MessageType type, cons t char* data, size_t size)
73 {
74 DCHECK(m_websocket);
75
76 mojom::blink::WebSocketMessageType typeToPass;
77 switch (type) {
78 case WebSocketHandle::MessageTypeContinuation:
79 typeToPass = mojom::blink::WebSocketMessageType::CONTINUATION;
80 break;
81 case WebSocketHandle::MessageTypeText:
82 typeToPass = mojom::blink::WebSocketMessageType::TEXT;
83 break;
84 case WebSocketHandle::MessageTypeBinary:
85 typeToPass = mojom::blink::WebSocketMessageType::BINARY;
86 break;
87 default:
88 NOTREACHED();
89 return;
90 }
91
92 NETWORK_DVLOG(1) << this << " send(" << fin << ", " << typeToPass << ", " << "(data size = " << size << "))";
93
94 // TODO(darin): Avoid this copy.
95 Vector<uint8_t> dataToPass(size);
96 std::copy(data, data + size, dataToPass.begin());
97
98 m_websocket->SendFrame(fin, typeToPass, dataToPass);
99 }
100
101 void WebSocketHandleImpl::flowControl(int64_t quota)
102 {
103 DCHECK(m_websocket);
104
105 NETWORK_DVLOG(1) << this << " flowControl(" << quota << ")";
106
107 m_websocket->SendFlowControl(quota);
108 }
109
110 void WebSocketHandleImpl::close(unsigned short code, const String& reason)
111 {
112 DCHECK(m_websocket);
113
114 NETWORK_DVLOG(1) << this << " close(" << code << ", " << reason << ")";
115
116 m_websocket->StartClosingHandshake(code, reason.isNull() ? emptyString() : r eason);
117 }
118
119 void WebSocketHandleImpl::disconnect()
120 {
121 m_websocket.reset();
122 m_client = nullptr;
123 }
124
125 void WebSocketHandleImpl::onConnectionError()
126 {
127 if (!Platform::current()) {
yhirano 2016/08/30 07:43:10 This branch can be deleted (see https://codereview
128 // In the renderer shutdown sequence, mojo channels are destructed and
129 // this function is called. On the other hand, blink objects became
130 // invalid *silently*, which means we must not touch |*client_| any
131 // more.
132 // TODO(yhirano): Remove this code once the shutdown sequence is fixed.
133 disconnect();
134 return;
135 }
136
137 // Our connection to the WebSocket was dropped. This could be due to
138 // exceeding the maximum number of concurrent websockets from this process.
139
140 // TODO(darin): Communicate a more specific error here (see crbug/634502).
141 OnFailChannel(
142 "Error in connection establishment: net:"
143 ":ERR_INSUFFICIENT_RESOURCES");
144 }
145
146 void WebSocketHandleImpl::OnFailChannel(const String& message)
147 {
148 NETWORK_DVLOG(1) << this << " OnFailChannel(" << message << ")";
149
150 WebSocketHandleClient* client = m_client;
151 disconnect();
152 if (!client)
153 return;
154
155 client->didFail(this, message);
156 // |this| can be deleted here.
157 }
158
159 void WebSocketHandleImpl::OnStartOpeningHandshake(mojom::blink::WebSocketHandsha keRequestPtr request)
160 {
161 NETWORK_DVLOG(1) << this << " OnStartOpeningHandshake(" << request->url.getS tring() << ")";
162
163 RefPtr<WebSocketHandshakeRequest> requestToPass = WebSocketHandshakeRequest: :create(request->url);
164 for (size_t i = 0; i < request->headers.size(); ++i) {
165 const mojom::blink::HttpHeaderPtr& header = request->headers[i];
166 requestToPass->addHeaderField(AtomicString(header->name), AtomicString(h eader->value));
167 }
168 requestToPass->setHeadersText(request->headers_text);
169 m_client->didStartOpeningHandshake(this, requestToPass);
170 }
171
172 void WebSocketHandleImpl::OnFinishOpeningHandshake(mojom::blink::WebSocketHandsh akeResponsePtr response)
173 {
174 NETWORK_DVLOG(1) << this << " OnFinishOpeningHandshake(" << response->url.ge tString() << ")";
175
176 WebSocketHandshakeResponse responseToPass;
177 responseToPass.setStatusCode(response->status_code);
178 responseToPass.setStatusText(response->status_text);
179 for (size_t i = 0; i < response->headers.size(); ++i) {
180 const mojom::blink::HttpHeaderPtr& header = response->headers[i];
181 responseToPass.addHeaderField(AtomicString(header->name), AtomicString(h eader->value));
182 }
183 responseToPass.setHeadersText(response->headers_text);
184 m_client->didFinishOpeningHandshake(this, &responseToPass);
185 }
186
187 void WebSocketHandleImpl::OnAddChannelResponse(const String& protocol, const Str ing& extensions)
188 {
189 NETWORK_DVLOG(1) << this << " OnAddChannelResponse(" << protocol << ", " << extensions << ")";
190
191 if (!m_client)
192 return;
193
194 m_client->didConnect(this, protocol, extensions);
195 // |this| can be deleted here.
196 }
197
198 void WebSocketHandleImpl::OnDataFrame(bool fin, mojom::blink::WebSocketMessageTy pe type, const Vector<uint8_t>& data)
199 {
200 NETWORK_DVLOG(1) << this << " OnDataFrame(" << fin << ", " << type << ", " < < "(data size = " << data.size() << "))";
201 if (!m_client)
202 return;
203
204 WebSocketHandle::MessageType typeToPass = WebSocketHandle::MessageTypeContin uation;
205 switch (type) {
206 case mojom::blink::WebSocketMessageType::CONTINUATION:
207 typeToPass = WebSocketHandle::MessageTypeContinuation;
208 break;
209 case mojom::blink::WebSocketMessageType::TEXT:
210 typeToPass = WebSocketHandle::MessageTypeText;
211 break;
212 case mojom::blink::WebSocketMessageType::BINARY:
213 typeToPass = WebSocketHandle::MessageTypeBinary;
214 break;
215 }
216 const char* dataToPass = reinterpret_cast<const char*>(data.isEmpty() ? null ptr : &data[0]);
217 m_client->didReceiveData(this, fin, typeToPass, dataToPass, data.size());
218 // |this| can be deleted here.
219 }
220
221 void WebSocketHandleImpl::OnFlowControl(int64_t quota)
222 {
223 NETWORK_DVLOG(1) << this << " OnFlowControl(" << quota << ")";
224 if (!m_client)
225 return;
226
227 m_client->didReceiveFlowControl(this, quota);
228 // |this| can be deleted here.
229 }
230
231 void WebSocketHandleImpl::OnDropChannel(bool wasClean, uint16_t code, const Stri ng& reason)
232 {
233 NETWORK_DVLOG(1) << this << " OnDropChannel(" << wasClean << ", " << code << ", " << reason << ")";
234
235 WebSocketHandleClient* client = m_client;
236 disconnect();
237 if (!client)
238 return;
239
240 client->didClose(this, wasClean, code, reason);
241 // |this| can be deleted here.
242 }
243
244 void WebSocketHandleImpl::OnClosingHandshake()
245 {
246 NETWORK_DVLOG(1) << this << " OnClosingHandshake()";
247 if (!m_client)
248 return;
249
250 m_client->didStartClosingHandshake(this);
251 // |this| can be deleted here.
252 }
253
254 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698