| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/usb/usb_api.h" | 5 #include "chrome/browser/extensions/api/usb/usb_api.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "chrome/browser/extensions/api/usb/usb_device_resource.h" | 10 #include "chrome/browser/extensions/api/usb/usb_device_resource.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 } | 38 } |
| 39 | 39 |
| 40 UsbAsyncApiFunction::~UsbAsyncApiFunction() { | 40 UsbAsyncApiFunction::~UsbAsyncApiFunction() { |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool UsbAsyncApiFunction::PrePrepare() { | 43 bool UsbAsyncApiFunction::PrePrepare() { |
| 44 manager_ = ExtensionSystem::Get(profile())->usb_device_resource_manager(); | 44 manager_ = ExtensionSystem::Get(profile())->usb_device_resource_manager(); |
| 45 return manager_ != NULL; | 45 return manager_ != NULL; |
| 46 } | 46 } |
| 47 | 47 |
| 48 UsbDeviceResource* UsbAsyncApiFunction::GetUsbDeviceResource( |
| 49 int api_resource_id) { |
| 50 return manager_->Get(extension_->id(), api_resource_id); |
| 51 } |
| 52 |
| 53 void UsbAsyncApiFunction::RemoveUsbDeviceResource(int api_resource_id) { |
| 54 manager_->Remove(extension_->id(), api_resource_id); |
| 55 } |
| 56 |
| 48 UsbFindDeviceFunction::UsbFindDeviceFunction() : event_notifier_(NULL) {} | 57 UsbFindDeviceFunction::UsbFindDeviceFunction() : event_notifier_(NULL) {} |
| 49 | 58 |
| 50 UsbFindDeviceFunction::~UsbFindDeviceFunction() {} | 59 UsbFindDeviceFunction::~UsbFindDeviceFunction() {} |
| 51 | 60 |
| 52 void UsbFindDeviceFunction::SetDeviceForTest(UsbDevice* device) { | 61 void UsbFindDeviceFunction::SetDeviceForTest(UsbDevice* device) { |
| 53 device_for_test_ = device; | 62 device_for_test_ = device; |
| 54 } | 63 } |
| 55 | 64 |
| 56 bool UsbFindDeviceFunction::Prepare() { | 65 bool UsbFindDeviceFunction::Prepare() { |
| 57 parameters_ = FindDevice::Params::Create(*args_); | 66 parameters_ = FindDevice::Params::Create(*args_); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 69 profile()); | 78 profile()); |
| 70 device = service->FindDevice(parameters_->vendor_id, | 79 device = service->FindDevice(parameters_->vendor_id, |
| 71 parameters_->product_id); | 80 parameters_->product_id); |
| 72 } | 81 } |
| 73 | 82 |
| 74 if (!device) { | 83 if (!device) { |
| 75 SetResult(base::Value::CreateNullValue()); | 84 SetResult(base::Value::CreateNullValue()); |
| 76 return; | 85 return; |
| 77 } | 86 } |
| 78 | 87 |
| 79 UsbDeviceResource* const resource = new UsbDeviceResource(event_notifier_, | 88 UsbDeviceResource* const resource = new UsbDeviceResource(extension_->id(), |
| 89 event_notifier_, |
| 80 device); | 90 device); |
| 81 | 91 |
| 82 Device result; | 92 Device result; |
| 83 result.handle = manager_->Add(resource); | 93 result.handle = manager_->Add(resource); |
| 84 result.vendor_id = parameters_->vendor_id; | 94 result.vendor_id = parameters_->vendor_id; |
| 85 result.product_id = parameters_->product_id; | 95 result.product_id = parameters_->product_id; |
| 86 results_ = FindDevice::Results::Create(result); | 96 results_ = FindDevice::Results::Create(result); |
| 87 } | 97 } |
| 88 | 98 |
| 89 bool UsbFindDeviceFunction::Respond() { | 99 bool UsbFindDeviceFunction::Respond() { |
| 90 return true; | 100 return true; |
| 91 } | 101 } |
| 92 | 102 |
| 93 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} | 103 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} |
| 94 | 104 |
| 95 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} | 105 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} |
| 96 | 106 |
| 97 bool UsbCloseDeviceFunction::Prepare() { | 107 bool UsbCloseDeviceFunction::Prepare() { |
| 98 parameters_ = CloseDevice::Params::Create(*args_); | 108 parameters_ = CloseDevice::Params::Create(*args_); |
| 99 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 109 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 100 return true; | 110 return true; |
| 101 } | 111 } |
| 102 | 112 |
| 103 void UsbCloseDeviceFunction::Work() { | 113 void UsbCloseDeviceFunction::Work() { |
| 104 UsbDeviceResource* const device = manager_->Get(parameters_->device.handle); | 114 UsbDeviceResource* const device = GetUsbDeviceResource( |
| 115 parameters_->device.handle); |
| 116 |
| 105 if (device) | 117 if (device) |
| 106 device->Close(); | 118 device->Close(); |
| 107 | 119 |
| 108 manager_->Remove(parameters_->device.handle); | 120 RemoveUsbDeviceResource(parameters_->device.handle); |
| 109 } | 121 } |
| 110 | 122 |
| 111 bool UsbCloseDeviceFunction::Respond() { | 123 bool UsbCloseDeviceFunction::Respond() { |
| 112 return true; | 124 return true; |
| 113 } | 125 } |
| 114 | 126 |
| 115 UsbControlTransferFunction::UsbControlTransferFunction() {} | 127 UsbControlTransferFunction::UsbControlTransferFunction() {} |
| 116 | 128 |
| 117 UsbControlTransferFunction::~UsbControlTransferFunction() {} | 129 UsbControlTransferFunction::~UsbControlTransferFunction() {} |
| 118 | 130 |
| 119 bool UsbControlTransferFunction::Prepare() { | 131 bool UsbControlTransferFunction::Prepare() { |
| 120 parameters_ = ControlTransfer::Params::Create(*args_); | 132 parameters_ = ControlTransfer::Params::Create(*args_); |
| 121 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 133 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 122 return true; | 134 return true; |
| 123 } | 135 } |
| 124 | 136 |
| 125 void UsbControlTransferFunction::Work() { | 137 void UsbControlTransferFunction::Work() { |
| 126 UsbDeviceResource* const device = manager_->Get(parameters_->device.handle); | 138 UsbDeviceResource* const device = GetUsbDeviceResource( |
| 139 parameters_->device.handle); |
| 140 |
| 127 if (device) { | 141 if (device) { |
| 128 device->ControlTransfer(parameters_->transfer_info); | 142 device->ControlTransfer(parameters_->transfer_info); |
| 129 } | 143 } |
| 130 } | 144 } |
| 131 | 145 |
| 132 bool UsbControlTransferFunction::Respond() { | 146 bool UsbControlTransferFunction::Respond() { |
| 133 return true; | 147 return true; |
| 134 } | 148 } |
| 135 | 149 |
| 136 bool UsbBulkTransferFunction::Prepare() { | 150 bool UsbBulkTransferFunction::Prepare() { |
| 137 parameters_ = BulkTransfer::Params::Create(*args_); | 151 parameters_ = BulkTransfer::Params::Create(*args_); |
| 138 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 152 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 139 return true; | 153 return true; |
| 140 } | 154 } |
| 141 | 155 |
| 142 UsbBulkTransferFunction::UsbBulkTransferFunction() {} | 156 UsbBulkTransferFunction::UsbBulkTransferFunction() {} |
| 143 | 157 |
| 144 UsbBulkTransferFunction::~UsbBulkTransferFunction() {} | 158 UsbBulkTransferFunction::~UsbBulkTransferFunction() {} |
| 145 | 159 |
| 146 void UsbBulkTransferFunction::Work() { | 160 void UsbBulkTransferFunction::Work() { |
| 147 UsbDeviceResource* const device = manager_->Get(parameters_->device.handle); | 161 UsbDeviceResource* const device = GetUsbDeviceResource( |
| 162 parameters_->device.handle); |
| 163 |
| 148 if (device) { | 164 if (device) { |
| 149 device->BulkTransfer(parameters_->transfer_info); | 165 device->BulkTransfer(parameters_->transfer_info); |
| 150 } | 166 } |
| 151 } | 167 } |
| 152 | 168 |
| 153 bool UsbBulkTransferFunction::Respond() { | 169 bool UsbBulkTransferFunction::Respond() { |
| 154 return true; | 170 return true; |
| 155 } | 171 } |
| 156 | 172 |
| 157 UsbInterruptTransferFunction::UsbInterruptTransferFunction() {} | 173 UsbInterruptTransferFunction::UsbInterruptTransferFunction() {} |
| 158 | 174 |
| 159 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() {} | 175 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() {} |
| 160 | 176 |
| 161 bool UsbInterruptTransferFunction::Prepare() { | 177 bool UsbInterruptTransferFunction::Prepare() { |
| 162 parameters_ = InterruptTransfer::Params::Create(*args_); | 178 parameters_ = InterruptTransfer::Params::Create(*args_); |
| 163 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 179 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 164 return true; | 180 return true; |
| 165 } | 181 } |
| 166 | 182 |
| 167 void UsbInterruptTransferFunction::Work() { | 183 void UsbInterruptTransferFunction::Work() { |
| 168 UsbDeviceResource* const device = manager_->Get(parameters_->device.handle); | 184 UsbDeviceResource* const device = GetUsbDeviceResource( |
| 185 parameters_->device.handle); |
| 186 |
| 169 if (device) { | 187 if (device) { |
| 170 device->InterruptTransfer(parameters_->transfer_info); | 188 device->InterruptTransfer(parameters_->transfer_info); |
| 171 } | 189 } |
| 172 } | 190 } |
| 173 | 191 |
| 174 bool UsbInterruptTransferFunction::Respond() { | 192 bool UsbInterruptTransferFunction::Respond() { |
| 175 return true; | 193 return true; |
| 176 } | 194 } |
| 177 | 195 |
| 178 UsbIsochronousTransferFunction::UsbIsochronousTransferFunction() {} | 196 UsbIsochronousTransferFunction::UsbIsochronousTransferFunction() {} |
| 179 | 197 |
| 180 UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() {} | 198 UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() {} |
| 181 | 199 |
| 182 bool UsbIsochronousTransferFunction::Prepare() { | 200 bool UsbIsochronousTransferFunction::Prepare() { |
| 183 parameters_ = IsochronousTransfer::Params::Create(*args_); | 201 parameters_ = IsochronousTransfer::Params::Create(*args_); |
| 184 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 202 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 185 return true; | 203 return true; |
| 186 } | 204 } |
| 187 | 205 |
| 188 void UsbIsochronousTransferFunction::Work() { | 206 void UsbIsochronousTransferFunction::Work() { |
| 189 UsbDeviceResource* const device = manager_->Get(parameters_->device.handle); | 207 UsbDeviceResource* const device = GetUsbDeviceResource( |
| 208 parameters_->device.handle); |
| 209 |
| 190 if (device) { | 210 if (device) { |
| 191 device->IsochronousTransfer(parameters_->transfer_info); | 211 device->IsochronousTransfer(parameters_->transfer_info); |
| 192 } | 212 } |
| 193 } | 213 } |
| 194 | 214 |
| 195 bool UsbIsochronousTransferFunction::Respond() { | 215 bool UsbIsochronousTransferFunction::Respond() { |
| 196 return true; | 216 return true; |
| 197 } | 217 } |
| 198 | 218 |
| 199 } // namespace extensions | 219 } // namespace extensions |
| OLD | NEW |