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 bool UsbFindDeviceFunction::Prepare() { |
| 29 parameters_ = FindDevice::Params::Create(*args_); |
| 30 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 31 event_notifier_ = CreateEventNotifier(ExtractSrcId(2)); |
| 32 return true; |
| 33 } |
| 34 |
| 35 void UsbFindDeviceFunction::Work() { |
| 36 UsbService* const service = |
| 37 UsbServiceFactory::GetInstance()->GetForProfile(profile()); |
| 38 DCHECK(service) << "No UsbService associated with profile."; |
| 39 |
| 40 UsbDevice* const device = service->FindDevice(parameters_->vendor_id, |
| 41 parameters_->product_id); |
| 42 if (!device) { |
| 43 result_.reset(base::Value::CreateNullValue()); |
| 44 return; |
| 45 } |
| 46 |
| 47 UsbDeviceResource* const resource = new UsbDeviceResource(event_notifier_, |
| 48 device); |
| 49 |
| 50 Device result; |
| 51 result.handle = controller()->AddAPIResource(resource); |
| 52 result.vendor_id = parameters_->vendor_id; |
| 53 result.product_id = parameters_->product_id; |
| 54 result_ = result.ToValue(); |
| 55 } |
| 56 |
| 57 bool UsbFindDeviceFunction::Respond() { |
| 58 return true; |
| 59 } |
| 60 |
| 61 bool UsbCloseDeviceFunction::Prepare() { |
| 62 parameters_ = CloseDevice::Params::Create(*args_); |
| 63 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 64 return true; |
| 65 } |
| 66 |
| 67 void UsbCloseDeviceFunction::Work() { |
| 68 controller()->RemoveAPIResource(parameters_->device.handle); |
| 69 } |
| 70 |
| 71 bool UsbCloseDeviceFunction::Respond() { |
| 72 return true; |
| 73 } |
| 74 |
| 75 bool UsbControlTransferFunction::Prepare() { |
| 76 parameters_ = ControlTransfer::Params::Create(*args_); |
| 77 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 78 return true; |
| 79 } |
| 80 |
| 81 void UsbControlTransferFunction::Work() { |
| 82 UsbDeviceResource* const device = controller()->GetUsbDeviceResource( |
| 83 parameters_->device.handle); |
| 84 if (device) { |
| 85 device->ControlTransfer(parameters_->transfer_info); |
| 86 } |
| 87 } |
| 88 |
| 89 bool UsbControlTransferFunction::Respond() { |
| 90 return true; |
| 91 } |
| 92 |
| 93 bool UsbBulkTransferFunction::Prepare() { |
| 94 parameters_ = BulkTransfer::Params::Create(*args_); |
| 95 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 96 return true; |
| 97 } |
| 98 |
| 99 void UsbBulkTransferFunction::Work() { |
| 100 UsbDeviceResource* const device = controller()->GetUsbDeviceResource( |
| 101 parameters_->device.handle); |
| 102 if (device) { |
| 103 device->BulkTransfer(parameters_->transfer_info); |
| 104 } |
| 105 } |
| 106 |
| 107 bool UsbBulkTransferFunction::Respond() { |
| 108 return true; |
| 109 } |
| 110 |
| 111 bool UsbInterruptTransferFunction::Prepare() { |
| 112 parameters_ = InterruptTransfer::Params::Create(*args_); |
| 113 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 114 return true; |
| 115 } |
| 116 |
| 117 void UsbInterruptTransferFunction::Work() { |
| 118 UsbDeviceResource* const device = controller()->GetUsbDeviceResource( |
| 119 parameters_->device.handle); |
| 120 if (device) { |
| 121 device->InterruptTransfer(parameters_->transfer_info); |
| 122 } |
| 123 } |
| 124 |
| 125 bool UsbInterruptTransferFunction::Respond() { |
| 126 return true; |
| 127 } |
| 128 |
| 129 } // namespace extensions |
OLD | NEW |