Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(400)

Side by Side Diff: content/browser/renderer_host/websocket_host.cc

Issue 972963002: Per-renderer WebSocket throttling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix content_unittests Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 307
308 void WebSocketEventHandler::SSLErrorHandlerDelegate::ContinueSSLRequest() { 308 void WebSocketEventHandler::SSLErrorHandlerDelegate::ContinueSSLRequest() {
309 DVLOG(3) << "SSLErrorHandlerDelegate::ContinueSSLRequest"; 309 DVLOG(3) << "SSLErrorHandlerDelegate::ContinueSSLRequest";
310 callbacks_->ContinueSSLRequest(); 310 callbacks_->ContinueSSLRequest();
311 } 311 }
312 312
313 } // namespace 313 } // namespace
314 314
315 WebSocketHost::WebSocketHost(int routing_id, 315 WebSocketHost::WebSocketHost(int routing_id,
316 WebSocketDispatcherHost* dispatcher, 316 WebSocketDispatcherHost* dispatcher,
317 net::URLRequestContext* url_request_context) 317 net::URLRequestContext* url_request_context,
318 base::TimeDelta delay)
318 : dispatcher_(dispatcher), 319 : dispatcher_(dispatcher),
319 url_request_context_(url_request_context), 320 url_request_context_(url_request_context),
320 routing_id_(routing_id) { 321 routing_id_(routing_id),
322 delay_(delay),
323 pending_flow_control_quota_(0),
324 handshake_succeeded_(false),
325 weak_ptr_factory_(this) {
321 DVLOG(1) << "WebSocketHost: created routing_id=" << routing_id; 326 DVLOG(1) << "WebSocketHost: created routing_id=" << routing_id;
322 } 327 }
323 328
324 WebSocketHost::~WebSocketHost() {} 329 WebSocketHost::~WebSocketHost() {}
325 330
326 void WebSocketHost::GoAway() { 331 void WebSocketHost::GoAway() {
327 OnDropChannel(false, static_cast<uint16>(net::kWebSocketErrorGoingAway), ""); 332 OnDropChannel(false, static_cast<uint16>(net::kWebSocketErrorGoingAway), "");
328 } 333 }
329 334
330 bool WebSocketHost::OnMessageReceived(const IPC::Message& message) { 335 bool WebSocketHost::OnMessageReceived(const IPC::Message& message) {
(...skipping 13 matching lines...) Expand all
344 const std::vector<std::string>& requested_protocols, 349 const std::vector<std::string>& requested_protocols,
345 const url::Origin& origin, 350 const url::Origin& origin,
346 int render_frame_id) { 351 int render_frame_id) {
347 DVLOG(3) << "WebSocketHost::OnAddChannelRequest" 352 DVLOG(3) << "WebSocketHost::OnAddChannelRequest"
348 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url 353 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url
349 << "\" requested_protocols=\"" 354 << "\" requested_protocols=\""
350 << JoinString(requested_protocols, ", ") << "\" origin=\"" 355 << JoinString(requested_protocols, ", ") << "\" origin=\""
351 << origin.string() << "\""; 356 << origin.string() << "\"";
352 357
353 DCHECK(!channel_); 358 DCHECK(!channel_);
359 if (delay_ > base::TimeDelta::FromMilliseconds(0)) {
Adam Rice 2015/03/04 08:20:50 You can just use base::TimeDelta() for a zero dela
hiroshige 2015/03/04 11:13:15 Done.
360 base::MessageLoop::current()->PostDelayedTask(
361 FROM_HERE,
362 base::Bind(&WebSocketHost::AddChannel,
363 weak_ptr_factory_.GetWeakPtr(),
364 socket_url,
365 requested_protocols,
366 origin,
367 render_frame_id),
368 delay_);
369 } else {
370 AddChannel(socket_url, requested_protocols, origin, render_frame_id);
371 }
372 }
373
374 void WebSocketHost::AddChannel(
375 const GURL& socket_url,
376 const std::vector<std::string>& requested_protocols,
377 const url::Origin& origin,
378 int render_frame_id) {
379 DVLOG(3) << "WebSocketHost::AddChannel"
380 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url
381 << "\" requested_protocols=\""
382 << JoinString(requested_protocols, ", ") << "\" origin=\""
383 << origin.string() << "\"";
384
385 DCHECK(!channel_);
386
354 scoped_ptr<net::WebSocketEventInterface> event_interface( 387 scoped_ptr<net::WebSocketEventInterface> event_interface(
355 new WebSocketEventHandler(dispatcher_, routing_id_, render_frame_id)); 388 new WebSocketEventHandler(dispatcher_, routing_id_, render_frame_id));
356 channel_.reset( 389 channel_.reset(
357 new net::WebSocketChannel(event_interface.Pass(), url_request_context_)); 390 new net::WebSocketChannel(event_interface.Pass(), url_request_context_));
358 channel_->SendAddChannelRequest(socket_url, requested_protocols, origin); 391 channel_->SendAddChannelRequest(socket_url, requested_protocols, origin);
392
393 if (pending_flow_control_quota_ > 0) {
394 channel_->SendFlowControl(pending_flow_control_quota_);
395 pending_flow_control_quota_ = 0;
396 }
359 } 397 }
360 398
361 void WebSocketHost::OnSendFrame(bool fin, 399 void WebSocketHost::OnSendFrame(bool fin,
362 WebSocketMessageType type, 400 WebSocketMessageType type,
363 const std::vector<char>& data) { 401 const std::vector<char>& data) {
364 DVLOG(3) << "WebSocketHost::OnSendFrame" 402 DVLOG(3) << "WebSocketHost::OnSendFrame"
365 << " routing_id=" << routing_id_ << " fin=" << fin 403 << " routing_id=" << routing_id_ << " fin=" << fin
366 << " type=" << type << " data is " << data.size() << " bytes"; 404 << " type=" << type << " data is " << data.size() << " bytes";
367 405
368 DCHECK(channel_); 406 DCHECK(channel_);
369 channel_->SendFrame(fin, MessageTypeToOpCode(type), data); 407 channel_->SendFrame(fin, MessageTypeToOpCode(type), data);
370 } 408 }
371 409
372 void WebSocketHost::OnFlowControl(int64 quota) { 410 void WebSocketHost::OnFlowControl(int64 quota) {
373 DVLOG(3) << "WebSocketHost::OnFlowControl" 411 DVLOG(3) << "WebSocketHost::OnFlowControl"
374 << " routing_id=" << routing_id_ << " quota=" << quota; 412 << " routing_id=" << routing_id_ << " quota=" << quota;
375 413
376 DCHECK(channel_); 414 if (!channel_) {
415 // WebSocketChannel is not yet created due to the delay introduced by
416 // Per-renderer WebSocket throttling.
417 // SendFlowControl() is called after WebSocketChannel is created.
418 pending_flow_control_quota_ += quota;
419 return;
420 }
421
377 channel_->SendFlowControl(quota); 422 channel_->SendFlowControl(quota);
378 } 423 }
379 424
380 void WebSocketHost::OnDropChannel(bool was_clean, 425 void WebSocketHost::OnDropChannel(bool was_clean,
381 uint16 code, 426 uint16 code,
382 const std::string& reason) { 427 const std::string& reason) {
383 DVLOG(3) << "WebSocketHost::OnDropChannel" 428 DVLOG(3) << "WebSocketHost::OnDropChannel"
384 << " routing_id=" << routing_id_ << " was_clean=" << was_clean 429 << " routing_id=" << routing_id_ << " was_clean=" << was_clean
385 << " code=" << code << " reason=\"" << reason << "\""; 430 << " code=" << code << " reason=\"" << reason << "\"";
386 431
387 DCHECK(channel_); 432 if (!channel_) {
433 // WebSocketChannel is not yet created due to the delay introduced by
434 // Per-renderer WebSocket throttling.
Adam Rice 2015/03/05 07:48:27 s/Per/per/ Also in one other comment above.
hiroshige 2015/03/06 06:04:29 Done.
435 WebSocketDispatcherHost::WebSocketHostState result =
436 dispatcher_->DoDropChannel(routing_id_,
437 false,
438 net::kWebSocketErrorAbnormalClosure,
439 "");
440 DCHECK_EQ(WebSocketDispatcherHost::WEBSOCKET_HOST_DELETED, result);
441 return;
442 }
443
388 // TODO(yhirano): Handle |was_clean| appropriately. 444 // TODO(yhirano): Handle |was_clean| appropriately.
389 channel_->StartClosingHandshake(code, reason); 445 channel_->StartClosingHandshake(code, reason);
390 } 446 }
391 447
392 } // namespace content 448 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698