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) | |
317 : dispatcher_(dispatcher), | 316 : dispatcher_(dispatcher), |
318 url_request_context_(url_request_context), | 317 url_request_context_(url_request_context), |
319 routing_id_(routing_id), | 318 routing_id_(routing_id) { |
320 delay_(delay), | |
321 pending_flow_control_quota_(0), | |
322 handshake_succeeded_(false), | |
323 weak_ptr_factory_(this) { | |
324 DVLOG(1) << "WebSocketHost: created routing_id=" << routing_id; | 319 DVLOG(1) << "WebSocketHost: created routing_id=" << routing_id; |
325 } | 320 } |
326 | 321 |
327 WebSocketHost::~WebSocketHost() {} | 322 WebSocketHost::~WebSocketHost() {} |
328 | 323 |
329 void WebSocketHost::GoAway() { | 324 void WebSocketHost::GoAway() { |
330 OnDropChannel(false, static_cast<uint16>(net::kWebSocketErrorGoingAway), ""); | 325 OnDropChannel(false, static_cast<uint16>(net::kWebSocketErrorGoingAway), ""); |
331 } | 326 } |
332 | 327 |
333 bool WebSocketHost::OnMessageReceived(const IPC::Message& message) { | 328 bool WebSocketHost::OnMessageReceived(const IPC::Message& message) { |
(...skipping 13 matching lines...) Expand all Loading... |
347 const std::vector<std::string>& requested_protocols, | 342 const std::vector<std::string>& requested_protocols, |
348 const url::Origin& origin, | 343 const url::Origin& origin, |
349 int render_frame_id) { | 344 int render_frame_id) { |
350 DVLOG(3) << "WebSocketHost::OnAddChannelRequest" | 345 DVLOG(3) << "WebSocketHost::OnAddChannelRequest" |
351 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url | 346 << " routing_id=" << routing_id_ << " socket_url=\"" << socket_url |
352 << "\" requested_protocols=\"" | 347 << "\" requested_protocols=\"" |
353 << JoinString(requested_protocols, ", ") << "\" origin=\"" | 348 << JoinString(requested_protocols, ", ") << "\" origin=\"" |
354 << origin.string() << "\""; | 349 << origin.string() << "\""; |
355 | 350 |
356 DCHECK(!channel_); | 351 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 | |
385 scoped_ptr<net::WebSocketEventInterface> event_interface( | 352 scoped_ptr<net::WebSocketEventInterface> event_interface( |
386 new WebSocketEventHandler(dispatcher_, routing_id_, render_frame_id)); | 353 new WebSocketEventHandler(dispatcher_, routing_id_, render_frame_id)); |
387 channel_.reset( | 354 channel_.reset( |
388 new net::WebSocketChannel(event_interface.Pass(), url_request_context_)); | 355 new net::WebSocketChannel(event_interface.Pass(), url_request_context_)); |
389 channel_->SendAddChannelRequest(socket_url, requested_protocols, origin); | 356 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 } | |
395 } | 357 } |
396 | 358 |
397 void WebSocketHost::OnSendFrame(bool fin, | 359 void WebSocketHost::OnSendFrame(bool fin, |
398 WebSocketMessageType type, | 360 WebSocketMessageType type, |
399 const std::vector<char>& data) { | 361 const std::vector<char>& data) { |
400 DVLOG(3) << "WebSocketHost::OnSendFrame" | 362 DVLOG(3) << "WebSocketHost::OnSendFrame" |
401 << " routing_id=" << routing_id_ << " fin=" << fin | 363 << " routing_id=" << routing_id_ << " fin=" << fin |
402 << " type=" << type << " data is " << data.size() << " bytes"; | 364 << " type=" << type << " data is " << data.size() << " bytes"; |
403 | 365 |
404 DCHECK(channel_); | 366 DCHECK(channel_); |
405 channel_->SendFrame(fin, MessageTypeToOpCode(type), data); | 367 channel_->SendFrame(fin, MessageTypeToOpCode(type), data); |
406 } | 368 } |
407 | 369 |
408 void WebSocketHost::OnFlowControl(int64 quota) { | 370 void WebSocketHost::OnFlowControl(int64 quota) { |
409 DVLOG(3) << "WebSocketHost::OnFlowControl" | 371 DVLOG(3) << "WebSocketHost::OnFlowControl" |
410 << " routing_id=" << routing_id_ << " quota=" << quota; | 372 << " routing_id=" << routing_id_ << " quota=" << quota; |
411 | 373 |
412 if (!channel_) { | 374 DCHECK(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 | |
420 channel_->SendFlowControl(quota); | 375 channel_->SendFlowControl(quota); |
421 } | 376 } |
422 | 377 |
423 void WebSocketHost::OnDropChannel(bool was_clean, | 378 void WebSocketHost::OnDropChannel(bool was_clean, |
424 uint16 code, | 379 uint16 code, |
425 const std::string& reason) { | 380 const std::string& reason) { |
426 DVLOG(3) << "WebSocketHost::OnDropChannel" | 381 DVLOG(3) << "WebSocketHost::OnDropChannel" |
427 << " routing_id=" << routing_id_ << " was_clean=" << was_clean | 382 << " routing_id=" << routing_id_ << " was_clean=" << was_clean |
428 << " code=" << code << " reason=\"" << reason << "\""; | 383 << " code=" << code << " reason=\"" << reason << "\""; |
429 | 384 |
430 if (!channel_) { | 385 DCHECK(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 | |
442 // TODO(yhirano): Handle |was_clean| appropriately. | 386 // TODO(yhirano): Handle |was_clean| appropriately. |
443 channel_->StartClosingHandshake(code, reason); | 387 channel_->StartClosingHandshake(code, reason); |
444 } | 388 } |
445 | 389 |
446 } // namespace content | 390 } // namespace content |
OLD | NEW |