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/bluetooth_low_energy/bluetooth_low_energy_api.h
" | 5 #include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.h
" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <iterator> | 9 #include <iterator> |
10 #include <utility> | 10 #include <utility> |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 } | 347 } |
348 | 348 |
349 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() {} | 349 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() {} |
350 | 350 |
351 void BluetoothLowEnergyAPI::Shutdown() { | 351 void BluetoothLowEnergyAPI::Shutdown() { |
352 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 352 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
353 } | 353 } |
354 | 354 |
355 namespace api { | 355 namespace api { |
356 | 356 |
357 BluetoothLowEnergyExtensionFunctionDeprecated:: | |
358 BluetoothLowEnergyExtensionFunctionDeprecated() {} | |
359 | |
360 BluetoothLowEnergyExtensionFunctionDeprecated:: | |
361 ~BluetoothLowEnergyExtensionFunctionDeprecated() {} | |
362 | |
363 bool BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync() { | |
364 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
365 | |
366 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { | |
367 error_ = kErrorPermissionDenied; | |
368 return false; | |
369 } | |
370 | |
371 BluetoothLowEnergyEventRouter* event_router = | |
372 GetEventRouter(browser_context()); | |
373 if (!event_router->IsBluetoothSupported()) { | |
374 SetError(kErrorPlatformNotSupported); | |
375 return false; | |
376 } | |
377 | |
378 // It is safe to pass |this| here as ExtensionFunction is refcounted. | |
379 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( | |
380 &DoWorkCallback<bool>, | |
381 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork, | |
382 this)))) { | |
383 SetError(kErrorAdapterNotInitialized); | |
384 return false; | |
385 } | |
386 | |
387 return true; | |
388 } | |
389 | |
390 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() | 357 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() |
391 : event_router_(nullptr) {} | 358 : event_router_(nullptr) {} |
392 | 359 |
393 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} | 360 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} |
394 | 361 |
395 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { | 362 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { |
396 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 363 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
397 | 364 |
398 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) | 365 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) |
399 return RespondNow(Error(kErrorPermissionDenied)); | 366 return RespondNow(Error(kErrorPermissionDenied)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 | 412 |
446 // Causes link error on Windows. API will never be on Windows, so #ifdefing. | 413 // Causes link error on Windows. API will never be on Windows, so #ifdefing. |
447 #if !defined(OS_WIN) | 414 #if !defined(OS_WIN) |
448 params_ = Params::Create(*args_); | 415 params_ = Params::Create(*args_); |
449 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); | 416 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); |
450 #endif | 417 #endif |
451 | 418 |
452 return BluetoothLowEnergyExtensionFunction::Run(); | 419 return BluetoothLowEnergyExtensionFunction::Run(); |
453 } | 420 } |
454 | 421 |
455 bool BluetoothLowEnergyConnectFunction::DoWork() { | 422 void BluetoothLowEnergyConnectFunction::DoWork() { |
456 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 423 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
457 | 424 |
458 BluetoothLowEnergyEventRouter* event_router = | 425 BluetoothLowEnergyEventRouter* event_router = |
459 GetEventRouter(browser_context()); | 426 GetEventRouter(browser_context()); |
460 | 427 |
461 // The adapter must be initialized at this point, but return an error instead | 428 // The adapter must be initialized at this point, but return an error instead |
462 // of asserting. | 429 // of asserting. |
463 if (!event_router->HasAdapter()) { | 430 if (!event_router->HasAdapter()) { |
464 SetError(kErrorAdapterNotInitialized); | 431 Respond(Error(kErrorAdapterNotInitialized)); |
465 SendResponse(false); | 432 return; |
466 return false; | |
467 } | 433 } |
468 | 434 |
469 std::unique_ptr<apibtle::Connect::Params> params( | 435 std::unique_ptr<apibtle::Connect::Params> params( |
470 apibtle::Connect::Params::Create(*args_)); | 436 apibtle::Connect::Params::Create(*args_)); |
471 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 437 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
472 | 438 |
473 bool persistent = false; // Not persistent by default. | 439 bool persistent = false; // Not persistent by default. |
474 apibtle::ConnectProperties* properties = params->properties.get(); | 440 apibtle::ConnectProperties* properties = params->properties.get(); |
475 if (properties) | 441 if (properties) |
476 persistent = properties->persistent; | 442 persistent = properties->persistent; |
477 | 443 |
478 event_router->Connect( | 444 event_router->Connect( |
479 persistent, extension(), params->device_address, | 445 persistent, extension(), params->device_address, |
480 base::Bind(&BluetoothLowEnergyConnectFunction::SuccessCallback, this), | 446 base::Bind(&BluetoothLowEnergyConnectFunction::SuccessCallback, this), |
481 base::Bind(&BluetoothLowEnergyConnectFunction::ErrorCallback, this)); | 447 base::Bind(&BluetoothLowEnergyConnectFunction::ErrorCallback, this)); |
482 | |
483 return true; | |
484 } | 448 } |
485 | 449 |
486 void BluetoothLowEnergyConnectFunction::SuccessCallback() { | 450 void BluetoothLowEnergyConnectFunction::SuccessCallback() { |
487 SendResponse(true); | 451 Respond(NoArguments()); |
488 } | 452 } |
489 | 453 |
490 void BluetoothLowEnergyConnectFunction::ErrorCallback( | 454 void BluetoothLowEnergyConnectFunction::ErrorCallback( |
491 BluetoothLowEnergyEventRouter::Status status) { | 455 BluetoothLowEnergyEventRouter::Status status) { |
492 SetError(StatusToString(status)); | 456 Respond(Error(StatusToString(status))); |
493 SendResponse(false); | |
494 } | 457 } |
495 | 458 |
496 bool BluetoothLowEnergyDisconnectFunction::DoWork() { | 459 void BluetoothLowEnergyDisconnectFunction::DoWork() { |
497 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 460 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
498 | 461 |
499 BluetoothLowEnergyEventRouter* event_router = | 462 BluetoothLowEnergyEventRouter* event_router = |
500 GetEventRouter(browser_context()); | 463 GetEventRouter(browser_context()); |
501 | 464 |
502 // The adapter must be initialized at this point, but return an error instead | 465 // The adapter must be initialized at this point, but return an error instead |
503 // of asserting. | 466 // of asserting. |
504 if (!event_router->HasAdapter()) { | 467 if (!event_router->HasAdapter()) { |
505 SetError(kErrorAdapterNotInitialized); | 468 Respond(Error(kErrorAdapterNotInitialized)); |
506 SendResponse(false); | 469 return; |
507 return false; | |
508 } | 470 } |
509 | 471 |
510 std::unique_ptr<apibtle::Disconnect::Params> params( | 472 std::unique_ptr<apibtle::Disconnect::Params> params( |
511 apibtle::Disconnect::Params::Create(*args_)); | 473 apibtle::Disconnect::Params::Create(*args_)); |
512 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 474 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
513 | 475 |
514 event_router->Disconnect( | 476 event_router->Disconnect( |
515 extension(), params->device_address, | 477 extension(), params->device_address, |
516 base::Bind(&BluetoothLowEnergyDisconnectFunction::SuccessCallback, this), | 478 base::Bind(&BluetoothLowEnergyDisconnectFunction::SuccessCallback, this), |
517 base::Bind(&BluetoothLowEnergyDisconnectFunction::ErrorCallback, this)); | 479 base::Bind(&BluetoothLowEnergyDisconnectFunction::ErrorCallback, this)); |
518 | |
519 return true; | |
520 } | 480 } |
521 | 481 |
522 void BluetoothLowEnergyDisconnectFunction::SuccessCallback() { | 482 void BluetoothLowEnergyDisconnectFunction::SuccessCallback() { |
523 SendResponse(true); | 483 Respond(NoArguments()); |
524 } | 484 } |
525 | 485 |
526 void BluetoothLowEnergyDisconnectFunction::ErrorCallback( | 486 void BluetoothLowEnergyDisconnectFunction::ErrorCallback( |
527 BluetoothLowEnergyEventRouter::Status status) { | 487 BluetoothLowEnergyEventRouter::Status status) { |
528 SetError(StatusToString(status)); | 488 Respond(Error(StatusToString(status))); |
529 SendResponse(false); | |
530 } | 489 } |
531 | 490 |
532 bool BluetoothLowEnergyGetServiceFunction::DoWork() { | 491 void BluetoothLowEnergyGetServiceFunction::DoWork() { |
533 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 492 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
534 | 493 |
535 BluetoothLowEnergyEventRouter* event_router = | 494 BluetoothLowEnergyEventRouter* event_router = |
536 GetEventRouter(browser_context()); | 495 GetEventRouter(browser_context()); |
537 | 496 |
538 // The adapter must be initialized at this point, but return an error instead | 497 // The adapter must be initialized at this point, but return an error instead |
539 // of asserting. | 498 // of asserting. |
540 if (!event_router->HasAdapter()) { | 499 if (!event_router->HasAdapter()) { |
541 SetError(kErrorAdapterNotInitialized); | 500 Respond(Error(kErrorAdapterNotInitialized)); |
542 SendResponse(false); | 501 return; |
543 return false; | |
544 } | 502 } |
545 | 503 |
546 std::unique_ptr<apibtle::GetService::Params> params( | 504 std::unique_ptr<apibtle::GetService::Params> params( |
547 apibtle::GetService::Params::Create(*args_)); | 505 apibtle::GetService::Params::Create(*args_)); |
548 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 506 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
549 | 507 |
550 apibtle::Service service; | 508 apibtle::Service service; |
551 BluetoothLowEnergyEventRouter::Status status = | 509 BluetoothLowEnergyEventRouter::Status status = |
552 event_router->GetService(params->service_id, &service); | 510 event_router->GetService(params->service_id, &service); |
553 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 511 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
554 SetError(StatusToString(status)); | 512 Respond(Error(StatusToString(status))); |
555 SendResponse(false); | 513 return; |
556 return false; | |
557 } | 514 } |
558 | 515 |
559 results_ = apibtle::GetService::Results::Create(service); | 516 Respond(ArgumentList(apibtle::GetService::Results::Create(service))); |
560 SendResponse(true); | |
561 | |
562 return true; | |
563 } | 517 } |
564 | 518 |
565 bool BluetoothLowEnergyGetServicesFunction::DoWork() { | 519 void BluetoothLowEnergyGetServicesFunction::DoWork() { |
566 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 520 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
567 | 521 |
568 BluetoothLowEnergyEventRouter* event_router = | 522 BluetoothLowEnergyEventRouter* event_router = |
569 GetEventRouter(browser_context()); | 523 GetEventRouter(browser_context()); |
570 | 524 |
571 // The adapter must be initialized at this point, but return an error instead | 525 // The adapter must be initialized at this point, but return an error instead |
572 // of asserting. | 526 // of asserting. |
573 if (!event_router->HasAdapter()) { | 527 if (!event_router->HasAdapter()) { |
574 SetError(kErrorAdapterNotInitialized); | 528 Respond(Error(kErrorAdapterNotInitialized)); |
575 SendResponse(false); | 529 return; |
576 return false; | |
577 } | 530 } |
578 | 531 |
579 std::unique_ptr<apibtle::GetServices::Params> params( | 532 std::unique_ptr<apibtle::GetServices::Params> params( |
580 apibtle::GetServices::Params::Create(*args_)); | 533 apibtle::GetServices::Params::Create(*args_)); |
581 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 534 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
582 | 535 |
583 BluetoothLowEnergyEventRouter::ServiceList service_list; | 536 BluetoothLowEnergyEventRouter::ServiceList service_list; |
584 if (!event_router->GetServices(params->device_address, &service_list)) { | 537 if (!event_router->GetServices(params->device_address, &service_list)) { |
585 SetError(kErrorNotFound); | 538 Respond(Error(kErrorNotFound)); |
586 SendResponse(false); | 539 return; |
587 return false; | |
588 } | 540 } |
589 | 541 |
590 results_ = apibtle::GetServices::Results::Create(service_list); | 542 Respond(ArgumentList(apibtle::GetServices::Results::Create(service_list))); |
591 SendResponse(true); | |
592 | |
593 return true; | |
594 } | 543 } |
595 | 544 |
596 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { | 545 void BluetoothLowEnergyGetCharacteristicFunction::DoWork() { |
597 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 546 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
598 | 547 |
599 BluetoothLowEnergyEventRouter* event_router = | 548 BluetoothLowEnergyEventRouter* event_router = |
600 GetEventRouter(browser_context()); | 549 GetEventRouter(browser_context()); |
601 | 550 |
602 // The adapter must be initialized at this point, but return an error instead | 551 // The adapter must be initialized at this point, but return an error instead |
603 // of asserting. | 552 // of asserting. |
604 if (!event_router->HasAdapter()) { | 553 if (!event_router->HasAdapter()) { |
605 SetError(kErrorAdapterNotInitialized); | 554 Respond(Error(kErrorAdapterNotInitialized)); |
606 SendResponse(false); | 555 return; |
607 return false; | |
608 } | 556 } |
609 | 557 |
610 std::unique_ptr<apibtle::GetCharacteristic::Params> params( | 558 std::unique_ptr<apibtle::GetCharacteristic::Params> params( |
611 apibtle::GetCharacteristic::Params::Create(*args_)); | 559 apibtle::GetCharacteristic::Params::Create(*args_)); |
612 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 560 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
613 | 561 |
614 apibtle::Characteristic characteristic; | 562 apibtle::Characteristic characteristic; |
615 BluetoothLowEnergyEventRouter::Status status = | 563 BluetoothLowEnergyEventRouter::Status status = |
616 event_router->GetCharacteristic(extension(), params->characteristic_id, | 564 event_router->GetCharacteristic(extension(), params->characteristic_id, |
617 &characteristic); | 565 &characteristic); |
618 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 566 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
619 SetError(StatusToString(status)); | 567 Respond(Error(StatusToString(status))); |
620 SendResponse(false); | 568 return; |
621 return false; | |
622 } | 569 } |
623 | 570 |
624 // Manually construct the result instead of using | 571 // Manually construct the result instead of using |
625 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of | 572 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of |
626 // enums correctly. | 573 // enums correctly. |
627 SetResult(apibtle::CharacteristicToValue(&characteristic)); | 574 Respond(OneArgument(apibtle::CharacteristicToValue(&characteristic))); |
628 SendResponse(true); | |
629 | |
630 return true; | |
631 } | 575 } |
632 | 576 |
633 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { | 577 void BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { |
634 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 578 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
635 | 579 |
636 BluetoothLowEnergyEventRouter* event_router = | 580 BluetoothLowEnergyEventRouter* event_router = |
637 GetEventRouter(browser_context()); | 581 GetEventRouter(browser_context()); |
638 | 582 |
639 // The adapter must be initialized at this point, but return an error instead | 583 // The adapter must be initialized at this point, but return an error instead |
640 // of asserting. | 584 // of asserting. |
641 if (!event_router->HasAdapter()) { | 585 if (!event_router->HasAdapter()) { |
642 SetError(kErrorAdapterNotInitialized); | 586 Respond(Error(kErrorAdapterNotInitialized)); |
643 SendResponse(false); | 587 return; |
644 return false; | |
645 } | 588 } |
646 | 589 |
647 std::unique_ptr<apibtle::GetCharacteristics::Params> params( | 590 std::unique_ptr<apibtle::GetCharacteristics::Params> params( |
648 apibtle::GetCharacteristics::Params::Create(*args_)); | 591 apibtle::GetCharacteristics::Params::Create(*args_)); |
649 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 592 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
650 | 593 |
651 BluetoothLowEnergyEventRouter::CharacteristicList characteristic_list; | 594 BluetoothLowEnergyEventRouter::CharacteristicList characteristic_list; |
652 BluetoothLowEnergyEventRouter::Status status = | 595 BluetoothLowEnergyEventRouter::Status status = |
653 event_router->GetCharacteristics(extension(), params->service_id, | 596 event_router->GetCharacteristics(extension(), params->service_id, |
654 &characteristic_list); | 597 &characteristic_list); |
655 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 598 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
656 SetError(StatusToString(status)); | 599 Respond(Error(StatusToString(status))); |
657 SendResponse(false); | 600 return; |
658 return false; | |
659 } | 601 } |
660 | 602 |
661 // Manually construct the result instead of using | 603 // Manually construct the result instead of using |
662 // apibtle::GetCharacteristics::Result::Create as it doesn't convert lists of | 604 // apibtle::GetCharacteristics::Result::Create as it doesn't convert lists of |
663 // enums correctly. | 605 // enums correctly. |
664 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 606 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
665 for (apibtle::Characteristic& characteristic : characteristic_list) | 607 for (apibtle::Characteristic& characteristic : characteristic_list) |
666 result->Append(apibtle::CharacteristicToValue(&characteristic)); | 608 result->Append(apibtle::CharacteristicToValue(&characteristic)); |
667 | 609 |
668 SetResult(std::move(result)); | 610 Respond(OneArgument(std::move(result))); |
669 SendResponse(true); | |
670 | |
671 return true; | |
672 } | 611 } |
673 | 612 |
674 bool BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { | 613 void BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { |
675 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 614 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
676 | 615 |
677 BluetoothLowEnergyEventRouter* event_router = | 616 BluetoothLowEnergyEventRouter* event_router = |
678 GetEventRouter(browser_context()); | 617 GetEventRouter(browser_context()); |
679 | 618 |
680 // The adapter must be initialized at this point, but return an error instead | 619 // The adapter must be initialized at this point, but return an error instead |
681 // of asserting. | 620 // of asserting. |
682 if (!event_router->HasAdapter()) { | 621 if (!event_router->HasAdapter()) { |
683 SetError(kErrorAdapterNotInitialized); | 622 Respond(Error(kErrorAdapterNotInitialized)); |
684 SendResponse(false); | 623 return; |
685 return false; | |
686 } | 624 } |
687 | 625 |
688 std::unique_ptr<apibtle::GetIncludedServices::Params> params( | 626 std::unique_ptr<apibtle::GetIncludedServices::Params> params( |
689 apibtle::GetIncludedServices::Params::Create(*args_)); | 627 apibtle::GetIncludedServices::Params::Create(*args_)); |
690 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 628 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
691 | 629 |
692 BluetoothLowEnergyEventRouter::ServiceList service_list; | 630 BluetoothLowEnergyEventRouter::ServiceList service_list; |
693 BluetoothLowEnergyEventRouter::Status status = | 631 BluetoothLowEnergyEventRouter::Status status = |
694 event_router->GetIncludedServices(params->service_id, &service_list); | 632 event_router->GetIncludedServices(params->service_id, &service_list); |
695 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 633 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
696 SetError(StatusToString(status)); | 634 Respond(Error(StatusToString(status))); |
697 SendResponse(false); | 635 return; |
698 return false; | |
699 } | 636 } |
700 | 637 |
701 results_ = apibtle::GetIncludedServices::Results::Create(service_list); | 638 Respond(ArgumentList( |
702 SendResponse(true); | 639 apibtle::GetIncludedServices::Results::Create(service_list))); |
703 | |
704 return true; | |
705 } | 640 } |
706 | 641 |
707 bool BluetoothLowEnergyGetDescriptorFunction::DoWork() { | 642 void BluetoothLowEnergyGetDescriptorFunction::DoWork() { |
708 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 643 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
709 | 644 |
710 BluetoothLowEnergyEventRouter* event_router = | 645 BluetoothLowEnergyEventRouter* event_router = |
711 GetEventRouter(browser_context()); | 646 GetEventRouter(browser_context()); |
712 | 647 |
713 // The adapter must be initialized at this point, but return an error instead | 648 // The adapter must be initialized at this point, but return an error instead |
714 // of asserting. | 649 // of asserting. |
715 if (!event_router->HasAdapter()) { | 650 if (!event_router->HasAdapter()) { |
716 SetError(kErrorAdapterNotInitialized); | 651 Respond(Error(kErrorAdapterNotInitialized)); |
717 SendResponse(false); | 652 return; |
718 return false; | |
719 } | 653 } |
720 | 654 |
721 std::unique_ptr<apibtle::GetDescriptor::Params> params( | 655 std::unique_ptr<apibtle::GetDescriptor::Params> params( |
722 apibtle::GetDescriptor::Params::Create(*args_)); | 656 apibtle::GetDescriptor::Params::Create(*args_)); |
723 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 657 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
724 | 658 |
725 apibtle::Descriptor descriptor; | 659 apibtle::Descriptor descriptor; |
726 BluetoothLowEnergyEventRouter::Status status = event_router->GetDescriptor( | 660 BluetoothLowEnergyEventRouter::Status status = event_router->GetDescriptor( |
727 extension(), params->descriptor_id, &descriptor); | 661 extension(), params->descriptor_id, &descriptor); |
728 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 662 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
729 SetError(StatusToString(status)); | 663 Respond(Error(StatusToString(status))); |
730 SendResponse(false); | 664 return; |
731 return false; | |
732 } | 665 } |
733 | 666 |
734 // Manually construct the result instead of using | 667 // Manually construct the result instead of using |
735 // apibtle::GetDescriptor::Result::Create as it doesn't convert lists of enums | 668 // apibtle::GetDescriptor::Result::Create as it doesn't convert lists of enums |
736 // correctly. | 669 // correctly. |
737 SetResult(apibtle::DescriptorToValue(&descriptor)); | 670 Respond(OneArgument(apibtle::DescriptorToValue(&descriptor))); |
738 SendResponse(true); | |
739 | |
740 return true; | |
741 } | 671 } |
742 | 672 |
743 bool BluetoothLowEnergyGetDescriptorsFunction::DoWork() { | 673 void BluetoothLowEnergyGetDescriptorsFunction::DoWork() { |
744 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 674 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
745 | 675 |
746 BluetoothLowEnergyEventRouter* event_router = | 676 BluetoothLowEnergyEventRouter* event_router = |
747 GetEventRouter(browser_context()); | 677 GetEventRouter(browser_context()); |
748 | 678 |
749 // The adapter must be initialized at this point, but return an error instead | 679 // The adapter must be initialized at this point, but return an error instead |
750 // of asserting. | 680 // of asserting. |
751 if (!event_router->HasAdapter()) { | 681 if (!event_router->HasAdapter()) { |
752 SetError(kErrorAdapterNotInitialized); | 682 Respond(Error(kErrorAdapterNotInitialized)); |
753 SendResponse(false); | 683 return; |
754 return false; | |
755 } | 684 } |
756 | 685 |
757 std::unique_ptr<apibtle::GetDescriptors::Params> params( | 686 std::unique_ptr<apibtle::GetDescriptors::Params> params( |
758 apibtle::GetDescriptors::Params::Create(*args_)); | 687 apibtle::GetDescriptors::Params::Create(*args_)); |
759 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 688 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
760 | 689 |
761 BluetoothLowEnergyEventRouter::DescriptorList descriptor_list; | 690 BluetoothLowEnergyEventRouter::DescriptorList descriptor_list; |
762 BluetoothLowEnergyEventRouter::Status status = event_router->GetDescriptors( | 691 BluetoothLowEnergyEventRouter::Status status = event_router->GetDescriptors( |
763 extension(), params->characteristic_id, &descriptor_list); | 692 extension(), params->characteristic_id, &descriptor_list); |
764 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 693 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
765 SetError(StatusToString(status)); | 694 Respond(Error(StatusToString(status))); |
766 SendResponse(false); | 695 return; |
767 return false; | |
768 } | 696 } |
769 | 697 |
770 // Manually construct the result instead of using | 698 // Manually construct the result instead of using |
771 // apibtle::GetDescriptors::Result::Create as it doesn't convert lists of | 699 // apibtle::GetDescriptors::Result::Create as it doesn't convert lists of |
772 // enums correctly. | 700 // enums correctly. |
773 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 701 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
774 for (apibtle::Descriptor& descriptor : descriptor_list) | 702 for (apibtle::Descriptor& descriptor : descriptor_list) |
775 result->Append(apibtle::DescriptorToValue(&descriptor)); | 703 result->Append(apibtle::DescriptorToValue(&descriptor)); |
776 | 704 |
777 SetResult(std::move(result)); | 705 Respond(OneArgument(std::move(result))); |
778 SendResponse(true); | |
779 | |
780 return true; | |
781 } | 706 } |
782 | 707 |
783 bool BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { | 708 void BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { |
784 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 709 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
785 | 710 |
786 BluetoothLowEnergyEventRouter* event_router = | 711 BluetoothLowEnergyEventRouter* event_router = |
787 GetEventRouter(browser_context()); | 712 GetEventRouter(browser_context()); |
788 | 713 |
789 // The adapter must be initialized at this point, but return an error instead | 714 // The adapter must be initialized at this point, but return an error instead |
790 // of asserting. | 715 // of asserting. |
791 if (!event_router->HasAdapter()) { | 716 if (!event_router->HasAdapter()) { |
792 SetError(kErrorAdapterNotInitialized); | 717 Respond(Error(kErrorAdapterNotInitialized)); |
793 SendResponse(false); | 718 return; |
794 return false; | |
795 } | 719 } |
796 | 720 |
797 std::unique_ptr<apibtle::ReadCharacteristicValue::Params> params( | 721 std::unique_ptr<apibtle::ReadCharacteristicValue::Params> params( |
798 apibtle::ReadCharacteristicValue::Params::Create(*args_)); | 722 apibtle::ReadCharacteristicValue::Params::Create(*args_)); |
799 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 723 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
800 | 724 |
801 instance_id_ = params->characteristic_id; | 725 instance_id_ = params->characteristic_id; |
802 event_router->ReadCharacteristicValue( | 726 event_router->ReadCharacteristicValue( |
803 extension(), instance_id_, | 727 extension(), instance_id_, |
804 base::Bind( | 728 base::Bind( |
805 &BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback, | 729 &BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback, |
806 this), | 730 this), |
807 base::Bind( | 731 base::Bind( |
808 &BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback, | 732 &BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback, |
809 this)); | 733 this)); |
810 | |
811 return true; | |
812 } | 734 } |
813 | 735 |
814 void BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback() { | 736 void BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback() { |
815 // Obtain info on the characteristic and see whether or not the characteristic | 737 // Obtain info on the characteristic and see whether or not the characteristic |
816 // is still around. | 738 // is still around. |
817 apibtle::Characteristic characteristic; | 739 apibtle::Characteristic characteristic; |
818 BluetoothLowEnergyEventRouter::Status status = | 740 BluetoothLowEnergyEventRouter::Status status = |
819 GetEventRouter(browser_context()) | 741 GetEventRouter(browser_context()) |
820 ->GetCharacteristic(extension(), instance_id_, &characteristic); | 742 ->GetCharacteristic(extension(), instance_id_, &characteristic); |
821 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 743 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
822 SetError(StatusToString(status)); | 744 Respond(Error(StatusToString(status))); |
823 SendResponse(false); | |
824 return; | 745 return; |
825 } | 746 } |
826 | 747 |
827 // Manually construct the result instead of using | 748 // Manually construct the result instead of using |
828 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of | 749 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of |
829 // enums correctly. | 750 // enums correctly. |
830 SetResult(apibtle::CharacteristicToValue(&characteristic)); | 751 Respond(OneArgument(apibtle::CharacteristicToValue(&characteristic))); |
831 SendResponse(true); | |
832 } | 752 } |
833 | 753 |
834 void BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback( | 754 void BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback( |
835 BluetoothLowEnergyEventRouter::Status status) { | 755 BluetoothLowEnergyEventRouter::Status status) { |
836 SetError(StatusToString(status)); | 756 Respond(Error(StatusToString(status))); |
837 SendResponse(false); | |
838 } | 757 } |
839 | 758 |
840 bool BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { | 759 void BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { |
841 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 760 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
842 | 761 |
843 BluetoothLowEnergyEventRouter* event_router = | 762 BluetoothLowEnergyEventRouter* event_router = |
844 GetEventRouter(browser_context()); | 763 GetEventRouter(browser_context()); |
845 | 764 |
846 // The adapter must be initialized at this point, but return an error instead | 765 // The adapter must be initialized at this point, but return an error instead |
847 // of asserting. | 766 // of asserting. |
848 if (!event_router->HasAdapter()) { | 767 if (!event_router->HasAdapter()) { |
849 SetError(kErrorAdapterNotInitialized); | 768 Respond(Error(kErrorAdapterNotInitialized)); |
850 SendResponse(false); | 769 return; |
851 return false; | |
852 } | 770 } |
853 | 771 |
854 std::unique_ptr<apibtle::WriteCharacteristicValue::Params> params( | 772 std::unique_ptr<apibtle::WriteCharacteristicValue::Params> params( |
855 apibtle::WriteCharacteristicValue::Params::Create(*args_)); | 773 apibtle::WriteCharacteristicValue::Params::Create(*args_)); |
856 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 774 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
857 | 775 |
858 std::vector<uint8_t> value(params->value.begin(), params->value.end()); | 776 std::vector<uint8_t> value(params->value.begin(), params->value.end()); |
859 event_router->WriteCharacteristicValue( | 777 event_router->WriteCharacteristicValue( |
860 extension(), params->characteristic_id, value, | 778 extension(), params->characteristic_id, value, |
861 base::Bind( | 779 base::Bind( |
862 &BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback, | 780 &BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback, |
863 this), | 781 this), |
864 base::Bind( | 782 base::Bind( |
865 &BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback, | 783 &BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback, |
866 this)); | 784 this)); |
867 | |
868 return true; | |
869 } | 785 } |
870 | 786 |
871 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() { | 787 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() { |
872 results_ = apibtle::WriteCharacteristicValue::Results::Create(); | 788 Respond(ArgumentList(apibtle::WriteCharacteristicValue::Results::Create())); |
873 SendResponse(true); | |
874 } | 789 } |
875 | 790 |
876 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback( | 791 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback( |
877 BluetoothLowEnergyEventRouter::Status status) { | 792 BluetoothLowEnergyEventRouter::Status status) { |
878 SetError(StatusToString(status)); | 793 Respond(Error(StatusToString(status))); |
879 SendResponse(false); | |
880 } | 794 } |
881 | 795 |
882 bool BluetoothLowEnergyStartCharacteristicNotificationsFunction::DoWork() { | 796 void BluetoothLowEnergyStartCharacteristicNotificationsFunction::DoWork() { |
883 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 797 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
884 | 798 |
885 BluetoothLowEnergyEventRouter* event_router = | 799 BluetoothLowEnergyEventRouter* event_router = |
886 GetEventRouter(browser_context()); | 800 GetEventRouter(browser_context()); |
887 | 801 |
888 // The adapter must be initialized at this point, but return an error instead | 802 // The adapter must be initialized at this point, but return an error instead |
889 // of asserting. | 803 // of asserting. |
890 if (!event_router->HasAdapter()) { | 804 if (!event_router->HasAdapter()) { |
891 SetError(kErrorAdapterNotInitialized); | 805 Respond(Error(kErrorAdapterNotInitialized)); |
892 SendResponse(false); | 806 return; |
893 return false; | |
894 } | 807 } |
895 | 808 |
896 std::unique_ptr<apibtle::StartCharacteristicNotifications::Params> params( | 809 std::unique_ptr<apibtle::StartCharacteristicNotifications::Params> params( |
897 apibtle::StartCharacteristicNotifications::Params::Create(*args_)); | 810 apibtle::StartCharacteristicNotifications::Params::Create(*args_)); |
898 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 811 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
899 | 812 |
900 bool persistent = false; // Not persistent by default. | 813 bool persistent = false; // Not persistent by default. |
901 apibtle::NotificationProperties* properties = params->properties.get(); | 814 apibtle::NotificationProperties* properties = params->properties.get(); |
902 if (properties) | 815 if (properties) |
903 persistent = properties->persistent; | 816 persistent = properties->persistent; |
904 | 817 |
905 event_router->StartCharacteristicNotifications( | 818 event_router->StartCharacteristicNotifications( |
906 persistent, extension(), params->characteristic_id, | 819 persistent, extension(), params->characteristic_id, |
907 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction:: | 820 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction:: |
908 SuccessCallback, | 821 SuccessCallback, |
909 this), | 822 this), |
910 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction:: | 823 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction:: |
911 ErrorCallback, | 824 ErrorCallback, |
912 this)); | 825 this)); |
913 | |
914 return true; | |
915 } | 826 } |
916 | 827 |
917 void BluetoothLowEnergyStartCharacteristicNotificationsFunction:: | 828 void BluetoothLowEnergyStartCharacteristicNotificationsFunction:: |
918 SuccessCallback() { | 829 SuccessCallback() { |
919 SendResponse(true); | 830 Respond(NoArguments()); |
920 } | 831 } |
921 | 832 |
922 void BluetoothLowEnergyStartCharacteristicNotificationsFunction::ErrorCallback( | 833 void BluetoothLowEnergyStartCharacteristicNotificationsFunction::ErrorCallback( |
923 BluetoothLowEnergyEventRouter::Status status) { | 834 BluetoothLowEnergyEventRouter::Status status) { |
924 SetError(StatusToString(status)); | 835 Respond(Error(StatusToString(status))); |
925 SendResponse(false); | |
926 } | 836 } |
927 | 837 |
928 bool BluetoothLowEnergyStopCharacteristicNotificationsFunction::DoWork() { | 838 void BluetoothLowEnergyStopCharacteristicNotificationsFunction::DoWork() { |
929 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 839 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
930 | 840 |
931 BluetoothLowEnergyEventRouter* event_router = | 841 BluetoothLowEnergyEventRouter* event_router = |
932 GetEventRouter(browser_context()); | 842 GetEventRouter(browser_context()); |
933 | 843 |
934 // The adapter must be initialized at this point, but return an error instead | 844 // The adapter must be initialized at this point, but return an error instead |
935 // of asserting. | 845 // of asserting. |
936 if (!event_router->HasAdapter()) { | 846 if (!event_router->HasAdapter()) { |
937 SetError(kErrorAdapterNotInitialized); | 847 Respond(Error(kErrorAdapterNotInitialized)); |
938 SendResponse(false); | 848 return; |
939 return false; | |
940 } | 849 } |
941 | 850 |
942 std::unique_ptr<apibtle::StopCharacteristicNotifications::Params> params( | 851 std::unique_ptr<apibtle::StopCharacteristicNotifications::Params> params( |
943 apibtle::StopCharacteristicNotifications::Params::Create(*args_)); | 852 apibtle::StopCharacteristicNotifications::Params::Create(*args_)); |
944 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 853 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
945 | 854 |
946 event_router->StopCharacteristicNotifications( | 855 event_router->StopCharacteristicNotifications( |
947 extension(), params->characteristic_id, | 856 extension(), params->characteristic_id, |
948 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction:: | 857 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction:: |
949 SuccessCallback, | 858 SuccessCallback, |
950 this), | 859 this), |
951 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction:: | 860 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction:: |
952 ErrorCallback, | 861 ErrorCallback, |
953 this)); | 862 this)); |
954 | |
955 return true; | |
956 } | 863 } |
957 | 864 |
958 void BluetoothLowEnergyStopCharacteristicNotificationsFunction:: | 865 void BluetoothLowEnergyStopCharacteristicNotificationsFunction:: |
959 SuccessCallback() { | 866 SuccessCallback() { |
960 SendResponse(true); | 867 Respond(NoArguments()); |
961 } | 868 } |
962 | 869 |
963 void BluetoothLowEnergyStopCharacteristicNotificationsFunction::ErrorCallback( | 870 void BluetoothLowEnergyStopCharacteristicNotificationsFunction::ErrorCallback( |
964 BluetoothLowEnergyEventRouter::Status status) { | 871 BluetoothLowEnergyEventRouter::Status status) { |
965 SetError(StatusToString(status)); | 872 Respond(Error(StatusToString(status))); |
966 SendResponse(false); | |
967 } | 873 } |
968 | 874 |
969 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { | 875 void BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { |
970 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 876 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
971 | 877 |
972 BluetoothLowEnergyEventRouter* event_router = | 878 BluetoothLowEnergyEventRouter* event_router = |
973 GetEventRouter(browser_context()); | 879 GetEventRouter(browser_context()); |
974 | 880 |
975 // The adapter must be initialized at this point, but return an error instead | 881 // The adapter must be initialized at this point, but return an error instead |
976 // of asserting. | 882 // of asserting. |
977 if (!event_router->HasAdapter()) { | 883 if (!event_router->HasAdapter()) { |
978 SetError(kErrorAdapterNotInitialized); | 884 Respond(Error(kErrorAdapterNotInitialized)); |
979 SendResponse(false); | 885 return; |
980 return false; | |
981 } | 886 } |
982 | 887 |
983 std::unique_ptr<apibtle::ReadDescriptorValue::Params> params( | 888 std::unique_ptr<apibtle::ReadDescriptorValue::Params> params( |
984 apibtle::ReadDescriptorValue::Params::Create(*args_)); | 889 apibtle::ReadDescriptorValue::Params::Create(*args_)); |
985 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 890 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
986 | 891 |
987 instance_id_ = params->descriptor_id; | 892 instance_id_ = params->descriptor_id; |
988 event_router->ReadDescriptorValue( | 893 event_router->ReadDescriptorValue( |
989 extension(), instance_id_, | 894 extension(), instance_id_, |
990 base::Bind( | 895 base::Bind( |
991 &BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback, | 896 &BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback, |
992 this), | 897 this), |
993 base::Bind(&BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback, | 898 base::Bind(&BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback, |
994 this)); | 899 this)); |
995 | |
996 return true; | |
997 } | 900 } |
998 | 901 |
999 void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() { | 902 void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() { |
1000 // Obtain info on the descriptor and see whether or not the descriptor is | 903 // Obtain info on the descriptor and see whether or not the descriptor is |
1001 // still around. | 904 // still around. |
1002 apibtle::Descriptor descriptor; | 905 apibtle::Descriptor descriptor; |
1003 BluetoothLowEnergyEventRouter::Status status = | 906 BluetoothLowEnergyEventRouter::Status status = |
1004 GetEventRouter(browser_context()) | 907 GetEventRouter(browser_context()) |
1005 ->GetDescriptor(extension(), instance_id_, &descriptor); | 908 ->GetDescriptor(extension(), instance_id_, &descriptor); |
1006 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 909 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
1007 SetError(StatusToString(status)); | 910 Respond(Error(StatusToString(status))); |
1008 SendResponse(false); | |
1009 return; | 911 return; |
1010 } | 912 } |
1011 | 913 |
1012 // Manually construct the result instead of using | 914 // Manually construct the result instead of using |
1013 // apibtle::GetDescriptor::Results::Create as it doesn't convert lists of | 915 // apibtle::GetDescriptor::Results::Create as it doesn't convert lists of |
1014 // enums correctly. | 916 // enums correctly. |
1015 SetResult(apibtle::DescriptorToValue(&descriptor)); | 917 Respond(OneArgument(apibtle::DescriptorToValue(&descriptor))); |
1016 SendResponse(true); | |
1017 } | 918 } |
1018 | 919 |
1019 void BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback( | 920 void BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback( |
1020 BluetoothLowEnergyEventRouter::Status status) { | 921 BluetoothLowEnergyEventRouter::Status status) { |
1021 SetError(StatusToString(status)); | 922 Respond(Error(StatusToString(status))); |
1022 SendResponse(false); | |
1023 } | 923 } |
1024 | 924 |
1025 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 925 void BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
1026 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 926 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1027 | 927 |
1028 BluetoothLowEnergyEventRouter* event_router = | 928 BluetoothLowEnergyEventRouter* event_router = |
1029 GetEventRouter(browser_context()); | 929 GetEventRouter(browser_context()); |
1030 | 930 |
1031 // The adapter must be initialized at this point, but return an error instead | 931 // The adapter must be initialized at this point, but return an error instead |
1032 // of asserting. | 932 // of asserting. |
1033 if (!event_router->HasAdapter()) { | 933 if (!event_router->HasAdapter()) { |
1034 SetError(kErrorAdapterNotInitialized); | 934 Respond(Error(kErrorAdapterNotInitialized)); |
1035 SendResponse(false); | 935 return; |
1036 return false; | |
1037 } | 936 } |
1038 | 937 |
1039 std::unique_ptr<apibtle::WriteDescriptorValue::Params> params( | 938 std::unique_ptr<apibtle::WriteDescriptorValue::Params> params( |
1040 apibtle::WriteDescriptorValue::Params::Create(*args_)); | 939 apibtle::WriteDescriptorValue::Params::Create(*args_)); |
1041 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 940 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
1042 | 941 |
1043 std::vector<uint8_t> value(params->value.begin(), params->value.end()); | 942 std::vector<uint8_t> value(params->value.begin(), params->value.end()); |
1044 event_router->WriteDescriptorValue( | 943 event_router->WriteDescriptorValue( |
1045 extension(), params->descriptor_id, value, | 944 extension(), params->descriptor_id, value, |
1046 base::Bind( | 945 base::Bind( |
1047 &BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback, | 946 &BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback, |
1048 this), | 947 this), |
1049 base::Bind(&BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback, | 948 base::Bind(&BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback, |
1050 this)); | 949 this)); |
1051 | |
1052 return true; | |
1053 } | 950 } |
1054 | 951 |
1055 void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() { | 952 void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() { |
1056 results_ = apibtle::WriteDescriptorValue::Results::Create(); | 953 Respond(ArgumentList(apibtle::WriteDescriptorValue::Results::Create())); |
1057 SendResponse(true); | |
1058 } | 954 } |
1059 | 955 |
1060 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback( | 956 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback( |
1061 BluetoothLowEnergyEventRouter::Status status) { | 957 BluetoothLowEnergyEventRouter::Status status) { |
1062 SetError(StatusToString(status)); | 958 Respond(Error(StatusToString(status))); |
1063 SendResponse(false); | |
1064 } | 959 } |
1065 | 960 |
1066 BluetoothLowEnergyAdvertisementFunction:: | 961 BluetoothLowEnergyAdvertisementFunction:: |
1067 BluetoothLowEnergyAdvertisementFunction() | 962 BluetoothLowEnergyAdvertisementFunction() |
1068 : advertisements_manager_(nullptr) {} | 963 : advertisements_manager_(nullptr) {} |
1069 | 964 |
1070 BluetoothLowEnergyAdvertisementFunction:: | 965 BluetoothLowEnergyAdvertisementFunction:: |
1071 ~BluetoothLowEnergyAdvertisementFunction() {} | 966 ~BluetoothLowEnergyAdvertisementFunction() {} |
1072 | 967 |
1073 int BluetoothLowEnergyAdvertisementFunction::AddAdvertisement( | 968 int BluetoothLowEnergyAdvertisementFunction::AddAdvertisement( |
(...skipping 13 matching lines...) Expand all Loading... |
1087 int advertisement_id) { | 982 int advertisement_id) { |
1088 DCHECK(advertisements_manager_); | 983 DCHECK(advertisements_manager_); |
1089 advertisements_manager_->Remove(extension_id(), advertisement_id); | 984 advertisements_manager_->Remove(extension_id(), advertisement_id); |
1090 } | 985 } |
1091 | 986 |
1092 const base::hash_set<int>* | 987 const base::hash_set<int>* |
1093 BluetoothLowEnergyAdvertisementFunction::GetAdvertisementIds() { | 988 BluetoothLowEnergyAdvertisementFunction::GetAdvertisementIds() { |
1094 return advertisements_manager_->GetResourceIds(extension_id()); | 989 return advertisements_manager_->GetResourceIds(extension_id()); |
1095 } | 990 } |
1096 | 991 |
1097 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { | 992 ExtensionFunction::ResponseAction |
| 993 BluetoothLowEnergyAdvertisementFunction::Run() { |
1098 Initialize(); | 994 Initialize(); |
1099 | 995 |
1100 // Check permission in the manifest. | 996 // Check permission in the manifest. |
1101 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) { | 997 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) { |
1102 SetError(kErrorPermissionDenied); | 998 return RespondNow(Error(kErrorPermissionDenied)); |
1103 return false; | |
1104 } | 999 } |
1105 | 1000 |
1106 // For advertisement API to be available the app has to be either auto | 1001 // For advertisement API to be available the app has to be either auto |
1107 // launched in Kiosk Mode or the enable-ble-advertisement-in-apps | 1002 // launched in Kiosk Mode or the enable-ble-advertisement-in-apps |
1108 // should be set. | 1003 // should be set. |
1109 if (!(IsAutoLaunchedKioskApp(extension()->id()) || | 1004 if (!(IsAutoLaunchedKioskApp(extension()->id()) || |
1110 IsPeripheralFlagEnabled())) { | 1005 IsPeripheralFlagEnabled())) { |
1111 SetError(kErrorPermissionDenied); | 1006 return RespondNow(Error(kErrorPermissionDenied)); |
1112 return false; | |
1113 } | 1007 } |
1114 | 1008 |
1115 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); | 1009 return BluetoothLowEnergyExtensionFunction::Run(); |
1116 } | 1010 } |
1117 | 1011 |
1118 void BluetoothLowEnergyAdvertisementFunction::Initialize() { | 1012 void BluetoothLowEnergyAdvertisementFunction::Initialize() { |
1119 advertisements_manager_ = | 1013 advertisements_manager_ = |
1120 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); | 1014 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); |
1121 } | 1015 } |
1122 | 1016 |
1123 // RegisterAdvertisement: | 1017 // RegisterAdvertisement: |
1124 | 1018 |
1125 bool BluetoothLowEnergyRegisterAdvertisementFunction::DoWork() { | 1019 void BluetoothLowEnergyRegisterAdvertisementFunction::DoWork() { |
1126 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1020 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1127 | 1021 |
1128 BluetoothLowEnergyEventRouter* event_router = | 1022 BluetoothLowEnergyEventRouter* event_router = |
1129 GetEventRouter(browser_context()); | 1023 GetEventRouter(browser_context()); |
1130 | 1024 |
1131 // The adapter must be initialized at this point, but return an error instead | 1025 // The adapter must be initialized at this point, but return an error instead |
1132 // of asserting. | 1026 // of asserting. |
1133 if (!event_router->HasAdapter()) { | 1027 if (!event_router->HasAdapter()) { |
1134 SetError(kErrorAdapterNotInitialized); | 1028 Respond(Error(kErrorAdapterNotInitialized)); |
1135 SendResponse(false); | 1029 return; |
1136 return false; | |
1137 } | 1030 } |
1138 | 1031 |
1139 std::unique_ptr<apibtle::RegisterAdvertisement::Params> params( | 1032 std::unique_ptr<apibtle::RegisterAdvertisement::Params> params( |
1140 apibtle::RegisterAdvertisement::Params::Create(*args_)); | 1033 apibtle::RegisterAdvertisement::Params::Create(*args_)); |
1141 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 1034 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
1142 | 1035 |
1143 std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement_data( | 1036 std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement_data( |
1144 new device::BluetoothAdvertisement::Data( | 1037 new device::BluetoothAdvertisement::Data( |
1145 params->advertisement.type == | 1038 params->advertisement.type == |
1146 apibtle::AdvertisementType::ADVERTISEMENT_TYPE_BROADCAST | 1039 apibtle::AdvertisementType::ADVERTISEMENT_TYPE_BROADCAST |
(...skipping 16 matching lines...) Expand all Loading... |
1163 } | 1056 } |
1164 | 1057 |
1165 event_router->adapter()->RegisterAdvertisement( | 1058 event_router->adapter()->RegisterAdvertisement( |
1166 std::move(advertisement_data), | 1059 std::move(advertisement_data), |
1167 base::Bind( | 1060 base::Bind( |
1168 &BluetoothLowEnergyRegisterAdvertisementFunction::SuccessCallback, | 1061 &BluetoothLowEnergyRegisterAdvertisementFunction::SuccessCallback, |
1169 this), | 1062 this), |
1170 base::Bind( | 1063 base::Bind( |
1171 &BluetoothLowEnergyRegisterAdvertisementFunction::ErrorCallback, | 1064 &BluetoothLowEnergyRegisterAdvertisementFunction::ErrorCallback, |
1172 this)); | 1065 this)); |
1173 | |
1174 return true; | |
1175 } | 1066 } |
1176 | 1067 |
1177 void BluetoothLowEnergyRegisterAdvertisementFunction::SuccessCallback( | 1068 void BluetoothLowEnergyRegisterAdvertisementFunction::SuccessCallback( |
1178 scoped_refptr<device::BluetoothAdvertisement> advertisement) { | 1069 scoped_refptr<device::BluetoothAdvertisement> advertisement) { |
1179 results_ = apibtle::RegisterAdvertisement::Results::Create(AddAdvertisement( | 1070 Respond(ArgumentList( |
1180 new BluetoothApiAdvertisement(extension_id(), advertisement))); | 1071 apibtle::RegisterAdvertisement::Results::Create(AddAdvertisement( |
1181 SendResponse(true); | 1072 new BluetoothApiAdvertisement(extension_id(), advertisement))))); |
1182 } | 1073 } |
1183 | 1074 |
1184 void BluetoothLowEnergyRegisterAdvertisementFunction::ErrorCallback( | 1075 void BluetoothLowEnergyRegisterAdvertisementFunction::ErrorCallback( |
1185 device::BluetoothAdvertisement::ErrorCode status) { | 1076 device::BluetoothAdvertisement::ErrorCode status) { |
1186 switch (status) { | 1077 switch (status) { |
1187 case device::BluetoothAdvertisement::ErrorCode:: | 1078 case device::BluetoothAdvertisement::ErrorCode:: |
1188 ERROR_ADVERTISEMENT_ALREADY_EXISTS: | 1079 ERROR_ADVERTISEMENT_ALREADY_EXISTS: |
1189 SetError(kStatusAdvertisementAlreadyExists); | 1080 Respond(Error(kStatusAdvertisementAlreadyExists)); |
1190 break; | 1081 break; |
1191 case device::BluetoothAdvertisement::ErrorCode:: | 1082 case device::BluetoothAdvertisement::ErrorCode:: |
1192 ERROR_ADVERTISEMENT_INVALID_LENGTH: | 1083 ERROR_ADVERTISEMENT_INVALID_LENGTH: |
1193 SetError(kErrorInvalidAdvertisementLength); | 1084 Respond(Error(kErrorInvalidAdvertisementLength)); |
1194 break; | 1085 break; |
1195 default: | 1086 default: |
1196 SetError(kErrorOperationFailed); | 1087 Respond(Error(kErrorOperationFailed)); |
1197 } | 1088 } |
1198 SendResponse(false); | |
1199 } | 1089 } |
1200 | 1090 |
1201 // UnregisterAdvertisement: | 1091 // UnregisterAdvertisement: |
1202 | 1092 |
1203 bool BluetoothLowEnergyUnregisterAdvertisementFunction::DoWork() { | 1093 void BluetoothLowEnergyUnregisterAdvertisementFunction::DoWork() { |
1204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1094 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1205 | 1095 |
1206 BluetoothLowEnergyEventRouter* event_router = | 1096 BluetoothLowEnergyEventRouter* event_router = |
1207 GetEventRouter(browser_context()); | 1097 GetEventRouter(browser_context()); |
1208 | 1098 |
1209 // If we don't have an initialized adapter, unregistering is a no-op. | 1099 // If we don't have an initialized adapter, unregistering is a no-op. |
1210 if (!event_router->HasAdapter()) | 1100 if (!event_router->HasAdapter()) |
1211 return true; | 1101 return; |
1212 | 1102 |
1213 std::unique_ptr<apibtle::UnregisterAdvertisement::Params> params( | 1103 std::unique_ptr<apibtle::UnregisterAdvertisement::Params> params( |
1214 apibtle::UnregisterAdvertisement::Params::Create(*args_)); | 1104 apibtle::UnregisterAdvertisement::Params::Create(*args_)); |
1215 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 1105 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
1216 | 1106 |
1217 BluetoothApiAdvertisement* advertisement = | 1107 BluetoothApiAdvertisement* advertisement = |
1218 GetAdvertisement(params->advertisement_id); | 1108 GetAdvertisement(params->advertisement_id); |
1219 if (!advertisement) { | 1109 if (!advertisement) { |
1220 error_ = kStatusAdvertisementDoesNotExist; | 1110 Respond(Error(kStatusAdvertisementDoesNotExist)); |
1221 SendResponse(false); | 1111 return; |
1222 return false; | |
1223 } | 1112 } |
1224 | 1113 |
1225 advertisement->advertisement()->Unregister( | 1114 advertisement->advertisement()->Unregister( |
1226 base::Bind( | 1115 base::Bind( |
1227 &BluetoothLowEnergyUnregisterAdvertisementFunction::SuccessCallback, | 1116 &BluetoothLowEnergyUnregisterAdvertisementFunction::SuccessCallback, |
1228 this, params->advertisement_id), | 1117 this, params->advertisement_id), |
1229 base::Bind( | 1118 base::Bind( |
1230 &BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback, | 1119 &BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback, |
1231 this, params->advertisement_id)); | 1120 this, params->advertisement_id)); |
1232 | |
1233 return true; | |
1234 } | 1121 } |
1235 | 1122 |
1236 void BluetoothLowEnergyUnregisterAdvertisementFunction::SuccessCallback( | 1123 void BluetoothLowEnergyUnregisterAdvertisementFunction::SuccessCallback( |
1237 int advertisement_id) { | 1124 int advertisement_id) { |
1238 RemoveAdvertisement(advertisement_id); | 1125 RemoveAdvertisement(advertisement_id); |
1239 SendResponse(true); | 1126 Respond(NoArguments()); |
1240 } | 1127 } |
1241 | 1128 |
1242 void BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback( | 1129 void BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback( |
1243 int advertisement_id, | 1130 int advertisement_id, |
1244 device::BluetoothAdvertisement::ErrorCode status) { | 1131 device::BluetoothAdvertisement::ErrorCode status) { |
1245 RemoveAdvertisement(advertisement_id); | 1132 RemoveAdvertisement(advertisement_id); |
1246 switch (status) { | 1133 switch (status) { |
1247 case device::BluetoothAdvertisement::ErrorCode:: | 1134 case device::BluetoothAdvertisement::ErrorCode:: |
1248 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: | 1135 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: |
1249 SetError(kStatusAdvertisementDoesNotExist); | 1136 Respond(Error(kStatusAdvertisementDoesNotExist)); |
1250 break; | 1137 break; |
1251 default: | 1138 default: |
1252 SetError(kErrorOperationFailed); | 1139 Respond(Error(kErrorOperationFailed)); |
1253 } | 1140 } |
1254 SendResponse(false); | |
1255 } | 1141 } |
1256 | 1142 |
1257 // ResetAdvertising: | 1143 // ResetAdvertising: |
1258 | 1144 |
1259 bool BluetoothLowEnergyResetAdvertisingFunction::DoWork() { | 1145 void BluetoothLowEnergyResetAdvertisingFunction::DoWork() { |
1260 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 1146 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
1261 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1147 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1262 | 1148 |
1263 BluetoothLowEnergyEventRouter* event_router = | 1149 BluetoothLowEnergyEventRouter* event_router = |
1264 GetEventRouter(browser_context()); | 1150 GetEventRouter(browser_context()); |
1265 | 1151 |
1266 // If the adapter is not initialized, there is nothing to reset. | 1152 // If the adapter is not initialized, there is nothing to reset. |
1267 if (!event_router->HasAdapter()) { | 1153 if (!event_router->HasAdapter()) { |
1268 SendResponse(true); | 1154 Respond(NoArguments()); |
1269 return true; | 1155 return; |
1270 } | 1156 } |
1271 | 1157 |
1272 const base::hash_set<int>* advertisement_ids = GetAdvertisementIds(); | 1158 const base::hash_set<int>* advertisement_ids = GetAdvertisementIds(); |
1273 if (!advertisement_ids || advertisement_ids->empty()) { | 1159 if (!advertisement_ids || advertisement_ids->empty()) { |
1274 SendResponse(true); | 1160 Respond(NoArguments()); |
1275 return true; | 1161 return; |
1276 } | 1162 } |
1277 | 1163 |
1278 // Copy the hash set, as RemoveAdvertisement can change advertisement_ids | 1164 // Copy the hash set, as RemoveAdvertisement can change advertisement_ids |
1279 // while we are iterating over it. | 1165 // while we are iterating over it. |
1280 base::hash_set<int> advertisement_ids_tmp = *advertisement_ids; | 1166 base::hash_set<int> advertisement_ids_tmp = *advertisement_ids; |
1281 for (int advertisement_id : advertisement_ids_tmp) { | 1167 for (int advertisement_id : advertisement_ids_tmp) { |
1282 RemoveAdvertisement(advertisement_id); | 1168 RemoveAdvertisement(advertisement_id); |
1283 } | 1169 } |
1284 | 1170 |
1285 event_router->adapter()->ResetAdvertising( | 1171 event_router->adapter()->ResetAdvertising( |
1286 base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback, | 1172 base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback, |
1287 this), | 1173 this), |
1288 base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback, | 1174 base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback, |
1289 this)); | 1175 this)); |
1290 #endif | 1176 #endif |
1291 | |
1292 return true; | |
1293 } | 1177 } |
1294 | 1178 |
1295 void BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback() { | 1179 void BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback() { |
1296 SendResponse(true); | 1180 Respond(NoArguments()); |
1297 } | 1181 } |
1298 | 1182 |
1299 void BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback( | 1183 void BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback( |
1300 device::BluetoothAdvertisement::ErrorCode status) { | 1184 device::BluetoothAdvertisement::ErrorCode status) { |
1301 error_ = kErrorOperationFailed; | 1185 Respond(Error(kErrorOperationFailed)); |
1302 SendResponse(false); | |
1303 } | 1186 } |
1304 | 1187 |
1305 // SetAdvertisingInterval: | 1188 // SetAdvertisingInterval: |
1306 | 1189 |
1307 template class BLEPeripheralExtensionFunction< | 1190 template class BLEPeripheralExtensionFunction< |
1308 apibtle::SetAdvertisingInterval::Params>; | 1191 apibtle::SetAdvertisingInterval::Params>; |
1309 | 1192 |
1310 void BluetoothLowEnergySetAdvertisingIntervalFunction::DoWork() { | 1193 void BluetoothLowEnergySetAdvertisingIntervalFunction::DoWork() { |
1311 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 1194 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
1312 BluetoothLowEnergyEventRouter* event_router = | 1195 BluetoothLowEnergyEventRouter* event_router = |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1531 params_->response.value->end()); | 1414 params_->response.value->end()); |
1532 } | 1415 } |
1533 event_router_->HandleRequestResponse( | 1416 event_router_->HandleRequestResponse( |
1534 extension(), params_->response.request_id, params_->response.is_error, | 1417 extension(), params_->response.request_id, params_->response.is_error, |
1535 uint8_vector); | 1418 uint8_vector); |
1536 Respond(NoArguments()); | 1419 Respond(NoArguments()); |
1537 } | 1420 } |
1538 | 1421 |
1539 } // namespace api | 1422 } // namespace api |
1540 } // namespace extensions | 1423 } // namespace extensions |
OLD | NEW |