| 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 <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 | 124 |
| 125 static base::LazyInstance<BrowserContextKeyedAPIFactory<CastChannelAPI> > | 125 static base::LazyInstance<BrowserContextKeyedAPIFactory<CastChannelAPI> > |
| 126 g_factory = LAZY_INSTANCE_INITIALIZER; | 126 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 127 | 127 |
| 128 // static | 128 // static |
| 129 BrowserContextKeyedAPIFactory<CastChannelAPI>* | 129 BrowserContextKeyedAPIFactory<CastChannelAPI>* |
| 130 CastChannelAPI::GetFactoryInstance() { | 130 CastChannelAPI::GetFactoryInstance() { |
| 131 return g_factory.Pointer(); | 131 return g_factory.Pointer(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 scoped_ptr<CastSocket> CastChannelAPI::CreateCastSocket( | |
| 135 const std::string& extension_id, const net::IPEndPoint& ip_endpoint, | |
| 136 ChannelAuthType channel_auth, const base::TimeDelta& timeout) { | |
| 137 if (socket_for_test_.get()) { | |
| 138 return socket_for_test_.Pass(); | |
| 139 } else { | |
| 140 return scoped_ptr<CastSocket>( | |
| 141 new CastSocket(extension_id, | |
| 142 ip_endpoint, | |
| 143 channel_auth, | |
| 144 this, | |
| 145 ExtensionsBrowserClient::Get()->GetNetLog(), | |
| 146 timeout, | |
| 147 logger_)); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void CastChannelAPI::SetSocketForTest(scoped_ptr<CastSocket> socket_for_test) { | 134 void CastChannelAPI::SetSocketForTest(scoped_ptr<CastSocket> socket_for_test) { |
| 152 socket_for_test_ = socket_for_test.Pass(); | 135 socket_for_test_ = socket_for_test.Pass(); |
| 153 } | 136 } |
| 154 | 137 |
| 138 scoped_ptr<cast_channel::CastSocket> CastChannelAPI::GetSocketForTest() { |
| 139 return socket_for_test_.Pass(); |
| 140 } |
| 141 |
| 155 void CastChannelAPI::OnError(const CastSocket* socket, | 142 void CastChannelAPI::OnError(const CastSocket* socket, |
| 156 cast_channel::ChannelError error_state, | 143 cast_channel::ChannelError error_state, |
| 157 const cast_channel::LastErrors& last_errors) { | 144 const cast_channel::LastErrors& last_errors) { |
| 158 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 145 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 159 ChannelInfo channel_info; | 146 ChannelInfo channel_info; |
| 160 FillChannelInfo(*socket, &channel_info); | 147 FillChannelInfo(*socket, &channel_info); |
| 161 channel_info.error_state = error_state; | 148 channel_info.error_state = error_state; |
| 162 ErrorInfo error_info; | 149 ErrorInfo error_info; |
| 163 FillErrorInfo(error_state, last_errors, &error_info); | 150 FillErrorInfo(error_state, last_errors, &error_info); |
| 164 scoped_ptr<base::ListValue> results = | 151 scoped_ptr<base::ListValue> results = |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 return false; | 347 return false; |
| 361 } | 348 } |
| 362 channel_auth_ = connect_info_->auth; | 349 channel_auth_ = connect_info_->auth; |
| 363 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); | 350 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); |
| 364 return true; | 351 return true; |
| 365 } | 352 } |
| 366 | 353 |
| 367 void CastChannelOpenFunction::AsyncWorkStart() { | 354 void CastChannelOpenFunction::AsyncWorkStart() { |
| 368 DCHECK(api_); | 355 DCHECK(api_); |
| 369 DCHECK(ip_endpoint_.get()); | 356 DCHECK(ip_endpoint_.get()); |
| 370 scoped_ptr<CastSocket> socket = api_->CreateCastSocket( | 357 scoped_ptr<CastSocket> socket = api_->GetSocketForTest(); |
| 371 extension_->id(), | 358 if (!socket.get()) { |
| 372 *ip_endpoint_, | 359 socket.reset(new CastSocket( |
| 373 channel_auth_, | 360 extension_->id(), |
| 374 base::TimeDelta::FromMilliseconds(connect_info_->timeout.get() | 361 *ip_endpoint_, |
| 375 ? *connect_info_->timeout | 362 channel_auth_, |
| 376 : kDefaultConnectTimeoutMillis)); | 363 api_, |
| 364 ExtensionsBrowserClient::Get()->GetNetLog(), |
| 365 base::TimeDelta::FromMilliseconds(connect_info_->timeout.get() |
| 366 ? *connect_info_->timeout |
| 367 : kDefaultConnectTimeoutMillis), |
| 368 api_->GetLogger())); |
| 369 } |
| 377 new_channel_id_ = AddSocket(socket.release()); | 370 new_channel_id_ = AddSocket(socket.release()); |
| 378 CastSocket* new_socket = GetSocket(new_channel_id_); | 371 CastSocket* new_socket = GetSocket(new_channel_id_); |
| 379 api_->GetLogger()->LogNewSocketEvent(*new_socket); | 372 api_->GetLogger()->LogNewSocketEvent(*new_socket); |
| 380 new_socket->Connect(base::Bind(&CastChannelOpenFunction::OnOpen, this)); | 373 new_socket->Connect(base::Bind(&CastChannelOpenFunction::OnOpen, this)); |
| 381 } | 374 } |
| 382 | 375 |
| 383 void CastChannelOpenFunction::OnOpen(int result) { | 376 void CastChannelOpenFunction::OnOpen(int result) { |
| 384 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 377 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 385 VLOG(1) << "Connect finished, OnOpen invoked."; | 378 VLOG(1) << "Connect finished, OnOpen invoked."; |
| 386 CastSocket* socket = GetSocket(new_channel_id_); | 379 CastSocket* socket = GetSocket(new_channel_id_); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 417 case base::Value::TYPE_BINARY: | 410 case base::Value::TYPE_BINARY: |
| 418 break; | 411 break; |
| 419 default: | 412 default: |
| 420 SetError("Invalid type of message_info.data"); | 413 SetError("Invalid type of message_info.data"); |
| 421 return false; | 414 return false; |
| 422 } | 415 } |
| 423 return true; | 416 return true; |
| 424 } | 417 } |
| 425 | 418 |
| 426 void CastChannelSendFunction::AsyncWorkStart() { | 419 void CastChannelSendFunction::AsyncWorkStart() { |
| 427 CastSocket* socket = GetSocketOrCompleteWithError( | 420 CastSocket* socket = GetSocket(params_->channel.channel_id); |
| 428 params_->channel.channel_id); | 421 if (!socket) { |
| 429 if (socket) | 422 SetResultFromError(params_->channel.channel_id, |
| 430 socket->SendMessage(params_->message, | 423 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); |
| 431 base::Bind(&CastChannelSendFunction::OnSend, this)); | 424 AsyncWorkCompleted(); |
| 425 return; |
| 426 } |
| 427 socket->SendMessage(params_->message, |
| 428 base::Bind(&CastChannelSendFunction::OnSend, this)); |
| 432 } | 429 } |
| 433 | 430 |
| 434 void CastChannelSendFunction::OnSend(int result) { | 431 void CastChannelSendFunction::OnSend(int result) { |
| 435 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 432 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 436 int channel_id = params_->channel.channel_id; | 433 int channel_id = params_->channel.channel_id; |
| 437 CastSocket* socket = GetSocket(channel_id); | 434 CastSocket* socket = GetSocket(channel_id); |
| 438 if (result < 0 || !socket) { | 435 if (result < 0 || !socket) { |
| 439 SetResultFromError(channel_id, | 436 SetResultFromError(channel_id, |
| 440 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 437 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| 441 } else { | 438 } else { |
| 442 SetResultFromSocket(*socket); | 439 SetResultFromSocket(*socket); |
| 443 } | 440 } |
| 444 AsyncWorkCompleted(); | 441 AsyncWorkCompleted(); |
| 445 } | 442 } |
| 446 | 443 |
| 447 CastChannelCloseFunction::CastChannelCloseFunction() { } | 444 CastChannelCloseFunction::CastChannelCloseFunction() { } |
| 448 | 445 |
| 449 CastChannelCloseFunction::~CastChannelCloseFunction() { } | 446 CastChannelCloseFunction::~CastChannelCloseFunction() { } |
| 450 | 447 |
| 451 bool CastChannelCloseFunction::Prepare() { | 448 bool CastChannelCloseFunction::Prepare() { |
| 452 params_ = Close::Params::Create(*args_); | 449 params_ = Close::Params::Create(*args_); |
| 453 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 450 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 454 return true; | 451 return true; |
| 455 } | 452 } |
| 456 | 453 |
| 457 void CastChannelCloseFunction::AsyncWorkStart() { | 454 void CastChannelCloseFunction::AsyncWorkStart() { |
| 458 CastSocket* socket = GetSocketOrCompleteWithError( | 455 CastSocket* socket = GetSocket(params_->channel.channel_id); |
| 459 params_->channel.channel_id); | 456 if (!socket) { |
| 460 if (socket) | 457 SetResultFromError(params_->channel.channel_id, |
| 458 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); |
| 459 AsyncWorkCompleted(); |
| 460 } else { |
| 461 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this)); | 461 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this)); |
| 462 } |
| 462 } | 463 } |
| 463 | 464 |
| 464 void CastChannelCloseFunction::OnClose(int result) { | 465 void CastChannelCloseFunction::OnClose(int result) { |
| 465 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 466 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 466 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; | 467 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; |
| 467 int channel_id = params_->channel.channel_id; | 468 int channel_id = params_->channel.channel_id; |
| 468 CastSocket* socket = GetSocket(channel_id); | 469 CastSocket* socket = GetSocket(channel_id); |
| 469 if (result < 0 || !socket) { | 470 if (result < 0 || !socket) { |
| 470 SetResultFromError(channel_id, | 471 SetResultFromError(channel_id, |
| 471 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 472 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 } else { | 504 } else { |
| 504 SetError("Unable to get logs."); | 505 SetError("Unable to get logs."); |
| 505 } | 506 } |
| 506 | 507 |
| 507 api_->GetLogger()->Reset(); | 508 api_->GetLogger()->Reset(); |
| 508 | 509 |
| 509 AsyncWorkCompleted(); | 510 AsyncWorkCompleted(); |
| 510 } | 511 } |
| 511 | 512 |
| 512 } // namespace extensions | 513 } // namespace extensions |
| OLD | NEW |