| 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 <string> | 12 #include <string> |
| 12 #include <utility> | 13 #include <utility> |
| 13 | 14 |
| 14 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 15 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/ptr_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/time/default_clock.h" | 19 #include "base/time/default_clock.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 22 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 22 #include "extensions/browser/api/cast_channel/cast_socket.h" | 23 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 23 #include "extensions/browser/api/cast_channel/keep_alive_delegate.h" | 24 #include "extensions/browser/api/cast_channel/keep_alive_delegate.h" |
| 24 #include "extensions/browser/api/cast_channel/logger.h" | 25 #include "extensions/browser/api/cast_channel/logger.h" |
| 25 #include "extensions/browser/event_router.h" | 26 #include "extensions/browser/event_router.h" |
| 26 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 27 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 52 using cast_channel::Logger; | 53 using cast_channel::Logger; |
| 53 using cast_channel::MessageInfo; | 54 using cast_channel::MessageInfo; |
| 54 using cast_channel::ReadyState; | 55 using cast_channel::ReadyState; |
| 55 using content::BrowserThread; | 56 using content::BrowserThread; |
| 56 | 57 |
| 57 namespace { | 58 namespace { |
| 58 | 59 |
| 59 // T is an extension dictionary (MessageInfo or ChannelInfo) | 60 // T is an extension dictionary (MessageInfo or ChannelInfo) |
| 60 template <class T> | 61 template <class T> |
| 61 std::string ParamToString(const T& info) { | 62 std::string ParamToString(const T& info) { |
| 62 scoped_ptr<base::DictionaryValue> dict = info.ToValue(); | 63 std::unique_ptr<base::DictionaryValue> dict = info.ToValue(); |
| 63 std::string out; | 64 std::string out; |
| 64 base::JSONWriter::Write(*dict, &out); | 65 base::JSONWriter::Write(*dict, &out); |
| 65 return out; | 66 return out; |
| 66 } | 67 } |
| 67 | 68 |
| 68 // Fills |channel_info| from the destination and state of |socket|. | 69 // Fills |channel_info| from the destination and state of |socket|. |
| 69 void FillChannelInfo(const CastSocket& socket, ChannelInfo* channel_info) { | 70 void FillChannelInfo(const CastSocket& socket, ChannelInfo* channel_info) { |
| 70 DCHECK(channel_info); | 71 DCHECK(channel_info); |
| 71 channel_info->channel_id = socket.id(); | 72 channel_info->channel_id = socket.id(); |
| 72 const net::IPEndPoint& ip_endpoint = socket.ip_endpoint(); | 73 const net::IPEndPoint& ip_endpoint = socket.ip_endpoint(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 102 | 103 |
| 103 bool IsValidConnectInfoIpAddress(const ConnectInfo& connect_info) { | 104 bool IsValidConnectInfoIpAddress(const ConnectInfo& connect_info) { |
| 104 net::IPAddress ip_address; | 105 net::IPAddress ip_address; |
| 105 return ip_address.AssignFromIPLiteral(connect_info.ip_address); | 106 return ip_address.AssignFromIPLiteral(connect_info.ip_address); |
| 106 } | 107 } |
| 107 | 108 |
| 108 } // namespace | 109 } // namespace |
| 109 | 110 |
| 110 CastChannelAPI::CastChannelAPI(content::BrowserContext* context) | 111 CastChannelAPI::CastChannelAPI(content::BrowserContext* context) |
| 111 : browser_context_(context), | 112 : browser_context_(context), |
| 112 logger_(new Logger(make_scoped_ptr<base::Clock>(new base::DefaultClock), | 113 logger_(new Logger(base::WrapUnique<base::Clock>(new base::DefaultClock), |
| 113 base::Time::UnixEpoch())) { | 114 base::Time::UnixEpoch())) { |
| 114 DCHECK(browser_context_); | 115 DCHECK(browser_context_); |
| 115 } | 116 } |
| 116 | 117 |
| 117 // static | 118 // static |
| 118 CastChannelAPI* CastChannelAPI::Get(content::BrowserContext* context) { | 119 CastChannelAPI* CastChannelAPI::Get(content::BrowserContext* context) { |
| 119 return BrowserContextKeyedAPIFactory<CastChannelAPI>::Get(context); | 120 return BrowserContextKeyedAPIFactory<CastChannelAPI>::Get(context); |
| 120 } | 121 } |
| 121 | 122 |
| 122 scoped_refptr<Logger> CastChannelAPI::GetLogger() { | 123 scoped_refptr<Logger> CastChannelAPI::GetLogger() { |
| 123 return logger_; | 124 return logger_; |
| 124 } | 125 } |
| 125 | 126 |
| 126 void CastChannelAPI::SendEvent(const std::string& extension_id, | 127 void CastChannelAPI::SendEvent(const std::string& extension_id, |
| 127 scoped_ptr<Event> event) { | 128 std::unique_ptr<Event> event) { |
| 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 129 EventRouter* event_router = EventRouter::Get(GetBrowserContext()); | 130 EventRouter* event_router = EventRouter::Get(GetBrowserContext()); |
| 130 if (event_router) { | 131 if (event_router) { |
| 131 event_router->DispatchEventToExtension(extension_id, std::move(event)); | 132 event_router->DispatchEventToExtension(extension_id, std::move(event)); |
| 132 } | 133 } |
| 133 } | 134 } |
| 134 | 135 |
| 135 static base::LazyInstance<BrowserContextKeyedAPIFactory<CastChannelAPI> > | 136 static base::LazyInstance<BrowserContextKeyedAPIFactory<CastChannelAPI> > |
| 136 g_factory = LAZY_INSTANCE_INITIALIZER; | 137 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 137 | 138 |
| 138 // static | 139 // static |
| 139 BrowserContextKeyedAPIFactory<CastChannelAPI>* | 140 BrowserContextKeyedAPIFactory<CastChannelAPI>* |
| 140 CastChannelAPI::GetFactoryInstance() { | 141 CastChannelAPI::GetFactoryInstance() { |
| 141 return g_factory.Pointer(); | 142 return g_factory.Pointer(); |
| 142 } | 143 } |
| 143 | 144 |
| 144 void CastChannelAPI::SetSocketForTest(scoped_ptr<CastSocket> socket_for_test) { | 145 void CastChannelAPI::SetSocketForTest( |
| 146 std::unique_ptr<CastSocket> socket_for_test) { |
| 145 socket_for_test_ = std::move(socket_for_test); | 147 socket_for_test_ = std::move(socket_for_test); |
| 146 } | 148 } |
| 147 | 149 |
| 148 scoped_ptr<CastSocket> CastChannelAPI::GetSocketForTest() { | 150 std::unique_ptr<CastSocket> CastChannelAPI::GetSocketForTest() { |
| 149 return std::move(socket_for_test_); | 151 return std::move(socket_for_test_); |
| 150 } | 152 } |
| 151 | 153 |
| 152 content::BrowserContext* CastChannelAPI::GetBrowserContext() const { | 154 content::BrowserContext* CastChannelAPI::GetBrowserContext() const { |
| 153 return browser_context_; | 155 return browser_context_; |
| 154 } | 156 } |
| 155 | 157 |
| 156 void CastChannelAPI::SetPingTimeoutTimerForTest(scoped_ptr<base::Timer> timer) { | 158 void CastChannelAPI::SetPingTimeoutTimerForTest( |
| 159 std::unique_ptr<base::Timer> timer) { |
| 157 injected_timeout_timer_ = std::move(timer); | 160 injected_timeout_timer_ = std::move(timer); |
| 158 } | 161 } |
| 159 | 162 |
| 160 scoped_ptr<base::Timer> CastChannelAPI::GetInjectedTimeoutTimerForTest() { | 163 std::unique_ptr<base::Timer> CastChannelAPI::GetInjectedTimeoutTimerForTest() { |
| 161 return std::move(injected_timeout_timer_); | 164 return std::move(injected_timeout_timer_); |
| 162 } | 165 } |
| 163 | 166 |
| 164 CastChannelAPI::~CastChannelAPI() {} | 167 CastChannelAPI::~CastChannelAPI() {} |
| 165 | 168 |
| 166 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() { | 169 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() { |
| 167 } | 170 } |
| 168 | 171 |
| 169 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } | 172 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } |
| 170 | 173 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 channel_auth_ = connect_info.auth; | 298 channel_auth_ = connect_info.auth; |
| 296 ip_endpoint_.reset(ParseConnectInfo(connect_info)); | 299 ip_endpoint_.reset(ParseConnectInfo(connect_info)); |
| 297 return true; | 300 return true; |
| 298 } | 301 } |
| 299 | 302 |
| 300 void CastChannelOpenFunction::AsyncWorkStart() { | 303 void CastChannelOpenFunction::AsyncWorkStart() { |
| 301 DCHECK(api_); | 304 DCHECK(api_); |
| 302 DCHECK(ip_endpoint_.get()); | 305 DCHECK(ip_endpoint_.get()); |
| 303 const ConnectInfo& connect_info = params_->connect_info; | 306 const ConnectInfo& connect_info = params_->connect_info; |
| 304 CastSocket* socket; | 307 CastSocket* socket; |
| 305 scoped_ptr<CastSocket> test_socket = api_->GetSocketForTest(); | 308 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); |
| 306 if (test_socket.get()) { | 309 if (test_socket.get()) { |
| 307 socket = test_socket.release(); | 310 socket = test_socket.release(); |
| 308 } else { | 311 } else { |
| 309 socket = new cast_channel::CastSocketImpl( | 312 socket = new cast_channel::CastSocketImpl( |
| 310 extension_->id(), *ip_endpoint_, channel_auth_, | 313 extension_->id(), *ip_endpoint_, channel_auth_, |
| 311 ExtensionsBrowserClient::Get()->GetNetLog(), | 314 ExtensionsBrowserClient::Get()->GetNetLog(), |
| 312 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() | 315 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() |
| 313 ? *connect_info.timeout.get() | 316 ? *connect_info.timeout.get() |
| 314 : kDefaultConnectTimeoutMillis), | 317 : kDefaultConnectTimeoutMillis), |
| 315 liveness_timeout_ > base::TimeDelta(), api_->GetLogger(), | 318 liveness_timeout_ > base::TimeDelta(), api_->GetLogger(), |
| 316 connect_info.capabilities.get() ? *connect_info.capabilities.get() | 319 connect_info.capabilities.get() ? *connect_info.capabilities.get() |
| 317 : CastDeviceCapability::NONE); | 320 : CastDeviceCapability::NONE); |
| 318 } | 321 } |
| 319 new_channel_id_ = AddSocket(socket); | 322 new_channel_id_ = AddSocket(socket); |
| 320 api_->GetLogger()->LogNewSocketEvent(*socket); | 323 api_->GetLogger()->LogNewSocketEvent(*socket); |
| 321 | 324 |
| 322 // Construct read delegates. | 325 // Construct read delegates. |
| 323 scoped_ptr<api::cast_channel::CastTransport::Delegate> delegate( | 326 std::unique_ptr<api::cast_channel::CastTransport::Delegate> delegate( |
| 324 make_scoped_ptr(new CastMessageHandler( | 327 base::WrapUnique(new CastMessageHandler( |
| 325 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr()), socket, | 328 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr()), socket, |
| 326 api_->GetLogger()))); | 329 api_->GetLogger()))); |
| 327 if (socket->keep_alive()) { | 330 if (socket->keep_alive()) { |
| 328 // Wrap read delegate in a KeepAliveDelegate for timeout handling. | 331 // Wrap read delegate in a KeepAliveDelegate for timeout handling. |
| 329 api::cast_channel::KeepAliveDelegate* keep_alive = | 332 api::cast_channel::KeepAliveDelegate* keep_alive = |
| 330 new api::cast_channel::KeepAliveDelegate( | 333 new api::cast_channel::KeepAliveDelegate( |
| 331 socket, api_->GetLogger(), std::move(delegate), ping_interval_, | 334 socket, api_->GetLogger(), std::move(delegate), ping_interval_, |
| 332 liveness_timeout_); | 335 liveness_timeout_); |
| 333 scoped_ptr<base::Timer> injected_timer = | 336 std::unique_ptr<base::Timer> injected_timer = |
| 334 api_->GetInjectedTimeoutTimerForTest(); | 337 api_->GetInjectedTimeoutTimerForTest(); |
| 335 if (injected_timer) { | 338 if (injected_timer) { |
| 336 keep_alive->SetTimersForTest( | 339 keep_alive->SetTimersForTest( |
| 337 make_scoped_ptr(new base::Timer(false, false)), | 340 base::WrapUnique(new base::Timer(false, false)), |
| 338 std::move(injected_timer)); | 341 std::move(injected_timer)); |
| 339 } | 342 } |
| 340 delegate.reset(keep_alive); | 343 delegate.reset(keep_alive); |
| 341 } | 344 } |
| 342 | 345 |
| 343 api_->GetLogger()->LogNewSocketEvent(*socket); | 346 api_->GetLogger()->LogNewSocketEvent(*socket); |
| 344 socket->Connect(std::move(delegate), | 347 socket->Connect(std::move(delegate), |
| 345 base::Bind(&CastChannelOpenFunction::OnOpen, this)); | 348 base::Bind(&CastChannelOpenFunction::OnOpen, this)); |
| 346 } | 349 } |
| 347 | 350 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 } | 471 } |
| 469 | 472 |
| 470 bool CastChannelGetLogsFunction::Prepare() { | 473 bool CastChannelGetLogsFunction::Prepare() { |
| 471 return true; | 474 return true; |
| 472 } | 475 } |
| 473 | 476 |
| 474 void CastChannelGetLogsFunction::AsyncWorkStart() { | 477 void CastChannelGetLogsFunction::AsyncWorkStart() { |
| 475 DCHECK(api_); | 478 DCHECK(api_); |
| 476 | 479 |
| 477 size_t length = 0; | 480 size_t length = 0; |
| 478 scoped_ptr<char[]> out = api_->GetLogger()->GetLogs(&length); | 481 std::unique_ptr<char[]> out = api_->GetLogger()->GetLogs(&length); |
| 479 if (out.get()) { | 482 if (out.get()) { |
| 480 SetResult(new base::BinaryValue(std::move(out), length)); | 483 SetResult(new base::BinaryValue(std::move(out), length)); |
| 481 } else { | 484 } else { |
| 482 SetError("Unable to get logs."); | 485 SetError("Unable to get logs."); |
| 483 } | 486 } |
| 484 | 487 |
| 485 api_->GetLogger()->Reset(); | 488 api_->GetLogger()->Reset(); |
| 486 | 489 |
| 487 AsyncWorkCompleted(); | 490 AsyncWorkCompleted(); |
| 488 } | 491 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 503 cast_channel::ChannelError error_state) { | 506 cast_channel::ChannelError error_state) { |
| 504 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 507 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 505 | 508 |
| 506 ChannelInfo channel_info; | 509 ChannelInfo channel_info; |
| 507 FillChannelInfo(*socket_, &channel_info); | 510 FillChannelInfo(*socket_, &channel_info); |
| 508 channel_info.error_state = error_state; | 511 channel_info.error_state = error_state; |
| 509 ErrorInfo error_info; | 512 ErrorInfo error_info; |
| 510 FillErrorInfo(error_state, logger_->GetLastErrors(socket_->id()), | 513 FillErrorInfo(error_state, logger_->GetLastErrors(socket_->id()), |
| 511 &error_info); | 514 &error_info); |
| 512 | 515 |
| 513 scoped_ptr<base::ListValue> results = | 516 std::unique_ptr<base::ListValue> results = |
| 514 OnError::Create(channel_info, error_info); | 517 OnError::Create(channel_info, error_info); |
| 515 scoped_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_ERROR, | 518 std::unique_ptr<Event> event(new Event( |
| 516 OnError::kEventName, std::move(results))); | 519 events::CAST_CHANNEL_ON_ERROR, OnError::kEventName, std::move(results))); |
| 517 BrowserThread::PostTask( | 520 BrowserThread::PostTask( |
| 518 BrowserThread::UI, FROM_HERE, | 521 BrowserThread::UI, FROM_HERE, |
| 519 base::Bind(ui_dispatch_cb_, socket_->owner_extension_id(), | 522 base::Bind(ui_dispatch_cb_, socket_->owner_extension_id(), |
| 520 base::Passed(std::move(event)))); | 523 base::Passed(std::move(event)))); |
| 521 } | 524 } |
| 522 | 525 |
| 523 void CastChannelOpenFunction::CastMessageHandler::OnMessage( | 526 void CastChannelOpenFunction::CastMessageHandler::OnMessage( |
| 524 const CastMessage& message) { | 527 const CastMessage& message) { |
| 525 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 526 | 529 |
| 527 MessageInfo message_info; | 530 MessageInfo message_info; |
| 528 cast_channel::CastMessageToMessageInfo(message, &message_info); | 531 cast_channel::CastMessageToMessageInfo(message, &message_info); |
| 529 ChannelInfo channel_info; | 532 ChannelInfo channel_info; |
| 530 FillChannelInfo(*socket_, &channel_info); | 533 FillChannelInfo(*socket_, &channel_info); |
| 531 VLOG(1) << "Received message " << ParamToString(message_info) | 534 VLOG(1) << "Received message " << ParamToString(message_info) |
| 532 << " on channel " << ParamToString(channel_info); | 535 << " on channel " << ParamToString(channel_info); |
| 533 | 536 |
| 534 scoped_ptr<base::ListValue> results = | 537 std::unique_ptr<base::ListValue> results = |
| 535 OnMessage::Create(channel_info, message_info); | 538 OnMessage::Create(channel_info, message_info); |
| 536 scoped_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, | 539 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, |
| 537 OnMessage::kEventName, std::move(results))); | 540 OnMessage::kEventName, |
| 541 std::move(results))); |
| 538 BrowserThread::PostTask( | 542 BrowserThread::PostTask( |
| 539 BrowserThread::UI, FROM_HERE, | 543 BrowserThread::UI, FROM_HERE, |
| 540 base::Bind(ui_dispatch_cb_, socket_->owner_extension_id(), | 544 base::Bind(ui_dispatch_cb_, socket_->owner_extension_id(), |
| 541 base::Passed(std::move(event)))); | 545 base::Passed(std::move(event)))); |
| 542 } | 546 } |
| 543 | 547 |
| 544 void CastChannelOpenFunction::CastMessageHandler::Start() { | 548 void CastChannelOpenFunction::CastMessageHandler::Start() { |
| 545 } | 549 } |
| 546 | 550 |
| 547 CastChannelSetAuthorityKeysFunction::CastChannelSetAuthorityKeysFunction() { | 551 CastChannelSetAuthorityKeysFunction::CastChannelSetAuthorityKeysFunction() { |
| 548 } | 552 } |
| 549 | 553 |
| 550 CastChannelSetAuthorityKeysFunction::~CastChannelSetAuthorityKeysFunction() { | 554 CastChannelSetAuthorityKeysFunction::~CastChannelSetAuthorityKeysFunction() { |
| 551 } | 555 } |
| 552 | 556 |
| 553 bool CastChannelSetAuthorityKeysFunction::Prepare() { | 557 bool CastChannelSetAuthorityKeysFunction::Prepare() { |
| 554 return true; | 558 return true; |
| 555 } | 559 } |
| 556 | 560 |
| 557 void CastChannelSetAuthorityKeysFunction::AsyncWorkStart() { | 561 void CastChannelSetAuthorityKeysFunction::AsyncWorkStart() { |
| 558 // TODO(eroman): crbug.com/601171: Delete this once the API is | 562 // TODO(eroman): crbug.com/601171: Delete this once the API is |
| 559 // removed. It is currently a no-op. | 563 // removed. It is currently a no-op. |
| 560 AsyncWorkCompleted(); | 564 AsyncWorkCompleted(); |
| 561 } | 565 } |
| 562 | 566 |
| 563 } // namespace extensions | 567 } // namespace extensions |
| OLD | NEW |