Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: extensions/browser/api/usb/usb_api.cc

Issue 1902873002: Convert //extensions/browser/api from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/browser/api/usb/usb_api.h ('k') | extensions/browser/api/usb/usb_apitest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/usb/usb_api.h" 5 #include "extensions/browser/api/usb/usb_api.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <memory>
8 #include <numeric> 9 #include <numeric>
9 #include <string> 10 #include <string>
10 #include <utility> 11 #include <utility>
11 #include <vector> 12 #include <vector>
12 13
13 #include "base/barrier_closure.h" 14 #include "base/barrier_closure.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "device/core/device_client.h" 15 #include "device/core/device_client.h"
16 #include "device/usb/usb_descriptors.h" 16 #include "device/usb/usb_descriptors.h"
17 #include "device/usb/usb_device_handle.h" 17 #include "device/usb/usb_device_handle.h"
18 #include "device/usb/usb_service.h" 18 #include "device/usb/usb_service.h"
19 #include "extensions/browser/api/device_permissions_manager.h" 19 #include "extensions/browser/api/device_permissions_manager.h"
20 #include "extensions/browser/api/device_permissions_prompt.h" 20 #include "extensions/browser/api/device_permissions_prompt.h"
21 #include "extensions/browser/api/extensions_api_client.h" 21 #include "extensions/browser/api/extensions_api_client.h"
22 #include "extensions/browser/api/usb/usb_device_resource.h" 22 #include "extensions/browser/api/usb/usb_device_resource.h"
23 #include "extensions/browser/api/usb/usb_guid_map.h" 23 #include "extensions/browser/api/usb/usb_guid_map.h"
24 #include "extensions/browser/extension_system.h" 24 #include "extensions/browser/extension_system.h"
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 452
453 UsbTransferFunction::UsbTransferFunction() { 453 UsbTransferFunction::UsbTransferFunction() {
454 } 454 }
455 455
456 UsbTransferFunction::~UsbTransferFunction() { 456 UsbTransferFunction::~UsbTransferFunction() {
457 } 457 }
458 458
459 void UsbTransferFunction::OnCompleted(UsbTransferStatus status, 459 void UsbTransferFunction::OnCompleted(UsbTransferStatus status,
460 scoped_refptr<net::IOBuffer> data, 460 scoped_refptr<net::IOBuffer> data,
461 size_t length) { 461 size_t length) {
462 scoped_ptr<base::DictionaryValue> transfer_info(new base::DictionaryValue()); 462 std::unique_ptr<base::DictionaryValue> transfer_info(
463 new base::DictionaryValue());
463 transfer_info->SetInteger(kResultCodeKey, status); 464 transfer_info->SetInteger(kResultCodeKey, status);
464 transfer_info->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( 465 transfer_info->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer(
465 data->data(), length)); 466 data->data(), length));
466 467
467 if (status == device::USB_TRANSFER_COMPLETED) { 468 if (status == device::USB_TRANSFER_COMPLETED) {
468 Respond(OneArgument(std::move(transfer_info))); 469 Respond(OneArgument(std::move(transfer_info)));
469 } else { 470 } else {
470 scoped_ptr<base::ListValue> error_args(new base::ListValue()); 471 std::unique_ptr<base::ListValue> error_args(new base::ListValue());
471 error_args->Append(std::move(transfer_info)); 472 error_args->Append(std::move(transfer_info));
472 // Using ErrorWithArguments is discouraged but required to provide the 473 // Using ErrorWithArguments is discouraged but required to provide the
473 // detailed transfer info as the transfer may have partially succeeded. 474 // detailed transfer info as the transfer may have partially succeeded.
474 Respond(ErrorWithArguments(std::move(error_args), 475 Respond(ErrorWithArguments(std::move(error_args),
475 ConvertTransferStatusToApi(status))); 476 ConvertTransferStatusToApi(status)));
476 } 477 }
477 } 478 }
478 479
479 UsbFindDevicesFunction::UsbFindDevicesFunction() { 480 UsbFindDevicesFunction::UsbFindDevicesFunction() {
480 } 481 }
481 482
482 UsbFindDevicesFunction::~UsbFindDevicesFunction() { 483 UsbFindDevicesFunction::~UsbFindDevicesFunction() {
483 } 484 }
484 485
485 ExtensionFunction::ResponseAction UsbFindDevicesFunction::Run() { 486 ExtensionFunction::ResponseAction UsbFindDevicesFunction::Run() {
486 scoped_ptr<extensions::api::usb::FindDevices::Params> parameters = 487 std::unique_ptr<extensions::api::usb::FindDevices::Params> parameters =
487 FindDevices::Params::Create(*args_); 488 FindDevices::Params::Create(*args_);
488 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 489 EXTENSION_FUNCTION_VALIDATE(parameters.get());
489 490
490 vendor_id_ = parameters->options.vendor_id; 491 vendor_id_ = parameters->options.vendor_id;
491 product_id_ = parameters->options.product_id; 492 product_id_ = parameters->options.product_id;
492 int interface_id = parameters->options.interface_id.get() 493 int interface_id = parameters->options.interface_id.get()
493 ? *parameters->options.interface_id.get() 494 ? *parameters->options.interface_id.get()
494 : UsbDevicePermissionData::ANY_INTERFACE; 495 : UsbDevicePermissionData::ANY_INTERFACE;
495 UsbDevicePermission::CheckParam param(vendor_id_, product_id_, interface_id); 496 UsbDevicePermission::CheckParam param(vendor_id_, product_id_, interface_id);
496 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( 497 if (!extension()->permissions_data()->CheckAPIPermissionWithParam(
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 Respond(OneArgument(std::move(result_))); 543 Respond(OneArgument(std::move(result_)));
543 } 544 }
544 545
545 UsbGetDevicesFunction::UsbGetDevicesFunction() { 546 UsbGetDevicesFunction::UsbGetDevicesFunction() {
546 } 547 }
547 548
548 UsbGetDevicesFunction::~UsbGetDevicesFunction() { 549 UsbGetDevicesFunction::~UsbGetDevicesFunction() {
549 } 550 }
550 551
551 ExtensionFunction::ResponseAction UsbGetDevicesFunction::Run() { 552 ExtensionFunction::ResponseAction UsbGetDevicesFunction::Run() {
552 scoped_ptr<extensions::api::usb::GetDevices::Params> parameters = 553 std::unique_ptr<extensions::api::usb::GetDevices::Params> parameters =
553 GetDevices::Params::Create(*args_); 554 GetDevices::Params::Create(*args_);
554 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 555 EXTENSION_FUNCTION_VALIDATE(parameters.get());
555 556
556 if (parameters->options.filters) { 557 if (parameters->options.filters) {
557 filters_.resize(parameters->options.filters->size()); 558 filters_.resize(parameters->options.filters->size());
558 for (size_t i = 0; i < parameters->options.filters->size(); ++i) { 559 for (size_t i = 0; i < parameters->options.filters->size(); ++i) {
559 ConvertDeviceFilter(parameters->options.filters->at(i), &filters_[i]); 560 ConvertDeviceFilter(parameters->options.filters->at(i), &filters_[i]);
560 } 561 }
561 } 562 }
562 if (parameters->options.vendor_id) { 563 if (parameters->options.vendor_id) {
563 filters_.resize(filters_.size() + 1); 564 filters_.resize(filters_.size() + 1);
564 filters_.back().SetVendorId(*parameters->options.vendor_id); 565 filters_.back().SetVendorId(*parameters->options.vendor_id);
565 if (parameters->options.product_id) { 566 if (parameters->options.product_id) {
566 filters_.back().SetProductId(*parameters->options.product_id); 567 filters_.back().SetProductId(*parameters->options.product_id);
567 } 568 }
568 } 569 }
569 570
570 UsbService* service = device::DeviceClient::Get()->GetUsbService(); 571 UsbService* service = device::DeviceClient::Get()->GetUsbService();
571 if (!service) { 572 if (!service) {
572 return RespondNow(Error(kErrorInitService)); 573 return RespondNow(Error(kErrorInitService));
573 } 574 }
574 575
575 service->GetDevices( 576 service->GetDevices(
576 base::Bind(&UsbGetDevicesFunction::OnGetDevicesComplete, this)); 577 base::Bind(&UsbGetDevicesFunction::OnGetDevicesComplete, this));
577 return RespondLater(); 578 return RespondLater();
578 } 579 }
579 580
580 void UsbGetDevicesFunction::OnGetDevicesComplete( 581 void UsbGetDevicesFunction::OnGetDevicesComplete(
581 const std::vector<scoped_refptr<UsbDevice>>& devices) { 582 const std::vector<scoped_refptr<UsbDevice>>& devices) {
582 scoped_ptr<base::ListValue> result(new base::ListValue()); 583 std::unique_ptr<base::ListValue> result(new base::ListValue());
583 UsbGuidMap* guid_map = UsbGuidMap::Get(browser_context()); 584 UsbGuidMap* guid_map = UsbGuidMap::Get(browser_context());
584 for (const scoped_refptr<UsbDevice>& device : devices) { 585 for (const scoped_refptr<UsbDevice>& device : devices) {
585 if ((filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_)) && 586 if ((filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_)) &&
586 HasDevicePermission(device)) { 587 HasDevicePermission(device)) {
587 Device api_device; 588 Device api_device;
588 guid_map->GetApiDevice(device, &api_device); 589 guid_map->GetApiDevice(device, &api_device);
589 result->Append(api_device.ToValue()); 590 result->Append(api_device.ToValue());
590 } 591 }
591 } 592 }
592 593
593 Respond(OneArgument(std::move(result))); 594 Respond(OneArgument(std::move(result)));
594 } 595 }
595 596
596 UsbGetUserSelectedDevicesFunction::UsbGetUserSelectedDevicesFunction() { 597 UsbGetUserSelectedDevicesFunction::UsbGetUserSelectedDevicesFunction() {
597 } 598 }
598 599
599 UsbGetUserSelectedDevicesFunction::~UsbGetUserSelectedDevicesFunction() { 600 UsbGetUserSelectedDevicesFunction::~UsbGetUserSelectedDevicesFunction() {
600 } 601 }
601 602
602 ExtensionFunction::ResponseAction UsbGetUserSelectedDevicesFunction::Run() { 603 ExtensionFunction::ResponseAction UsbGetUserSelectedDevicesFunction::Run() {
603 scoped_ptr<extensions::api::usb::GetUserSelectedDevices::Params> parameters = 604 std::unique_ptr<extensions::api::usb::GetUserSelectedDevices::Params>
604 GetUserSelectedDevices::Params::Create(*args_); 605 parameters = GetUserSelectedDevices::Params::Create(*args_);
605 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 606 EXTENSION_FUNCTION_VALIDATE(parameters.get());
606 607
607 if (!user_gesture()) { 608 if (!user_gesture()) {
608 return RespondNow(OneArgument(new base::ListValue())); 609 return RespondNow(OneArgument(new base::ListValue()));
609 } 610 }
610 611
611 bool multiple = false; 612 bool multiple = false;
612 if (parameters->options.multiple) { 613 if (parameters->options.multiple) {
613 multiple = *parameters->options.multiple; 614 multiple = *parameters->options.multiple;
614 } 615 }
(...skipping 13 matching lines...) Expand all
628 } 629 }
629 630
630 prompt_->AskForUsbDevices( 631 prompt_->AskForUsbDevices(
631 extension(), browser_context(), multiple, filters, 632 extension(), browser_context(), multiple, filters,
632 base::Bind(&UsbGetUserSelectedDevicesFunction::OnDevicesChosen, this)); 633 base::Bind(&UsbGetUserSelectedDevicesFunction::OnDevicesChosen, this));
633 return RespondLater(); 634 return RespondLater();
634 } 635 }
635 636
636 void UsbGetUserSelectedDevicesFunction::OnDevicesChosen( 637 void UsbGetUserSelectedDevicesFunction::OnDevicesChosen(
637 const std::vector<scoped_refptr<UsbDevice>>& devices) { 638 const std::vector<scoped_refptr<UsbDevice>>& devices) {
638 scoped_ptr<base::ListValue> result(new base::ListValue()); 639 std::unique_ptr<base::ListValue> result(new base::ListValue());
639 UsbGuidMap* guid_map = UsbGuidMap::Get(browser_context()); 640 UsbGuidMap* guid_map = UsbGuidMap::Get(browser_context());
640 for (const auto& device : devices) { 641 for (const auto& device : devices) {
641 Device api_device; 642 Device api_device;
642 guid_map->GetApiDevice(device, &api_device); 643 guid_map->GetApiDevice(device, &api_device);
643 result->Append(api_device.ToValue()); 644 result->Append(api_device.ToValue());
644 } 645 }
645 646
646 Respond(OneArgument(std::move(result))); 647 Respond(OneArgument(std::move(result)));
647 } 648 }
648 649
649 UsbGetConfigurationsFunction::UsbGetConfigurationsFunction() {} 650 UsbGetConfigurationsFunction::UsbGetConfigurationsFunction() {}
650 651
651 UsbGetConfigurationsFunction::~UsbGetConfigurationsFunction() {} 652 UsbGetConfigurationsFunction::~UsbGetConfigurationsFunction() {}
652 653
653 ExtensionFunction::ResponseAction UsbGetConfigurationsFunction::Run() { 654 ExtensionFunction::ResponseAction UsbGetConfigurationsFunction::Run() {
654 scoped_ptr<extensions::api::usb::GetConfigurations::Params> parameters = 655 std::unique_ptr<extensions::api::usb::GetConfigurations::Params> parameters =
655 GetConfigurations::Params::Create(*args_); 656 GetConfigurations::Params::Create(*args_);
656 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 657 EXTENSION_FUNCTION_VALIDATE(parameters.get());
657 658
658 UsbService* service = device::DeviceClient::Get()->GetUsbService(); 659 UsbService* service = device::DeviceClient::Get()->GetUsbService();
659 if (!service) { 660 if (!service) {
660 return RespondNow(Error(kErrorInitService)); 661 return RespondNow(Error(kErrorInitService));
661 } 662 }
662 663
663 std::string guid; 664 std::string guid;
664 if (!UsbGuidMap::Get(browser_context()) 665 if (!UsbGuidMap::Get(browser_context())
665 ->GetGuidFromId(parameters->device.device, &guid)) { 666 ->GetGuidFromId(parameters->device.device, &guid)) {
666 return RespondNow(Error(kErrorNoDevice)); 667 return RespondNow(Error(kErrorNoDevice));
667 } 668 }
668 669
669 scoped_refptr<UsbDevice> device = service->GetDevice(guid); 670 scoped_refptr<UsbDevice> device = service->GetDevice(guid);
670 if (!device.get()) { 671 if (!device.get()) {
671 return RespondNow(Error(kErrorNoDevice)); 672 return RespondNow(Error(kErrorNoDevice));
672 } 673 }
673 674
674 if (!HasDevicePermission(device)) { 675 if (!HasDevicePermission(device)) {
675 // This function must act as if there is no such device. Otherwise it can be 676 // This function must act as if there is no such device. Otherwise it can be
676 // used to fingerprint unauthorized devices. 677 // used to fingerprint unauthorized devices.
677 return RespondNow(Error(kErrorNoDevice)); 678 return RespondNow(Error(kErrorNoDevice));
678 } 679 }
679 680
680 scoped_ptr<base::ListValue> configs(new base::ListValue()); 681 std::unique_ptr<base::ListValue> configs(new base::ListValue());
681 const UsbConfigDescriptor* active_config = device->GetActiveConfiguration(); 682 const UsbConfigDescriptor* active_config = device->GetActiveConfiguration();
682 for (const UsbConfigDescriptor& config : device->configurations()) { 683 for (const UsbConfigDescriptor& config : device->configurations()) {
683 ConfigDescriptor api_config = ConvertConfigDescriptor(config); 684 ConfigDescriptor api_config = ConvertConfigDescriptor(config);
684 if (active_config && 685 if (active_config &&
685 config.configuration_value == active_config->configuration_value) { 686 config.configuration_value == active_config->configuration_value) {
686 api_config.active = true; 687 api_config.active = true;
687 } 688 }
688 configs->Append(api_config.ToValue()); 689 configs->Append(api_config.ToValue());
689 } 690 }
690 return RespondNow(OneArgument(std::move(configs))); 691 return RespondNow(OneArgument(std::move(configs)));
691 } 692 }
692 693
693 UsbRequestAccessFunction::UsbRequestAccessFunction() { 694 UsbRequestAccessFunction::UsbRequestAccessFunction() {
694 } 695 }
695 696
696 UsbRequestAccessFunction::~UsbRequestAccessFunction() { 697 UsbRequestAccessFunction::~UsbRequestAccessFunction() {
697 } 698 }
698 699
699 ExtensionFunction::ResponseAction UsbRequestAccessFunction::Run() { 700 ExtensionFunction::ResponseAction UsbRequestAccessFunction::Run() {
700 scoped_ptr<extensions::api::usb::RequestAccess::Params> parameters = 701 std::unique_ptr<extensions::api::usb::RequestAccess::Params> parameters =
701 RequestAccess::Params::Create(*args_); 702 RequestAccess::Params::Create(*args_);
702 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 703 EXTENSION_FUNCTION_VALIDATE(parameters.get());
703 return RespondNow(OneArgument(new base::FundamentalValue(true))); 704 return RespondNow(OneArgument(new base::FundamentalValue(true)));
704 } 705 }
705 706
706 UsbOpenDeviceFunction::UsbOpenDeviceFunction() { 707 UsbOpenDeviceFunction::UsbOpenDeviceFunction() {
707 } 708 }
708 709
709 UsbOpenDeviceFunction::~UsbOpenDeviceFunction() { 710 UsbOpenDeviceFunction::~UsbOpenDeviceFunction() {
710 } 711 }
711 712
712 ExtensionFunction::ResponseAction UsbOpenDeviceFunction::Run() { 713 ExtensionFunction::ResponseAction UsbOpenDeviceFunction::Run() {
713 scoped_ptr<extensions::api::usb::OpenDevice::Params> parameters = 714 std::unique_ptr<extensions::api::usb::OpenDevice::Params> parameters =
714 OpenDevice::Params::Create(*args_); 715 OpenDevice::Params::Create(*args_);
715 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 716 EXTENSION_FUNCTION_VALIDATE(parameters.get());
716 717
717 UsbService* service = device::DeviceClient::Get()->GetUsbService(); 718 UsbService* service = device::DeviceClient::Get()->GetUsbService();
718 if (!service) { 719 if (!service) {
719 return RespondNow(Error(kErrorInitService)); 720 return RespondNow(Error(kErrorInitService));
720 } 721 }
721 722
722 std::string guid; 723 std::string guid;
723 if (!UsbGuidMap::Get(browser_context()) 724 if (!UsbGuidMap::Get(browser_context())
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 device->vendor_id(), device->product_id()))); 758 device->vendor_id(), device->product_id())));
758 } 759 }
759 760
760 UsbSetConfigurationFunction::UsbSetConfigurationFunction() { 761 UsbSetConfigurationFunction::UsbSetConfigurationFunction() {
761 } 762 }
762 763
763 UsbSetConfigurationFunction::~UsbSetConfigurationFunction() { 764 UsbSetConfigurationFunction::~UsbSetConfigurationFunction() {
764 } 765 }
765 766
766 ExtensionFunction::ResponseAction UsbSetConfigurationFunction::Run() { 767 ExtensionFunction::ResponseAction UsbSetConfigurationFunction::Run() {
767 scoped_ptr<extensions::api::usb::SetConfiguration::Params> parameters = 768 std::unique_ptr<extensions::api::usb::SetConfiguration::Params> parameters =
768 SetConfiguration::Params::Create(*args_); 769 SetConfiguration::Params::Create(*args_);
769 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 770 EXTENSION_FUNCTION_VALIDATE(parameters.get());
770 771
771 scoped_refptr<UsbDeviceHandle> device_handle = 772 scoped_refptr<UsbDeviceHandle> device_handle =
772 GetDeviceHandle(parameters->handle); 773 GetDeviceHandle(parameters->handle);
773 if (!device_handle.get()) { 774 if (!device_handle.get()) {
774 return RespondNow(Error(kErrorNoConnection)); 775 return RespondNow(Error(kErrorNoConnection));
775 } 776 }
776 777
777 device_handle->SetConfiguration( 778 device_handle->SetConfiguration(
(...skipping 10 matching lines...) Expand all
788 } 789 }
789 } 790 }
790 791
791 UsbGetConfigurationFunction::UsbGetConfigurationFunction() { 792 UsbGetConfigurationFunction::UsbGetConfigurationFunction() {
792 } 793 }
793 794
794 UsbGetConfigurationFunction::~UsbGetConfigurationFunction() { 795 UsbGetConfigurationFunction::~UsbGetConfigurationFunction() {
795 } 796 }
796 797
797 ExtensionFunction::ResponseAction UsbGetConfigurationFunction::Run() { 798 ExtensionFunction::ResponseAction UsbGetConfigurationFunction::Run() {
798 scoped_ptr<extensions::api::usb::GetConfiguration::Params> parameters = 799 std::unique_ptr<extensions::api::usb::GetConfiguration::Params> parameters =
799 GetConfiguration::Params::Create(*args_); 800 GetConfiguration::Params::Create(*args_);
800 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 801 EXTENSION_FUNCTION_VALIDATE(parameters.get());
801 802
802 scoped_refptr<UsbDeviceHandle> device_handle = 803 scoped_refptr<UsbDeviceHandle> device_handle =
803 GetDeviceHandle(parameters->handle); 804 GetDeviceHandle(parameters->handle);
804 if (!device_handle.get()) { 805 if (!device_handle.get()) {
805 return RespondNow(Error(kErrorNoConnection)); 806 return RespondNow(Error(kErrorNoConnection));
806 } 807 }
807 808
808 const UsbConfigDescriptor* config_descriptor = 809 const UsbConfigDescriptor* config_descriptor =
809 device_handle->GetDevice()->GetActiveConfiguration(); 810 device_handle->GetDevice()->GetActiveConfiguration();
810 if (config_descriptor) { 811 if (config_descriptor) {
811 ConfigDescriptor config = ConvertConfigDescriptor(*config_descriptor); 812 ConfigDescriptor config = ConvertConfigDescriptor(*config_descriptor);
812 config.active = true; 813 config.active = true;
813 return RespondNow(OneArgument(config.ToValue())); 814 return RespondNow(OneArgument(config.ToValue()));
814 } else { 815 } else {
815 return RespondNow(Error(kErrorNotConfigured)); 816 return RespondNow(Error(kErrorNotConfigured));
816 } 817 }
817 } 818 }
818 819
819 UsbListInterfacesFunction::UsbListInterfacesFunction() { 820 UsbListInterfacesFunction::UsbListInterfacesFunction() {
820 } 821 }
821 822
822 UsbListInterfacesFunction::~UsbListInterfacesFunction() { 823 UsbListInterfacesFunction::~UsbListInterfacesFunction() {
823 } 824 }
824 825
825 ExtensionFunction::ResponseAction UsbListInterfacesFunction::Run() { 826 ExtensionFunction::ResponseAction UsbListInterfacesFunction::Run() {
826 scoped_ptr<extensions::api::usb::ListInterfaces::Params> parameters = 827 std::unique_ptr<extensions::api::usb::ListInterfaces::Params> parameters =
827 ListInterfaces::Params::Create(*args_); 828 ListInterfaces::Params::Create(*args_);
828 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 829 EXTENSION_FUNCTION_VALIDATE(parameters.get());
829 830
830 scoped_refptr<UsbDeviceHandle> device_handle = 831 scoped_refptr<UsbDeviceHandle> device_handle =
831 GetDeviceHandle(parameters->handle); 832 GetDeviceHandle(parameters->handle);
832 if (!device_handle.get()) { 833 if (!device_handle.get()) {
833 return RespondNow(Error(kErrorNoConnection)); 834 return RespondNow(Error(kErrorNoConnection));
834 } 835 }
835 836
836 const UsbConfigDescriptor* config_descriptor = 837 const UsbConfigDescriptor* config_descriptor =
837 device_handle->GetDevice()->GetActiveConfiguration(); 838 device_handle->GetDevice()->GetActiveConfiguration();
838 if (config_descriptor) { 839 if (config_descriptor) {
839 ConfigDescriptor config = ConvertConfigDescriptor(*config_descriptor); 840 ConfigDescriptor config = ConvertConfigDescriptor(*config_descriptor);
840 841
841 scoped_ptr<base::ListValue> result(new base::ListValue); 842 std::unique_ptr<base::ListValue> result(new base::ListValue);
842 for (size_t i = 0; i < config.interfaces.size(); ++i) { 843 for (size_t i = 0; i < config.interfaces.size(); ++i) {
843 result->Append(config.interfaces[i].ToValue()); 844 result->Append(config.interfaces[i].ToValue());
844 } 845 }
845 846
846 return RespondNow(OneArgument(std::move(result))); 847 return RespondNow(OneArgument(std::move(result)));
847 } else { 848 } else {
848 return RespondNow(Error(kErrorNotConfigured)); 849 return RespondNow(Error(kErrorNotConfigured));
849 } 850 }
850 } 851 }
851 852
852 UsbCloseDeviceFunction::UsbCloseDeviceFunction() { 853 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {
853 } 854 }
854 855
855 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() { 856 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {
856 } 857 }
857 858
858 ExtensionFunction::ResponseAction UsbCloseDeviceFunction::Run() { 859 ExtensionFunction::ResponseAction UsbCloseDeviceFunction::Run() {
859 scoped_ptr<extensions::api::usb::CloseDevice::Params> parameters = 860 std::unique_ptr<extensions::api::usb::CloseDevice::Params> parameters =
860 CloseDevice::Params::Create(*args_); 861 CloseDevice::Params::Create(*args_);
861 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 862 EXTENSION_FUNCTION_VALIDATE(parameters.get());
862 863
863 scoped_refptr<UsbDeviceHandle> device_handle = 864 scoped_refptr<UsbDeviceHandle> device_handle =
864 GetDeviceHandle(parameters->handle); 865 GetDeviceHandle(parameters->handle);
865 if (!device_handle.get()) { 866 if (!device_handle.get()) {
866 return RespondNow(Error(kErrorNoConnection)); 867 return RespondNow(Error(kErrorNoConnection));
867 } 868 }
868 869
869 // The device handle is closed when the resource is destroyed. 870 // The device handle is closed when the resource is destroyed.
870 ReleaseDeviceHandle(parameters->handle); 871 ReleaseDeviceHandle(parameters->handle);
871 return RespondNow(NoArguments()); 872 return RespondNow(NoArguments());
872 } 873 }
873 874
874 UsbClaimInterfaceFunction::UsbClaimInterfaceFunction() { 875 UsbClaimInterfaceFunction::UsbClaimInterfaceFunction() {
875 } 876 }
876 877
877 UsbClaimInterfaceFunction::~UsbClaimInterfaceFunction() { 878 UsbClaimInterfaceFunction::~UsbClaimInterfaceFunction() {
878 } 879 }
879 880
880 ExtensionFunction::ResponseAction UsbClaimInterfaceFunction::Run() { 881 ExtensionFunction::ResponseAction UsbClaimInterfaceFunction::Run() {
881 scoped_ptr<extensions::api::usb::ClaimInterface::Params> parameters = 882 std::unique_ptr<extensions::api::usb::ClaimInterface::Params> parameters =
882 ClaimInterface::Params::Create(*args_); 883 ClaimInterface::Params::Create(*args_);
883 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 884 EXTENSION_FUNCTION_VALIDATE(parameters.get());
884 885
885 scoped_refptr<UsbDeviceHandle> device_handle = 886 scoped_refptr<UsbDeviceHandle> device_handle =
886 GetDeviceHandle(parameters->handle); 887 GetDeviceHandle(parameters->handle);
887 if (!device_handle.get()) { 888 if (!device_handle.get()) {
888 return RespondNow(Error(kErrorNoConnection)); 889 return RespondNow(Error(kErrorNoConnection));
889 } 890 }
890 891
891 device_handle->ClaimInterface( 892 device_handle->ClaimInterface(
(...skipping 10 matching lines...) Expand all
902 } 903 }
903 } 904 }
904 905
905 UsbReleaseInterfaceFunction::UsbReleaseInterfaceFunction() { 906 UsbReleaseInterfaceFunction::UsbReleaseInterfaceFunction() {
906 } 907 }
907 908
908 UsbReleaseInterfaceFunction::~UsbReleaseInterfaceFunction() { 909 UsbReleaseInterfaceFunction::~UsbReleaseInterfaceFunction() {
909 } 910 }
910 911
911 ExtensionFunction::ResponseAction UsbReleaseInterfaceFunction::Run() { 912 ExtensionFunction::ResponseAction UsbReleaseInterfaceFunction::Run() {
912 scoped_ptr<extensions::api::usb::ReleaseInterface::Params> parameters = 913 std::unique_ptr<extensions::api::usb::ReleaseInterface::Params> parameters =
913 ReleaseInterface::Params::Create(*args_); 914 ReleaseInterface::Params::Create(*args_);
914 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 915 EXTENSION_FUNCTION_VALIDATE(parameters.get());
915 916
916 scoped_refptr<UsbDeviceHandle> device_handle = 917 scoped_refptr<UsbDeviceHandle> device_handle =
917 GetDeviceHandle(parameters->handle); 918 GetDeviceHandle(parameters->handle);
918 if (!device_handle.get()) { 919 if (!device_handle.get()) {
919 return RespondNow(Error(kErrorNoConnection)); 920 return RespondNow(Error(kErrorNoConnection));
920 } 921 }
921 922
922 device_handle->ReleaseInterface( 923 device_handle->ReleaseInterface(
(...skipping 12 matching lines...) Expand all
935 UsbSetInterfaceAlternateSettingFunction:: 936 UsbSetInterfaceAlternateSettingFunction::
936 UsbSetInterfaceAlternateSettingFunction() { 937 UsbSetInterfaceAlternateSettingFunction() {
937 } 938 }
938 939
939 UsbSetInterfaceAlternateSettingFunction:: 940 UsbSetInterfaceAlternateSettingFunction::
940 ~UsbSetInterfaceAlternateSettingFunction() { 941 ~UsbSetInterfaceAlternateSettingFunction() {
941 } 942 }
942 943
943 ExtensionFunction::ResponseAction 944 ExtensionFunction::ResponseAction
944 UsbSetInterfaceAlternateSettingFunction::Run() { 945 UsbSetInterfaceAlternateSettingFunction::Run() {
945 scoped_ptr<extensions::api::usb::SetInterfaceAlternateSetting::Params> 946 std::unique_ptr<extensions::api::usb::SetInterfaceAlternateSetting::Params>
946 parameters = SetInterfaceAlternateSetting::Params::Create(*args_); 947 parameters = SetInterfaceAlternateSetting::Params::Create(*args_);
947 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 948 EXTENSION_FUNCTION_VALIDATE(parameters.get());
948 949
949 scoped_refptr<UsbDeviceHandle> device_handle = 950 scoped_refptr<UsbDeviceHandle> device_handle =
950 GetDeviceHandle(parameters->handle); 951 GetDeviceHandle(parameters->handle);
951 if (!device_handle.get()) { 952 if (!device_handle.get()) {
952 return RespondNow(Error(kErrorNoConnection)); 953 return RespondNow(Error(kErrorNoConnection));
953 } 954 }
954 955
955 device_handle->SetInterfaceAlternateSetting( 956 device_handle->SetInterfaceAlternateSetting(
(...skipping 10 matching lines...) Expand all
966 } 967 }
967 } 968 }
968 969
969 UsbControlTransferFunction::UsbControlTransferFunction() { 970 UsbControlTransferFunction::UsbControlTransferFunction() {
970 } 971 }
971 972
972 UsbControlTransferFunction::~UsbControlTransferFunction() { 973 UsbControlTransferFunction::~UsbControlTransferFunction() {
973 } 974 }
974 975
975 ExtensionFunction::ResponseAction UsbControlTransferFunction::Run() { 976 ExtensionFunction::ResponseAction UsbControlTransferFunction::Run() {
976 scoped_ptr<extensions::api::usb::ControlTransfer::Params> parameters = 977 std::unique_ptr<extensions::api::usb::ControlTransfer::Params> parameters =
977 ControlTransfer::Params::Create(*args_); 978 ControlTransfer::Params::Create(*args_);
978 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 979 EXTENSION_FUNCTION_VALIDATE(parameters.get());
979 980
980 scoped_refptr<UsbDeviceHandle> device_handle = 981 scoped_refptr<UsbDeviceHandle> device_handle =
981 GetDeviceHandle(parameters->handle); 982 GetDeviceHandle(parameters->handle);
982 if (!device_handle.get()) { 983 if (!device_handle.get()) {
983 return RespondNow(Error(kErrorNoConnection)); 984 return RespondNow(Error(kErrorNoConnection));
984 } 985 }
985 986
986 const ControlTransferInfo& transfer = parameters->transfer_info; 987 const ControlTransferInfo& transfer = parameters->transfer_info;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1023 return RespondLater(); 1024 return RespondLater();
1024 } 1025 }
1025 1026
1026 UsbBulkTransferFunction::UsbBulkTransferFunction() { 1027 UsbBulkTransferFunction::UsbBulkTransferFunction() {
1027 } 1028 }
1028 1029
1029 UsbBulkTransferFunction::~UsbBulkTransferFunction() { 1030 UsbBulkTransferFunction::~UsbBulkTransferFunction() {
1030 } 1031 }
1031 1032
1032 ExtensionFunction::ResponseAction UsbBulkTransferFunction::Run() { 1033 ExtensionFunction::ResponseAction UsbBulkTransferFunction::Run() {
1033 scoped_ptr<extensions::api::usb::BulkTransfer::Params> parameters = 1034 std::unique_ptr<extensions::api::usb::BulkTransfer::Params> parameters =
1034 BulkTransfer::Params::Create(*args_); 1035 BulkTransfer::Params::Create(*args_);
1035 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 1036 EXTENSION_FUNCTION_VALIDATE(parameters.get());
1036 1037
1037 scoped_refptr<UsbDeviceHandle> device_handle = 1038 scoped_refptr<UsbDeviceHandle> device_handle =
1038 GetDeviceHandle(parameters->handle); 1039 GetDeviceHandle(parameters->handle);
1039 if (!device_handle.get()) { 1040 if (!device_handle.get()) {
1040 return RespondNow(Error(kErrorNoConnection)); 1041 return RespondNow(Error(kErrorNoConnection));
1041 } 1042 }
1042 1043
1043 const GenericTransferInfo& transfer = parameters->transfer_info; 1044 const GenericTransferInfo& transfer = parameters->transfer_info;
(...skipping 25 matching lines...) Expand all
1069 return RespondLater(); 1070 return RespondLater();
1070 } 1071 }
1071 1072
1072 UsbInterruptTransferFunction::UsbInterruptTransferFunction() { 1073 UsbInterruptTransferFunction::UsbInterruptTransferFunction() {
1073 } 1074 }
1074 1075
1075 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() { 1076 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() {
1076 } 1077 }
1077 1078
1078 ExtensionFunction::ResponseAction UsbInterruptTransferFunction::Run() { 1079 ExtensionFunction::ResponseAction UsbInterruptTransferFunction::Run() {
1079 scoped_ptr<extensions::api::usb::InterruptTransfer::Params> parameters = 1080 std::unique_ptr<extensions::api::usb::InterruptTransfer::Params> parameters =
1080 InterruptTransfer::Params::Create(*args_); 1081 InterruptTransfer::Params::Create(*args_);
1081 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 1082 EXTENSION_FUNCTION_VALIDATE(parameters.get());
1082 1083
1083 scoped_refptr<UsbDeviceHandle> device_handle = 1084 scoped_refptr<UsbDeviceHandle> device_handle =
1084 GetDeviceHandle(parameters->handle); 1085 GetDeviceHandle(parameters->handle);
1085 if (!device_handle.get()) { 1086 if (!device_handle.get()) {
1086 return RespondNow(Error(kErrorNoConnection)); 1087 return RespondNow(Error(kErrorNoConnection));
1087 } 1088 }
1088 1089
1089 const GenericTransferInfo& transfer = parameters->transfer_info; 1090 const GenericTransferInfo& transfer = parameters->transfer_info;
(...skipping 25 matching lines...) Expand all
1115 return RespondLater(); 1116 return RespondLater();
1116 } 1117 }
1117 1118
1118 UsbIsochronousTransferFunction::UsbIsochronousTransferFunction() { 1119 UsbIsochronousTransferFunction::UsbIsochronousTransferFunction() {
1119 } 1120 }
1120 1121
1121 UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() { 1122 UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() {
1122 } 1123 }
1123 1124
1124 ExtensionFunction::ResponseAction UsbIsochronousTransferFunction::Run() { 1125 ExtensionFunction::ResponseAction UsbIsochronousTransferFunction::Run() {
1125 scoped_ptr<extensions::api::usb::IsochronousTransfer::Params> parameters = 1126 std::unique_ptr<extensions::api::usb::IsochronousTransfer::Params>
1126 IsochronousTransfer::Params::Create(*args_); 1127 parameters = IsochronousTransfer::Params::Create(*args_);
1127 EXTENSION_FUNCTION_VALIDATE(parameters.get()); 1128 EXTENSION_FUNCTION_VALIDATE(parameters.get());
1128 1129
1129 scoped_refptr<UsbDeviceHandle> device_handle = 1130 scoped_refptr<UsbDeviceHandle> device_handle =
1130 GetDeviceHandle(parameters->handle); 1131 GetDeviceHandle(parameters->handle);
1131 if (!device_handle.get()) { 1132 if (!device_handle.get()) {
1132 return RespondNow(Error(kErrorNoConnection)); 1133 return RespondNow(Error(kErrorNoConnection));
1133 } 1134 }
1134 1135
1135 const IsochronousTransferInfo& transfer = parameters->transfer_info; 1136 const IsochronousTransferInfo& transfer = parameters->transfer_info;
1136 const GenericTransferInfo& generic_transfer = transfer.transfer_info; 1137 const GenericTransferInfo& generic_transfer = transfer.transfer_info;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 } 1180 }
1180 1181
1181 void UsbIsochronousTransferFunction::OnCompleted( 1182 void UsbIsochronousTransferFunction::OnCompleted(
1182 scoped_refptr<net::IOBuffer> data, 1183 scoped_refptr<net::IOBuffer> data,
1183 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) { 1184 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) {
1184 size_t length = std::accumulate( 1185 size_t length = std::accumulate(
1185 packets.begin(), packets.end(), 0, 1186 packets.begin(), packets.end(), 0,
1186 [](const size_t& a, const UsbDeviceHandle::IsochronousPacket& packet) { 1187 [](const size_t& a, const UsbDeviceHandle::IsochronousPacket& packet) {
1187 return a + packet.transferred_length; 1188 return a + packet.transferred_length;
1188 }); 1189 });
1189 scoped_ptr<char[]> buffer(new char[length]); 1190 std::unique_ptr<char[]> buffer(new char[length]);
1190 1191
1191 UsbTransferStatus status = device::USB_TRANSFER_COMPLETED; 1192 UsbTransferStatus status = device::USB_TRANSFER_COMPLETED;
1192 size_t buffer_offset = 0; 1193 size_t buffer_offset = 0;
1193 size_t data_offset = 0; 1194 size_t data_offset = 0;
1194 for (const auto& packet : packets) { 1195 for (const auto& packet : packets) {
1195 // Capture the error status of the first unsuccessful packet. 1196 // Capture the error status of the first unsuccessful packet.
1196 if (status == device::USB_TRANSFER_COMPLETED && 1197 if (status == device::USB_TRANSFER_COMPLETED &&
1197 packet.status != device::USB_TRANSFER_COMPLETED) { 1198 packet.status != device::USB_TRANSFER_COMPLETED) {
1198 status = packet.status; 1199 status = packet.status;
1199 } 1200 }
1200 1201
1201 memcpy(&buffer[buffer_offset], data->data() + data_offset, 1202 memcpy(&buffer[buffer_offset], data->data() + data_offset,
1202 packet.transferred_length); 1203 packet.transferred_length);
1203 buffer_offset += packet.transferred_length; 1204 buffer_offset += packet.transferred_length;
1204 data_offset += packet.length; 1205 data_offset += packet.length;
1205 } 1206 }
1206 1207
1207 scoped_ptr<base::DictionaryValue> transfer_info(new base::DictionaryValue()); 1208 std::unique_ptr<base::DictionaryValue> transfer_info(
1209 new base::DictionaryValue());
1208 transfer_info->SetInteger(kResultCodeKey, status); 1210 transfer_info->SetInteger(kResultCodeKey, status);
1209 transfer_info->Set(kDataKey, 1211 transfer_info->Set(kDataKey,
1210 new base::BinaryValue(std::move(buffer), length)); 1212 new base::BinaryValue(std::move(buffer), length));
1211 if (status == device::USB_TRANSFER_COMPLETED) { 1213 if (status == device::USB_TRANSFER_COMPLETED) {
1212 Respond(OneArgument(std::move(transfer_info))); 1214 Respond(OneArgument(std::move(transfer_info)));
1213 } else { 1215 } else {
1214 scoped_ptr<base::ListValue> error_args(new base::ListValue()); 1216 std::unique_ptr<base::ListValue> error_args(new base::ListValue());
1215 error_args->Append(std::move(transfer_info)); 1217 error_args->Append(std::move(transfer_info));
1216 // Using ErrorWithArguments is discouraged but required to provide the 1218 // Using ErrorWithArguments is discouraged but required to provide the
1217 // detailed transfer info as the transfer may have partially succeeded. 1219 // detailed transfer info as the transfer may have partially succeeded.
1218 Respond(ErrorWithArguments(std::move(error_args), 1220 Respond(ErrorWithArguments(std::move(error_args),
1219 ConvertTransferStatusToApi(status))); 1221 ConvertTransferStatusToApi(status)));
1220 } 1222 }
1221 } 1223 }
1222 1224
1223 UsbResetDeviceFunction::UsbResetDeviceFunction() { 1225 UsbResetDeviceFunction::UsbResetDeviceFunction() {
1224 } 1226 }
(...skipping 20 matching lines...) Expand all
1245 if (success) { 1247 if (success) {
1246 Respond(OneArgument(new base::FundamentalValue(true))); 1248 Respond(OneArgument(new base::FundamentalValue(true)));
1247 } else { 1249 } else {
1248 scoped_refptr<UsbDeviceHandle> device_handle = 1250 scoped_refptr<UsbDeviceHandle> device_handle =
1249 GetDeviceHandle(parameters_->handle); 1251 GetDeviceHandle(parameters_->handle);
1250 if (device_handle.get()) { 1252 if (device_handle.get()) {
1251 device_handle->Close(); 1253 device_handle->Close();
1252 } 1254 }
1253 ReleaseDeviceHandle(parameters_->handle); 1255 ReleaseDeviceHandle(parameters_->handle);
1254 1256
1255 scoped_ptr<base::ListValue> error_args(new base::ListValue()); 1257 std::unique_ptr<base::ListValue> error_args(new base::ListValue());
1256 error_args->AppendBoolean(false); 1258 error_args->AppendBoolean(false);
1257 // Using ErrorWithArguments is discouraged but required to maintain 1259 // Using ErrorWithArguments is discouraged but required to maintain
1258 // compatibility with existing applications. 1260 // compatibility with existing applications.
1259 Respond(ErrorWithArguments(std::move(error_args), kErrorResetDevice)); 1261 Respond(ErrorWithArguments(std::move(error_args), kErrorResetDevice));
1260 } 1262 }
1261 } 1263 }
1262 1264
1263 } // namespace extensions 1265 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/usb/usb_api.h ('k') | extensions/browser/api/usb/usb_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698