Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" | 5 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 VLOG(2) << "IP: " << ip_address_str << " Port: " << port_str; | 237 VLOG(2) << "IP: " << ip_address_str << " Port: " << port_str; |
| 238 int port; | 238 int port; |
| 239 if (!base::StringToInt(port_str, &port)) | 239 if (!base::StringToInt(port_str, &port)) |
| 240 return false; | 240 return false; |
| 241 connect_info->ip_address = ip_address_str; | 241 connect_info->ip_address = ip_address_str; |
| 242 connect_info->port = port; | 242 connect_info->port = port; |
| 243 connect_info->auth = auth_required ? | 243 connect_info->auth = auth_required ? |
| 244 cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED : | 244 cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED : |
| 245 cast_channel::CHANNEL_AUTH_TYPE_SSL; | 245 cast_channel::CHANNEL_AUTH_TYPE_SSL; |
| 246 return true; | 246 return true; |
| 247 }; | 247 } |
| 248 | 248 |
| 249 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( | 249 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( |
| 250 const ConnectInfo& connect_info) { | 250 const ConnectInfo& connect_info) { |
| 251 net::IPAddressNumber ip_address; | 251 net::IPAddressNumber ip_address; |
| 252 if (!net::ParseIPLiteralToNumber(connect_info.ip_address, &ip_address)) { | 252 if (!net::ParseIPLiteralToNumber(connect_info.ip_address, &ip_address)) { |
| 253 return NULL; | 253 return NULL; |
| 254 } | 254 } |
| 255 if (connect_info.port < 0 || connect_info.port > | 255 if (connect_info.port < 0 || connect_info.port > |
| 256 std::numeric_limits<unsigned short>::max()) { | 256 std::numeric_limits<unsigned short>::max()) { |
| 257 return NULL; | 257 return NULL; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 273 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 273 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 274 // The connect_info parameter may be a string URL like cast:// or casts:// or | 274 // The connect_info parameter may be a string URL like cast:// or casts:// or |
| 275 // a ConnectInfo object. | 275 // a ConnectInfo object. |
| 276 std::string cast_url; | 276 std::string cast_url; |
| 277 switch (params_->connect_info->GetType()) { | 277 switch (params_->connect_info->GetType()) { |
| 278 case base::Value::TYPE_STRING: | 278 case base::Value::TYPE_STRING: |
| 279 CHECK(params_->connect_info->GetAsString(&cast_url)); | 279 CHECK(params_->connect_info->GetAsString(&cast_url)); |
| 280 connect_info_.reset(new ConnectInfo); | 280 connect_info_.reset(new ConnectInfo); |
| 281 if (!ParseChannelUrl(GURL(cast_url), connect_info_.get())) { | 281 if (!ParseChannelUrl(GURL(cast_url), connect_info_.get())) { |
| 282 connect_info_.reset(); | 282 connect_info_.reset(); |
| 283 SetError("Invalid Cast URL " + cast_url); | |
| 284 return false; | |
| 283 } | 285 } |
| 284 break; | 286 break; |
| 285 case base::Value::TYPE_DICTIONARY: | 287 case base::Value::TYPE_DICTIONARY: |
| 286 connect_info_ = ConnectInfo::FromValue(*(params_->connect_info)); | 288 connect_info_ = ConnectInfo::FromValue(*(params_->connect_info)); |
| 287 break; | 289 break; |
| 288 default: | 290 default: |
| 289 break; | 291 break; |
| 290 } | 292 } |
| 291 if (connect_info_.get()) { | 293 if (!connect_info_.get()) { |
| 292 channel_auth_ = connect_info_->auth; | 294 SetError("Invalid connect_info"); |
| 293 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); | 295 return false; |
| 294 return ip_endpoint_.get() != NULL; | |
| 295 } | 296 } |
| 296 return false; | 297 channel_auth_ = connect_info_->auth; |
| 298 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); | |
| 299 if (!ip_endpoint_.get()) { | |
| 300 SetError("Invalid connect_info"); | |
|
Wez
2014/07/16 22:42:33
nit: May be helpful to be able to distinguish this
mark a. foltz
2014/07/17 19:26:51
Done.
| |
| 301 return false; | |
| 302 } | |
| 303 return true; | |
| 297 } | 304 } |
| 298 | 305 |
| 299 void CastChannelOpenFunction::AsyncWorkStart() { | 306 void CastChannelOpenFunction::AsyncWorkStart() { |
| 300 DCHECK(api_); | 307 DCHECK(api_); |
| 301 DCHECK(ip_endpoint_.get()); | 308 DCHECK(ip_endpoint_.get()); |
| 302 scoped_ptr<CastSocket> socket = api_->CreateCastSocket( | 309 scoped_ptr<CastSocket> socket = api_->CreateCastSocket( |
| 303 extension_->id(), *ip_endpoint_, channel_auth_); | 310 extension_->id(), *ip_endpoint_, channel_auth_); |
| 304 new_channel_id_ = AddSocket(socket.release()); | 311 new_channel_id_ = AddSocket(socket.release()); |
| 305 GetSocket(new_channel_id_)->Connect( | 312 GetSocket(new_channel_id_)->Connect( |
| 306 base::Bind(&CastChannelOpenFunction::OnOpen, this)); | 313 base::Bind(&CastChannelOpenFunction::OnOpen, this)); |
| 307 } | 314 } |
| 308 | 315 |
| 309 void CastChannelOpenFunction::OnOpen(int result) { | 316 void CastChannelOpenFunction::OnOpen(int result) { |
| 310 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 317 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 311 SetResultFromSocket(new_channel_id_); | 318 SetResultFromSocket(new_channel_id_); |
| 312 AsyncWorkCompleted(); | 319 AsyncWorkCompleted(); |
| 313 } | 320 } |
| 314 | 321 |
| 315 CastChannelSendFunction::CastChannelSendFunction() { } | 322 CastChannelSendFunction::CastChannelSendFunction() { } |
| 316 | 323 |
| 317 CastChannelSendFunction::~CastChannelSendFunction() { } | 324 CastChannelSendFunction::~CastChannelSendFunction() { } |
| 318 | 325 |
| 319 bool CastChannelSendFunction::Prepare() { | 326 bool CastChannelSendFunction::Prepare() { |
| 320 params_ = Send::Params::Create(*args_); | 327 params_ = Send::Params::Create(*args_); |
| 321 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 328 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 329 if (params_->message.namespace_.empty()) { | |
| 330 SetError("message_info.namespace_ is required"); | |
| 331 return false; | |
| 332 } | |
| 333 if (params_->message.source_id.empty()) { | |
| 334 SetError("message_info.source_id is required"); | |
| 335 return false; | |
| 336 } | |
| 337 if (params_->message.destination_id.empty()) { | |
| 338 SetError("message_info.destination_id is required"); | |
| 339 return false; | |
| 340 } | |
| 341 switch (params_->message.data->GetType()) { | |
| 342 case base::Value::TYPE_STRING: | |
| 343 case base::Value::TYPE_BINARY: | |
| 344 break; | |
| 345 default: | |
| 346 SetError("Invalid type of message_info.data"); | |
| 347 return false; | |
| 348 } | |
| 322 return true; | 349 return true; |
| 323 } | 350 } |
| 324 | 351 |
| 325 void CastChannelSendFunction::AsyncWorkStart() { | 352 void CastChannelSendFunction::AsyncWorkStart() { |
| 326 CastSocket* socket = GetSocketOrCompleteWithError( | 353 CastSocket* socket = GetSocketOrCompleteWithError( |
| 327 params_->channel.channel_id); | 354 params_->channel.channel_id); |
| 328 if (socket) | 355 if (socket) |
| 329 socket->SendMessage(params_->message, | 356 socket->SendMessage(params_->message, |
| 330 base::Bind(&CastChannelSendFunction::OnSend, this)); | 357 base::Bind(&CastChannelSendFunction::OnSend, this)); |
| 331 } | 358 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 391 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| 365 } else { | 392 } else { |
| 366 int channel_id = params_->channel.channel_id; | 393 int channel_id = params_->channel.channel_id; |
| 367 SetResultFromSocket(channel_id); | 394 SetResultFromSocket(channel_id); |
| 368 RemoveSocket(channel_id); | 395 RemoveSocket(channel_id); |
| 369 } | 396 } |
| 370 AsyncWorkCompleted(); | 397 AsyncWorkCompleted(); |
| 371 } | 398 } |
| 372 | 399 |
| 373 } // namespace extensions | 400 } // namespace extensions |
| OLD | NEW |