| 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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 SetResultFromChannelInfo(channel_info); | 209 SetResultFromChannelInfo(channel_info); |
| 210 SetError("Channel error = " + base::IntToString(error)); | 210 SetError("Channel error = " + base::IntToString(error)); |
| 211 } | 211 } |
| 212 | 212 |
| 213 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( | 213 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( |
| 214 const ChannelInfo& channel_info) { | 214 const ChannelInfo& channel_info) { |
| 215 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 215 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 216 SetResult(channel_info.ToValue()); | 216 SetResult(channel_info.ToValue()); |
| 217 } | 217 } |
| 218 | 218 |
| 219 CastChannelOpenFunction::CastChannelOpenFunction() | 219 CastChannelOpenFunction::CastChannelOpenFunction() {} |
| 220 : new_channel_id_(0) { | |
| 221 } | |
| 222 | 220 |
| 223 CastChannelOpenFunction::~CastChannelOpenFunction() { } | 221 CastChannelOpenFunction::~CastChannelOpenFunction() { } |
| 224 | 222 |
| 225 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( | 223 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( |
| 226 const ConnectInfo& connect_info) { | 224 const ConnectInfo& connect_info) { |
| 227 net::IPAddress ip_address; | 225 net::IPAddress ip_address; |
| 228 CHECK(ip_address.AssignFromIPLiteral(connect_info.ip_address)); | 226 CHECK(ip_address.AssignFromIPLiteral(connect_info.ip_address)); |
| 229 return new net::IPEndPoint(ip_address, | 227 return new net::IPEndPoint(ip_address, |
| 230 static_cast<uint16_t>(connect_info.port)); | 228 static_cast<uint16_t>(connect_info.port)); |
| 231 } | 229 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } | 269 } |
| 272 | 270 |
| 273 ip_endpoint_.reset(ParseConnectInfo(connect_info)); | 271 ip_endpoint_.reset(ParseConnectInfo(connect_info)); |
| 274 return true; | 272 return true; |
| 275 } | 273 } |
| 276 | 274 |
| 277 void CastChannelOpenFunction::AsyncWorkStart() { | 275 void CastChannelOpenFunction::AsyncWorkStart() { |
| 278 DCHECK(api_); | 276 DCHECK(api_); |
| 279 DCHECK(ip_endpoint_.get()); | 277 DCHECK(ip_endpoint_.get()); |
| 280 const ConnectInfo& connect_info = params_->connect_info; | 278 const ConnectInfo& connect_info = params_->connect_info; |
| 281 CastSocket* socket; | 279 |
| 282 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); | 280 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); |
| 283 if (test_socket.get()) { | 281 if (test_socket.get()) |
| 284 socket = test_socket.release(); | 282 cast_socket_service_->SetSocketForTest(std::move(test_socket)); |
| 285 } else { | |
| 286 socket = new CastSocketImpl( | |
| 287 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(), | |
| 288 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() | |
| 289 ? *connect_info.timeout | |
| 290 : kDefaultConnectTimeoutMillis), | |
| 291 liveness_timeout_, ping_interval_, api_->GetLogger(), | |
| 292 connect_info.capabilities.get() ? *connect_info.capabilities | |
| 293 : CastDeviceCapability::NONE); | |
| 294 } | |
| 295 new_channel_id_ = cast_socket_service_->AddSocket(base::WrapUnique(socket)); | |
| 296 | 283 |
| 297 auto* observer = cast_socket_service_->GetObserver(extension_->id()); | 284 auto* observer = cast_socket_service_->GetObserver(extension_->id()); |
| 298 if (!observer) { | 285 if (!observer) { |
| 299 observer = | 286 observer = |
| 300 new CastMessageHandler(base::Bind(&CastChannelAPI::SendEvent, | 287 new CastMessageHandler(base::Bind(&CastChannelAPI::SendEvent, |
| 301 api_->AsWeakPtr(), extension_->id()), | 288 api_->AsWeakPtr(), extension_->id()), |
| 302 api_->GetLogger()); | 289 api_->GetLogger()); |
| 303 cast_socket_service_->AddObserver(extension_->id(), | 290 cast_socket_service_->AddObserver(extension_->id(), |
| 304 base::WrapUnique(observer)); | 291 base::WrapUnique(observer)); |
| 305 } | 292 } |
| 306 | 293 |
| 307 socket->AddObserver(observer); | 294 cast_socket_service_->OpenSocket( |
| 308 // Construct read delegates. | 295 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(), |
| 309 socket->Connect(base::Bind(&CastChannelOpenFunction::OnOpen, this)); | 296 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() |
| 297 ? *connect_info.timeout |
| 298 : kDefaultConnectTimeoutMillis), |
| 299 liveness_timeout_, ping_interval_, api_->GetLogger(), |
| 300 connect_info.capabilities.get() ? *connect_info.capabilities |
| 301 : CastDeviceCapability::NONE, |
| 302 base::Bind(&CastChannelOpenFunction::OnOpen, this), observer); |
| 310 } | 303 } |
| 311 | 304 |
| 312 void CastChannelOpenFunction::OnOpen(ChannelError result) { | 305 void CastChannelOpenFunction::OnOpen(int channel_id, ChannelError result) { |
| 313 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 306 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 314 VLOG(1) << "Connect finished, OnOpen invoked."; | 307 VLOG(1) << "Connect finished, OnOpen invoked."; |
| 315 // TODO: If we failed to open the CastSocket, we may want to clean up here, | 308 // TODO: If we failed to open the CastSocket, we may want to clean up here, |
| 316 // rather than relying on the extension to call close(). This can be done by | 309 // rather than relying on the extension to call close(). This can be done by |
| 317 // calling RemoveSocket() and api_->GetLogger()->ClearLastError(channel_id). | 310 // calling RemoveSocket() and api_->GetLogger()->ClearLastError(channel_id). |
| 318 if (result != ChannelError::UNKNOWN) { | 311 if (result != ChannelError::UNKNOWN) { |
| 319 CastSocket* socket = cast_socket_service_->GetSocket(new_channel_id_); | 312 CastSocket* socket = cast_socket_service_->GetSocket(channel_id); |
| 320 CHECK(socket); | 313 CHECK(socket); |
| 321 SetResultFromSocket(*socket); | 314 SetResultFromSocket(*socket); |
| 322 } else { | 315 } else { |
| 323 // The socket is being destroyed. | 316 // The socket is being destroyed. |
| 324 SetResultFromError(new_channel_id_, | 317 SetResultFromError(channel_id, api::cast_channel::CHANNEL_ERROR_UNKNOWN); |
| 325 api::cast_channel::CHANNEL_ERROR_UNKNOWN); | |
| 326 } | 318 } |
| 327 | 319 |
| 328 AsyncWorkCompleted(); | 320 AsyncWorkCompleted(); |
| 329 } | 321 } |
| 330 | 322 |
| 331 CastChannelSendFunction::CastChannelSendFunction() { } | 323 CastChannelSendFunction::CastChannelSendFunction() { } |
| 332 | 324 |
| 333 CastChannelSendFunction::~CastChannelSendFunction() { } | 325 CastChannelSendFunction::~CastChannelSendFunction() { } |
| 334 | 326 |
| 335 bool CastChannelSendFunction::Prepare() { | 327 bool CastChannelSendFunction::Prepare() { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 OnMessage::Create(channel_info, message_info); | 474 OnMessage::Create(channel_info, message_info); |
| 483 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, | 475 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, |
| 484 OnMessage::kEventName, | 476 OnMessage::kEventName, |
| 485 std::move(results))); | 477 std::move(results))); |
| 486 BrowserThread::PostTask( | 478 BrowserThread::PostTask( |
| 487 BrowserThread::UI, FROM_HERE, | 479 BrowserThread::UI, FROM_HERE, |
| 488 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); | 480 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); |
| 489 } | 481 } |
| 490 | 482 |
| 491 } // namespace extensions | 483 } // namespace extensions |
| OLD | NEW |