| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/cast_channel/cast_channel_api.h" | 5 #include "extensions/browser/api/cast_channel/cast_channel_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/time/default_clock.h" | |
| 20 #include "base/values.h" | 19 #include "base/values.h" |
| 21 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 22 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 21 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 23 #include "extensions/browser/api/cast_channel/cast_socket.h" | 22 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 24 #include "extensions/browser/api/cast_channel/keep_alive_delegate.h" | 23 #include "extensions/browser/api/cast_channel/keep_alive_delegate.h" |
| 25 #include "extensions/browser/api/cast_channel/logger.h" | 24 #include "extensions/browser/api/cast_channel/logger.h" |
| 26 #include "extensions/browser/event_router.h" | 25 #include "extensions/browser/event_router.h" |
| 27 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 26 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| 28 #include "extensions/common/api/cast_channel/logging.pb.h" | 27 #include "extensions/common/api/cast_channel/logging.pb.h" |
| 29 #include "net/base/ip_address.h" | 28 #include "net/base/ip_address.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 } | 101 } |
| 103 | 102 |
| 104 bool IsValidConnectInfoIpAddress(const ConnectInfo& connect_info) { | 103 bool IsValidConnectInfoIpAddress(const ConnectInfo& connect_info) { |
| 105 net::IPAddress ip_address; | 104 net::IPAddress ip_address; |
| 106 return ip_address.AssignFromIPLiteral(connect_info.ip_address); | 105 return ip_address.AssignFromIPLiteral(connect_info.ip_address); |
| 107 } | 106 } |
| 108 | 107 |
| 109 } // namespace | 108 } // namespace |
| 110 | 109 |
| 111 CastChannelAPI::CastChannelAPI(content::BrowserContext* context) | 110 CastChannelAPI::CastChannelAPI(content::BrowserContext* context) |
| 112 : browser_context_(context), | 111 : browser_context_(context), logger_(new Logger()) { |
| 113 logger_(new Logger(base::WrapUnique<base::Clock>(new base::DefaultClock), | |
| 114 base::Time::UnixEpoch())) { | |
| 115 DCHECK(browser_context_); | 112 DCHECK(browser_context_); |
| 116 } | 113 } |
| 117 | 114 |
| 118 // static | 115 // static |
| 119 CastChannelAPI* CastChannelAPI::Get(content::BrowserContext* context) { | 116 CastChannelAPI* CastChannelAPI::Get(content::BrowserContext* context) { |
| 120 return BrowserContextKeyedAPIFactory<CastChannelAPI>::Get(context); | 117 return BrowserContextKeyedAPIFactory<CastChannelAPI>::Get(context); |
| 121 } | 118 } |
| 122 | 119 |
| 123 scoped_refptr<Logger> CastChannelAPI::GetLogger() { | 120 scoped_refptr<Logger> CastChannelAPI::GetLogger() { |
| 124 return logger_; | 121 return logger_; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 extension_->id(), *ip_endpoint_, channel_auth_, | 310 extension_->id(), *ip_endpoint_, channel_auth_, |
| 314 ExtensionsBrowserClient::Get()->GetNetLog(), | 311 ExtensionsBrowserClient::Get()->GetNetLog(), |
| 315 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() | 312 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() |
| 316 ? *connect_info.timeout | 313 ? *connect_info.timeout |
| 317 : kDefaultConnectTimeoutMillis), | 314 : kDefaultConnectTimeoutMillis), |
| 318 liveness_timeout_ > base::TimeDelta(), api_->GetLogger(), | 315 liveness_timeout_ > base::TimeDelta(), api_->GetLogger(), |
| 319 connect_info.capabilities.get() ? *connect_info.capabilities | 316 connect_info.capabilities.get() ? *connect_info.capabilities |
| 320 : CastDeviceCapability::NONE); | 317 : CastDeviceCapability::NONE); |
| 321 } | 318 } |
| 322 new_channel_id_ = AddSocket(socket); | 319 new_channel_id_ = AddSocket(socket); |
| 323 api_->GetLogger()->LogNewSocketEvent(*socket); | |
| 324 | 320 |
| 325 // Construct read delegates. | 321 // Construct read delegates. |
| 326 std::unique_ptr<api::cast_channel::CastTransport::Delegate> delegate( | 322 std::unique_ptr<api::cast_channel::CastTransport::Delegate> delegate( |
| 327 base::MakeUnique<CastMessageHandler>( | 323 base::MakeUnique<CastMessageHandler>( |
| 328 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr()), socket, | 324 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr()), socket, |
| 329 api_->GetLogger())); | 325 api_->GetLogger())); |
| 330 if (socket->keep_alive()) { | 326 if (socket->keep_alive()) { |
| 331 // Wrap read delegate in a KeepAliveDelegate for timeout handling. | 327 // Wrap read delegate in a KeepAliveDelegate for timeout handling. |
| 332 api::cast_channel::KeepAliveDelegate* keep_alive = | 328 api::cast_channel::KeepAliveDelegate* keep_alive = |
| 333 new api::cast_channel::KeepAliveDelegate( | 329 new api::cast_channel::KeepAliveDelegate( |
| 334 socket, api_->GetLogger(), std::move(delegate), ping_interval_, | 330 socket, api_->GetLogger(), std::move(delegate), ping_interval_, |
| 335 liveness_timeout_); | 331 liveness_timeout_); |
| 336 std::unique_ptr<base::Timer> injected_timer = | 332 std::unique_ptr<base::Timer> injected_timer = |
| 337 api_->GetInjectedTimeoutTimerForTest(); | 333 api_->GetInjectedTimeoutTimerForTest(); |
| 338 if (injected_timer) { | 334 if (injected_timer) { |
| 339 keep_alive->SetTimersForTest(base::MakeUnique<base::Timer>(false, false), | 335 keep_alive->SetTimersForTest(base::MakeUnique<base::Timer>(false, false), |
| 340 std::move(injected_timer)); | 336 std::move(injected_timer)); |
| 341 } | 337 } |
| 342 delegate.reset(keep_alive); | 338 delegate.reset(keep_alive); |
| 343 } | 339 } |
| 344 | 340 |
| 345 api_->GetLogger()->LogNewSocketEvent(*socket); | |
| 346 socket->Connect(std::move(delegate), | 341 socket->Connect(std::move(delegate), |
| 347 base::Bind(&CastChannelOpenFunction::OnOpen, this)); | 342 base::Bind(&CastChannelOpenFunction::OnOpen, this)); |
| 348 } | 343 } |
| 349 | 344 |
| 350 void CastChannelOpenFunction::OnOpen(cast_channel::ChannelError result) { | 345 void CastChannelOpenFunction::OnOpen(cast_channel::ChannelError result) { |
| 351 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 346 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 352 VLOG(1) << "Connect finished, OnOpen invoked."; | 347 VLOG(1) << "Connect finished, OnOpen invoked."; |
| 353 CastSocket* socket = GetSocket(new_channel_id_); | 348 CastSocket* socket = GetSocket(new_channel_id_); |
| 354 if (!socket) { | 349 if (!socket) { |
| 355 SetResultFromError(new_channel_id_, result); | 350 SetResultFromError(new_channel_id_, result); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 } else { | 413 } else { |
| 419 SetResultFromSocket(*socket); | 414 SetResultFromSocket(*socket); |
| 420 } | 415 } |
| 421 AsyncWorkCompleted(); | 416 AsyncWorkCompleted(); |
| 422 } | 417 } |
| 423 | 418 |
| 424 CastChannelCloseFunction::CastChannelCloseFunction() { } | 419 CastChannelCloseFunction::CastChannelCloseFunction() { } |
| 425 | 420 |
| 426 CastChannelCloseFunction::~CastChannelCloseFunction() { } | 421 CastChannelCloseFunction::~CastChannelCloseFunction() { } |
| 427 | 422 |
| 423 bool CastChannelCloseFunction::PrePrepare() { |
| 424 api_ = CastChannelAPI::Get(browser_context()); |
| 425 return CastChannelAsyncApiFunction::PrePrepare(); |
| 426 } |
| 427 |
| 428 bool CastChannelCloseFunction::Prepare() { | 428 bool CastChannelCloseFunction::Prepare() { |
| 429 params_ = Close::Params::Create(*args_); | 429 params_ = Close::Params::Create(*args_); |
| 430 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 430 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 431 return true; | 431 return true; |
| 432 } | 432 } |
| 433 | 433 |
| 434 void CastChannelCloseFunction::AsyncWorkStart() { | 434 void CastChannelCloseFunction::AsyncWorkStart() { |
| 435 CastSocket* socket = GetSocket(params_->channel.channel_id); | 435 CastSocket* socket = GetSocket(params_->channel.channel_id); |
| 436 if (!socket) { | 436 if (!socket) { |
| 437 SetResultFromError(params_->channel.channel_id, | 437 SetResultFromError(params_->channel.channel_id, |
| 438 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); | 438 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); |
| 439 AsyncWorkCompleted(); | 439 AsyncWorkCompleted(); |
| 440 } else { | 440 } else { |
| 441 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this)); | 441 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this)); |
| 442 } | 442 } |
| 443 } | 443 } |
| 444 | 444 |
| 445 void CastChannelCloseFunction::OnClose(int result) { | 445 void CastChannelCloseFunction::OnClose(int result) { |
| 446 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 446 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 447 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; | 447 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; |
| 448 int channel_id = params_->channel.channel_id; | 448 int channel_id = params_->channel.channel_id; |
| 449 CastSocket* socket = GetSocket(channel_id); | 449 CastSocket* socket = GetSocket(channel_id); |
| 450 if (result < 0 || !socket) { | 450 if (result < 0 || !socket) { |
| 451 SetResultFromError(channel_id, | 451 SetResultFromError(channel_id, |
| 452 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 452 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| 453 } else { | 453 } else { |
| 454 SetResultFromSocket(*socket); | 454 SetResultFromSocket(*socket); |
| 455 // This will delete |socket|. | 455 // This will delete |socket|. |
| 456 RemoveSocket(channel_id); | 456 RemoveSocket(channel_id); |
| 457 api_->GetLogger()->ClearLastErrors(channel_id); |
| 457 } | 458 } |
| 458 AsyncWorkCompleted(); | 459 AsyncWorkCompleted(); |
| 459 } | 460 } |
| 460 | 461 |
| 461 CastChannelGetLogsFunction::CastChannelGetLogsFunction() { | |
| 462 } | |
| 463 | |
| 464 CastChannelGetLogsFunction::~CastChannelGetLogsFunction() { | |
| 465 } | |
| 466 | |
| 467 bool CastChannelGetLogsFunction::PrePrepare() { | |
| 468 api_ = CastChannelAPI::Get(browser_context()); | |
| 469 return CastChannelAsyncApiFunction::PrePrepare(); | |
| 470 } | |
| 471 | |
| 472 bool CastChannelGetLogsFunction::Prepare() { | |
| 473 return true; | |
| 474 } | |
| 475 | |
| 476 void CastChannelGetLogsFunction::AsyncWorkStart() { | |
| 477 DCHECK(api_); | |
| 478 | |
| 479 size_t length = 0; | |
| 480 std::unique_ptr<char[]> out = api_->GetLogger()->GetLogs(&length); | |
| 481 if (out.get()) { | |
| 482 SetResult(base::MakeUnique<base::BinaryValue>(std::move(out), length)); | |
| 483 } else { | |
| 484 SetError("Unable to get logs."); | |
| 485 } | |
| 486 | |
| 487 api_->GetLogger()->Reset(); | |
| 488 | |
| 489 AsyncWorkCompleted(); | |
| 490 } | |
| 491 | |
| 492 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler( | 462 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler( |
| 493 const EventDispatchCallback& ui_dispatch_cb, | 463 const EventDispatchCallback& ui_dispatch_cb, |
| 494 cast_channel::CastSocket* socket, | 464 cast_channel::CastSocket* socket, |
| 495 scoped_refptr<Logger> logger) | 465 scoped_refptr<Logger> logger) |
| 496 : ui_dispatch_cb_(ui_dispatch_cb), socket_(socket), logger_(logger) { | 466 : ui_dispatch_cb_(ui_dispatch_cb), socket_(socket), logger_(logger) { |
| 497 DCHECK(socket_); | 467 DCHECK(socket_); |
| 498 DCHECK(logger_); | 468 DCHECK(logger_); |
| 499 } | 469 } |
| 500 | 470 |
| 501 CastChannelOpenFunction::CastMessageHandler::~CastMessageHandler() { | 471 CastChannelOpenFunction::CastMessageHandler::~CastMessageHandler() { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 std::move(results))); | 510 std::move(results))); |
| 541 BrowserThread::PostTask( | 511 BrowserThread::PostTask( |
| 542 BrowserThread::UI, FROM_HERE, | 512 BrowserThread::UI, FROM_HERE, |
| 543 base::Bind(ui_dispatch_cb_, socket_->owner_extension_id(), | 513 base::Bind(ui_dispatch_cb_, socket_->owner_extension_id(), |
| 544 base::Passed(std::move(event)))); | 514 base::Passed(std::move(event)))); |
| 545 } | 515 } |
| 546 | 516 |
| 547 void CastChannelOpenFunction::CastMessageHandler::Start() { | 517 void CastChannelOpenFunction::CastMessageHandler::Start() { |
| 548 } | 518 } |
| 549 | 519 |
| 550 CastChannelSetAuthorityKeysFunction::CastChannelSetAuthorityKeysFunction() { | |
| 551 } | |
| 552 | |
| 553 CastChannelSetAuthorityKeysFunction::~CastChannelSetAuthorityKeysFunction() { | |
| 554 } | |
| 555 | |
| 556 bool CastChannelSetAuthorityKeysFunction::Prepare() { | |
| 557 return true; | |
| 558 } | |
| 559 | |
| 560 void CastChannelSetAuthorityKeysFunction::AsyncWorkStart() { | |
| 561 // TODO(eroman): crbug.com/601171: Delete this once the API is | |
| 562 // removed. It is currently a no-op. | |
| 563 AsyncWorkCompleted(); | |
| 564 } | |
| 565 | |
| 566 } // namespace extensions | 520 } // namespace extensions |
| OLD | NEW |