| 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 "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 22 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 23 #include "extensions/browser/api/cast_channel/cast_socket.h" | 23 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 24 #include "extensions/browser/api/cast_channel/cast_socket_service.h" |
| 25 #include "extensions/browser/api/cast_channel/cast_socket_service_factory.h" |
| 24 #include "extensions/browser/api/cast_channel/keep_alive_delegate.h" | 26 #include "extensions/browser/api/cast_channel/keep_alive_delegate.h" |
| 25 #include "extensions/browser/api/cast_channel/logger.h" | 27 #include "extensions/browser/api/cast_channel/logger.h" |
| 26 #include "extensions/browser/event_router.h" | 28 #include "extensions/browser/event_router.h" |
| 27 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 29 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| 28 #include "extensions/common/api/cast_channel/logging.pb.h" | 30 #include "extensions/common/api/cast_channel/logging.pb.h" |
| 29 #include "net/base/ip_address.h" | 31 #include "net/base/ip_address.h" |
| 30 #include "net/base/ip_endpoint.h" | 32 #include "net/base/ip_endpoint.h" |
| 31 #include "net/base/net_errors.h" | 33 #include "net/base/net_errors.h" |
| 32 | 34 |
| 33 // Default timeout interval for connection setup. | 35 // Default timeout interval for connection setup. |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 std::unique_ptr<base::Timer> timer) { | 160 std::unique_ptr<base::Timer> timer) { |
| 159 injected_timeout_timer_ = std::move(timer); | 161 injected_timeout_timer_ = std::move(timer); |
| 160 } | 162 } |
| 161 | 163 |
| 162 std::unique_ptr<base::Timer> CastChannelAPI::GetInjectedTimeoutTimerForTest() { | 164 std::unique_ptr<base::Timer> CastChannelAPI::GetInjectedTimeoutTimerForTest() { |
| 163 return std::move(injected_timeout_timer_); | 165 return std::move(injected_timeout_timer_); |
| 164 } | 166 } |
| 165 | 167 |
| 166 CastChannelAPI::~CastChannelAPI() {} | 168 CastChannelAPI::~CastChannelAPI() {} |
| 167 | 169 |
| 168 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() { | 170 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() |
| 169 } | 171 : cast_socket_service_(nullptr) {} |
| 170 | 172 |
| 171 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } | 173 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } |
| 172 | 174 |
| 173 bool CastChannelAsyncApiFunction::PrePrepare() { | 175 bool CastChannelAsyncApiFunction::PrePrepare() { |
| 174 DCHECK(ApiResourceManager<CastSocket>::Get(browser_context())); | 176 cast_socket_service_ = |
| 175 sockets_ = ApiResourceManager<CastSocket>::Get(browser_context())->data_; | 177 api::cast_channel::CastSocketServiceFactory::GetInstance() |
| 176 DCHECK(sockets_); | 178 ->GetForBrowserContext(browser_context()); |
| 179 DCHECK(cast_socket_service_); |
| 177 return true; | 180 return true; |
| 178 } | 181 } |
| 179 | 182 |
| 180 bool CastChannelAsyncApiFunction::Respond() { | 183 bool CastChannelAsyncApiFunction::Respond() { |
| 181 return GetError().empty(); | 184 return GetError().empty(); |
| 182 } | 185 } |
| 183 | 186 |
| 184 CastSocket* CastChannelAsyncApiFunction::GetSocketOrCompleteWithError( | 187 CastSocket* CastChannelAsyncApiFunction::GetSocketOrCompleteWithError( |
| 185 int channel_id) { | 188 int channel_id) { |
| 186 CastSocket* socket = GetSocket(channel_id); | 189 CastSocket* socket = GetSocket(channel_id); |
| 187 if (!socket) { | 190 if (!socket) { |
| 188 SetResultFromError(channel_id, | 191 SetResultFromError(channel_id, |
| 189 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); | 192 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); |
| 190 AsyncWorkCompleted(); | 193 AsyncWorkCompleted(); |
| 191 } | 194 } |
| 192 return socket; | 195 return socket; |
| 193 } | 196 } |
| 194 | 197 |
| 195 int CastChannelAsyncApiFunction::AddSocket(CastSocket* socket) { | 198 int CastChannelAsyncApiFunction::AddSocket(std::unique_ptr<CastSocket> socket) { |
| 196 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 199 auto* sockets = cast_socket_service_->GetOrCreateSocketRegistry(); |
| 197 DCHECK(socket); | 200 return sockets->AddSocket(std::move(socket)); |
| 198 const int id = sockets_->Add(socket); | |
| 199 socket->set_id(id); | |
| 200 return id; | |
| 201 } | 201 } |
| 202 | 202 |
| 203 void CastChannelAsyncApiFunction::RemoveSocket(int channel_id) { | 203 void CastChannelAsyncApiFunction::RemoveSocket(int channel_id) { |
| 204 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 204 auto* sockets = cast_socket_service_->GetOrCreateSocketRegistry(); |
| 205 sockets_->Remove(extension_->id(), channel_id); | 205 sockets->RemoveSocket(channel_id); |
| 206 } | 206 } |
| 207 | 207 |
| 208 void CastChannelAsyncApiFunction::SetResultFromSocket( | 208 void CastChannelAsyncApiFunction::SetResultFromSocket( |
| 209 const CastSocket& socket) { | 209 const CastSocket& socket) { |
| 210 ChannelInfo channel_info; | 210 ChannelInfo channel_info; |
| 211 FillChannelInfo(socket, &channel_info); | 211 FillChannelInfo(socket, &channel_info); |
| 212 ChannelError error = socket.error_state(); | 212 ChannelError error = socket.error_state(); |
| 213 if (error != cast_channel::CHANNEL_ERROR_NONE) { | 213 if (error != cast_channel::CHANNEL_ERROR_NONE) { |
| 214 SetError("Channel socket error = " + base::IntToString(error)); | 214 SetError("Channel socket error = " + base::IntToString(error)); |
| 215 } | 215 } |
| 216 SetResultFromChannelInfo(channel_info); | 216 SetResultFromChannelInfo(channel_info); |
| 217 } | 217 } |
| 218 | 218 |
| 219 void CastChannelAsyncApiFunction::SetResultFromError(int channel_id, | 219 void CastChannelAsyncApiFunction::SetResultFromError(int channel_id, |
| 220 ChannelError error) { | 220 ChannelError error) { |
| 221 ChannelInfo channel_info; | 221 ChannelInfo channel_info; |
| 222 channel_info.channel_id = channel_id; | 222 channel_info.channel_id = channel_id; |
| 223 channel_info.ready_state = cast_channel::READY_STATE_CLOSED; | 223 channel_info.ready_state = cast_channel::READY_STATE_CLOSED; |
| 224 channel_info.error_state = error; | 224 channel_info.error_state = error; |
| 225 channel_info.connect_info.ip_address = ""; | 225 channel_info.connect_info.ip_address = ""; |
| 226 channel_info.connect_info.port = 0; | 226 channel_info.connect_info.port = 0; |
| 227 channel_info.connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED; | 227 channel_info.connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED; |
| 228 SetResultFromChannelInfo(channel_info); | 228 SetResultFromChannelInfo(channel_info); |
| 229 SetError("Channel error = " + base::IntToString(error)); | 229 SetError("Channel error = " + base::IntToString(error)); |
| 230 } | 230 } |
| 231 | 231 |
| 232 CastSocket* CastChannelAsyncApiFunction::GetSocket(int channel_id) const { | 232 CastSocket* CastChannelAsyncApiFunction::GetSocket(int channel_id) const { |
| 233 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 233 auto* sockets = cast_socket_service_->GetOrCreateSocketRegistry(); |
| 234 return sockets_->Get(extension_->id(), channel_id); | 234 return sockets->GetSocket(channel_id); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( | 237 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( |
| 238 const ChannelInfo& channel_info) { | 238 const ChannelInfo& channel_info) { |
| 239 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 239 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 240 SetResult(channel_info.ToValue()); | 240 SetResult(channel_info.ToValue()); |
| 241 } | 241 } |
| 242 | 242 |
| 243 CastChannelOpenFunction::CastChannelOpenFunction() | 243 CastChannelOpenFunction::CastChannelOpenFunction() |
| 244 : new_channel_id_(0) { | 244 : new_channel_id_(0) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 socket = new cast_channel::CastSocketImpl( | 311 socket = new cast_channel::CastSocketImpl( |
| 312 extension_->id(), *ip_endpoint_, channel_auth_, | 312 extension_->id(), *ip_endpoint_, channel_auth_, |
| 313 ExtensionsBrowserClient::Get()->GetNetLog(), | 313 ExtensionsBrowserClient::Get()->GetNetLog(), |
| 314 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() | 314 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() |
| 315 ? *connect_info.timeout | 315 ? *connect_info.timeout |
| 316 : kDefaultConnectTimeoutMillis), | 316 : kDefaultConnectTimeoutMillis), |
| 317 liveness_timeout_ > base::TimeDelta(), api_->GetLogger(), | 317 liveness_timeout_ > base::TimeDelta(), api_->GetLogger(), |
| 318 connect_info.capabilities.get() ? *connect_info.capabilities | 318 connect_info.capabilities.get() ? *connect_info.capabilities |
| 319 : CastDeviceCapability::NONE); | 319 : CastDeviceCapability::NONE); |
| 320 } | 320 } |
| 321 new_channel_id_ = AddSocket(socket); | 321 new_channel_id_ = AddSocket(base::WrapUnique(socket)); |
| 322 | 322 |
| 323 // Construct read delegates. | 323 // Construct read delegates. |
| 324 std::unique_ptr<api::cast_channel::CastTransport::Delegate> delegate( | 324 std::unique_ptr<api::cast_channel::CastTransport::Delegate> delegate( |
| 325 base::MakeUnique<CastMessageHandler>( | 325 base::MakeUnique<CastMessageHandler>( |
| 326 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr()), socket, | 326 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr()), socket, |
| 327 api_->GetLogger())); | 327 api_->GetLogger())); |
| 328 if (socket->keep_alive()) { | 328 if (socket->keep_alive()) { |
| 329 // Wrap read delegate in a KeepAliveDelegate for timeout handling. | 329 // Wrap read delegate in a KeepAliveDelegate for timeout handling. |
| 330 api::cast_channel::KeepAliveDelegate* keep_alive = | 330 api::cast_channel::KeepAliveDelegate* keep_alive = |
| 331 new api::cast_channel::KeepAliveDelegate( | 331 new api::cast_channel::KeepAliveDelegate( |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 BrowserThread::PostTask( | 519 BrowserThread::PostTask( |
| 520 BrowserThread::UI, FROM_HERE, | 520 BrowserThread::UI, FROM_HERE, |
| 521 base::Bind(ui_dispatch_cb_, socket_->owner_extension_id(), | 521 base::Bind(ui_dispatch_cb_, socket_->owner_extension_id(), |
| 522 base::Passed(std::move(event)))); | 522 base::Passed(std::move(event)))); |
| 523 } | 523 } |
| 524 | 524 |
| 525 void CastChannelOpenFunction::CastMessageHandler::Start() { | 525 void CastChannelOpenFunction::CastMessageHandler::Start() { |
| 526 } | 526 } |
| 527 | 527 |
| 528 } // namespace extensions | 528 } // namespace extensions |
| OLD | NEW |