Chromium Code Reviews| 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> |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 SetResultFromChannelInfo(channel_info); | 217 SetResultFromChannelInfo(channel_info); |
| 218 SetError("Channel error = " + base::IntToString(error)); | 218 SetError("Channel error = " + base::IntToString(error)); |
| 219 } | 219 } |
| 220 | 220 |
| 221 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( | 221 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( |
| 222 const ChannelInfo& channel_info) { | 222 const ChannelInfo& channel_info) { |
| 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 224 SetResult(channel_info.ToValue()); | 224 SetResult(channel_info.ToValue()); |
| 225 } | 225 } |
| 226 | 226 |
| 227 CastChannelOpenFunction::CastChannelOpenFunction() | 227 CastChannelOpenFunction::CastChannelOpenFunction() {} |
| 228 : new_channel_id_(0) { | |
| 229 } | |
| 230 | 228 |
| 231 CastChannelOpenFunction::~CastChannelOpenFunction() { } | 229 CastChannelOpenFunction::~CastChannelOpenFunction() { } |
| 232 | 230 |
| 233 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( | 231 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( |
| 234 const ConnectInfo& connect_info) { | 232 const ConnectInfo& connect_info) { |
| 235 net::IPAddress ip_address; | 233 net::IPAddress ip_address; |
| 236 CHECK(ip_address.AssignFromIPLiteral(connect_info.ip_address)); | 234 CHECK(ip_address.AssignFromIPLiteral(connect_info.ip_address)); |
| 237 return new net::IPEndPoint(ip_address, | 235 return new net::IPEndPoint(ip_address, |
| 238 static_cast<uint16_t>(connect_info.port)); | 236 static_cast<uint16_t>(connect_info.port)); |
| 239 } | 237 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 | 278 |
| 281 channel_auth_ = | 279 channel_auth_ = |
| 282 api::cast_channel::ToChannelAuthTypeInternal(connect_info.auth); | 280 api::cast_channel::ToChannelAuthTypeInternal(connect_info.auth); |
| 283 ip_endpoint_.reset(ParseConnectInfo(connect_info)); | 281 ip_endpoint_.reset(ParseConnectInfo(connect_info)); |
| 284 return true; | 282 return true; |
| 285 } | 283 } |
| 286 | 284 |
| 287 void CastChannelOpenFunction::AsyncWorkStart() { | 285 void CastChannelOpenFunction::AsyncWorkStart() { |
| 288 DCHECK(api_); | 286 DCHECK(api_); |
| 289 DCHECK(ip_endpoint_.get()); | 287 DCHECK(ip_endpoint_.get()); |
| 288 | |
| 289 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); | |
| 290 if (test_socket) | |
| 291 cast_socket_service_->SetSocketForTest(std::move(test_socket)); | |
| 292 | |
| 293 std::unique_ptr<base::Timer> injected_timer = | |
| 294 api_->GetInjectedTimeoutTimerForTest(); | |
| 295 if (injected_timer) | |
| 296 cast_socket_service_->SetPingTimeoutTimerForTest(std::move(injected_timer)); | |
| 297 | |
| 290 const ConnectInfo& connect_info = params_->connect_info; | 298 const ConnectInfo& connect_info = params_->connect_info; |
| 291 CastSocket* socket; | 299 base::TimeDelta connect_timeout = base::TimeDelta::FromMilliseconds( |
| 292 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); | 300 connect_info.timeout.get() ? *connect_info.timeout |
| 293 if (test_socket.get()) { | 301 : kDefaultConnectTimeoutMillis); |
| 294 socket = test_socket.release(); | 302 int capabilities = connect_info.capabilities.get() |
| 295 } else { | 303 ? *connect_info.capabilities |
| 296 socket = new CastSocketImpl( | 304 : CastDeviceCapability::NONE; |
| 297 extension_->id(), *ip_endpoint_, channel_auth_, | 305 cast_socket_service_->OpenSocket( |
| 298 ExtensionsBrowserClient::Get()->GetNetLog(), | 306 *ip_endpoint_, channel_auth_, ExtensionsBrowserClient::Get()->GetNetLog(), |
| 299 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() | 307 connect_timeout, ping_interval_, liveness_timeout_, api_->GetLogger(), |
| 300 ? *connect_info.timeout | 308 capabilities, base::Bind(&CastChannelOpenFunction::OnOpen, this)); |
| 301 : kDefaultConnectTimeoutMillis), | |
| 302 liveness_timeout_ > base::TimeDelta(), api_->GetLogger(), | |
| 303 connect_info.capabilities.get() ? *connect_info.capabilities | |
| 304 : CastDeviceCapability::NONE); | |
| 305 } | |
| 306 new_channel_id_ = cast_socket_service_->AddSocket(base::WrapUnique(socket)); | |
| 307 | |
| 308 // Construct read delegates. | |
| 309 std::unique_ptr<CastTransport::Delegate> delegate( | |
| 310 base::MakeUnique<CastMessageHandler>( | |
| 311 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr(), | |
| 312 extension_->id()), | |
| 313 socket, api_->GetLogger())); | |
| 314 if (socket->keep_alive()) { | |
| 315 // Wrap read delegate in a KeepAliveDelegate for timeout handling. | |
| 316 KeepAliveDelegate* keep_alive = | |
| 317 new KeepAliveDelegate(socket, api_->GetLogger(), std::move(delegate), | |
| 318 ping_interval_, liveness_timeout_); | |
| 319 std::unique_ptr<base::Timer> injected_timer = | |
| 320 api_->GetInjectedTimeoutTimerForTest(); | |
| 321 if (injected_timer) { | |
| 322 keep_alive->SetTimersForTest(base::MakeUnique<base::Timer>(false, false), | |
| 323 std::move(injected_timer)); | |
| 324 } | |
| 325 delegate.reset(keep_alive); | |
| 326 } | |
| 327 | |
| 328 socket->Connect(std::move(delegate), | |
| 329 base::Bind(&CastChannelOpenFunction::OnOpen, this)); | |
| 330 } | 309 } |
| 331 | 310 |
| 332 void CastChannelOpenFunction::OnOpen(ChannelError result) { | 311 void CastChannelOpenFunction::OnOpen(int channel_id, ChannelError result) { |
| 333 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 312 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 334 VLOG(1) << "Connect finished, OnOpen invoked."; | 313 VLOG(1) << "Connect finished, OnOpen invoked."; |
| 335 // TODO: If we failed to open the CastSocket, we may want to clean up here, | 314 // TODO: If we failed to open the CastSocket, we may want to clean up here, |
| 336 // rather than relying on the extension to call close(). This can be done by | 315 // rather than relying on the extension to call close(). This can be done by |
| 337 // calling RemoveSocket() and api_->GetLogger()->ClearLastErrors(channel_id). | 316 // calling RemoveSocket() and api_->GetLogger()->ClearLastErrors(channel_id). |
| 338 if (result != ChannelError::UNKNOWN) { | 317 if (result != ChannelError::UNKNOWN) { |
| 339 CastSocket* socket = cast_socket_service_->GetSocket(new_channel_id_); | 318 CastSocket* socket = cast_socket_service_->GetSocket(channel_id); |
| 340 CHECK(socket); | 319 CHECK(socket); |
| 320 | |
| 321 std::unique_ptr<CastTransport::Delegate> delegate( | |
| 322 base::MakeUnique<CastMessageHandler>( | |
| 323 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr(), | |
| 324 extension_->id()), | |
| 325 socket, api_->GetLogger())); | |
|
mark a. foltz
2017/06/12 21:14:09
You might want to pass the logger as an argument t
zhaobin
2017/06/20 01:37:42
Code removed. api_->GetLogger() is moved back to C
| |
| 326 cast_socket_service_->RegisterDelegate(channel_id, std::move(delegate)); | |
| 327 | |
| 341 SetResultFromSocket(*socket); | 328 SetResultFromSocket(*socket); |
| 342 } else { | 329 } else { |
| 343 // The socket is being destroyed. | 330 // The socket is being destroyed. |
| 344 SetResultFromError(new_channel_id_, | 331 SetResultFromError(channel_id, api::cast_channel::CHANNEL_ERROR_UNKNOWN); |
| 345 api::cast_channel::CHANNEL_ERROR_UNKNOWN); | |
| 346 } | 332 } |
| 347 | 333 |
| 348 AsyncWorkCompleted(); | 334 AsyncWorkCompleted(); |
| 349 } | 335 } |
| 350 | 336 |
| 351 CastChannelSendFunction::CastChannelSendFunction() { } | 337 CastChannelSendFunction::CastChannelSendFunction() { } |
| 352 | 338 |
| 353 CastChannelSendFunction::~CastChannelSendFunction() { } | 339 CastChannelSendFunction::~CastChannelSendFunction() { } |
| 354 | 340 |
| 355 bool CastChannelSendFunction::Prepare() { | 341 bool CastChannelSendFunction::Prepare() { |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 505 std::move(results))); | 491 std::move(results))); |
| 506 BrowserThread::PostTask( | 492 BrowserThread::PostTask( |
| 507 BrowserThread::UI, FROM_HERE, | 493 BrowserThread::UI, FROM_HERE, |
| 508 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); | 494 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); |
| 509 } | 495 } |
| 510 | 496 |
| 511 void CastChannelOpenFunction::CastMessageHandler::Start() { | 497 void CastChannelOpenFunction::CastMessageHandler::Start() { |
| 512 } | 498 } |
| 513 | 499 |
| 514 } // namespace extensions | 500 } // namespace extensions |
| OLD | NEW |