| 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_message_util.h" | |
| 22 #include "components/cast_channel/cast_socket.h" | |
| 23 #include "components/cast_channel/cast_socket_service.h" | |
| 24 #include "components/cast_channel/cast_socket_service_factory.h" | |
| 25 #include "components/cast_channel/keep_alive_delegate.h" | |
| 26 #include "components/cast_channel/logger.h" | |
| 27 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 28 #include "extensions/browser/api/cast_channel/cast_channel_enum_util.h" | 22 #include "extensions/browser/api/cast_channel/cast_channel_enum_util.h" |
| 29 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 23 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 24 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 25 #include "extensions/browser/api/cast_channel/cast_socket_service.h" |
| 26 #include "extensions/browser/api/cast_channel/cast_socket_service_factory.h" |
| 27 #include "extensions/browser/api/cast_channel/keep_alive_delegate.h" |
| 28 #include "extensions/browser/api/cast_channel/logger.h" |
| 30 #include "extensions/browser/event_router.h" | 29 #include "extensions/browser/event_router.h" |
| 31 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 30 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| 32 #include "extensions/common/api/cast_channel/logging.pb.h" | 31 #include "extensions/common/api/cast_channel/logging.pb.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" |
| 36 | 35 |
| 37 // Default timeout interval for connection setup. | 36 // Default timeout interval for connection setup. |
| 38 // Used if not otherwise specified at ConnectInfo::timeout. | 37 // Used if not otherwise specified at ConnectInfo::timeout. |
| 39 const int kDefaultConnectTimeoutMillis = 5000; // 5 seconds. | 38 const int kDefaultConnectTimeoutMillis = 5000; // 5 seconds. |
| 40 | 39 |
| 41 namespace extensions { | 40 namespace extensions { |
| 42 | 41 |
| 43 namespace Close = api::cast_channel::Close; | 42 namespace Close = cast_channel::Close; |
| 44 namespace OnError = api::cast_channel::OnError; | 43 namespace OnError = cast_channel::OnError; |
| 45 namespace OnMessage = api::cast_channel::OnMessage; | 44 namespace OnMessage = cast_channel::OnMessage; |
| 46 namespace Open = api::cast_channel::Open; | 45 namespace Open = cast_channel::Open; |
| 47 namespace Send = api::cast_channel::Send; | 46 namespace Send = cast_channel::Send; |
| 48 using api::cast_channel::ChannelInfo; | 47 using ::cast_channel::ChannelError; |
| 49 using api::cast_channel::ConnectInfo; | |
| 50 using api::cast_channel::ErrorInfo; | |
| 51 using api::cast_channel::MessageInfo; | |
| 52 using cast_channel::ChannelError; | |
| 53 using cast_channel::CastDeviceCapability; | 48 using cast_channel::CastDeviceCapability; |
| 54 using cast_channel::CastMessage; | 49 using cast_channel::CastMessage; |
| 55 using cast_channel::CastSocket; | 50 using cast_channel::CastSocket; |
| 56 using cast_channel::CastSocketImpl; | 51 using cast_channel::CastSocketImpl; |
| 57 using cast_channel::CastTransport; | 52 using cast_channel::CastTransport; |
| 53 using cast_channel::ChannelInfo; |
| 54 using cast_channel::ConnectInfo; |
| 55 using cast_channel::ErrorInfo; |
| 58 using cast_channel::KeepAliveDelegate; | 56 using cast_channel::KeepAliveDelegate; |
| 59 using cast_channel::LastErrors; | 57 using cast_channel::LastErrors; |
| 60 using cast_channel::Logger; | 58 using cast_channel::Logger; |
| 61 | 59 using cast_channel::MessageInfo; |
| 62 using content::BrowserThread; | 60 using content::BrowserThread; |
| 63 | 61 |
| 64 namespace { | 62 namespace { |
| 65 | 63 |
| 66 // T is an extension dictionary (MessageInfo or ChannelInfo) | 64 // T is an extension dictionary (MessageInfo or ChannelInfo) |
| 67 template <class T> | 65 template <class T> |
| 68 std::string ParamToString(const T& info) { | 66 std::string ParamToString(const T& info) { |
| 69 std::unique_ptr<base::DictionaryValue> dict = info.ToValue(); | 67 std::unique_ptr<base::DictionaryValue> dict = info.ToValue(); |
| 70 std::string out; | 68 std::string out; |
| 71 base::JSONWriter::Write(*dict, &out); | 69 base::JSONWriter::Write(*dict, &out); |
| 72 return out; | 70 return out; |
| 73 } | 71 } |
| 74 | 72 |
| 75 // Fills |channel_info| from the destination and state of |socket|. | 73 // Fills |channel_info| from the destination and state of |socket|. |
| 76 void FillChannelInfo(const CastSocket& socket, ChannelInfo* channel_info) { | 74 void FillChannelInfo(const CastSocket& socket, ChannelInfo* channel_info) { |
| 77 DCHECK(channel_info); | 75 DCHECK(channel_info); |
| 78 channel_info->channel_id = socket.id(); | 76 channel_info->channel_id = socket.id(); |
| 79 const net::IPEndPoint& ip_endpoint = socket.ip_endpoint(); | 77 const net::IPEndPoint& ip_endpoint = socket.ip_endpoint(); |
| 80 channel_info->connect_info.ip_address = ip_endpoint.ToStringWithoutPort(); | 78 channel_info->connect_info.ip_address = ip_endpoint.ToStringWithoutPort(); |
| 81 channel_info->connect_info.port = ip_endpoint.port(); | 79 channel_info->connect_info.port = ip_endpoint.port(); |
| 82 channel_info->connect_info.auth = | 80 channel_info->connect_info.auth = |
| 83 api::cast_channel::ToChannelAuthType(socket.channel_auth()); | 81 cast_channel::ToChannelAuthType(socket.channel_auth()); |
| 84 channel_info->ready_state = | 82 channel_info->ready_state = cast_channel::ToReadyState(socket.ready_state()); |
| 85 api::cast_channel::ToReadyState(socket.ready_state()); | |
| 86 channel_info->error_state = | 83 channel_info->error_state = |
| 87 api::cast_channel::ToChannelError(socket.error_state()); | 84 cast_channel::ToChannelError(socket.error_state()); |
| 88 channel_info->keep_alive = socket.keep_alive(); | 85 channel_info->keep_alive = socket.keep_alive(); |
| 89 channel_info->audio_only = socket.audio_only(); | 86 channel_info->audio_only = socket.audio_only(); |
| 90 } | 87 } |
| 91 | 88 |
| 92 // Fills |error_info| from |error_state| and |last_errors|. | 89 // Fills |error_info| from |error_state| and |last_errors|. |
| 93 void FillErrorInfo(api::cast_channel::ChannelError error_state, | 90 void FillErrorInfo(cast_channel::ChannelError error_state, |
| 94 const LastErrors& last_errors, | 91 const LastErrors& last_errors, |
| 95 ErrorInfo* error_info) { | 92 ErrorInfo* error_info) { |
| 96 error_info->error_state = error_state; | 93 error_info->error_state = error_state; |
| 97 if (last_errors.event_type != cast_channel::proto::EVENT_TYPE_UNKNOWN) | 94 if (last_errors.event_type != cast_channel::proto::EVENT_TYPE_UNKNOWN) |
| 98 error_info->event_type.reset(new int(last_errors.event_type)); | 95 error_info->event_type.reset(new int(last_errors.event_type)); |
| 99 if (last_errors.challenge_reply_error_type != | 96 if (last_errors.challenge_reply_error_type != |
| 100 cast_channel::proto::CHALLENGE_REPLY_ERROR_NONE) { | 97 cast_channel::proto::CHALLENGE_REPLY_ERROR_NONE) { |
| 101 error_info->challenge_reply_error_type.reset( | 98 error_info->challenge_reply_error_type.reset( |
| 102 new int(last_errors.challenge_reply_error_type)); | 99 new int(last_errors.challenge_reply_error_type)); |
| 103 } | 100 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 171 |
| 175 CastChannelAPI::~CastChannelAPI() {} | 172 CastChannelAPI::~CastChannelAPI() {} |
| 176 | 173 |
| 177 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() | 174 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() |
| 178 : cast_socket_service_(nullptr) {} | 175 : cast_socket_service_(nullptr) {} |
| 179 | 176 |
| 180 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } | 177 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } |
| 181 | 178 |
| 182 bool CastChannelAsyncApiFunction::PrePrepare() { | 179 bool CastChannelAsyncApiFunction::PrePrepare() { |
| 183 cast_socket_service_ = | 180 cast_socket_service_ = |
| 184 cast_channel::CastSocketServiceFactory::GetForBrowserContext( | 181 api::cast_channel::CastSocketServiceFactory::GetForBrowserContext( |
| 185 browser_context()); | 182 browser_context()); |
| 186 DCHECK(cast_socket_service_); | 183 DCHECK(cast_socket_service_); |
| 187 return true; | 184 return true; |
| 188 } | 185 } |
| 189 | 186 |
| 190 bool CastChannelAsyncApiFunction::Respond() { | 187 bool CastChannelAsyncApiFunction::Respond() { |
| 191 return GetError().empty(); | 188 return GetError().empty(); |
| 192 } | 189 } |
| 193 | 190 |
| 194 void CastChannelAsyncApiFunction::SetResultFromSocket( | 191 void CastChannelAsyncApiFunction::SetResultFromSocket( |
| 195 const CastSocket& socket) { | 192 const CastSocket& socket) { |
| 196 ChannelInfo channel_info; | 193 ChannelInfo channel_info; |
| 197 FillChannelInfo(socket, &channel_info); | 194 FillChannelInfo(socket, &channel_info); |
| 198 api::cast_channel::ChannelError error = | 195 cast_channel::ChannelError error = |
| 199 api::cast_channel::ToChannelError(socket.error_state()); | 196 cast_channel::ToChannelError(socket.error_state()); |
| 200 if (error != api::cast_channel::CHANNEL_ERROR_NONE) { | 197 if (error != cast_channel::CHANNEL_ERROR_NONE) { |
| 201 SetError("Channel socket error = " + base::IntToString(error)); | 198 SetError("Channel socket error = " + base::IntToString(error)); |
| 202 } | 199 } |
| 203 SetResultFromChannelInfo(channel_info); | 200 SetResultFromChannelInfo(channel_info); |
| 204 } | 201 } |
| 205 | 202 |
| 206 void CastChannelAsyncApiFunction::SetResultFromError( | 203 void CastChannelAsyncApiFunction::SetResultFromError( |
| 207 int channel_id, | 204 int channel_id, |
| 208 api::cast_channel::ChannelError error) { | 205 cast_channel::ChannelError error) { |
| 209 ChannelInfo channel_info; | 206 ChannelInfo channel_info; |
| 210 channel_info.channel_id = channel_id; | 207 channel_info.channel_id = channel_id; |
| 211 channel_info.ready_state = api::cast_channel::READY_STATE_CLOSED; | 208 channel_info.ready_state = cast_channel::READY_STATE_CLOSED; |
| 212 channel_info.error_state = error; | 209 channel_info.error_state = error; |
| 213 channel_info.connect_info.ip_address = ""; | 210 channel_info.connect_info.ip_address = ""; |
| 214 channel_info.connect_info.port = 0; | 211 channel_info.connect_info.port = 0; |
| 215 channel_info.connect_info.auth = | 212 channel_info.connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED; |
| 216 api::cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED; | |
| 217 SetResultFromChannelInfo(channel_info); | 213 SetResultFromChannelInfo(channel_info); |
| 218 SetError("Channel error = " + base::IntToString(error)); | 214 SetError("Channel error = " + base::IntToString(error)); |
| 219 } | 215 } |
| 220 | 216 |
| 221 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( | 217 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( |
| 222 const ChannelInfo& channel_info) { | 218 const ChannelInfo& channel_info) { |
| 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 219 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 224 SetResult(channel_info.ToValue()); | 220 SetResult(channel_info.ToValue()); |
| 225 } | 221 } |
| 226 | 222 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 SetError("livenessTimeout and pingInterval must be set together."); | 267 SetError("livenessTimeout and pingInterval must be set together."); |
| 272 } else if (liveness_timeout_ < ping_interval_) { | 268 } else if (liveness_timeout_ < ping_interval_) { |
| 273 SetError("livenessTimeout must be longer than pingTimeout."); | 269 SetError("livenessTimeout must be longer than pingTimeout."); |
| 274 } | 270 } |
| 275 } | 271 } |
| 276 | 272 |
| 277 if (!GetError().empty()) { | 273 if (!GetError().empty()) { |
| 278 return false; | 274 return false; |
| 279 } | 275 } |
| 280 | 276 |
| 281 channel_auth_ = | 277 channel_auth_ = cast_channel::ToChannelAuthTypeInternal(connect_info.auth); |
| 282 api::cast_channel::ToChannelAuthTypeInternal(connect_info.auth); | |
| 283 ip_endpoint_.reset(ParseConnectInfo(connect_info)); | 278 ip_endpoint_.reset(ParseConnectInfo(connect_info)); |
| 284 return true; | 279 return true; |
| 285 } | 280 } |
| 286 | 281 |
| 287 void CastChannelOpenFunction::AsyncWorkStart() { | 282 void CastChannelOpenFunction::AsyncWorkStart() { |
| 288 DCHECK(api_); | 283 DCHECK(api_); |
| 289 DCHECK(ip_endpoint_.get()); | 284 DCHECK(ip_endpoint_.get()); |
| 290 const ConnectInfo& connect_info = params_->connect_info; | 285 const ConnectInfo& connect_info = params_->connect_info; |
| 291 CastSocket* socket; | 286 CastSocket* socket; |
| 292 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); | 287 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 VLOG(1) << "Connect finished, OnOpen invoked."; | 329 VLOG(1) << "Connect finished, OnOpen invoked."; |
| 335 // TODO: If we failed to open the CastSocket, we may want to clean up here, | 330 // 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 | 331 // rather than relying on the extension to call close(). This can be done by |
| 337 // calling RemoveSocket() and api_->GetLogger()->ClearLastErrors(channel_id). | 332 // calling RemoveSocket() and api_->GetLogger()->ClearLastErrors(channel_id). |
| 338 if (result != ChannelError::UNKNOWN) { | 333 if (result != ChannelError::UNKNOWN) { |
| 339 CastSocket* socket = cast_socket_service_->GetSocket(new_channel_id_); | 334 CastSocket* socket = cast_socket_service_->GetSocket(new_channel_id_); |
| 340 CHECK(socket); | 335 CHECK(socket); |
| 341 SetResultFromSocket(*socket); | 336 SetResultFromSocket(*socket); |
| 342 } else { | 337 } else { |
| 343 // The socket is being destroyed. | 338 // The socket is being destroyed. |
| 344 SetResultFromError(new_channel_id_, | 339 SetResultFromError(new_channel_id_, cast_channel::CHANNEL_ERROR_UNKNOWN); |
| 345 api::cast_channel::CHANNEL_ERROR_UNKNOWN); | |
| 346 } | 340 } |
| 347 | 341 |
| 348 AsyncWorkCompleted(); | 342 AsyncWorkCompleted(); |
| 349 } | 343 } |
| 350 | 344 |
| 351 CastChannelSendFunction::CastChannelSendFunction() { } | 345 CastChannelSendFunction::CastChannelSendFunction() { } |
| 352 | 346 |
| 353 CastChannelSendFunction::~CastChannelSendFunction() { } | 347 CastChannelSendFunction::~CastChannelSendFunction() { } |
| 354 | 348 |
| 355 bool CastChannelSendFunction::Prepare() { | 349 bool CastChannelSendFunction::Prepare() { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 376 return false; | 370 return false; |
| 377 } | 371 } |
| 378 return true; | 372 return true; |
| 379 } | 373 } |
| 380 | 374 |
| 381 void CastChannelSendFunction::AsyncWorkStart() { | 375 void CastChannelSendFunction::AsyncWorkStart() { |
| 382 CastSocket* socket = | 376 CastSocket* socket = |
| 383 cast_socket_service_->GetSocket(params_->channel.channel_id); | 377 cast_socket_service_->GetSocket(params_->channel.channel_id); |
| 384 if (!socket) { | 378 if (!socket) { |
| 385 SetResultFromError(params_->channel.channel_id, | 379 SetResultFromError(params_->channel.channel_id, |
| 386 api::cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); | 380 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); |
| 387 AsyncWorkCompleted(); | 381 AsyncWorkCompleted(); |
| 388 return; | 382 return; |
| 389 } | 383 } |
| 390 CastMessage message_to_send; | 384 CastMessage message_to_send; |
| 391 if (!MessageInfoToCastMessage(params_->message, &message_to_send)) { | 385 if (!MessageInfoToCastMessage(params_->message, &message_to_send)) { |
| 392 SetResultFromError(params_->channel.channel_id, | 386 SetResultFromError(params_->channel.channel_id, |
| 393 api::cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | 387 cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); |
| 394 AsyncWorkCompleted(); | 388 AsyncWorkCompleted(); |
| 395 return; | 389 return; |
| 396 } | 390 } |
| 397 socket->transport()->SendMessage( | 391 socket->transport()->SendMessage( |
| 398 message_to_send, base::Bind(&CastChannelSendFunction::OnSend, this)); | 392 message_to_send, base::Bind(&CastChannelSendFunction::OnSend, this)); |
| 399 } | 393 } |
| 400 | 394 |
| 401 void CastChannelSendFunction::OnSend(int result) { | 395 void CastChannelSendFunction::OnSend(int result) { |
| 402 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 396 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 403 int channel_id = params_->channel.channel_id; | 397 int channel_id = params_->channel.channel_id; |
| 404 CastSocket* socket = cast_socket_service_->GetSocket(channel_id); | 398 CastSocket* socket = cast_socket_service_->GetSocket(channel_id); |
| 405 if (result < 0 || !socket) { | 399 if (result < 0 || !socket) { |
| 406 SetResultFromError(channel_id, | 400 SetResultFromError(channel_id, |
| 407 api::cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 401 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| 408 } else { | 402 } else { |
| 409 SetResultFromSocket(*socket); | 403 SetResultFromSocket(*socket); |
| 410 } | 404 } |
| 411 AsyncWorkCompleted(); | 405 AsyncWorkCompleted(); |
| 412 } | 406 } |
| 413 | 407 |
| 414 CastChannelCloseFunction::CastChannelCloseFunction() { } | 408 CastChannelCloseFunction::CastChannelCloseFunction() { } |
| 415 | 409 |
| 416 CastChannelCloseFunction::~CastChannelCloseFunction() { } | 410 CastChannelCloseFunction::~CastChannelCloseFunction() { } |
| 417 | 411 |
| 418 bool CastChannelCloseFunction::PrePrepare() { | 412 bool CastChannelCloseFunction::PrePrepare() { |
| 419 api_ = CastChannelAPI::Get(browser_context()); | 413 api_ = CastChannelAPI::Get(browser_context()); |
| 420 return CastChannelAsyncApiFunction::PrePrepare(); | 414 return CastChannelAsyncApiFunction::PrePrepare(); |
| 421 } | 415 } |
| 422 | 416 |
| 423 bool CastChannelCloseFunction::Prepare() { | 417 bool CastChannelCloseFunction::Prepare() { |
| 424 params_ = Close::Params::Create(*args_); | 418 params_ = Close::Params::Create(*args_); |
| 425 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 419 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 426 return true; | 420 return true; |
| 427 } | 421 } |
| 428 | 422 |
| 429 void CastChannelCloseFunction::AsyncWorkStart() { | 423 void CastChannelCloseFunction::AsyncWorkStart() { |
| 430 CastSocket* socket = | 424 CastSocket* socket = |
| 431 cast_socket_service_->GetSocket(params_->channel.channel_id); | 425 cast_socket_service_->GetSocket(params_->channel.channel_id); |
| 432 if (!socket) { | 426 if (!socket) { |
| 433 SetResultFromError(params_->channel.channel_id, | 427 SetResultFromError(params_->channel.channel_id, |
| 434 api::cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); | 428 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); |
| 435 AsyncWorkCompleted(); | 429 AsyncWorkCompleted(); |
| 436 } else { | 430 } else { |
| 437 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this)); | 431 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this)); |
| 438 } | 432 } |
| 439 } | 433 } |
| 440 | 434 |
| 441 void CastChannelCloseFunction::OnClose(int result) { | 435 void CastChannelCloseFunction::OnClose(int result) { |
| 442 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 436 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 443 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; | 437 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; |
| 444 int channel_id = params_->channel.channel_id; | 438 int channel_id = params_->channel.channel_id; |
| 445 CastSocket* socket = cast_socket_service_->GetSocket(channel_id); | 439 CastSocket* socket = cast_socket_service_->GetSocket(channel_id); |
| 446 if (result < 0 || !socket) { | 440 if (result < 0 || !socket) { |
| 447 SetResultFromError(channel_id, | 441 SetResultFromError(channel_id, |
| 448 api::cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 442 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| 449 } else { | 443 } else { |
| 450 SetResultFromSocket(*socket); | 444 SetResultFromSocket(*socket); |
| 451 // This will delete |socket|. | 445 // This will delete |socket|. |
| 452 cast_socket_service_->RemoveSocket(channel_id); | 446 cast_socket_service_->RemoveSocket(channel_id); |
| 453 api_->GetLogger()->ClearLastErrors(channel_id); | 447 api_->GetLogger()->ClearLastErrors(channel_id); |
| 454 } | 448 } |
| 455 AsyncWorkCompleted(); | 449 AsyncWorkCompleted(); |
| 456 } | 450 } |
| 457 | 451 |
| 458 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler( | 452 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler( |
| 459 const EventDispatchCallback& ui_dispatch_cb, | 453 const EventDispatchCallback& ui_dispatch_cb, |
| 460 CastSocket* socket, | 454 CastSocket* socket, |
| 461 scoped_refptr<Logger> logger) | 455 scoped_refptr<Logger> logger) |
| 462 : ui_dispatch_cb_(ui_dispatch_cb), socket_(socket), logger_(logger) { | 456 : ui_dispatch_cb_(ui_dispatch_cb), socket_(socket), logger_(logger) { |
| 463 DCHECK(socket_); | 457 DCHECK(socket_); |
| 464 DCHECK(logger_); | 458 DCHECK(logger_); |
| 465 } | 459 } |
| 466 | 460 |
| 467 CastChannelOpenFunction::CastMessageHandler::~CastMessageHandler() { | 461 CastChannelOpenFunction::CastMessageHandler::~CastMessageHandler() { |
| 468 } | 462 } |
| 469 | 463 |
| 470 void CastChannelOpenFunction::CastMessageHandler::OnError( | 464 void CastChannelOpenFunction::CastMessageHandler::OnError( |
| 471 ChannelError error_state) { | 465 ChannelError error_state) { |
| 472 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 466 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 473 | 467 |
| 474 ChannelInfo channel_info; | 468 ChannelInfo channel_info; |
| 475 FillChannelInfo(*socket_, &channel_info); | 469 FillChannelInfo(*socket_, &channel_info); |
| 476 channel_info.error_state = api::cast_channel::ToChannelError(error_state); | 470 channel_info.error_state = cast_channel::ToChannelError(error_state); |
| 477 ErrorInfo error_info; | 471 ErrorInfo error_info; |
| 478 FillErrorInfo(channel_info.error_state, logger_->GetLastErrors(socket_->id()), | 472 FillErrorInfo(channel_info.error_state, logger_->GetLastErrors(socket_->id()), |
| 479 &error_info); | 473 &error_info); |
| 480 | 474 |
| 481 std::unique_ptr<base::ListValue> results = | 475 std::unique_ptr<base::ListValue> results = |
| 482 OnError::Create(channel_info, error_info); | 476 OnError::Create(channel_info, error_info); |
| 483 std::unique_ptr<Event> event(new Event( | 477 std::unique_ptr<Event> event(new Event( |
| 484 events::CAST_CHANNEL_ON_ERROR, OnError::kEventName, std::move(results))); | 478 events::CAST_CHANNEL_ON_ERROR, OnError::kEventName, std::move(results))); |
| 485 BrowserThread::PostTask( | 479 BrowserThread::PostTask( |
| 486 BrowserThread::UI, FROM_HERE, | 480 BrowserThread::UI, FROM_HERE, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 505 std::move(results))); | 499 std::move(results))); |
| 506 BrowserThread::PostTask( | 500 BrowserThread::PostTask( |
| 507 BrowserThread::UI, FROM_HERE, | 501 BrowserThread::UI, FROM_HERE, |
| 508 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); | 502 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); |
| 509 } | 503 } |
| 510 | 504 |
| 511 void CastChannelOpenFunction::CastMessageHandler::Start() { | 505 void CastChannelOpenFunction::CastMessageHandler::Start() { |
| 512 } | 506 } |
| 513 | 507 |
| 514 } // namespace extensions | 508 } // namespace extensions |
| OLD | NEW |