OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/websocket_host.h" | 5 #include "content/browser/renderer_host/websocket_host.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "content/browser/renderer_host/websocket_dispatcher_host.h" | 10 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 | 305 |
306 void WebSocketEventHandler::SSLErrorHandlerDelegate::ContinueSSLRequest() { | 306 void WebSocketEventHandler::SSLErrorHandlerDelegate::ContinueSSLRequest() { |
307 DVLOG(3) << "SSLErrorHandlerDelegate::ContinueSSLRequest"; | 307 DVLOG(3) << "SSLErrorHandlerDelegate::ContinueSSLRequest"; |
308 callbacks_->ContinueSSLRequest(); | 308 callbacks_->ContinueSSLRequest(); |
309 } | 309 } |
310 | 310 |
311 } // namespace | 311 } // namespace |
312 | 312 |
313 WebSocketHost::WebSocketHost(int routing_id, | 313 WebSocketHost::WebSocketHost(int routing_id, |
314 WebSocketDispatcherHost* dispatcher, | 314 WebSocketDispatcherHost* dispatcher, |
315 net::URLRequestContext* url_request_context) | 315 net::URLRequestContext* url_request_context, |
| 316 base::TimeDelta delay) |
316 : dispatcher_(dispatcher), | 317 : dispatcher_(dispatcher), |
317 url_request_context_(url_request_context), | 318 url_request_context_(url_request_context), |
318 routing_id_(routing_id) { | 319 routing_id_(routing_id), |
| 320 delay_(delay), |
| 321 pending_flow_control_quota_(0), |
| 322 handshake_succeeded_(false), |
| 323 weak_ptr_factory_(this) { |
319 DVLOG(1) << "WebSocketHost: created routing_id=" << routing_id; | 324 DVLOG(1) << "WebSocketHost: created routing_id=" << routing_id; |
320 } | 325 } |
321 | 326 |
322 WebSocketHost::~WebSocketHost() {} | 327 WebSocketHost::~WebSocketHost() {} |
323 | 328 |
324 void WebSocketHost::GoAway() { | 329 void WebSocketHost::GoAway() { |
325 OnDropChannel(false, static_cast<uint16>(net::kWebSocketErrorGoingAway), ""); | 330 OnDropChannel(false, static_cast<uint16>(net::kWebSocketErrorGoingAway), ""); |
326 } | 331 } |
327 | 332 |
328 bool WebSocketHost::OnMessageReceived(const IPC::Message& message) { | 333 bool WebSocketHost::OnMessageReceived(const IPC::Message& message) { |
(...skipping 13 matching lines...) Expand all Loading... |
342 const std::vector<std::string>& requested_protocols, | 347 const std::vector<std::string>& requested_protocols, |
343 const url::Origin& origin, | 348 const url::Origin& origin, |
344 int render_frame_id) { | 349 int render_frame_id) { |
345 DVLOG(3) << "WebSocketHost::OnAddChannelRequest" | 350 DVLOG(3) << "WebSocketHost::OnAddChannelRequest" |
346 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url | 351 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url |
347 << "\" requested_protocols=\"" | 352 << "\" requested_protocols=\"" |
348 << JoinString(requested_protocols, ", ") << "\" origin=\"" | 353 << JoinString(requested_protocols, ", ") << "\" origin=\"" |
349 << origin.string() << "\""; | 354 << origin.string() << "\""; |
350 | 355 |
351 DCHECK(!channel_); | 356 DCHECK(!channel_); |
| 357 if (delay_ > base::TimeDelta()) { |
| 358 base::MessageLoop::current()->PostDelayedTask( |
| 359 FROM_HERE, |
| 360 base::Bind(&WebSocketHost::AddChannel, |
| 361 weak_ptr_factory_.GetWeakPtr(), |
| 362 socket_url, |
| 363 requested_protocols, |
| 364 origin, |
| 365 render_frame_id), |
| 366 delay_); |
| 367 } else { |
| 368 AddChannel(socket_url, requested_protocols, origin, render_frame_id); |
| 369 } |
| 370 } |
| 371 |
| 372 void WebSocketHost::AddChannel( |
| 373 const GURL& socket_url, |
| 374 const std::vector<std::string>& requested_protocols, |
| 375 const url::Origin& origin, |
| 376 int render_frame_id) { |
| 377 DVLOG(3) << "WebSocketHost::AddChannel" |
| 378 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url |
| 379 << "\" requested_protocols=\"" |
| 380 << JoinString(requested_protocols, ", ") << "\" origin=\"" |
| 381 << origin.string() << "\""; |
| 382 |
| 383 DCHECK(!channel_); |
| 384 |
352 scoped_ptr<net::WebSocketEventInterface> event_interface( | 385 scoped_ptr<net::WebSocketEventInterface> event_interface( |
353 new WebSocketEventHandler(dispatcher_, routing_id_, render_frame_id)); | 386 new WebSocketEventHandler(dispatcher_, routing_id_, render_frame_id)); |
354 channel_.reset( | 387 channel_.reset( |
355 new net::WebSocketChannel(event_interface.Pass(), url_request_context_)); | 388 new net::WebSocketChannel(event_interface.Pass(), url_request_context_)); |
356 channel_->SendAddChannelRequest(socket_url, requested_protocols, origin); | 389 channel_->SendAddChannelRequest(socket_url, requested_protocols, origin); |
| 390 |
| 391 if (pending_flow_control_quota_ > 0) { |
| 392 channel_->SendFlowControl(pending_flow_control_quota_); |
| 393 pending_flow_control_quota_ = 0; |
| 394 } |
357 } | 395 } |
358 | 396 |
359 void WebSocketHost::OnSendFrame(bool fin, | 397 void WebSocketHost::OnSendFrame(bool fin, |
360 WebSocketMessageType type, | 398 WebSocketMessageType type, |
361 const std::vector<char>& data) { | 399 const std::vector<char>& data) { |
362 DVLOG(3) << "WebSocketHost::OnSendFrame" | 400 DVLOG(3) << "WebSocketHost::OnSendFrame" |
363 << " routing_id=" << routing_id_ << " fin=" << fin | 401 << " routing_id=" << routing_id_ << " fin=" << fin |
364 << " type=" << type << " data is " << data.size() << " bytes"; | 402 << " type=" << type << " data is " << data.size() << " bytes"; |
365 | 403 |
366 DCHECK(channel_); | 404 DCHECK(channel_); |
367 channel_->SendFrame(fin, MessageTypeToOpCode(type), data); | 405 channel_->SendFrame(fin, MessageTypeToOpCode(type), data); |
368 } | 406 } |
369 | 407 |
370 void WebSocketHost::OnFlowControl(int64 quota) { | 408 void WebSocketHost::OnFlowControl(int64 quota) { |
371 DVLOG(3) << "WebSocketHost::OnFlowControl" | 409 DVLOG(3) << "WebSocketHost::OnFlowControl" |
372 << " routing_id=" << routing_id_ << " quota=" << quota; | 410 << " routing_id=" << routing_id_ << " quota=" << quota; |
373 | 411 |
374 DCHECK(channel_); | 412 if (!channel_) { |
| 413 // WebSocketChannel is not yet created due to the delay introduced by |
| 414 // per-renderer WebSocket throttling. |
| 415 // SendFlowControl() is called after WebSocketChannel is created. |
| 416 pending_flow_control_quota_ += quota; |
| 417 return; |
| 418 } |
| 419 |
375 channel_->SendFlowControl(quota); | 420 channel_->SendFlowControl(quota); |
376 } | 421 } |
377 | 422 |
378 void WebSocketHost::OnDropChannel(bool was_clean, | 423 void WebSocketHost::OnDropChannel(bool was_clean, |
379 uint16 code, | 424 uint16 code, |
380 const std::string& reason) { | 425 const std::string& reason) { |
381 DVLOG(3) << "WebSocketHost::OnDropChannel" | 426 DVLOG(3) << "WebSocketHost::OnDropChannel" |
382 << " routing_id=" << routing_id_ << " was_clean=" << was_clean | 427 << " routing_id=" << routing_id_ << " was_clean=" << was_clean |
383 << " code=" << code << " reason=\"" << reason << "\""; | 428 << " code=" << code << " reason=\"" << reason << "\""; |
384 | 429 |
385 DCHECK(channel_); | 430 if (!channel_) { |
| 431 // WebSocketChannel is not yet created due to the delay introduced by |
| 432 // per-renderer WebSocket throttling. |
| 433 WebSocketDispatcherHost::WebSocketHostState result = |
| 434 dispatcher_->DoDropChannel(routing_id_, |
| 435 false, |
| 436 net::kWebSocketErrorAbnormalClosure, |
| 437 ""); |
| 438 DCHECK_EQ(WebSocketDispatcherHost::WEBSOCKET_HOST_DELETED, result); |
| 439 return; |
| 440 } |
| 441 |
386 // TODO(yhirano): Handle |was_clean| appropriately. | 442 // TODO(yhirano): Handle |was_clean| appropriately. |
387 channel_->StartClosingHandshake(code, reason); | 443 channel_->StartClosingHandshake(code, reason); |
388 } | 444 } |
389 | 445 |
390 } // namespace content | 446 } // namespace content |
OLD | NEW |