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.h" | |
14 #include "chrome/common/extensions/api/experimental.usb.h" | |
15 | |
16 namespace BulkTransfer = extensions::api::experimental_usb::BulkTransfer; | |
17 namespace CloseDevice = extensions::api::experimental_usb::CloseDevice; | |
18 namespace ControlTransfer = extensions::api::experimental_usb::ControlTransfer; | |
19 namespace FindDevice = extensions::api::experimental_usb::FindDevice; | |
20 namespace InterruptTransfer = | |
21 extensions::api::experimental_usb::InterruptTransfer; | |
22 using extensions::api::experimental_usb::Device; | |
23 using std::vector; | |
24 | |
25 namespace extensions { | |
26 | |
27 bool UsbFindDeviceFunction::Prepare() { | |
28 parameters_ = FindDevice::Params::Create(*args_); | |
29 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | |
30 | |
31 const int source_id = ExtractSrcId(2); | |
miket_OOO
2012/04/25 23:10:37
This temporary variable is a little confusing beca
Garret Kelly
2012/04/26 20:13:44
Done.
| |
32 event_notifier_ = CreateEventNotifier(source_id); | |
33 | |
34 return true; | |
35 } | |
36 | |
37 void UsbFindDeviceFunction::Work() { | |
38 UsbService *const service = profile()->GetUsbService(); | |
39 DCHECK(service) << "No UsbService associated with profile."; | |
40 | |
41 UsbDevice *const device = service->FindDevice(parameters_->vendor_id, | |
miket_OOO
2012/04/25 23:10:37
Reminder on */& whitespace.
Garret Kelly
2012/04/26 20:13:44
Done.
| |
42 parameters_->product_id); | |
43 if (!device) { | |
44 result_.reset(base::Value::CreateNullValue()); | |
45 return; | |
46 } | |
47 | |
48 UsbDeviceResource *const resource = new UsbDeviceResource(event_notifier_, | |
49 device); | |
50 | |
51 Device result; | |
52 result.handle = controller()->AddAPIResource(resource); | |
53 result.vendor_id = parameters_->vendor_id; | |
54 result.product_id = parameters_->product_id; | |
55 result_ = result.ToValue(); | |
56 } | |
57 | |
58 bool UsbFindDeviceFunction::Respond() { | |
59 return true; | |
60 } | |
61 | |
62 bool UsbCloseDeviceFunction::Prepare() { | |
63 parameters_ = CloseDevice::Params::Create(*args_); | |
64 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | |
65 return true; | |
66 } | |
67 | |
68 void UsbCloseDeviceFunction::Work() { | |
69 controller()->RemoveAPIResource(parameters_->device.handle); | |
70 } | |
71 | |
72 bool UsbCloseDeviceFunction::Respond() { | |
73 return true; | |
74 } | |
75 | |
76 bool UsbControlTransferFunction::Prepare() { | |
77 parameters_ = ControlTransfer::Params::Create(*args_); | |
78 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | |
79 return true; | |
80 } | |
81 | |
82 void UsbControlTransferFunction::Work() { | |
83 UsbDeviceResource *const device = controller()->GetUsbDeviceResource( | |
84 parameters_->device.handle); | |
85 if (device) { | |
86 device->ControlTransfer(parameters_->transfer_info); | |
87 } | |
88 } | |
89 | |
90 bool UsbControlTransferFunction::Respond() { | |
91 return true; | |
92 } | |
93 | |
94 bool UsbBulkTransferFunction::Prepare() { | |
95 parameters_ = BulkTransfer::Params::Create(*args_); | |
96 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | |
97 return true; | |
98 } | |
99 | |
100 void UsbBulkTransferFunction::Work() { | |
101 UsbDeviceResource *const device = controller()->GetUsbDeviceResource( | |
102 parameters_->device.handle); | |
103 if (device) { | |
104 device->BulkTransfer(parameters_->transfer_info); | |
105 } | |
106 } | |
107 | |
108 bool UsbBulkTransferFunction::Respond() { | |
109 return true; | |
110 } | |
111 | |
112 bool UsbInterruptTransferFunction::Prepare() { | |
113 parameters_ = InterruptTransfer::Params::Create(*args_); | |
114 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | |
115 return true; | |
116 } | |
117 | |
118 void UsbInterruptTransferFunction::Work() { | |
119 UsbDeviceResource *const device = controller()->GetUsbDeviceResource( | |
120 parameters_->device.handle); | |
121 if (device) { | |
122 device->InterruptTransfer(parameters_->transfer_info); | |
123 } | |
124 } | |
125 | |
126 bool UsbInterruptTransferFunction::Respond() { | |
127 return true; | |
128 } | |
129 | |
130 } // namespace extensions | |
OLD | NEW |