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_HID_HID_API_H_
6#define EXTENSIONS_BROWSER_API_HID_HID_API_H_
7
8#include <string>
9
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/async_api_function.h"
14#include "extensions/browser/api/hid/hid_connection_resource.h"
15#include "extensions/browser/api/hid/hid_device_manager.h"
16#include "extensions/common/api/hid.h"
17
18namespace net {
19class IOBuffer;
20}  // namespace net
21
22namespace extensions {
23
24class HidAsyncApiFunction : public AsyncApiFunction {
25 public:
26  HidAsyncApiFunction();
27
28  virtual bool PrePrepare() OVERRIDE;
29  virtual bool Respond() OVERRIDE;
30
31 protected:
32  virtual ~HidAsyncApiFunction();
33
34  HidConnectionResource* GetHidConnectionResource(int api_resource_id);
35  void RemoveHidConnectionResource(int api_resource_id);
36
37  void CompleteWithError(const std::string& error);
38
39  HidDeviceManager* device_manager_;
40  ApiResourceManager<HidConnectionResource>* connection_manager_;
41
42 private:
43  DISALLOW_COPY_AND_ASSIGN(HidAsyncApiFunction);
44};
45
46class HidGetDevicesFunction : public HidAsyncApiFunction {
47 public:
48  DECLARE_EXTENSION_FUNCTION("hid.getDevices", HID_GETDEVICES);
49
50  HidGetDevicesFunction();
51
52 protected:
53  virtual bool Prepare() OVERRIDE;
54  virtual void AsyncWorkStart() OVERRIDE;
55
56  virtual ~HidGetDevicesFunction();
57
58  scoped_ptr<core_api::hid::GetDevices::Params> parameters_;
59
60 private:
61  DISALLOW_COPY_AND_ASSIGN(HidGetDevicesFunction);
62};
63
64class HidConnectFunction : public HidAsyncApiFunction {
65 public:
66  DECLARE_EXTENSION_FUNCTION("hid.connect", HID_CONNECT);
67
68  HidConnectFunction();
69
70 protected:
71  virtual bool Prepare() OVERRIDE;
72  virtual void AsyncWorkStart() OVERRIDE;
73
74 private:
75  virtual ~HidConnectFunction();
76
77  scoped_ptr<core_api::hid::Connect::Params> parameters_;
78
79  DISALLOW_COPY_AND_ASSIGN(HidConnectFunction);
80};
81
82class HidDisconnectFunction : public HidAsyncApiFunction {
83 public:
84  DECLARE_EXTENSION_FUNCTION("hid.disconnect", HID_DISCONNECT);
85
86  HidDisconnectFunction();
87
88 protected:
89  virtual bool Prepare() OVERRIDE;
90  virtual void AsyncWorkStart() OVERRIDE;
91
92 private:
93  virtual ~HidDisconnectFunction();
94
95  scoped_ptr<core_api::hid::Disconnect::Params> parameters_;
96
97  DISALLOW_COPY_AND_ASSIGN(HidDisconnectFunction);
98};
99
100class HidReceiveFunction : public HidAsyncApiFunction {
101 public:
102  DECLARE_EXTENSION_FUNCTION("hid.receive", HID_RECEIVE);
103
104  HidReceiveFunction();
105
106 protected:
107  virtual bool Prepare() OVERRIDE;
108  virtual void AsyncWorkStart() OVERRIDE;
109
110 private:
111  virtual ~HidReceiveFunction();
112
113  void OnFinished(bool success,
114                  scoped_refptr<net::IOBuffer> buffer,
115                  size_t size);
116
117  scoped_ptr<core_api::hid::Receive::Params> parameters_;
118
119  DISALLOW_COPY_AND_ASSIGN(HidReceiveFunction);
120};
121
122class HidSendFunction : public HidAsyncApiFunction {
123 public:
124  DECLARE_EXTENSION_FUNCTION("hid.send", HID_SEND);
125
126  HidSendFunction();
127
128 protected:
129  virtual bool Prepare() OVERRIDE;
130  virtual void AsyncWorkStart() OVERRIDE;
131
132 private:
133  virtual ~HidSendFunction();
134
135  void OnFinished(bool success);
136
137  scoped_ptr<core_api::hid::Send::Params> parameters_;
138
139  DISALLOW_COPY_AND_ASSIGN(HidSendFunction);
140};
141
142class HidReceiveFeatureReportFunction : public HidAsyncApiFunction {
143 public:
144  DECLARE_EXTENSION_FUNCTION("hid.receiveFeatureReport",
145                             HID_RECEIVEFEATUREREPORT);
146
147  HidReceiveFeatureReportFunction();
148
149 protected:
150  virtual bool Prepare() OVERRIDE;
151  virtual void AsyncWorkStart() OVERRIDE;
152
153 private:
154  virtual ~HidReceiveFeatureReportFunction();
155
156  void OnFinished(bool success,
157                  scoped_refptr<net::IOBuffer> buffer,
158                  size_t size);
159
160  scoped_ptr<core_api::hid::ReceiveFeatureReport::Params> parameters_;
161
162  DISALLOW_COPY_AND_ASSIGN(HidReceiveFeatureReportFunction);
163};
164
165class HidSendFeatureReportFunction : public HidAsyncApiFunction {
166 public:
167  DECLARE_EXTENSION_FUNCTION("hid.sendFeatureReport", HID_SENDFEATUREREPORT);
168
169  HidSendFeatureReportFunction();
170
171 protected:
172  virtual bool Prepare() OVERRIDE;
173  virtual void AsyncWorkStart() OVERRIDE;
174
175 private:
176  virtual ~HidSendFeatureReportFunction();
177
178  void OnFinished(bool success);
179
180  scoped_ptr<core_api::hid::SendFeatureReport::Params> parameters_;
181
182  DISALLOW_COPY_AND_ASSIGN(HidSendFeatureReportFunction);
183};
184
185}  // namespace extensions
186
187#endif  // EXTENSIONS_BROWSER_API_HID_HID_API_H_
188