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 "content/child/websocket_bridge.h" | |
6 | |
7 #include <stdint.h> | |
8 #include <string> | |
9 #include <utility> | |
10 #include <vector> | |
11 | |
12 #include "base/logging.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "content/child/child_thread_impl.h" | |
15 #include "content/child/websocket_dispatcher.h" | |
16 #include "content/common/websocket.h" | |
17 #include "content/common/websocket_messages.h" | |
18 #include "ipc/ipc_message.h" | |
19 #include "ipc/ipc_message_macros.h" | |
20 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" | |
21 #include "third_party/WebKit/public/platform/WebString.h" | |
22 #include "third_party/WebKit/public/platform/WebURL.h" | |
23 #include "third_party/WebKit/public/platform/WebVector.h" | |
24 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandle.
h" | |
25 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandleC
lient.h" | |
26 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandsha
keRequestInfo.h" | |
27 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandsha
keResponseInfo.h" | |
28 #include "url/gurl.h" | |
29 #include "url/origin.h" | |
30 | |
31 using blink::WebSecurityOrigin; | |
32 using blink::WebSocketHandle; | |
33 using blink::WebSocketHandleClient; | |
34 using blink::WebString; | |
35 using blink::WebURL; | |
36 using blink::WebVector; | |
37 | |
38 namespace content { | |
39 | |
40 namespace { | |
41 | |
42 const unsigned short kAbnormalShutdownOpCode = 1006; | |
43 | |
44 } // namespace | |
45 | |
46 WebSocketBridge::WebSocketBridge() | |
47 : channel_id_(kInvalidChannelId), | |
48 render_frame_id_(MSG_ROUTING_NONE), | |
49 client_(NULL) {} | |
50 | |
51 WebSocketBridge::~WebSocketBridge() { | |
52 if (channel_id_ != kInvalidChannelId) { | |
53 // The connection is abruptly disconnected by the renderer without | |
54 // closing handshake. | |
55 ChildThreadImpl::current()->Send( | |
56 new WebSocketMsg_DropChannel(channel_id_, | |
57 false, | |
58 kAbnormalShutdownOpCode, | |
59 std::string())); | |
60 } | |
61 Disconnect(); | |
62 } | |
63 | |
64 bool WebSocketBridge::OnMessageReceived(const IPC::Message& msg) { | |
65 bool handled = true; | |
66 IPC_BEGIN_MESSAGE_MAP(WebSocketBridge, msg) | |
67 IPC_MESSAGE_HANDLER(WebSocketMsg_AddChannelResponse, DidConnect) | |
68 IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyStartOpeningHandshake, | |
69 DidStartOpeningHandshake) | |
70 IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyFinishOpeningHandshake, | |
71 DidFinishOpeningHandshake) | |
72 IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyFailure, DidFail) | |
73 IPC_MESSAGE_HANDLER(WebSocketMsg_SendFrame, DidReceiveData) | |
74 IPC_MESSAGE_HANDLER(WebSocketMsg_FlowControl, DidReceiveFlowControl) | |
75 IPC_MESSAGE_HANDLER(WebSocketMsg_DropChannel, DidClose) | |
76 IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyClosing, | |
77 DidStartClosingHandshake) | |
78 IPC_MESSAGE_UNHANDLED(handled = false) | |
79 IPC_END_MESSAGE_MAP() | |
80 return handled; | |
81 } | |
82 | |
83 void WebSocketBridge::DidConnect(const std::string& selected_protocol, | |
84 const std::string& extensions) { | |
85 WebSocketHandleClient* client = client_; | |
86 DVLOG(1) << "WebSocketBridge::DidConnect(" | |
87 << selected_protocol << ", " | |
88 << extensions << ")"; | |
89 if (!client) | |
90 return; | |
91 | |
92 WebString protocol_to_pass = WebString::fromUTF8(selected_protocol); | |
93 WebString extensions_to_pass = WebString::fromUTF8(extensions); | |
94 client->didConnect(this, protocol_to_pass, extensions_to_pass); | |
95 // |this| can be deleted here. | |
96 } | |
97 | |
98 void WebSocketBridge::DidStartOpeningHandshake( | |
99 const WebSocketHandshakeRequest& request) { | |
100 DVLOG(1) << "WebSocketBridge::DidStartOpeningHandshake(" | |
101 << request.url << ")"; | |
102 // All strings are already encoded to ASCII in the browser. | |
103 blink::WebSocketHandshakeRequestInfo request_to_pass; | |
104 request_to_pass.setURL(WebURL(request.url)); | |
105 for (size_t i = 0; i < request.headers.size(); ++i) { | |
106 const std::pair<std::string, std::string>& header = request.headers[i]; | |
107 request_to_pass.addHeaderField(WebString::fromLatin1(header.first), | |
108 WebString::fromLatin1(header.second)); | |
109 } | |
110 request_to_pass.setHeadersText(WebString::fromLatin1(request.headers_text)); | |
111 client_->didStartOpeningHandshake(this, request_to_pass); | |
112 } | |
113 | |
114 void WebSocketBridge::DidFinishOpeningHandshake( | |
115 const WebSocketHandshakeResponse& response) { | |
116 DVLOG(1) << "WebSocketBridge::DidFinishOpeningHandshake(" | |
117 << response.url << ")"; | |
118 // All strings are already encoded to ASCII in the browser. | |
119 blink::WebSocketHandshakeResponseInfo response_to_pass; | |
120 response_to_pass.setStatusCode(response.status_code); | |
121 response_to_pass.setStatusText(WebString::fromLatin1(response.status_text)); | |
122 for (size_t i = 0; i < response.headers.size(); ++i) { | |
123 const std::pair<std::string, std::string>& header = response.headers[i]; | |
124 response_to_pass.addHeaderField(WebString::fromLatin1(header.first), | |
125 WebString::fromLatin1(header.second)); | |
126 } | |
127 response_to_pass.setHeadersText(WebString::fromLatin1(response.headers_text)); | |
128 client_->didFinishOpeningHandshake(this, response_to_pass); | |
129 } | |
130 | |
131 void WebSocketBridge::DidFail(const std::string& message) { | |
132 DVLOG(1) << "WebSocketBridge::DidFail(" << message << ")"; | |
133 WebSocketHandleClient* client = client_; | |
134 Disconnect(); | |
135 if (!client) | |
136 return; | |
137 | |
138 WebString message_to_pass = WebString::fromUTF8(message); | |
139 client->didFail(this, message_to_pass); | |
140 // |this| can be deleted here. | |
141 } | |
142 | |
143 void WebSocketBridge::DidReceiveData(bool fin, | |
144 WebSocketMessageType type, | |
145 const std::vector<char>& data) { | |
146 DVLOG(1) << "WebSocketBridge::DidReceiveData(" | |
147 << fin << ", " | |
148 << type << ", " | |
149 << "(data size = " << data.size() << "))"; | |
150 if (!client_) | |
151 return; | |
152 | |
153 WebSocketHandle::MessageType type_to_pass = | |
154 WebSocketHandle::MessageTypeContinuation; | |
155 switch (type) { | |
156 case WEB_SOCKET_MESSAGE_TYPE_CONTINUATION: | |
157 type_to_pass = WebSocketHandle::MessageTypeContinuation; | |
158 break; | |
159 case WEB_SOCKET_MESSAGE_TYPE_TEXT: | |
160 type_to_pass = WebSocketHandle::MessageTypeText; | |
161 break; | |
162 case WEB_SOCKET_MESSAGE_TYPE_BINARY: | |
163 type_to_pass = WebSocketHandle::MessageTypeBinary; | |
164 break; | |
165 } | |
166 const char* data_to_pass = data.empty() ? NULL : &data[0]; | |
167 client_->didReceiveData(this, fin, type_to_pass, data_to_pass, data.size()); | |
168 // |this| can be deleted here. | |
169 } | |
170 | |
171 void WebSocketBridge::DidReceiveFlowControl(int64_t quota) { | |
172 DVLOG(1) << "WebSocketBridge::DidReceiveFlowControl(" << quota << ")"; | |
173 if (!client_) | |
174 return; | |
175 | |
176 client_->didReceiveFlowControl(this, quota); | |
177 // |this| can be deleted here. | |
178 } | |
179 | |
180 void WebSocketBridge::DidClose(bool was_clean, | |
181 unsigned short code, | |
182 const std::string& reason) { | |
183 DVLOG(1) << "WebSocketBridge::DidClose(" | |
184 << was_clean << ", " | |
185 << code << ", " | |
186 << reason << ")"; | |
187 WebSocketHandleClient* client = client_; | |
188 Disconnect(); | |
189 if (!client) | |
190 return; | |
191 | |
192 WebString reason_to_pass = WebString::fromUTF8(reason); | |
193 client->didClose(this, was_clean, code, reason_to_pass); | |
194 // |this| can be deleted here. | |
195 } | |
196 | |
197 void WebSocketBridge::DidStartClosingHandshake() { | |
198 DVLOG(1) << "WebSocketBridge::DidStartClosingHandshake()"; | |
199 if (!client_) | |
200 return; | |
201 | |
202 client_->didStartClosingHandshake(this); | |
203 // |this| can be deleted here. | |
204 } | |
205 | |
206 void WebSocketBridge::connect(const WebURL& url, | |
207 const WebVector<WebString>& protocols, | |
208 const WebSecurityOrigin& origin, | |
209 const WebURL& first_party_for_cookies, | |
210 const WebString& user_agent_override, | |
211 WebSocketHandleClient* client) { | |
212 DCHECK_EQ(kInvalidChannelId, channel_id_); | |
213 WebSocketDispatcher* dispatcher = | |
214 ChildThreadImpl::current()->websocket_dispatcher(); | |
215 channel_id_ = dispatcher->AddBridge(this); | |
216 client_ = client; | |
217 | |
218 std::vector<std::string> protocols_to_pass; | |
219 for (size_t i = 0; i < protocols.size(); ++i) | |
220 protocols_to_pass.push_back(protocols[i].utf8()); | |
221 | |
222 DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" << url << ", (" | |
223 << base::JoinString(protocols_to_pass, ", ") << "), " | |
224 << origin.toString().utf8() << ")"; | |
225 | |
226 WebSocketHostMsg_AddChannelRequest_Params params; | |
227 params.socket_url = url; | |
228 params.requested_protocols = protocols_to_pass; | |
229 params.origin = origin; | |
230 params.first_party_for_cookies = first_party_for_cookies; | |
231 params.user_agent_override = user_agent_override.latin1(); | |
232 params.render_frame_id = render_frame_id_; | |
233 | |
234 // Headers (ie: User-Agent) are ISO Latin 1. | |
235 ChildThreadImpl::current()->Send(new WebSocketHostMsg_AddChannelRequest( | |
236 channel_id_, params)); | |
237 } | |
238 | |
239 void WebSocketBridge::send(bool fin, | |
240 WebSocketHandle::MessageType type, | |
241 const char* data, | |
242 size_t size) { | |
243 if (channel_id_ == kInvalidChannelId) | |
244 return; | |
245 | |
246 WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | |
247 switch (type) { | |
248 case WebSocketHandle::MessageTypeContinuation: | |
249 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | |
250 break; | |
251 case WebSocketHandle::MessageTypeText: | |
252 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT; | |
253 break; | |
254 case WebSocketHandle::MessageTypeBinary: | |
255 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY; | |
256 break; | |
257 } | |
258 | |
259 DVLOG(1) << "Bridge #" << channel_id_ << " Send(" | |
260 << fin << ", " << type_to_pass << ", " | |
261 << "(data size = " << size << "))"; | |
262 | |
263 ChildThreadImpl::current()->Send( | |
264 new WebSocketMsg_SendFrame(channel_id_, | |
265 fin, | |
266 type_to_pass, | |
267 std::vector<char>(data, data + size))); | |
268 } | |
269 | |
270 void WebSocketBridge::flowControl(int64_t quota) { | |
271 if (channel_id_ == kInvalidChannelId) | |
272 return; | |
273 | |
274 DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")"; | |
275 | |
276 ChildThreadImpl::current()->Send( | |
277 new WebSocketMsg_FlowControl(channel_id_, quota)); | |
278 } | |
279 | |
280 void WebSocketBridge::close(unsigned short code, | |
281 const WebString& reason) { | |
282 if (channel_id_ == kInvalidChannelId) | |
283 return; | |
284 | |
285 std::string reason_to_pass = reason.utf8(); | |
286 DVLOG(1) << "Bridge #" << channel_id_ << " Close(" | |
287 << code << ", " << reason_to_pass << ")"; | |
288 // This method is for closing handshake and hence |was_clean| shall be true. | |
289 ChildThreadImpl::current()->Send( | |
290 new WebSocketMsg_DropChannel(channel_id_, true, code, reason_to_pass)); | |
291 } | |
292 | |
293 void WebSocketBridge::Disconnect() { | |
294 if (channel_id_ == kInvalidChannelId) | |
295 return; | |
296 WebSocketDispatcher* dispatcher = | |
297 ChildThreadImpl::current()->websocket_dispatcher(); | |
298 dispatcher->RemoveBridge(channel_id_); | |
299 | |
300 channel_id_ = kInvalidChannelId; | |
301 client_ = NULL; | |
302 } | |
303 | |
304 } // namespace content | |
OLD | NEW |