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.
5 #ifndef EXTENSIONS_BROWSER_API_HID_HID_API_H_
6 #define EXTENSIONS_BROWSER_API_HID_HID_API_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "extensions/browser/api/api_resource_manager.h"
13 #include "extensions/browser/api/hid/hid_connection_resource.h"
14 #include "extensions/browser/api/hid/hid_device_manager.h"
15 #include "extensions/browser/extension_function.h"
16 #include "extensions/common/api/hid.h"
28 namespace extensions
{
30 class DevicePermissionsPrompt
;
32 class HidGetDevicesFunction
: public UIThreadExtensionFunction
{
34 DECLARE_EXTENSION_FUNCTION("hid.getDevices", HID_GETDEVICES
);
36 HidGetDevicesFunction();
39 ~HidGetDevicesFunction() override
;
42 ResponseAction
Run() override
;
44 void OnEnumerationComplete(scoped_ptr
<base::ListValue
> devices
);
46 DISALLOW_COPY_AND_ASSIGN(HidGetDevicesFunction
);
49 class HidGetUserSelectedDevicesFunction
: public UIThreadExtensionFunction
{
51 DECLARE_EXTENSION_FUNCTION("hid.getUserSelectedDevices",
52 HID_GETUSERSELECTEDDEVICES
)
54 HidGetUserSelectedDevicesFunction();
57 ~HidGetUserSelectedDevicesFunction() override
;
60 ResponseAction
Run() override
;
63 const std::vector
<scoped_refptr
<device::HidDeviceInfo
>>& devices
);
65 HidDeviceManager
* device_manager_
;
66 scoped_ptr
<DevicePermissionsPrompt
> prompt_
;
68 DISALLOW_COPY_AND_ASSIGN(HidGetUserSelectedDevicesFunction
);
71 class HidConnectFunction
: public UIThreadExtensionFunction
{
73 DECLARE_EXTENSION_FUNCTION("hid.connect", HID_CONNECT
);
78 ~HidConnectFunction() override
;
81 ResponseAction
Run() override
;
83 void OnConnectComplete(scoped_refptr
<device::HidConnection
> connection
);
85 ApiResourceManager
<HidConnectionResource
>* connection_manager_
;
87 DISALLOW_COPY_AND_ASSIGN(HidConnectFunction
);
90 class HidDisconnectFunction
: public UIThreadExtensionFunction
{
92 DECLARE_EXTENSION_FUNCTION("hid.disconnect", HID_DISCONNECT
);
94 HidDisconnectFunction();
97 ~HidDisconnectFunction() override
;
100 ResponseAction
Run() override
;
102 DISALLOW_COPY_AND_ASSIGN(HidDisconnectFunction
);
105 // Base class for extension functions that start some asynchronous work after
106 // looking up a HidConnection.
107 class HidConnectionIoFunction
: public UIThreadExtensionFunction
{
109 HidConnectionIoFunction();
112 ~HidConnectionIoFunction() override
;
114 virtual bool ValidateParameters() = 0;
115 virtual void StartWork(device::HidConnection
* connection
) = 0;
117 void set_connection_id(int connection_id
) { connection_id_
= connection_id
; }
120 // ExtensionFunction:
121 ResponseAction
Run() override
;
126 class HidReceiveFunction
: public HidConnectionIoFunction
{
128 DECLARE_EXTENSION_FUNCTION("hid.receive", HID_RECEIVE
);
130 HidReceiveFunction();
133 ~HidReceiveFunction() override
;
135 // HidConnectionIoFunction:
136 bool ValidateParameters() override
;
137 void StartWork(device::HidConnection
* connection
) override
;
139 void OnFinished(bool success
,
140 scoped_refptr
<net::IOBuffer
> buffer
,
143 scoped_ptr
<api::hid::Receive::Params
> parameters_
;
145 DISALLOW_COPY_AND_ASSIGN(HidReceiveFunction
);
148 class HidSendFunction
: public HidConnectionIoFunction
{
150 DECLARE_EXTENSION_FUNCTION("hid.send", HID_SEND
);
155 ~HidSendFunction() override
;
157 // HidConnectionIoFunction:
158 bool ValidateParameters() override
;
159 void StartWork(device::HidConnection
* connection
) override
;
161 void OnFinished(bool success
);
163 scoped_ptr
<api::hid::Send::Params
> parameters_
;
165 DISALLOW_COPY_AND_ASSIGN(HidSendFunction
);
168 class HidReceiveFeatureReportFunction
: public HidConnectionIoFunction
{
170 DECLARE_EXTENSION_FUNCTION("hid.receiveFeatureReport",
171 HID_RECEIVEFEATUREREPORT
);
173 HidReceiveFeatureReportFunction();
176 ~HidReceiveFeatureReportFunction() override
;
178 // HidConnectionIoFunction:
179 bool ValidateParameters() override
;
180 void StartWork(device::HidConnection
* connection
) override
;
182 void OnFinished(bool success
,
183 scoped_refptr
<net::IOBuffer
> buffer
,
186 scoped_ptr
<api::hid::ReceiveFeatureReport::Params
> parameters_
;
188 DISALLOW_COPY_AND_ASSIGN(HidReceiveFeatureReportFunction
);
191 class HidSendFeatureReportFunction
: public HidConnectionIoFunction
{
193 DECLARE_EXTENSION_FUNCTION("hid.sendFeatureReport", HID_SENDFEATUREREPORT
);
195 HidSendFeatureReportFunction();
198 ~HidSendFeatureReportFunction() override
;
200 // HidConnectionIoFunction:
201 bool ValidateParameters() override
;
202 void StartWork(device::HidConnection
* connection
) override
;
204 void OnFinished(bool success
);
206 scoped_ptr
<api::hid::SendFeatureReport::Params
> parameters_
;
208 DISALLOW_COPY_AND_ASSIGN(HidSendFeatureReportFunction
);
211 } // namespace extensions
213 #endif // EXTENSIONS_BROWSER_API_HID_HID_API_H_