• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef EXTENSIONS_BROWSER_API_USB_USB_API_H_
6 #define EXTENSIONS_BROWSER_API_USB_USB_API_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "device/usb/usb_device.h"
14 #include "device/usb/usb_device_filter.h"
15 #include "device/usb/usb_device_handle.h"
16 #include "extensions/browser/api/api_resource_manager.h"
17 #include "extensions/browser/api/async_api_function.h"
18 #include "extensions/common/api/usb.h"
19 #include "net/base/io_buffer.h"
20 
21 namespace extensions {
22 
23 class UsbDeviceResource;
24 
25 class UsbAsyncApiFunction : public AsyncApiFunction {
26  public:
27   UsbAsyncApiFunction();
28 
29  protected:
30   virtual ~UsbAsyncApiFunction();
31 
32   virtual bool PrePrepare() OVERRIDE;
33   virtual bool Respond() OVERRIDE;
34 
35   static void CreateDeviceFilter(
36       const extensions::core_api::usb::DeviceFilter& input,
37       device::UsbDeviceFilter* output);
38 
39   bool HasDevicePermission(scoped_refptr<device::UsbDevice> device);
40 
41   scoped_refptr<device::UsbDevice> GetDeviceOrCompleteWithError(
42       const extensions::core_api::usb::Device& input_device);
43 
44   scoped_refptr<device::UsbDeviceHandle> GetDeviceHandleOrCompleteWithError(
45       const extensions::core_api::usb::ConnectionHandle& input_device_handle);
46 
47   void RemoveUsbDeviceResource(int api_resource_id);
48 
49   void CompleteWithError(const std::string& error);
50 
51   ApiResourceManager<UsbDeviceResource>* manager_;
52 };
53 
54 class UsbAsyncApiTransferFunction : public UsbAsyncApiFunction {
55  protected:
56   UsbAsyncApiTransferFunction();
57   virtual ~UsbAsyncApiTransferFunction();
58 
59   bool ConvertDirectionSafely(const extensions::core_api::usb::Direction& input,
60                               device::UsbEndpointDirection* output);
61   bool ConvertRequestTypeSafely(
62       const extensions::core_api::usb::RequestType& input,
63       device::UsbDeviceHandle::TransferRequestType* output);
64   bool ConvertRecipientSafely(
65       const extensions::core_api::usb::Recipient& input,
66       device::UsbDeviceHandle::TransferRecipient* output);
67 
68   void OnCompleted(device::UsbTransferStatus status,
69                    scoped_refptr<net::IOBuffer> data,
70                    size_t length);
71 };
72 
73 class UsbFindDevicesFunction : public UsbAsyncApiFunction {
74  public:
75   DECLARE_EXTENSION_FUNCTION("usb.findDevices", USB_FINDDEVICES)
76 
77   UsbFindDevicesFunction();
78 
79  protected:
80   virtual ~UsbFindDevicesFunction();
81 
82   virtual bool Prepare() OVERRIDE;
83   virtual void AsyncWorkStart() OVERRIDE;
84 
85  private:
86   void OpenDevices(
87       scoped_ptr<std::vector<scoped_refptr<device::UsbDevice> > > devices);
88 
89   std::vector<scoped_refptr<device::UsbDeviceHandle> > device_handles_;
90   scoped_ptr<extensions::core_api::usb::FindDevices::Params> parameters_;
91 };
92 
93 class UsbGetDevicesFunction : public UsbAsyncApiFunction {
94  public:
95   DECLARE_EXTENSION_FUNCTION("usb.getDevices", USB_GETDEVICES)
96 
97   UsbGetDevicesFunction();
98 
99   virtual bool Prepare() OVERRIDE;
100   virtual void AsyncWorkStart() OVERRIDE;
101 
102  protected:
103   virtual ~UsbGetDevicesFunction();
104 
105  private:
106   void EnumerationCompletedFileThread(
107       scoped_ptr<std::vector<scoped_refptr<device::UsbDevice> > > devices);
108 
109   scoped_ptr<extensions::core_api::usb::GetDevices::Params> parameters_;
110 };
111 
112 class UsbRequestAccessFunction : public UsbAsyncApiFunction {
113  public:
114   DECLARE_EXTENSION_FUNCTION("usb.requestAccess", USB_REQUESTACCESS)
115 
116   UsbRequestAccessFunction();
117 
118   virtual bool Prepare() OVERRIDE;
119   virtual void AsyncWorkStart() OVERRIDE;
120 
121  protected:
122   virtual ~UsbRequestAccessFunction();
123 
124   void OnCompleted(bool success);
125 
126  private:
127   scoped_ptr<extensions::core_api::usb::RequestAccess::Params> parameters_;
128 };
129 
130 class UsbOpenDeviceFunction : public UsbAsyncApiFunction {
131  public:
132   DECLARE_EXTENSION_FUNCTION("usb.openDevice", USB_OPENDEVICE)
133 
134   UsbOpenDeviceFunction();
135 
136   virtual bool Prepare() OVERRIDE;
137   virtual void AsyncWorkStart() OVERRIDE;
138 
139  protected:
140   virtual ~UsbOpenDeviceFunction();
141 
142  private:
143   scoped_refptr<device::UsbDeviceHandle> handle_;
144   scoped_ptr<extensions::core_api::usb::OpenDevice::Params> parameters_;
145 };
146 
147 class UsbGetConfigurationFunction : public UsbAsyncApiFunction {
148  public:
149   DECLARE_EXTENSION_FUNCTION("usb.getConfiguration", USB_GETCONFIGURATION)
150 
151   UsbGetConfigurationFunction();
152 
153  protected:
154   virtual ~UsbGetConfigurationFunction();
155 
156   virtual bool Prepare() OVERRIDE;
157   virtual void AsyncWorkStart() OVERRIDE;
158 
159  private:
160   scoped_ptr<extensions::core_api::usb::GetConfiguration::Params> parameters_;
161 };
162 
163 class UsbListInterfacesFunction : public UsbAsyncApiFunction {
164  public:
165   DECLARE_EXTENSION_FUNCTION("usb.listInterfaces", USB_LISTINTERFACES)
166 
167   UsbListInterfacesFunction();
168 
169  protected:
170   virtual ~UsbListInterfacesFunction();
171 
172   virtual bool Prepare() OVERRIDE;
173   virtual void AsyncWorkStart() OVERRIDE;
174 
175  private:
176   scoped_ptr<extensions::core_api::usb::ListInterfaces::Params> parameters_;
177 };
178 
179 class UsbCloseDeviceFunction : public UsbAsyncApiFunction {
180  public:
181   DECLARE_EXTENSION_FUNCTION("usb.closeDevice", USB_CLOSEDEVICE)
182 
183   UsbCloseDeviceFunction();
184 
185  protected:
186   virtual ~UsbCloseDeviceFunction();
187 
188   virtual bool Prepare() OVERRIDE;
189   virtual void AsyncWorkStart() OVERRIDE;
190 
191  private:
192   scoped_ptr<extensions::core_api::usb::CloseDevice::Params> parameters_;
193 };
194 
195 class UsbClaimInterfaceFunction : public UsbAsyncApiFunction {
196  public:
197   DECLARE_EXTENSION_FUNCTION("usb.claimInterface", USB_CLAIMINTERFACE)
198 
199   UsbClaimInterfaceFunction();
200 
201  protected:
202   virtual ~UsbClaimInterfaceFunction();
203 
204   virtual bool Prepare() OVERRIDE;
205   virtual void AsyncWorkStart() OVERRIDE;
206 
207  private:
208   scoped_ptr<extensions::core_api::usb::ClaimInterface::Params> parameters_;
209 };
210 
211 class UsbReleaseInterfaceFunction : public UsbAsyncApiFunction {
212  public:
213   DECLARE_EXTENSION_FUNCTION("usb.releaseInterface", USB_RELEASEINTERFACE)
214 
215   UsbReleaseInterfaceFunction();
216 
217  protected:
218   virtual ~UsbReleaseInterfaceFunction();
219 
220   virtual bool Prepare() OVERRIDE;
221   virtual void AsyncWorkStart() OVERRIDE;
222 
223  private:
224   scoped_ptr<extensions::core_api::usb::ReleaseInterface::Params> parameters_;
225 };
226 
227 class UsbSetInterfaceAlternateSettingFunction : public UsbAsyncApiFunction {
228  public:
229   DECLARE_EXTENSION_FUNCTION("usb.setInterfaceAlternateSetting",
230                              USB_SETINTERFACEALTERNATESETTING)
231 
232   UsbSetInterfaceAlternateSettingFunction();
233 
234  private:
235   virtual ~UsbSetInterfaceAlternateSettingFunction();
236 
237   virtual bool Prepare() OVERRIDE;
238   virtual void AsyncWorkStart() OVERRIDE;
239 
240   scoped_ptr<extensions::core_api::usb::SetInterfaceAlternateSetting::Params>
241       parameters_;
242 };
243 
244 class UsbControlTransferFunction : public UsbAsyncApiTransferFunction {
245  public:
246   DECLARE_EXTENSION_FUNCTION("usb.controlTransfer", USB_CONTROLTRANSFER)
247 
248   UsbControlTransferFunction();
249 
250  protected:
251   virtual ~UsbControlTransferFunction();
252 
253   virtual bool Prepare() OVERRIDE;
254   virtual void AsyncWorkStart() OVERRIDE;
255 
256  private:
257   scoped_ptr<extensions::core_api::usb::ControlTransfer::Params> parameters_;
258 };
259 
260 class UsbBulkTransferFunction : public UsbAsyncApiTransferFunction {
261  public:
262   DECLARE_EXTENSION_FUNCTION("usb.bulkTransfer", USB_BULKTRANSFER)
263 
264   UsbBulkTransferFunction();
265 
266  protected:
267   virtual ~UsbBulkTransferFunction();
268 
269   virtual bool Prepare() OVERRIDE;
270   virtual void AsyncWorkStart() OVERRIDE;
271 
272  private:
273   scoped_ptr<extensions::core_api::usb::BulkTransfer::Params> parameters_;
274 };
275 
276 class UsbInterruptTransferFunction : public UsbAsyncApiTransferFunction {
277  public:
278   DECLARE_EXTENSION_FUNCTION("usb.interruptTransfer", USB_INTERRUPTTRANSFER)
279 
280   UsbInterruptTransferFunction();
281 
282  protected:
283   virtual ~UsbInterruptTransferFunction();
284 
285   virtual bool Prepare() OVERRIDE;
286   virtual void AsyncWorkStart() OVERRIDE;
287 
288  private:
289   scoped_ptr<extensions::core_api::usb::InterruptTransfer::Params> parameters_;
290 };
291 
292 class UsbIsochronousTransferFunction : public UsbAsyncApiTransferFunction {
293  public:
294   DECLARE_EXTENSION_FUNCTION("usb.isochronousTransfer", USB_ISOCHRONOUSTRANSFER)
295 
296   UsbIsochronousTransferFunction();
297 
298  protected:
299   virtual ~UsbIsochronousTransferFunction();
300 
301   virtual bool Prepare() OVERRIDE;
302   virtual void AsyncWorkStart() OVERRIDE;
303 
304  private:
305   scoped_ptr<extensions::core_api::usb::IsochronousTransfer::Params>
306       parameters_;
307 };
308 
309 class UsbResetDeviceFunction : public UsbAsyncApiFunction {
310  public:
311   DECLARE_EXTENSION_FUNCTION("usb.resetDevice", USB_RESETDEVICE)
312 
313   UsbResetDeviceFunction();
314 
315  protected:
316   virtual ~UsbResetDeviceFunction();
317 
318   virtual bool Prepare() OVERRIDE;
319   virtual void AsyncWorkStart() OVERRIDE;
320 
321  private:
322   scoped_ptr<extensions::core_api::usb::ResetDevice::Params> parameters_;
323 };
324 }  // namespace extensions
325 
326 #endif  // EXTENSIONS_BROWSER_API_USB_USB_API_H_
327