| 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 WebString& user_agent_override, | |
| 210 WebSocketHandleClient* client) { | |
| 211 DCHECK_EQ(kInvalidChannelId, channel_id_); | |
| 212 WebSocketDispatcher* dispatcher = | |
| 213 ChildThreadImpl::current()->websocket_dispatcher(); | |
| 214 channel_id_ = dispatcher->AddBridge(this); | |
| 215 client_ = client; | |
| 216 | |
| 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 | |
| 221 DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" << url << ", (" | |
| 222 << base::JoinString(protocols_to_pass, ", ") << "), " | |
| 223 << origin.toString().utf8() << ")"; | |
| 224 | |
| 225 // Headers (ie: User-Agent) are ISO Latin 1. | |
| 226 ChildThreadImpl::current()->Send(new WebSocketHostMsg_AddChannelRequest( | |
| 227 channel_id_, url, protocols_to_pass, origin, | |
| 228 user_agent_override.latin1(), render_frame_id_)); | |
| 229 } | |
| 230 | |
| 231 void WebSocketBridge::send(bool fin, | |
| 232 WebSocketHandle::MessageType type, | |
| 233 const char* data, | |
| 234 size_t size) { | |
| 235 if (channel_id_ == kInvalidChannelId) | |
| 236 return; | |
| 237 | |
| 238 WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | |
| 239 switch (type) { | |
| 240 case WebSocketHandle::MessageTypeContinuation: | |
| 241 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | |
| 242 break; | |
| 243 case WebSocketHandle::MessageTypeText: | |
| 244 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT; | |
| 245 break; | |
| 246 case WebSocketHandle::MessageTypeBinary: | |
| 247 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY; | |
| 248 break; | |
| 249 } | |
| 250 | |
| 251 DVLOG(1) << "Bridge #" << channel_id_ << " Send(" | |
| 252 << fin << ", " << type_to_pass << ", " | |
| 253 << "(data size = " << size << "))"; | |
| 254 | |
| 255 ChildThreadImpl::current()->Send( | |
| 256 new WebSocketMsg_SendFrame(channel_id_, | |
| 257 fin, | |
| 258 type_to_pass, | |
| 259 std::vector<char>(data, data + size))); | |
| 260 } | |
| 261 | |
| 262 void WebSocketBridge::flowControl(int64_t quota) { | |
| 263 if (channel_id_ == kInvalidChannelId) | |
| 264 return; | |
| 265 | |
| 266 DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")"; | |
| 267 | |
| 268 ChildThreadImpl::current()->Send( | |
| 269 new WebSocketMsg_FlowControl(channel_id_, quota)); | |
| 270 } | |
| 271 | |
| 272 void WebSocketBridge::close(unsigned short code, | |
| 273 const WebString& reason) { | |
| 274 if (channel_id_ == kInvalidChannelId) | |
| 275 return; | |
| 276 | |
| 277 std::string reason_to_pass = reason.utf8(); | |
| 278 DVLOG(1) << "Bridge #" << channel_id_ << " Close(" | |
| 279 << code << ", " << reason_to_pass << ")"; | |
| 280 // This method is for closing handshake and hence |was_clean| shall be true. | |
| 281 ChildThreadImpl::current()->Send( | |
| 282 new WebSocketMsg_DropChannel(channel_id_, true, code, reason_to_pass)); | |
| 283 } | |
| 284 | |
| 285 void WebSocketBridge::Disconnect() { | |
| 286 if (channel_id_ == kInvalidChannelId) | |
| 287 return; | |
| 288 WebSocketDispatcher* dispatcher = | |
| 289 ChildThreadImpl::current()->websocket_dispatcher(); | |
| 290 dispatcher->RemoveBridge(channel_id_); | |
| 291 | |
| 292 channel_id_ = kInvalidChannelId; | |
| 293 client_ = NULL; | |
| 294 } | |
| 295 | |
| 296 } // namespace content | |
| OLD | NEW |