OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/usb/usb_api.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "chrome/browser/extensions/api/api_resource_controller.h" |
| 11 #include "chrome/browser/extensions/api/usb/usb_device_resource.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/usb/usb_service_factory.h" |
| 14 #include "chrome/browser/usb/usb_service.h" |
| 15 #include "chrome/common/extensions/api/experimental.usb.h" |
| 16 |
| 17 namespace BulkTransfer = extensions::api::experimental_usb::BulkTransfer; |
| 18 namespace CloseDevice = extensions::api::experimental_usb::CloseDevice; |
| 19 namespace ControlTransfer = extensions::api::experimental_usb::ControlTransfer; |
| 20 namespace FindDevice = extensions::api::experimental_usb::FindDevice; |
| 21 namespace InterruptTransfer = |
| 22 extensions::api::experimental_usb::InterruptTransfer; |
| 23 using extensions::api::experimental_usb::Device; |
| 24 using std::vector; |
| 25 |
| 26 namespace extensions { |
| 27 |
| 28 UsbFindDeviceFunction::UsbFindDeviceFunction() {} |
| 29 |
| 30 UsbFindDeviceFunction::~UsbFindDeviceFunction() {} |
| 31 |
| 32 bool UsbFindDeviceFunction::Prepare() { |
| 33 parameters_ = FindDevice::Params::Create(*args_); |
| 34 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 35 event_notifier_ = CreateEventNotifier(ExtractSrcId(2)); |
| 36 return true; |
| 37 } |
| 38 |
| 39 void UsbFindDeviceFunction::Work() { |
| 40 UsbService* const service = |
| 41 UsbServiceFactory::GetInstance()->GetForProfile(profile()); |
| 42 DCHECK(service) << "No UsbService associated with profile."; |
| 43 |
| 44 UsbDevice* const device = service->FindDevice(parameters_->vendor_id, |
| 45 parameters_->product_id); |
| 46 if (!device) { |
| 47 result_.reset(base::Value::CreateNullValue()); |
| 48 return; |
| 49 } |
| 50 |
| 51 UsbDeviceResource* const resource = new UsbDeviceResource(event_notifier_, |
| 52 device); |
| 53 |
| 54 Device result; |
| 55 result.handle = controller()->AddAPIResource(resource); |
| 56 result.vendor_id = parameters_->vendor_id; |
| 57 result.product_id = parameters_->product_id; |
| 58 result_ = result.ToValue(); |
| 59 } |
| 60 |
| 61 bool UsbFindDeviceFunction::Respond() { |
| 62 return true; |
| 63 } |
| 64 |
| 65 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} |
| 66 |
| 67 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} |
| 68 |
| 69 bool UsbCloseDeviceFunction::Prepare() { |
| 70 parameters_ = CloseDevice::Params::Create(*args_); |
| 71 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 72 return true; |
| 73 } |
| 74 |
| 75 void UsbCloseDeviceFunction::Work() { |
| 76 controller()->RemoveAPIResource(parameters_->device.handle); |
| 77 } |
| 78 |
| 79 bool UsbCloseDeviceFunction::Respond() { |
| 80 return true; |
| 81 } |
| 82 |
| 83 UsbControlTransferFunction::UsbControlTransferFunction() {} |
| 84 |
| 85 UsbControlTransferFunction::~UsbControlTransferFunction() {} |
| 86 |
| 87 bool UsbControlTransferFunction::Prepare() { |
| 88 parameters_ = ControlTransfer::Params::Create(*args_); |
| 89 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 90 return true; |
| 91 } |
| 92 |
| 93 void UsbControlTransferFunction::Work() { |
| 94 UsbDeviceResource* const device = controller()->GetUsbDeviceResource( |
| 95 parameters_->device.handle); |
| 96 if (device) { |
| 97 device->ControlTransfer(parameters_->transfer_info); |
| 98 } |
| 99 } |
| 100 |
| 101 bool UsbControlTransferFunction::Respond() { |
| 102 return true; |
| 103 } |
| 104 |
| 105 bool UsbBulkTransferFunction::Prepare() { |
| 106 parameters_ = BulkTransfer::Params::Create(*args_); |
| 107 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 108 return true; |
| 109 } |
| 110 |
| 111 UsbBulkTransferFunction::UsbBulkTransferFunction() {} |
| 112 |
| 113 UsbBulkTransferFunction::~UsbBulkTransferFunction() {} |
| 114 |
| 115 void UsbBulkTransferFunction::Work() { |
| 116 UsbDeviceResource* const device = controller()->GetUsbDeviceResource( |
| 117 parameters_->device.handle); |
| 118 if (device) { |
| 119 device->BulkTransfer(parameters_->transfer_info); |
| 120 } |
| 121 } |
| 122 |
| 123 bool UsbBulkTransferFunction::Respond() { |
| 124 return true; |
| 125 } |
| 126 |
| 127 UsbInterruptTransferFunction::UsbInterruptTransferFunction() {} |
| 128 |
| 129 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() {} |
| 130 |
| 131 bool UsbInterruptTransferFunction::Prepare() { |
| 132 parameters_ = InterruptTransfer::Params::Create(*args_); |
| 133 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 134 return true; |
| 135 } |
| 136 |
| 137 void UsbInterruptTransferFunction::Work() { |
| 138 UsbDeviceResource* const device = controller()->GetUsbDeviceResource( |
| 139 parameters_->device.handle); |
| 140 if (device) { |
| 141 device->InterruptTransfer(parameters_->transfer_info); |
| 142 } |
| 143 } |
| 144 |
| 145 bool UsbInterruptTransferFunction::Respond() { |
| 146 return true; |
| 147 } |
| 148 |
| 149 } // namespace extensions |
OLD | NEW |