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 <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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 const net::IPEndPoint& ip_endpoint = socket.ip_endpoint(); | 58 const net::IPEndPoint& ip_endpoint = socket.ip_endpoint(); |
| 59 channel_info->connect_info.ip_address = ip_endpoint.ToStringWithoutPort(); | 59 channel_info->connect_info.ip_address = ip_endpoint.ToStringWithoutPort(); |
| 60 channel_info->connect_info.port = ip_endpoint.port(); | 60 channel_info->connect_info.port = ip_endpoint.port(); |
| 61 channel_info->connect_info.auth = socket.channel_auth(); | 61 channel_info->connect_info.auth = socket.channel_auth(); |
| 62 channel_info->ready_state = socket.ready_state(); | 62 channel_info->ready_state = socket.ready_state(); |
| 63 channel_info->error_state = socket.error_state(); | 63 channel_info->error_state = socket.error_state(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 bool IsValidConnectInfoPort(const ConnectInfo& connect_info) { | 66 bool IsValidConnectInfoPort(const ConnectInfo& connect_info) { |
| 67 return connect_info.port > 0 && connect_info.port < | 67 return connect_info.port > 0 && connect_info.port < |
| 68 std::numeric_limits<unsigned short>::max(); | 68 std::numeric_limits<uint16>::max(); |
|
Wez
2014/08/04 21:32:32
unit16_t
mark a. foltz
2014/08/04 22:20:43
Done.
| |
| 69 } | 69 } |
| 70 | 70 |
| 71 bool IsValidConnectInfoAuth(const ConnectInfo& connect_info) { | 71 bool IsValidConnectInfoAuth(const ConnectInfo& connect_info) { |
| 72 return connect_info.auth == cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED || | 72 return connect_info.auth == cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED || |
| 73 connect_info.auth == cast_channel::CHANNEL_AUTH_TYPE_SSL; | 73 connect_info.auth == cast_channel::CHANNEL_AUTH_TYPE_SSL; |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool IsValidConnectInfoIpAddress(const ConnectInfo& connect_info) { | 76 bool IsValidConnectInfoIpAddress(const ConnectInfo& connect_info) { |
| 77 net::IPAddressNumber ip_address; | 77 net::IPAddressNumber ip_address; |
| 78 return net::ParseIPLiteralToNumber(connect_info.ip_address, &ip_address); | 78 return net::ParseIPLiteralToNumber(connect_info.ip_address, &ip_address); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 } | 155 } |
| 156 | 156 |
| 157 bool CastChannelAsyncApiFunction::Respond() { | 157 bool CastChannelAsyncApiFunction::Respond() { |
| 158 return error_ != cast_channel::CHANNEL_ERROR_NONE; | 158 return error_ != cast_channel::CHANNEL_ERROR_NONE; |
| 159 } | 159 } |
| 160 | 160 |
| 161 CastSocket* CastChannelAsyncApiFunction::GetSocketOrCompleteWithError( | 161 CastSocket* CastChannelAsyncApiFunction::GetSocketOrCompleteWithError( |
| 162 int channel_id) { | 162 int channel_id) { |
| 163 CastSocket* socket = GetSocket(channel_id); | 163 CastSocket* socket = GetSocket(channel_id); |
| 164 if (!socket) { | 164 if (!socket) { |
| 165 SetResultFromError(cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); | 165 SetResultFromError(channel_id, |
| 166 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); | |
| 166 AsyncWorkCompleted(); | 167 AsyncWorkCompleted(); |
| 167 } | 168 } |
| 168 return socket; | 169 return socket; |
| 169 } | 170 } |
| 170 | 171 |
| 171 int CastChannelAsyncApiFunction::AddSocket(CastSocket* socket) { | 172 int CastChannelAsyncApiFunction::AddSocket(CastSocket* socket) { |
| 172 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 173 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 173 DCHECK(socket); | 174 DCHECK(socket); |
| 174 DCHECK(manager_); | 175 DCHECK(manager_); |
| 175 const int id = manager_->Add(socket); | 176 const int id = manager_->Add(socket); |
| 176 socket->set_id(id); | 177 socket->set_id(id); |
| 177 return id; | 178 return id; |
| 178 } | 179 } |
| 179 | 180 |
| 180 void CastChannelAsyncApiFunction::RemoveSocket(int channel_id) { | 181 void CastChannelAsyncApiFunction::RemoveSocket(int channel_id) { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 182 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 182 DCHECK(manager_); | 183 DCHECK(manager_); |
| 183 manager_->Remove(extension_->id(), channel_id); | 184 manager_->Remove(extension_->id(), channel_id); |
| 184 } | 185 } |
| 185 | 186 |
| 186 void CastChannelAsyncApiFunction::SetResultFromSocket(int channel_id) { | 187 void CastChannelAsyncApiFunction::SetResultFromSocket( |
| 187 CastSocket* socket = GetSocket(channel_id); | 188 const CastSocket& socket) { |
| 188 DCHECK(socket); | |
| 189 ChannelInfo channel_info; | 189 ChannelInfo channel_info; |
| 190 FillChannelInfo(*socket, &channel_info); | 190 FillChannelInfo(socket, &channel_info); |
| 191 error_ = socket->error_state(); | 191 error_ = socket.error_state(); |
| 192 SetResultFromChannelInfo(channel_info); | 192 SetResultFromChannelInfo(channel_info); |
| 193 } | 193 } |
| 194 | 194 |
| 195 void CastChannelAsyncApiFunction::SetResultFromError(ChannelError error) { | 195 void CastChannelAsyncApiFunction::SetResultFromError(int channel_id, |
| 196 ChannelError error) { | |
| 196 ChannelInfo channel_info; | 197 ChannelInfo channel_info; |
| 197 channel_info.channel_id = -1; | 198 channel_info.channel_id = channel_id; |
| 198 channel_info.url = ""; | 199 channel_info.url = ""; |
| 199 channel_info.ready_state = cast_channel::READY_STATE_CLOSED; | 200 channel_info.ready_state = cast_channel::READY_STATE_CLOSED; |
| 200 channel_info.error_state = error; | 201 channel_info.error_state = error; |
| 202 channel_info.connect_info.ip_address = ""; | |
| 203 channel_info.connect_info.port = 0; | |
| 204 channel_info.connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL; | |
| 201 SetResultFromChannelInfo(channel_info); | 205 SetResultFromChannelInfo(channel_info); |
| 202 error_ = error; | 206 error_ = error; |
| 203 } | 207 } |
| 204 | 208 |
| 205 CastSocket* CastChannelAsyncApiFunction::GetSocket(int channel_id) { | 209 CastSocket* CastChannelAsyncApiFunction::GetSocket(int channel_id) { |
| 206 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 207 DCHECK(manager_); | 211 DCHECK(manager_); |
| 208 return manager_->Get(extension_->id(), channel_id); | 212 return manager_->Get(extension_->id(), channel_id); |
| 209 } | 213 } |
| 210 | 214 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 ? *connect_info_->timeout | 335 ? *connect_info_->timeout |
| 332 : kDefaultConnectTimeoutMillis)); | 336 : kDefaultConnectTimeoutMillis)); |
| 333 new_channel_id_ = AddSocket(socket.release()); | 337 new_channel_id_ = AddSocket(socket.release()); |
| 334 GetSocket(new_channel_id_)->Connect( | 338 GetSocket(new_channel_id_)->Connect( |
| 335 base::Bind(&CastChannelOpenFunction::OnOpen, this)); | 339 base::Bind(&CastChannelOpenFunction::OnOpen, this)); |
| 336 } | 340 } |
| 337 | 341 |
| 338 void CastChannelOpenFunction::OnOpen(int result) { | 342 void CastChannelOpenFunction::OnOpen(int result) { |
| 339 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 343 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 340 VLOG(1) << "Connect finished, OnOpen invoked."; | 344 VLOG(1) << "Connect finished, OnOpen invoked."; |
| 341 SetResultFromSocket(new_channel_id_); | 345 CastSocket* socket = GetSocket(new_channel_id_); |
| 346 if (!socket) { | |
| 347 SetResultFromError(new_channel_id_, | |
| 348 cast_channel::CHANNEL_ERROR_CONNECT_ERROR); | |
| 349 } else { | |
| 350 SetResultFromSocket(*socket); | |
| 351 } | |
| 342 AsyncWorkCompleted(); | 352 AsyncWorkCompleted(); |
| 343 } | 353 } |
| 344 | 354 |
| 345 CastChannelSendFunction::CastChannelSendFunction() { } | 355 CastChannelSendFunction::CastChannelSendFunction() { } |
| 346 | 356 |
| 347 CastChannelSendFunction::~CastChannelSendFunction() { } | 357 CastChannelSendFunction::~CastChannelSendFunction() { } |
| 348 | 358 |
| 349 bool CastChannelSendFunction::Prepare() { | 359 bool CastChannelSendFunction::Prepare() { |
| 350 params_ = Send::Params::Create(*args_); | 360 params_ = Send::Params::Create(*args_); |
| 351 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 361 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 375 void CastChannelSendFunction::AsyncWorkStart() { | 385 void CastChannelSendFunction::AsyncWorkStart() { |
| 376 CastSocket* socket = GetSocketOrCompleteWithError( | 386 CastSocket* socket = GetSocketOrCompleteWithError( |
| 377 params_->channel.channel_id); | 387 params_->channel.channel_id); |
| 378 if (socket) | 388 if (socket) |
| 379 socket->SendMessage(params_->message, | 389 socket->SendMessage(params_->message, |
| 380 base::Bind(&CastChannelSendFunction::OnSend, this)); | 390 base::Bind(&CastChannelSendFunction::OnSend, this)); |
| 381 } | 391 } |
| 382 | 392 |
| 383 void CastChannelSendFunction::OnSend(int result) { | 393 void CastChannelSendFunction::OnSend(int result) { |
| 384 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 394 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 385 if (result < 0) { | 395 int channel_id = params_->channel.channel_id; |
| 386 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 396 CastSocket* socket = GetSocket(channel_id); |
| 397 if (result < 0 || !socket) { | |
| 398 SetResultFromError(channel_id, | |
| 399 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | |
| 387 } else { | 400 } else { |
| 388 SetResultFromSocket(params_->channel.channel_id); | 401 SetResultFromSocket(*socket); |
| 389 } | 402 } |
| 390 AsyncWorkCompleted(); | 403 AsyncWorkCompleted(); |
| 391 } | 404 } |
| 392 | 405 |
| 393 CastChannelCloseFunction::CastChannelCloseFunction() { } | 406 CastChannelCloseFunction::CastChannelCloseFunction() { } |
| 394 | 407 |
| 395 CastChannelCloseFunction::~CastChannelCloseFunction() { } | 408 CastChannelCloseFunction::~CastChannelCloseFunction() { } |
| 396 | 409 |
| 397 bool CastChannelCloseFunction::Prepare() { | 410 bool CastChannelCloseFunction::Prepare() { |
| 398 params_ = Close::Params::Create(*args_); | 411 params_ = Close::Params::Create(*args_); |
| 399 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 412 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 400 return true; | 413 return true; |
| 401 } | 414 } |
| 402 | 415 |
| 403 void CastChannelCloseFunction::AsyncWorkStart() { | 416 void CastChannelCloseFunction::AsyncWorkStart() { |
| 404 CastSocket* socket = GetSocketOrCompleteWithError( | 417 CastSocket* socket = GetSocketOrCompleteWithError( |
| 405 params_->channel.channel_id); | 418 params_->channel.channel_id); |
| 406 if (socket) | 419 if (socket) |
| 407 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this)); | 420 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this)); |
| 408 } | 421 } |
| 409 | 422 |
| 410 void CastChannelCloseFunction::OnClose(int result) { | 423 void CastChannelCloseFunction::OnClose(int result) { |
| 411 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 424 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 412 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; | 425 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; |
| 413 if (result < 0) { | 426 int channel_id = params_->channel.channel_id; |
| 414 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 427 CastSocket* socket = GetSocket(channel_id); |
| 428 if (result < 0 || !socket) { | |
| 429 SetResultFromError(channel_id, | |
| 430 cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | |
| 415 } else { | 431 } else { |
| 416 int channel_id = params_->channel.channel_id; | 432 SetResultFromSocket(*socket); |
| 417 SetResultFromSocket(channel_id); | 433 // This will delete the socket. Must not use |socket| from here forward. |
|
Wez
2014/08/04 21:32:32
Consider NULLing |socket| so things will blow up i
mark a. foltz
2014/08/04 22:20:43
Done.
| |
| 418 RemoveSocket(channel_id); | 434 RemoveSocket(channel_id); |
| 419 } | 435 } |
| 420 AsyncWorkCompleted(); | 436 AsyncWorkCompleted(); |
| 421 } | 437 } |
| 422 | 438 |
| 423 } // namespace extensions | 439 } // namespace extensions |
| OLD | NEW |