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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 VLOG(1) << "Sending message " << ParamToString(message_info) | 166 VLOG(1) << "Sending message " << ParamToString(message_info) |
167 << " to channel " << ParamToString(channel_info); | 167 << " to channel " << ParamToString(channel_info); |
168 scoped_ptr<Event> event(new Event(OnMessage::kEventName, results.Pass())); | 168 scoped_ptr<Event> event(new Event(OnMessage::kEventName, results.Pass())); |
169 extensions::EventRouter::Get(browser_context_) | 169 extensions::EventRouter::Get(browser_context_) |
170 ->DispatchEventToExtension(socket->owner_extension_id(), event.Pass()); | 170 ->DispatchEventToExtension(socket->owner_extension_id(), event.Pass()); |
171 } | 171 } |
172 | 172 |
173 CastChannelAPI::~CastChannelAPI() {} | 173 CastChannelAPI::~CastChannelAPI() {} |
174 | 174 |
175 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() | 175 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() |
176 : manager_(NULL), error_(cast_channel::CHANNEL_ERROR_NONE) { } | 176 : manager_(NULL), channel_error_(cast_channel::CHANNEL_ERROR_NONE) { |
177 } | |
177 | 178 |
178 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } | 179 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } |
179 | 180 |
180 bool CastChannelAsyncApiFunction::PrePrepare() { | 181 bool CastChannelAsyncApiFunction::PrePrepare() { |
181 manager_ = ApiResourceManager<CastSocket>::Get(browser_context()); | 182 manager_ = ApiResourceManager<CastSocket>::Get(browser_context()); |
182 return true; | 183 return true; |
183 } | 184 } |
184 | 185 |
185 bool CastChannelAsyncApiFunction::Respond() { | 186 bool CastChannelAsyncApiFunction::Respond() { |
186 return error_ == cast_channel::CHANNEL_ERROR_NONE; | 187 return channel_error_ == cast_channel::CHANNEL_ERROR_NONE; |
mark a. foltz
2014/10/31 22:52:35
It looks like we only use channel_error_ to indica
vadimgo
2014/10/31 23:32:39
Yes, it is cleaner with a bool. The logic became
| |
187 } | 188 } |
188 | 189 |
189 CastSocket* CastChannelAsyncApiFunction::GetSocketOrCompleteWithError( | 190 CastSocket* CastChannelAsyncApiFunction::GetSocketOrCompleteWithError( |
190 int channel_id) { | 191 int channel_id) { |
191 CastSocket* socket = GetSocket(channel_id); | 192 CastSocket* socket = GetSocket(channel_id); |
192 if (!socket) { | 193 if (!socket) { |
193 SetResultFromError(channel_id, | 194 SetResultFromError(channel_id, |
194 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); | 195 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); |
195 AsyncWorkCompleted(); | 196 AsyncWorkCompleted(); |
196 } | 197 } |
(...skipping 12 matching lines...) Expand all Loading... | |
209 void CastChannelAsyncApiFunction::RemoveSocket(int channel_id) { | 210 void CastChannelAsyncApiFunction::RemoveSocket(int channel_id) { |
210 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 211 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
211 DCHECK(manager_); | 212 DCHECK(manager_); |
212 manager_->Remove(extension_->id(), channel_id); | 213 manager_->Remove(extension_->id(), channel_id); |
213 } | 214 } |
214 | 215 |
215 void CastChannelAsyncApiFunction::SetResultFromSocket( | 216 void CastChannelAsyncApiFunction::SetResultFromSocket( |
216 const CastSocket& socket) { | 217 const CastSocket& socket) { |
217 ChannelInfo channel_info; | 218 ChannelInfo channel_info; |
218 FillChannelInfo(socket, &channel_info); | 219 FillChannelInfo(socket, &channel_info); |
219 error_ = socket.error_state(); | 220 channel_error_ = socket.error_state(); |
220 SetResultFromChannelInfo(channel_info); | 221 SetResultFromChannelInfo(channel_info); |
221 } | 222 } |
222 | 223 |
223 void CastChannelAsyncApiFunction::SetResultFromError(int channel_id, | 224 void CastChannelAsyncApiFunction::SetResultFromError( |
224 ChannelError error) { | 225 int channel_id, |
226 ChannelError channel_error) { | |
225 ChannelInfo channel_info; | 227 ChannelInfo channel_info; |
226 channel_info.channel_id = channel_id; | 228 channel_info.channel_id = channel_id; |
227 channel_info.url = ""; | 229 channel_info.url = ""; |
228 channel_info.ready_state = cast_channel::READY_STATE_CLOSED; | 230 channel_info.ready_state = cast_channel::READY_STATE_CLOSED; |
229 channel_info.error_state = error; | 231 channel_info.error_state = channel_error; |
230 channel_info.connect_info.ip_address = ""; | 232 channel_info.connect_info.ip_address = ""; |
231 channel_info.connect_info.port = 0; | 233 channel_info.connect_info.port = 0; |
232 channel_info.connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL; | 234 channel_info.connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL; |
233 SetResultFromChannelInfo(channel_info); | 235 SetResultFromChannelInfo(channel_info); |
234 error_ = error; | 236 channel_error_ = channel_error; |
237 } | |
238 | |
239 void CastChannelAsyncApiFunction::SetChannelError(const std::string& error, | |
mark a. foltz
2014/10/31 22:52:35
With the change I mentioned this may no longer be
vadimgo
2014/10/31 23:32:39
Done.
| |
240 ChannelError channel_error) { | |
241 SetError(error); | |
242 channel_error_ = channel_error; | |
235 } | 243 } |
236 | 244 |
237 CastSocket* CastChannelAsyncApiFunction::GetSocket(int channel_id) { | 245 CastSocket* CastChannelAsyncApiFunction::GetSocket(int channel_id) { |
238 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 246 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
239 DCHECK(manager_); | 247 DCHECK(manager_); |
240 return manager_->Get(extension_->id(), channel_id); | 248 return manager_->Get(extension_->id(), channel_id); |
241 } | 249 } |
242 | 250 |
243 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( | 251 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( |
244 const ChannelInfo& channel_info) { | 252 const ChannelInfo& channel_info) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 322 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
315 // The connect_info parameter may be a string URL like cast:// or casts:// or | 323 // The connect_info parameter may be a string URL like cast:// or casts:// or |
316 // a ConnectInfo object. | 324 // a ConnectInfo object. |
317 std::string cast_url; | 325 std::string cast_url; |
318 switch (params_->connect_info->GetType()) { | 326 switch (params_->connect_info->GetType()) { |
319 case base::Value::TYPE_STRING: | 327 case base::Value::TYPE_STRING: |
320 CHECK(params_->connect_info->GetAsString(&cast_url)); | 328 CHECK(params_->connect_info->GetAsString(&cast_url)); |
321 connect_info_.reset(new ConnectInfo); | 329 connect_info_.reset(new ConnectInfo); |
322 if (!ParseChannelUrl(GURL(cast_url), connect_info_.get())) { | 330 if (!ParseChannelUrl(GURL(cast_url), connect_info_.get())) { |
323 connect_info_.reset(); | 331 connect_info_.reset(); |
324 SetError("Invalid connect_info (invalid Cast URL " + cast_url + ")"); | 332 SetChannelError("Invalid connect_info (invalid Cast URL " + cast_url + |
333 ")"); | |
325 } | 334 } |
326 break; | 335 break; |
327 case base::Value::TYPE_DICTIONARY: | 336 case base::Value::TYPE_DICTIONARY: |
328 connect_info_ = ConnectInfo::FromValue(*(params_->connect_info)); | 337 connect_info_ = ConnectInfo::FromValue(*(params_->connect_info)); |
329 if (!connect_info_.get()) { | 338 if (!connect_info_.get()) { |
330 SetError("connect_info.auth is required"); | 339 SetChannelError("connect_info.auth is required"); |
331 } | 340 } |
332 break; | 341 break; |
333 default: | 342 default: |
334 SetError("Invalid connect_info (unknown type)"); | 343 SetChannelError("Invalid connect_info (unknown type)"); |
335 break; | 344 break; |
336 } | 345 } |
337 if (!connect_info_.get()) { | 346 if (!connect_info_.get()) { |
338 return false; | 347 return false; |
339 } | 348 } |
340 if (!IsValidConnectInfoPort(*connect_info_)) { | 349 if (!IsValidConnectInfoPort(*connect_info_)) { |
341 SetError("Invalid connect_info (invalid port)"); | 350 SetChannelError("Invalid connect_info (invalid port)"); |
342 } else if (!IsValidConnectInfoAuth(*connect_info_)) { | 351 } else if (!IsValidConnectInfoAuth(*connect_info_)) { |
343 SetError("Invalid connect_info (invalid auth)"); | 352 SetChannelError("Invalid connect_info (invalid auth)"); |
344 } else if (!IsValidConnectInfoIpAddress(*connect_info_)) { | 353 } else if (!IsValidConnectInfoIpAddress(*connect_info_)) { |
345 SetError("Invalid connect_info (invalid IP address)"); | 354 SetChannelError("Invalid connect_info (invalid IP address)"); |
346 } | 355 } |
347 if (!GetError().empty()) { | 356 if (!GetError().empty()) { |
348 return false; | 357 return false; |
349 } | 358 } |
350 channel_auth_ = connect_info_->auth; | 359 channel_auth_ = connect_info_->auth; |
351 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); | 360 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); |
352 return true; | 361 return true; |
353 } | 362 } |
354 | 363 |
355 void CastChannelOpenFunction::AsyncWorkStart() { | 364 void CastChannelOpenFunction::AsyncWorkStart() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
388 } | 397 } |
389 | 398 |
390 CastChannelSendFunction::CastChannelSendFunction() { } | 399 CastChannelSendFunction::CastChannelSendFunction() { } |
391 | 400 |
392 CastChannelSendFunction::~CastChannelSendFunction() { } | 401 CastChannelSendFunction::~CastChannelSendFunction() { } |
393 | 402 |
394 bool CastChannelSendFunction::Prepare() { | 403 bool CastChannelSendFunction::Prepare() { |
395 params_ = Send::Params::Create(*args_); | 404 params_ = Send::Params::Create(*args_); |
396 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 405 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
397 if (params_->message.namespace_.empty()) { | 406 if (params_->message.namespace_.empty()) { |
398 SetError("message_info.namespace_ is required"); | 407 SetChannelError("message_info.namespace_ is required"); |
399 return false; | 408 return false; |
400 } | 409 } |
401 if (params_->message.source_id.empty()) { | 410 if (params_->message.source_id.empty()) { |
402 SetError("message_info.source_id is required"); | 411 SetChannelError("message_info.source_id is required"); |
403 return false; | 412 return false; |
404 } | 413 } |
405 if (params_->message.destination_id.empty()) { | 414 if (params_->message.destination_id.empty()) { |
406 SetError("message_info.destination_id is required"); | 415 SetChannelError("message_info.destination_id is required"); |
407 return false; | 416 return false; |
408 } | 417 } |
409 switch (params_->message.data->GetType()) { | 418 switch (params_->message.data->GetType()) { |
410 case base::Value::TYPE_STRING: | 419 case base::Value::TYPE_STRING: |
411 case base::Value::TYPE_BINARY: | 420 case base::Value::TYPE_BINARY: |
412 break; | 421 break; |
413 default: | 422 default: |
414 SetError("Invalid type of message_info.data"); | 423 SetChannelError("Invalid type of message_info.data"); |
415 return false; | 424 return false; |
416 } | 425 } |
417 return true; | 426 return true; |
418 } | 427 } |
419 | 428 |
420 void CastChannelSendFunction::AsyncWorkStart() { | 429 void CastChannelSendFunction::AsyncWorkStart() { |
421 CastSocket* socket = GetSocket(params_->channel.channel_id); | 430 CastSocket* socket = GetSocket(params_->channel.channel_id); |
422 if (!socket) { | 431 if (!socket) { |
423 SetResultFromError(params_->channel.channel_id, | 432 SetResultFromError(params_->channel.channel_id, |
424 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); | 433 cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 } | 505 } |
497 | 506 |
498 void CastChannelGetLogsFunction::AsyncWorkStart() { | 507 void CastChannelGetLogsFunction::AsyncWorkStart() { |
499 DCHECK(api_); | 508 DCHECK(api_); |
500 | 509 |
501 size_t length = 0; | 510 size_t length = 0; |
502 scoped_ptr<char[]> out = api_->GetLogger()->GetLogs(&length); | 511 scoped_ptr<char[]> out = api_->GetLogger()->GetLogs(&length); |
503 if (out.get()) { | 512 if (out.get()) { |
504 SetResult(new base::BinaryValue(out.Pass(), length)); | 513 SetResult(new base::BinaryValue(out.Pass(), length)); |
505 } else { | 514 } else { |
506 SetError("Unable to get logs."); | 515 SetChannelError("Unable to get logs."); |
507 } | 516 } |
508 | 517 |
509 api_->GetLogger()->Reset(); | 518 api_->GetLogger()->Reset(); |
510 | 519 |
511 AsyncWorkCompleted(); | 520 AsyncWorkCompleted(); |
512 } | 521 } |
513 | 522 |
514 CastChannelSetAuthorityKeysFunction::CastChannelSetAuthorityKeysFunction() { | 523 CastChannelSetAuthorityKeysFunction::CastChannelSetAuthorityKeysFunction() { |
515 } | 524 } |
516 | 525 |
517 CastChannelSetAuthorityKeysFunction::~CastChannelSetAuthorityKeysFunction() { | 526 CastChannelSetAuthorityKeysFunction::~CastChannelSetAuthorityKeysFunction() { |
518 } | 527 } |
519 | 528 |
520 bool CastChannelSetAuthorityKeysFunction::Prepare() { | 529 bool CastChannelSetAuthorityKeysFunction::Prepare() { |
521 params_ = cast_channel::SetAuthorityKeys::Params::Create(*args_); | 530 params_ = cast_channel::SetAuthorityKeys::Params::Create(*args_); |
522 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 531 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
523 return true; | 532 return true; |
524 } | 533 } |
525 | 534 |
526 void CastChannelSetAuthorityKeysFunction::AsyncWorkStart() { | 535 void CastChannelSetAuthorityKeysFunction::AsyncWorkStart() { |
527 std::string& keys = params_->keys; | 536 std::string& keys = params_->keys; |
528 std::string& signature = params_->signature; | 537 std::string& signature = params_->signature; |
529 if (signature.empty() || keys.empty() || | 538 if (signature.empty() || keys.empty() || |
530 !cast_channel::SetTrustedCertificateAuthorities(keys, signature)) { | 539 !cast_channel::SetTrustedCertificateAuthorities(keys, signature)) { |
531 SetError("Unable to set authority keys."); | 540 SetChannelError("Unable to set authority keys."); |
532 } | 541 } |
533 | 542 |
534 AsyncWorkCompleted(); | 543 AsyncWorkCompleted(); |
535 } | 544 } |
536 | 545 |
537 } // namespace extensions | 546 } // namespace extensions |
OLD | NEW |