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