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.
5 #include "content/child/websocket_bridge.h"
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/WebSerializedOrigin.h"
21 #include "third_party/WebKit/public/platform/WebSocketHandle.h"
22 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h"
23 #include "third_party/WebKit/public/platform/WebSocketHandshakeRequestInfo.h"
24 #include "third_party/WebKit/public/platform/WebSocketHandshakeResponseInfo.h"
25 #include "third_party/WebKit/public/platform/WebString.h"
26 #include "third_party/WebKit/public/platform/WebURL.h"
27 #include "third_party/WebKit/public/platform/WebVector.h"
29 #include "url/origin.h"
31 using blink::WebSerializedOrigin
;
32 using blink::WebSocketHandle
;
33 using blink::WebSocketHandleClient
;
34 using blink::WebString
;
36 using blink::WebVector
;
42 const unsigned short kAbnormalShutdownOpCode
= 1006;
46 WebSocketBridge::WebSocketBridge()
47 : channel_id_(kInvalidChannelId
),
48 render_frame_id_(MSG_ROUTING_NONE
),
51 WebSocketBridge::~WebSocketBridge() {
52 if (channel_id_
!= kInvalidChannelId
) {
53 // The connection is abruptly disconnected by the renderer without
55 ChildThreadImpl::current()->Send(
56 new WebSocketMsg_DropChannel(channel_id_
,
58 kAbnormalShutdownOpCode
,
64 bool WebSocketBridge::OnMessageReceived(const IPC::Message
& msg
) {
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)
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
<< ", "
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.
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
));
110 request_to_pass
.setHeadersText(WebString::fromLatin1(request
.headers_text
));
111 client_
->didStartOpeningHandshake(this, request_to_pass
);
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
));
127 response_to_pass
.setHeadersText(WebString::fromLatin1(response
.headers_text
));
128 client_
->didFinishOpeningHandshake(this, response_to_pass
);
131 void WebSocketBridge::DidFail(const std::string
& message
) {
132 DVLOG(1) << "WebSocketBridge::DidFail(" << message
<< ")";
133 WebSocketHandleClient
* client
= client_
;
138 WebString message_to_pass
= WebString::fromUTF8(message
);
139 client
->didFail(this, message_to_pass
);
140 // |this| can be deleted here.
143 void WebSocketBridge::DidReceiveData(bool fin
,
144 WebSocketMessageType type
,
145 const std::vector
<char>& data
) {
146 DVLOG(1) << "WebSocketBridge::DidReceiveData("
149 << "(data size = " << data
.size() << "))";
153 WebSocketHandle::MessageType type_to_pass
=
154 WebSocketHandle::MessageTypeContinuation
;
156 case WEB_SOCKET_MESSAGE_TYPE_CONTINUATION
:
157 type_to_pass
= WebSocketHandle::MessageTypeContinuation
;
159 case WEB_SOCKET_MESSAGE_TYPE_TEXT
:
160 type_to_pass
= WebSocketHandle::MessageTypeText
;
162 case WEB_SOCKET_MESSAGE_TYPE_BINARY
:
163 type_to_pass
= WebSocketHandle::MessageTypeBinary
;
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.
171 void WebSocketBridge::DidReceiveFlowControl(int64_t quota
) {
172 DVLOG(1) << "WebSocketBridge::DidReceiveFlowControl(" << quota
<< ")";
176 client_
->didReceiveFlowControl(this, quota
);
177 // |this| can be deleted here.
180 void WebSocketBridge::DidClose(bool was_clean
,
182 const std::string
& reason
) {
183 DVLOG(1) << "WebSocketBridge::DidClose("
187 WebSocketHandleClient
* client
= client_
;
192 WebString reason_to_pass
= WebString::fromUTF8(reason
);
193 client
->didClose(this, was_clean
, code
, reason_to_pass
);
194 // |this| can be deleted here.
197 void WebSocketBridge::DidStartClosingHandshake() {
198 DVLOG(1) << "WebSocketBridge::DidStartClosingHandshake()";
202 client_
->didStartClosingHandshake(this);
203 // |this| can be deleted here.
206 void WebSocketBridge::connect(
208 const WebVector
<WebString
>& protocols
,
209 const WebSerializedOrigin
& origin
,
210 WebSocketHandleClient
* client
) {
211 DCHECK_EQ(kInvalidChannelId
, channel_id_
);
212 WebSocketDispatcher
* dispatcher
=
213 ChildThreadImpl::current()->websocket_dispatcher();
214 channel_id_
= dispatcher
->AddBridge(this);
217 std::vector
<std::string
> protocols_to_pass
;
218 for (size_t i
= 0; i
< protocols
.size(); ++i
)
219 protocols_to_pass
.push_back(protocols
[i
].utf8());
220 url::Origin
origin_to_pass(origin
);
222 DVLOG(1) << "Bridge#" << channel_id_
<< " Connect(" << url
<< ", ("
223 << JoinString(protocols_to_pass
, ", ") << "), "
224 << origin_to_pass
.string() << ")";
226 ChildThreadImpl::current()->Send(new WebSocketHostMsg_AddChannelRequest(
227 channel_id_
, url
, protocols_to_pass
, origin_to_pass
, render_frame_id_
));
230 void WebSocketBridge::send(bool fin
,
231 WebSocketHandle::MessageType type
,
234 if (channel_id_
== kInvalidChannelId
)
237 WebSocketMessageType type_to_pass
= WEB_SOCKET_MESSAGE_TYPE_CONTINUATION
;
239 case WebSocketHandle::MessageTypeContinuation
:
240 type_to_pass
= WEB_SOCKET_MESSAGE_TYPE_CONTINUATION
;
242 case WebSocketHandle::MessageTypeText
:
243 type_to_pass
= WEB_SOCKET_MESSAGE_TYPE_TEXT
;
245 case WebSocketHandle::MessageTypeBinary
:
246 type_to_pass
= WEB_SOCKET_MESSAGE_TYPE_BINARY
;
250 DVLOG(1) << "Bridge #" << channel_id_
<< " Send("
251 << fin
<< ", " << type_to_pass
<< ", "
252 << "(data size = " << size
<< "))";
254 ChildThreadImpl::current()->Send(
255 new WebSocketMsg_SendFrame(channel_id_
,
258 std::vector
<char>(data
, data
+ size
)));
261 void WebSocketBridge::flowControl(int64_t quota
) {
262 if (channel_id_
== kInvalidChannelId
)
265 DVLOG(1) << "Bridge #" << channel_id_
<< " FlowControl(" << quota
<< ")";
267 ChildThreadImpl::current()->Send(
268 new WebSocketMsg_FlowControl(channel_id_
, quota
));
271 void WebSocketBridge::close(unsigned short code
,
272 const WebString
& reason
) {
273 if (channel_id_
== kInvalidChannelId
)
276 std::string reason_to_pass
= reason
.utf8();
277 DVLOG(1) << "Bridge #" << channel_id_
<< " Close("
278 << code
<< ", " << reason_to_pass
<< ")";
279 // This method is for closing handshake and hence |was_clean| shall be true.
280 ChildThreadImpl::current()->Send(
281 new WebSocketMsg_DropChannel(channel_id_
, true, code
, reason_to_pass
));
284 void WebSocketBridge::Disconnect() {
285 if (channel_id_
== kInvalidChannelId
)
287 WebSocketDispatcher
* dispatcher
=
288 ChildThreadImpl::current()->websocket_dispatcher();
289 dispatcher
->RemoveBridge(channel_id_
);
291 channel_id_
= kInvalidChannelId
;
295 } // namespace content