| 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 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 17 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
| 18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "components/cast_channel/cast_channel_enum.h" | 21 #include "components/cast_channel/cast_channel_enum.h" |
| 22 #include "components/cast_channel/cast_message_util.h" | 22 #include "components/cast_channel/cast_message_util.h" |
| 23 #include "components/cast_channel/cast_socket.h" | 23 #include "components/cast_channel/cast_socket.h" |
| 24 #include "components/cast_channel/cast_socket_service.h" | 24 #include "components/cast_channel/cast_socket_service.h" |
| 25 #include "components/cast_channel/cast_socket_service_factory.h" | |
| 26 #include "components/cast_channel/keep_alive_delegate.h" | 25 #include "components/cast_channel/keep_alive_delegate.h" |
| 27 #include "components/cast_channel/logger.h" | 26 #include "components/cast_channel/logger.h" |
| 28 #include "components/cast_channel/proto/cast_channel.pb.h" | 27 #include "components/cast_channel/proto/cast_channel.pb.h" |
| 29 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 30 #include "extensions/browser/api/cast_channel/cast_channel_enum_util.h" | 29 #include "extensions/browser/api/cast_channel/cast_channel_enum_util.h" |
| 31 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 30 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 32 #include "extensions/browser/event_router.h" | 31 #include "extensions/browser/event_router.h" |
| 33 #include "net/base/ip_address.h" | 32 #include "net/base/ip_address.h" |
| 34 #include "net/base/ip_endpoint.h" | 33 #include "net/base/ip_endpoint.h" |
| 35 #include "net/base/net_errors.h" | 34 #include "net/base/net_errors.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 129 |
| 131 void CastChannelAPI::SendEvent(const std::string& extension_id, | 130 void CastChannelAPI::SendEvent(const std::string& extension_id, |
| 132 std::unique_ptr<Event> event) { | 131 std::unique_ptr<Event> event) { |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 132 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 134 EventRouter* event_router = EventRouter::Get(GetBrowserContext()); | 133 EventRouter* event_router = EventRouter::Get(GetBrowserContext()); |
| 135 if (event_router) { | 134 if (event_router) { |
| 136 event_router->DispatchEventToExtension(extension_id, std::move(event)); | 135 event_router->DispatchEventToExtension(extension_id, std::move(event)); |
| 137 } | 136 } |
| 138 } | 137 } |
| 139 | 138 |
| 139 cast_channel::CastSocket::Observer* CastChannelAPI::GetObserver( |
| 140 const std::string& extension_id, |
| 141 scoped_refptr<cast_channel::Logger> logger) { |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 143 if (!observer_) { |
| 144 observer_.reset(new CastMessageHandler( |
| 145 base::Bind(&CastChannelAPI::SendEvent, this->AsWeakPtr(), extension_id), |
| 146 logger)); |
| 147 } |
| 148 return observer_.get(); |
| 149 } |
| 150 |
| 140 static base::LazyInstance< | 151 static base::LazyInstance< |
| 141 BrowserContextKeyedAPIFactory<CastChannelAPI>>::DestructorAtExit g_factory = | 152 BrowserContextKeyedAPIFactory<CastChannelAPI>>::DestructorAtExit g_factory = |
| 142 LAZY_INSTANCE_INITIALIZER; | 153 LAZY_INSTANCE_INITIALIZER; |
| 143 | 154 |
| 144 // static | 155 // static |
| 145 BrowserContextKeyedAPIFactory<CastChannelAPI>* | 156 BrowserContextKeyedAPIFactory<CastChannelAPI>* |
| 146 CastChannelAPI::GetFactoryInstance() { | 157 CastChannelAPI::GetFactoryInstance() { |
| 147 return g_factory.Pointer(); | 158 return g_factory.Pointer(); |
| 148 } | 159 } |
| 149 | 160 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 161 } | 172 } |
| 162 | 173 |
| 163 CastChannelAPI::~CastChannelAPI() {} | 174 CastChannelAPI::~CastChannelAPI() {} |
| 164 | 175 |
| 165 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() | 176 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() |
| 166 : cast_socket_service_(nullptr) {} | 177 : cast_socket_service_(nullptr) {} |
| 167 | 178 |
| 168 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } | 179 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } |
| 169 | 180 |
| 170 bool CastChannelAsyncApiFunction::PrePrepare() { | 181 bool CastChannelAsyncApiFunction::PrePrepare() { |
| 171 cast_socket_service_ = | 182 cast_socket_service_ = cast_channel::CastSocketService::GetInstance(); |
| 172 cast_channel::CastSocketServiceFactory::GetForBrowserContext( | |
| 173 browser_context()); | |
| 174 DCHECK(cast_socket_service_); | 183 DCHECK(cast_socket_service_); |
| 175 return true; | 184 return true; |
| 176 } | 185 } |
| 177 | 186 |
| 178 bool CastChannelAsyncApiFunction::Respond() { | 187 bool CastChannelAsyncApiFunction::Respond() { |
| 179 return GetError().empty(); | 188 return GetError().empty(); |
| 180 } | 189 } |
| 181 | 190 |
| 182 void CastChannelAsyncApiFunction::SetResultFromSocket( | 191 void CastChannelAsyncApiFunction::SetResultFromSocket( |
| 183 const CastSocket& socket) { | 192 const CastSocket& socket) { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 | 279 |
| 271 void CastChannelOpenFunction::AsyncWorkStart() { | 280 void CastChannelOpenFunction::AsyncWorkStart() { |
| 272 DCHECK(api_); | 281 DCHECK(api_); |
| 273 DCHECK(ip_endpoint_.get()); | 282 DCHECK(ip_endpoint_.get()); |
| 274 const ConnectInfo& connect_info = params_->connect_info; | 283 const ConnectInfo& connect_info = params_->connect_info; |
| 275 | 284 |
| 276 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); | 285 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); |
| 277 if (test_socket.get()) | 286 if (test_socket.get()) |
| 278 cast_socket_service_->SetSocketForTest(std::move(test_socket)); | 287 cast_socket_service_->SetSocketForTest(std::move(test_socket)); |
| 279 | 288 |
| 280 auto* observer = cast_socket_service_->GetObserver(extension_->id()); | 289 auto* observer = |
| 281 if (!observer) { | 290 api_->GetObserver(extension_->id(), cast_socket_service_->GetLogger()); |
| 282 observer = cast_socket_service_->AddObserver( | |
| 283 extension_->id(), base::MakeUnique<CastMessageHandler>( | |
| 284 base::Bind(&CastChannelAPI::SendEvent, | |
| 285 api_->AsWeakPtr(), extension_->id()), | |
| 286 cast_socket_service_->GetLogger())); | |
| 287 } | |
| 288 | 291 |
| 289 cast_socket_service_->OpenSocket( | 292 cast_socket_service_->OpenSocket( |
| 290 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(), | 293 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(), |
| 291 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() | 294 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() |
| 292 ? *connect_info.timeout | 295 ? *connect_info.timeout |
| 293 : kDefaultConnectTimeoutMillis), | 296 : kDefaultConnectTimeoutMillis), |
| 294 liveness_timeout_, ping_interval_, | 297 liveness_timeout_, ping_interval_, |
| 295 connect_info.capabilities.get() ? *connect_info.capabilities | 298 connect_info.capabilities.get() ? *connect_info.capabilities |
| 296 : CastDeviceCapability::NONE, | 299 : CastDeviceCapability::NONE, |
| 297 base::Bind(&CastChannelOpenFunction::OnOpen, this), observer); | 300 base::Bind(&CastChannelOpenFunction::OnOpen, this), observer); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 api::cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 418 api::cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| 416 } else { | 419 } else { |
| 417 SetResultFromSocket(*socket); | 420 SetResultFromSocket(*socket); |
| 418 // This will delete |socket|. | 421 // This will delete |socket|. |
| 419 cast_socket_service_->RemoveSocket(channel_id); | 422 cast_socket_service_->RemoveSocket(channel_id); |
| 420 cast_socket_service_->GetLogger()->ClearLastError(channel_id); | 423 cast_socket_service_->GetLogger()->ClearLastError(channel_id); |
| 421 } | 424 } |
| 422 AsyncWorkCompleted(); | 425 AsyncWorkCompleted(); |
| 423 } | 426 } |
| 424 | 427 |
| 425 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler( | 428 CastChannelAPI::CastMessageHandler::CastMessageHandler( |
| 426 const EventDispatchCallback& ui_dispatch_cb, | 429 const EventDispatchCallback& ui_dispatch_cb, |
| 427 scoped_refptr<Logger> logger) | 430 scoped_refptr<Logger> logger) |
| 428 : ui_dispatch_cb_(ui_dispatch_cb), logger_(logger) { | 431 : ui_dispatch_cb_(ui_dispatch_cb), logger_(logger) { |
| 432 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 429 DCHECK(logger_); | 433 DCHECK(logger_); |
| 430 } | 434 } |
| 431 | 435 |
| 432 CastChannelOpenFunction::CastMessageHandler::~CastMessageHandler() { | 436 CastChannelAPI::CastMessageHandler::~CastMessageHandler() { |
| 437 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 438 cast_channel::CastSocketService::GetInstance()->RemoveObserver(this); |
| 433 } | 439 } |
| 434 | 440 |
| 435 void CastChannelOpenFunction::CastMessageHandler::OnError( | 441 void CastChannelAPI::CastMessageHandler::OnError( |
| 436 const cast_channel::CastSocket& socket, | 442 const cast_channel::CastSocket& socket, |
| 437 ChannelError error_state) { | 443 ChannelError error_state) { |
| 438 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 444 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 439 | 445 |
| 440 ChannelInfo channel_info; | 446 ChannelInfo channel_info; |
| 441 FillChannelInfo(socket, &channel_info); | 447 FillChannelInfo(socket, &channel_info); |
| 442 channel_info.error_state = api::cast_channel::ToChannelError(error_state); | 448 channel_info.error_state = api::cast_channel::ToChannelError(error_state); |
| 443 ErrorInfo error_info; | 449 ErrorInfo error_info; |
| 444 FillErrorInfo(channel_info.error_state, logger_->GetLastError(socket.id()), | 450 FillErrorInfo(channel_info.error_state, logger_->GetLastError(socket.id()), |
| 445 &error_info); | 451 &error_info); |
| 446 | 452 |
| 447 std::unique_ptr<base::ListValue> results = | 453 std::unique_ptr<base::ListValue> results = |
| 448 OnError::Create(channel_info, error_info); | 454 OnError::Create(channel_info, error_info); |
| 449 std::unique_ptr<Event> event(new Event( | 455 std::unique_ptr<Event> event(new Event( |
| 450 events::CAST_CHANNEL_ON_ERROR, OnError::kEventName, std::move(results))); | 456 events::CAST_CHANNEL_ON_ERROR, OnError::kEventName, std::move(results))); |
| 451 BrowserThread::PostTask( | 457 BrowserThread::PostTask( |
| 452 BrowserThread::UI, FROM_HERE, | 458 BrowserThread::UI, FROM_HERE, |
| 453 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); | 459 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); |
| 454 } | 460 } |
| 455 | 461 |
| 456 void CastChannelOpenFunction::CastMessageHandler::OnMessage( | 462 void CastChannelAPI::CastMessageHandler::OnMessage( |
| 457 const cast_channel::CastSocket& socket, | 463 const cast_channel::CastSocket& socket, |
| 458 const CastMessage& message) { | 464 const CastMessage& message) { |
| 459 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 465 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 460 | 466 |
| 461 MessageInfo message_info; | 467 MessageInfo message_info; |
| 462 CastMessageToMessageInfo(message, &message_info); | 468 CastMessageToMessageInfo(message, &message_info); |
| 463 ChannelInfo channel_info; | 469 ChannelInfo channel_info; |
| 464 FillChannelInfo(socket, &channel_info); | 470 FillChannelInfo(socket, &channel_info); |
| 465 VLOG(1) << "Received message " << ParamToString(message_info) | 471 VLOG(1) << "Received message " << ParamToString(message_info) |
| 466 << " on channel " << ParamToString(channel_info); | 472 << " on channel " << ParamToString(channel_info); |
| 467 | 473 |
| 468 std::unique_ptr<base::ListValue> results = | 474 std::unique_ptr<base::ListValue> results = |
| 469 OnMessage::Create(channel_info, message_info); | 475 OnMessage::Create(channel_info, message_info); |
| 470 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, | 476 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, |
| 471 OnMessage::kEventName, | 477 OnMessage::kEventName, |
| 472 std::move(results))); | 478 std::move(results))); |
| 473 BrowserThread::PostTask( | 479 BrowserThread::PostTask( |
| 474 BrowserThread::UI, FROM_HERE, | 480 BrowserThread::UI, FROM_HERE, |
| 475 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); | 481 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); |
| 476 } | 482 } |
| 477 | 483 |
| 478 } // namespace extensions | 484 } // namespace extensions |
| OLD | NEW |