| 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/browser/renderer_host/websocket_host.h" | |
| 6 | |
| 7 #include <inttypes.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/bind_helpers.h" | |
| 13 #include "base/location.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/ptr_util.h" | |
| 17 #include "base/single_thread_task_runner.h" | |
| 18 #include "base/strings/string_util.h" | |
| 19 #include "base/strings/stringprintf.h" | |
| 20 #include "base/threading/thread_task_runner_handle.h" | |
| 21 #include "content/browser/bad_message.h" | |
| 22 #include "content/browser/renderer_host/websocket_blob_sender.h" | |
| 23 #include "content/browser/renderer_host/websocket_dispatcher_host.h" | |
| 24 #include "content/browser/ssl/ssl_error_handler.h" | |
| 25 #include "content/browser/ssl/ssl_manager.h" | |
| 26 #include "content/common/websocket_messages.h" | |
| 27 #include "content/public/browser/browser_thread.h" | |
| 28 #include "content/public/browser/render_frame_host.h" | |
| 29 #include "content/public/browser/storage_partition.h" | |
| 30 #include "ipc/ipc_message_macros.h" | |
| 31 #include "net/base/net_errors.h" | |
| 32 #include "net/http/http_request_headers.h" | |
| 33 #include "net/http/http_response_headers.h" | |
| 34 #include "net/http/http_util.h" | |
| 35 #include "net/ssl/ssl_info.h" | |
| 36 #include "net/websockets/websocket_channel.h" | |
| 37 #include "net/websockets/websocket_errors.h" | |
| 38 #include "net/websockets/websocket_event_interface.h" | |
| 39 #include "net/websockets/websocket_frame.h" // for WebSocketFrameHeader::OpCode | |
| 40 #include "net/websockets/websocket_handshake_request_info.h" | |
| 41 #include "net/websockets/websocket_handshake_response_info.h" | |
| 42 #include "url/origin.h" | |
| 43 | |
| 44 namespace content { | |
| 45 | |
| 46 namespace { | |
| 47 | |
| 48 typedef net::WebSocketEventInterface::ChannelState ChannelState; | |
| 49 | |
| 50 // Convert a content::WebSocketMessageType to a | |
| 51 // net::WebSocketFrameHeader::OpCode | |
| 52 net::WebSocketFrameHeader::OpCode MessageTypeToOpCode( | |
| 53 WebSocketMessageType type) { | |
| 54 DCHECK(type == WEB_SOCKET_MESSAGE_TYPE_CONTINUATION || | |
| 55 type == WEB_SOCKET_MESSAGE_TYPE_TEXT || | |
| 56 type == WEB_SOCKET_MESSAGE_TYPE_BINARY); | |
| 57 typedef net::WebSocketFrameHeader::OpCode OpCode; | |
| 58 // These compile asserts verify that the same underlying values are used for | |
| 59 // both types, so we can simply cast between them. | |
| 60 static_assert(static_cast<OpCode>(WEB_SOCKET_MESSAGE_TYPE_CONTINUATION) == | |
| 61 net::WebSocketFrameHeader::kOpCodeContinuation, | |
| 62 "enum values must match for opcode continuation"); | |
| 63 static_assert(static_cast<OpCode>(WEB_SOCKET_MESSAGE_TYPE_TEXT) == | |
| 64 net::WebSocketFrameHeader::kOpCodeText, | |
| 65 "enum values must match for opcode text"); | |
| 66 static_assert(static_cast<OpCode>(WEB_SOCKET_MESSAGE_TYPE_BINARY) == | |
| 67 net::WebSocketFrameHeader::kOpCodeBinary, | |
| 68 "enum values must match for opcode binary"); | |
| 69 return static_cast<OpCode>(type); | |
| 70 } | |
| 71 | |
| 72 WebSocketMessageType OpCodeToMessageType( | |
| 73 net::WebSocketFrameHeader::OpCode opCode) { | |
| 74 DCHECK(opCode == net::WebSocketFrameHeader::kOpCodeContinuation || | |
| 75 opCode == net::WebSocketFrameHeader::kOpCodeText || | |
| 76 opCode == net::WebSocketFrameHeader::kOpCodeBinary); | |
| 77 // This cast is guaranteed valid by the static_assert() statements above. | |
| 78 return static_cast<WebSocketMessageType>(opCode); | |
| 79 } | |
| 80 | |
| 81 ChannelState StateCast(WebSocketDispatcherHost::WebSocketHostState host_state) { | |
| 82 const WebSocketDispatcherHost::WebSocketHostState WEBSOCKET_HOST_ALIVE = | |
| 83 WebSocketDispatcherHost::WEBSOCKET_HOST_ALIVE; | |
| 84 const WebSocketDispatcherHost::WebSocketHostState WEBSOCKET_HOST_DELETED = | |
| 85 WebSocketDispatcherHost::WEBSOCKET_HOST_DELETED; | |
| 86 | |
| 87 DCHECK(host_state == WEBSOCKET_HOST_ALIVE || | |
| 88 host_state == WEBSOCKET_HOST_DELETED); | |
| 89 // These compile asserts verify that we can get away with using static_cast<> | |
| 90 // for the conversion. | |
| 91 static_assert(static_cast<ChannelState>(WEBSOCKET_HOST_ALIVE) == | |
| 92 net::WebSocketEventInterface::CHANNEL_ALIVE, | |
| 93 "enum values must match for state_alive"); | |
| 94 static_assert(static_cast<ChannelState>(WEBSOCKET_HOST_DELETED) == | |
| 95 net::WebSocketEventInterface::CHANNEL_DELETED, | |
| 96 "enum values must match for state_deleted"); | |
| 97 return static_cast<ChannelState>(host_state); | |
| 98 } | |
| 99 | |
| 100 // Implementation of WebSocketBlobSender::Channel | |
| 101 class SendChannelImpl final : public WebSocketBlobSender::Channel { | |
| 102 public: | |
| 103 explicit SendChannelImpl(net::WebSocketChannel* channel) | |
| 104 : channel_(channel) {} | |
| 105 | |
| 106 // Implementation of WebSocketBlobSender::Channel | |
| 107 size_t GetSendQuota() const override { | |
| 108 return static_cast<size_t>(channel_->current_send_quota()); | |
| 109 } | |
| 110 | |
| 111 ChannelState SendFrame(bool fin, const std::vector<char>& data) override { | |
| 112 int opcode = first_frame_ ? net::WebSocketFrameHeader::kOpCodeBinary | |
| 113 : net::WebSocketFrameHeader::kOpCodeContinuation; | |
| 114 first_frame_ = false; | |
| 115 return channel_->SendFrame(fin, opcode, data); | |
| 116 } | |
| 117 | |
| 118 private: | |
| 119 net::WebSocketChannel* channel_; | |
| 120 bool first_frame_ = true; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(SendChannelImpl); | |
| 123 }; | |
| 124 | |
| 125 } // namespace | |
| 126 | |
| 127 // Implementation of net::WebSocketEventInterface. Receives events from our | |
| 128 // WebSocketChannel object. Each event is translated to an IPC and sent to the | |
| 129 // renderer or child process via WebSocketDispatcherHost. | |
| 130 class WebSocketHost::WebSocketEventHandler final | |
| 131 : public net::WebSocketEventInterface { | |
| 132 public: | |
| 133 WebSocketEventHandler(WebSocketDispatcherHost* dispatcher, | |
| 134 WebSocketHost* host, | |
| 135 int routing_id, | |
| 136 int render_frame_id); | |
| 137 ~WebSocketEventHandler() override; | |
| 138 | |
| 139 // net::WebSocketEventInterface implementation | |
| 140 | |
| 141 ChannelState OnAddChannelResponse(const std::string& selected_subprotocol, | |
| 142 const std::string& extensions) override; | |
| 143 ChannelState OnDataFrame(bool fin, | |
| 144 WebSocketMessageType type, | |
| 145 const std::vector<char>& data) override; | |
| 146 ChannelState OnClosingHandshake() override; | |
| 147 ChannelState OnFlowControl(int64_t quota) override; | |
| 148 ChannelState OnDropChannel(bool was_clean, | |
| 149 uint16_t code, | |
| 150 const std::string& reason) override; | |
| 151 ChannelState OnFailChannel(const std::string& message) override; | |
| 152 ChannelState OnStartOpeningHandshake( | |
| 153 std::unique_ptr<net::WebSocketHandshakeRequestInfo> request) override; | |
| 154 ChannelState OnFinishOpeningHandshake( | |
| 155 std::unique_ptr<net::WebSocketHandshakeResponseInfo> response) override; | |
| 156 ChannelState OnSSLCertificateError( | |
| 157 std::unique_ptr<net::WebSocketEventInterface::SSLErrorCallbacks> | |
| 158 callbacks, | |
| 159 const GURL& url, | |
| 160 const net::SSLInfo& ssl_info, | |
| 161 bool fatal) override; | |
| 162 | |
| 163 private: | |
| 164 class SSLErrorHandlerDelegate final : public SSLErrorHandler::Delegate { | |
| 165 public: | |
| 166 SSLErrorHandlerDelegate( | |
| 167 std::unique_ptr<net::WebSocketEventInterface::SSLErrorCallbacks> | |
| 168 callbacks); | |
| 169 ~SSLErrorHandlerDelegate() override; | |
| 170 | |
| 171 base::WeakPtr<SSLErrorHandler::Delegate> GetWeakPtr(); | |
| 172 | |
| 173 // SSLErrorHandler::Delegate methods | |
| 174 void CancelSSLRequest(int error, const net::SSLInfo* ssl_info) override; | |
| 175 void ContinueSSLRequest() override; | |
| 176 | |
| 177 private: | |
| 178 std::unique_ptr<net::WebSocketEventInterface::SSLErrorCallbacks> callbacks_; | |
| 179 base::WeakPtrFactory<SSLErrorHandlerDelegate> weak_ptr_factory_; | |
| 180 | |
| 181 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandlerDelegate); | |
| 182 }; | |
| 183 | |
| 184 WebSocketDispatcherHost* const dispatcher_; | |
| 185 WebSocketHost* const host_; | |
| 186 const int routing_id_; | |
| 187 const int render_frame_id_; | |
| 188 std::unique_ptr<SSLErrorHandlerDelegate> ssl_error_handler_delegate_; | |
| 189 | |
| 190 DISALLOW_COPY_AND_ASSIGN(WebSocketEventHandler); | |
| 191 }; | |
| 192 | |
| 193 WebSocketHost::WebSocketEventHandler::WebSocketEventHandler( | |
| 194 WebSocketDispatcherHost* dispatcher, | |
| 195 WebSocketHost* host, | |
| 196 int routing_id, | |
| 197 int render_frame_id) | |
| 198 : dispatcher_(dispatcher), | |
| 199 host_(host), | |
| 200 routing_id_(routing_id), | |
| 201 render_frame_id_(render_frame_id) {} | |
| 202 | |
| 203 WebSocketHost::WebSocketEventHandler::~WebSocketEventHandler() { | |
| 204 DVLOG(1) << "WebSocketEventHandler destroyed routing_id=" << routing_id_; | |
| 205 } | |
| 206 | |
| 207 ChannelState WebSocketHost::WebSocketEventHandler::OnAddChannelResponse( | |
| 208 const std::string& selected_protocol, | |
| 209 const std::string& extensions) { | |
| 210 DVLOG(3) << "WebSocketEventHandler::OnAddChannelResponse" | |
| 211 << " routing_id=" << routing_id_ | |
| 212 << " selected_protocol=\"" << selected_protocol << "\"" | |
| 213 << " extensions=\"" << extensions << "\""; | |
| 214 | |
| 215 return StateCast(dispatcher_->SendAddChannelResponse( | |
| 216 routing_id_, selected_protocol, extensions)); | |
| 217 } | |
| 218 | |
| 219 ChannelState WebSocketHost::WebSocketEventHandler::OnDataFrame( | |
| 220 bool fin, | |
| 221 net::WebSocketFrameHeader::OpCode type, | |
| 222 const std::vector<char>& data) { | |
| 223 DVLOG(3) << "WebSocketEventHandler::OnDataFrame" | |
| 224 << " routing_id=" << routing_id_ << " fin=" << fin | |
| 225 << " type=" << type << " data is " << data.size() << " bytes"; | |
| 226 | |
| 227 return StateCast(dispatcher_->SendFrame(routing_id_, fin, | |
| 228 OpCodeToMessageType(type), data)); | |
| 229 } | |
| 230 | |
| 231 ChannelState WebSocketHost::WebSocketEventHandler::OnClosingHandshake() { | |
| 232 DVLOG(3) << "WebSocketEventHandler::OnClosingHandshake" | |
| 233 << " routing_id=" << routing_id_; | |
| 234 | |
| 235 return StateCast(dispatcher_->NotifyClosingHandshake(routing_id_)); | |
| 236 } | |
| 237 | |
| 238 ChannelState WebSocketHost::WebSocketEventHandler::OnFlowControl( | |
| 239 int64_t quota) { | |
| 240 DVLOG(3) << "WebSocketEventHandler::OnFlowControl" | |
| 241 << " routing_id=" << routing_id_ << " quota=" << quota; | |
| 242 | |
| 243 if (host_->blob_sender_) | |
| 244 host_->blob_sender_->OnNewSendQuota(); | |
| 245 return StateCast(dispatcher_->SendFlowControl(routing_id_, quota)); | |
| 246 } | |
| 247 | |
| 248 ChannelState WebSocketHost::WebSocketEventHandler::OnDropChannel( | |
| 249 bool was_clean, | |
| 250 uint16_t code, | |
| 251 const std::string& reason) { | |
| 252 DVLOG(3) << "WebSocketEventHandler::OnDropChannel" | |
| 253 << " routing_id=" << routing_id_ << " was_clean=" << was_clean | |
| 254 << " code=" << code << " reason=\"" << reason << "\""; | |
| 255 | |
| 256 return StateCast( | |
| 257 dispatcher_->DoDropChannel(routing_id_, was_clean, code, reason)); | |
| 258 } | |
| 259 | |
| 260 ChannelState WebSocketHost::WebSocketEventHandler::OnFailChannel( | |
| 261 const std::string& message) { | |
| 262 DVLOG(3) << "WebSocketEventHandler::OnFailChannel" | |
| 263 << " routing_id=" << routing_id_ << " message=\"" << message << "\""; | |
| 264 | |
| 265 return StateCast(dispatcher_->NotifyFailure(routing_id_, message)); | |
| 266 } | |
| 267 | |
| 268 ChannelState WebSocketHost::WebSocketEventHandler::OnStartOpeningHandshake( | |
| 269 std::unique_ptr<net::WebSocketHandshakeRequestInfo> request) { | |
| 270 bool should_send = dispatcher_->CanReadRawCookies(); | |
| 271 DVLOG(3) << "WebSocketEventHandler::OnStartOpeningHandshake " | |
| 272 << "should_send=" << should_send; | |
| 273 | |
| 274 if (!should_send) | |
| 275 return WebSocketEventInterface::CHANNEL_ALIVE; | |
| 276 | |
| 277 WebSocketHandshakeRequest request_to_pass; | |
| 278 request_to_pass.url.Swap(&request->url); | |
| 279 net::HttpRequestHeaders::Iterator it(request->headers); | |
| 280 while (it.GetNext()) | |
| 281 request_to_pass.headers.push_back(std::make_pair(it.name(), it.value())); | |
| 282 request_to_pass.headers_text = | |
| 283 base::StringPrintf("GET %s HTTP/1.1\r\n", | |
| 284 request_to_pass.url.spec().c_str()) + | |
| 285 request->headers.ToString(); | |
| 286 request_to_pass.request_time = request->request_time; | |
| 287 | |
| 288 return StateCast( | |
| 289 dispatcher_->NotifyStartOpeningHandshake(routing_id_, request_to_pass)); | |
| 290 } | |
| 291 | |
| 292 ChannelState WebSocketHost::WebSocketEventHandler::OnFinishOpeningHandshake( | |
| 293 std::unique_ptr<net::WebSocketHandshakeResponseInfo> response) { | |
| 294 bool should_send = dispatcher_->CanReadRawCookies(); | |
| 295 DVLOG(3) << "WebSocketEventHandler::OnFinishOpeningHandshake " | |
| 296 << "should_send=" << should_send; | |
| 297 | |
| 298 if (!should_send) | |
| 299 return WebSocketEventInterface::CHANNEL_ALIVE; | |
| 300 | |
| 301 WebSocketHandshakeResponse response_to_pass; | |
| 302 response_to_pass.url.Swap(&response->url); | |
| 303 response_to_pass.status_code = response->status_code; | |
| 304 response_to_pass.status_text.swap(response->status_text); | |
| 305 size_t iter = 0; | |
| 306 std::string name, value; | |
| 307 while (response->headers->EnumerateHeaderLines(&iter, &name, &value)) | |
| 308 response_to_pass.headers.push_back(std::make_pair(name, value)); | |
| 309 response_to_pass.headers_text = | |
| 310 net::HttpUtil::ConvertHeadersBackToHTTPResponse( | |
| 311 response->headers->raw_headers()); | |
| 312 response_to_pass.response_time = response->response_time; | |
| 313 | |
| 314 return StateCast( | |
| 315 dispatcher_->NotifyFinishOpeningHandshake(routing_id_, response_to_pass)); | |
| 316 } | |
| 317 | |
| 318 ChannelState WebSocketHost::WebSocketEventHandler::OnSSLCertificateError( | |
| 319 std::unique_ptr<net::WebSocketEventInterface::SSLErrorCallbacks> callbacks, | |
| 320 const GURL& url, | |
| 321 const net::SSLInfo& ssl_info, | |
| 322 bool fatal) { | |
| 323 DVLOG(3) << "WebSocketEventHandler::OnSSLCertificateError" | |
| 324 << " routing_id=" << routing_id_ << " url=" << url.spec() | |
| 325 << " cert_status=" << ssl_info.cert_status << " fatal=" << fatal; | |
| 326 ssl_error_handler_delegate_.reset( | |
| 327 new SSLErrorHandlerDelegate(std::move(callbacks))); | |
| 328 SSLManager::OnSSLCertificateSubresourceError( | |
| 329 ssl_error_handler_delegate_->GetWeakPtr(), url, | |
| 330 dispatcher_->render_process_id(), render_frame_id_, ssl_info, fatal); | |
| 331 // The above method is always asynchronous. | |
| 332 return WebSocketEventInterface::CHANNEL_ALIVE; | |
| 333 } | |
| 334 | |
| 335 WebSocketHost::WebSocketEventHandler::SSLErrorHandlerDelegate:: | |
| 336 SSLErrorHandlerDelegate( | |
| 337 std::unique_ptr<net::WebSocketEventInterface::SSLErrorCallbacks> | |
| 338 callbacks) | |
| 339 : callbacks_(std::move(callbacks)), weak_ptr_factory_(this) {} | |
| 340 | |
| 341 WebSocketHost::WebSocketEventHandler::SSLErrorHandlerDelegate:: | |
| 342 ~SSLErrorHandlerDelegate() {} | |
| 343 | |
| 344 base::WeakPtr<SSLErrorHandler::Delegate> | |
| 345 WebSocketHost::WebSocketEventHandler::SSLErrorHandlerDelegate::GetWeakPtr() { | |
| 346 return weak_ptr_factory_.GetWeakPtr(); | |
| 347 } | |
| 348 | |
| 349 void WebSocketHost::WebSocketEventHandler::SSLErrorHandlerDelegate:: | |
| 350 CancelSSLRequest(int error, const net::SSLInfo* ssl_info) { | |
| 351 DVLOG(3) << "SSLErrorHandlerDelegate::CancelSSLRequest" | |
| 352 << " error=" << error | |
| 353 << " cert_status=" << (ssl_info ? ssl_info->cert_status | |
| 354 : static_cast<net::CertStatus>(-1)); | |
| 355 callbacks_->CancelSSLRequest(error, ssl_info); | |
| 356 } | |
| 357 | |
| 358 void WebSocketHost::WebSocketEventHandler::SSLErrorHandlerDelegate:: | |
| 359 ContinueSSLRequest() { | |
| 360 DVLOG(3) << "SSLErrorHandlerDelegate::ContinueSSLRequest"; | |
| 361 callbacks_->ContinueSSLRequest(); | |
| 362 } | |
| 363 | |
| 364 WebSocketHost::WebSocketHost(int routing_id, | |
| 365 WebSocketDispatcherHost* dispatcher, | |
| 366 net::URLRequestContext* url_request_context, | |
| 367 base::TimeDelta delay) | |
| 368 : dispatcher_(dispatcher), | |
| 369 url_request_context_(url_request_context), | |
| 370 routing_id_(routing_id), | |
| 371 delay_(delay), | |
| 372 pending_flow_control_quota_(0), | |
| 373 handshake_succeeded_(false), | |
| 374 weak_ptr_factory_(this) { | |
| 375 DVLOG(1) << "WebSocketHost: created routing_id=" << routing_id; | |
| 376 } | |
| 377 | |
| 378 WebSocketHost::~WebSocketHost() {} | |
| 379 | |
| 380 void WebSocketHost::GoAway() { | |
| 381 OnDropChannel(false, static_cast<uint16_t>(net::kWebSocketErrorGoingAway), | |
| 382 ""); | |
| 383 } | |
| 384 | |
| 385 bool WebSocketHost::OnMessageReceived(const IPC::Message& message) { | |
| 386 bool handled = true; | |
| 387 IPC_BEGIN_MESSAGE_MAP(WebSocketHost, message) | |
| 388 IPC_MESSAGE_HANDLER(WebSocketHostMsg_AddChannelRequest, OnAddChannelRequest) | |
| 389 IPC_MESSAGE_HANDLER(WebSocketHostMsg_SendBlob, OnSendBlob) | |
| 390 IPC_MESSAGE_HANDLER(WebSocketMsg_SendFrame, OnSendFrame) | |
| 391 IPC_MESSAGE_HANDLER(WebSocketMsg_FlowControl, OnFlowControl) | |
| 392 IPC_MESSAGE_HANDLER(WebSocketMsg_DropChannel, OnDropChannel) | |
| 393 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 394 IPC_END_MESSAGE_MAP() | |
| 395 return handled; | |
| 396 } | |
| 397 | |
| 398 void WebSocketHost::OnAddChannelRequest( | |
| 399 const GURL& socket_url, | |
| 400 const std::vector<std::string>& requested_protocols, | |
| 401 const url::Origin& origin, | |
| 402 int render_frame_id) { | |
| 403 DVLOG(3) << "WebSocketHost::OnAddChannelRequest" | |
| 404 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url | |
| 405 << "\" requested_protocols=\"" | |
| 406 << base::JoinString(requested_protocols, ", ") << "\" origin=\"" | |
| 407 << origin << "\""; | |
| 408 | |
| 409 DCHECK(!channel_); | |
| 410 if (delay_ > base::TimeDelta()) { | |
| 411 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 412 FROM_HERE, | |
| 413 base::Bind(&WebSocketHost::AddChannel, weak_ptr_factory_.GetWeakPtr(), | |
| 414 socket_url, requested_protocols, origin, render_frame_id), | |
| 415 delay_); | |
| 416 } else { | |
| 417 AddChannel(socket_url, requested_protocols, origin, render_frame_id); | |
| 418 } | |
| 419 // |this| may have been deleted here. | |
| 420 } | |
| 421 | |
| 422 void WebSocketHost::AddChannel( | |
| 423 const GURL& socket_url, | |
| 424 const std::vector<std::string>& requested_protocols, | |
| 425 const url::Origin& origin, | |
| 426 int render_frame_id) { | |
| 427 DVLOG(3) << "WebSocketHost::AddChannel" | |
| 428 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url | |
| 429 << "\" requested_protocols=\"" | |
| 430 << base::JoinString(requested_protocols, ", ") << "\" origin=\"" | |
| 431 << origin << "\""; | |
| 432 | |
| 433 DCHECK(!channel_); | |
| 434 | |
| 435 std::unique_ptr<net::WebSocketEventInterface> event_interface( | |
| 436 new WebSocketEventHandler(dispatcher_, this, routing_id_, | |
| 437 render_frame_id)); | |
| 438 channel_.reset(new net::WebSocketChannel(std::move(event_interface), | |
| 439 url_request_context_)); | |
| 440 | |
| 441 if (pending_flow_control_quota_ > 0) { | |
| 442 // channel_->SendFlowControl(pending_flow_control_quota_) must be called | |
| 443 // after channel_->SendAddChannelRequest() below. | |
| 444 // We post OnFlowControl() here using |weak_ptr_factory_| instead of | |
| 445 // calling SendFlowControl directly, because |this| may have been deleted | |
| 446 // after channel_->SendAddChannelRequest(). | |
| 447 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 448 FROM_HERE, base::Bind(&WebSocketHost::OnFlowControl, | |
| 449 weak_ptr_factory_.GetWeakPtr(), | |
| 450 pending_flow_control_quota_)); | |
| 451 pending_flow_control_quota_ = 0; | |
| 452 } | |
| 453 | |
| 454 channel_->SendAddChannelRequest(socket_url, requested_protocols, origin); | |
| 455 // |this| may have been deleted here. | |
| 456 } | |
| 457 | |
| 458 void WebSocketHost::OnSendBlob(const std::string& uuid, | |
| 459 uint64_t expected_size) { | |
| 460 DVLOG(3) << "WebSocketHost::OnSendBlob" | |
| 461 << " routing_id=" << routing_id_ << " uuid=" << uuid | |
| 462 << " expected_size=" << expected_size; | |
| 463 | |
| 464 DCHECK(channel_); | |
| 465 if (blob_sender_) { | |
| 466 bad_message::ReceivedBadMessage( | |
| 467 dispatcher_, bad_message::WSH_SEND_BLOB_DURING_BLOB_SEND); | |
| 468 return; | |
| 469 } | |
| 470 blob_sender_.reset(new WebSocketBlobSender( | |
| 471 base::WrapUnique(new SendChannelImpl(channel_.get())))); | |
| 472 StoragePartition* partition = dispatcher_->storage_partition(); | |
| 473 storage::FileSystemContext* file_system_context = | |
| 474 partition->GetFileSystemContext(); | |
| 475 | |
| 476 net::WebSocketEventInterface::ChannelState channel_state = | |
| 477 net::WebSocketEventInterface::CHANNEL_ALIVE; | |
| 478 | |
| 479 // This use of base::Unretained is safe because the WebSocketBlobSender object | |
| 480 // is owned by this object and will not call it back after destruction. | |
| 481 int rv = blob_sender_->Start( | |
| 482 uuid, expected_size, dispatcher_->blob_storage_context(), | |
| 483 file_system_context, | |
| 484 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(), | |
| 485 &channel_state, | |
| 486 base::Bind(&WebSocketHost::BlobSendComplete, base::Unretained(this))); | |
| 487 if (channel_state == net::WebSocketEventInterface::CHANNEL_ALIVE && | |
| 488 rv != net::ERR_IO_PENDING) | |
| 489 BlobSendComplete(rv); | |
| 490 // |this| may be destroyed here. | |
| 491 } | |
| 492 | |
| 493 void WebSocketHost::OnSendFrame(bool fin, | |
| 494 WebSocketMessageType type, | |
| 495 const std::vector<char>& data) { | |
| 496 DVLOG(3) << "WebSocketHost::OnSendFrame" | |
| 497 << " routing_id=" << routing_id_ << " fin=" << fin | |
| 498 << " type=" << type << " data is " << data.size() << " bytes"; | |
| 499 | |
| 500 DCHECK(channel_); | |
| 501 if (blob_sender_) { | |
| 502 bad_message::ReceivedBadMessage( | |
| 503 dispatcher_, bad_message::WSH_SEND_FRAME_DURING_BLOB_SEND); | |
| 504 return; | |
| 505 } | |
| 506 channel_->SendFrame(fin, MessageTypeToOpCode(type), data); | |
| 507 } | |
| 508 | |
| 509 void WebSocketHost::OnFlowControl(int64_t quota) { | |
| 510 DVLOG(3) << "WebSocketHost::OnFlowControl" | |
| 511 << " routing_id=" << routing_id_ << " quota=" << quota; | |
| 512 | |
| 513 if (!channel_) { | |
| 514 // WebSocketChannel is not yet created due to the delay introduced by | |
| 515 // per-renderer WebSocket throttling. | |
| 516 // SendFlowControl() is called after WebSocketChannel is created. | |
| 517 pending_flow_control_quota_ += quota; | |
| 518 return; | |
| 519 } | |
| 520 | |
| 521 ignore_result(channel_->SendFlowControl(quota)); | |
| 522 } | |
| 523 | |
| 524 void WebSocketHost::OnDropChannel(bool was_clean, | |
| 525 uint16_t code, | |
| 526 const std::string& reason) { | |
| 527 DVLOG(3) << "WebSocketHost::OnDropChannel" | |
| 528 << " routing_id=" << routing_id_ << " was_clean=" << was_clean | |
| 529 << " code=" << code << " reason=\"" << reason << "\""; | |
| 530 | |
| 531 if (!channel_) { | |
| 532 // WebSocketChannel is not yet created due to the delay introduced by | |
| 533 // per-renderer WebSocket throttling. | |
| 534 WebSocketDispatcherHost::WebSocketHostState result = | |
| 535 dispatcher_->DoDropChannel(routing_id_, false, | |
| 536 net::kWebSocketErrorAbnormalClosure, ""); | |
| 537 DCHECK_EQ(WebSocketDispatcherHost::WEBSOCKET_HOST_DELETED, result); | |
| 538 return; | |
| 539 } | |
| 540 | |
| 541 blob_sender_.reset(); | |
| 542 // TODO(yhirano): Handle |was_clean| appropriately. | |
| 543 ignore_result(channel_->StartClosingHandshake(code, reason)); | |
| 544 } | |
| 545 | |
| 546 void WebSocketHost::BlobSendComplete(int result) { | |
| 547 DVLOG(3) << "WebSocketHost::BlobSendComplete" | |
| 548 << " routing_id=" << routing_id_ | |
| 549 << " result=" << net::ErrorToString(result); | |
| 550 | |
| 551 // All paths through this method must reset blob_sender_, so take ownership | |
| 552 // at the beginning. | |
| 553 std::unique_ptr<WebSocketBlobSender> blob_sender(std::move(blob_sender_)); | |
| 554 switch (result) { | |
| 555 case net::OK: | |
| 556 ignore_result(dispatcher_->BlobSendComplete(routing_id_)); | |
| 557 // |this| may be destroyed here. | |
| 558 return; | |
| 559 | |
| 560 case net::ERR_UPLOAD_FILE_CHANGED: { | |
| 561 uint64_t expected_size = blob_sender->expected_size(); | |
| 562 uint64_t actual_size = blob_sender->ActualSize(); | |
| 563 if (expected_size != actual_size) { | |
| 564 ignore_result(dispatcher_->NotifyFailure( | |
| 565 routing_id_, | |
| 566 base::StringPrintf("Blob size mismatch; renderer size = %" PRIu64 | |
| 567 ", browser size = %" PRIu64, | |
| 568 expected_size, actual_size))); | |
| 569 // |this| is destroyed here. | |
| 570 return; | |
| 571 } // else fallthrough | |
| 572 } | |
| 573 | |
| 574 default: | |
| 575 ignore_result(dispatcher_->NotifyFailure( | |
| 576 routing_id_, | |
| 577 "Failed to load Blob: error code = " + net::ErrorToString(result))); | |
| 578 // |this| is destroyed here. | |
| 579 return; | |
| 580 } | |
| 581 } | |
| 582 | |
| 583 } // namespace content | |
| OLD | NEW |