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_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_
6#define EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_
7
8#include <string>
9
10#include "base/containers/hash_tables.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "device/bluetooth/bluetooth_adapter.h"
14#include "extensions/browser/api/api_resource_manager.h"
15#include "extensions/browser/api/async_api_function.h"
16#include "extensions/browser/api/bluetooth_socket/bluetooth_api_socket.h"
17#include "extensions/browser/extension_function.h"
18#include "extensions/browser/extension_function_histogram_value.h"
19#include "extensions/common/api/bluetooth_socket.h"
20
21namespace device {
22class BluetoothSocket;
23}
24
25namespace net {
26class IOBuffer;
27}
28
29namespace extensions {
30
31namespace core_api {
32
33class BluetoothSocketEventDispatcher;
34
35// Asynchronous API function that performs its work on the BluetoothApiSocket
36// thread while providing methods to manage resources of that class. This
37// follows the pattern of AsyncApiFunction, but does not derive from it,
38// because BluetoothApiSocket methods must be called on the UI Thread.
39class BluetoothSocketAsyncApiFunction : public AsyncExtensionFunction {
40 public:
41  BluetoothSocketAsyncApiFunction();
42
43 protected:
44  virtual ~BluetoothSocketAsyncApiFunction();
45
46  // AsyncExtensionFunction:
47  virtual bool RunAsync() OVERRIDE;
48
49  bool PrePrepare();
50  bool Respond();
51  void AsyncWorkCompleted();
52
53  virtual bool Prepare() = 0;
54  virtual void Work();
55  virtual void AsyncWorkStart();
56
57  content::BrowserThread::ID work_thread_id() const;
58
59  int AddSocket(BluetoothApiSocket* socket);
60  BluetoothApiSocket* GetSocket(int api_resource_id);
61  void RemoveSocket(int api_resource_id);
62  base::hash_set<int>* GetSocketIds();
63
64 private:
65  ApiResourceManager<BluetoothApiSocket>* manager_;
66};
67
68class BluetoothSocketCreateFunction : public BluetoothSocketAsyncApiFunction {
69 public:
70  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.create", BLUETOOTHSOCKET_CREATE);
71
72  BluetoothSocketCreateFunction();
73
74 protected:
75  virtual ~BluetoothSocketCreateFunction();
76
77  // BluetoothSocketAsyncApiFunction:
78  virtual bool Prepare() OVERRIDE;
79  virtual void Work() OVERRIDE;
80
81 private:
82  scoped_ptr<bluetooth_socket::Create::Params> params_;
83};
84
85class BluetoothSocketUpdateFunction : public BluetoothSocketAsyncApiFunction {
86 public:
87  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.update", BLUETOOTHSOCKET_UPDATE);
88
89  BluetoothSocketUpdateFunction();
90
91 protected:
92  virtual ~BluetoothSocketUpdateFunction();
93
94  // BluetoothSocketAsyncApiFunction:
95  virtual bool Prepare() OVERRIDE;
96  virtual void Work() OVERRIDE;
97
98 private:
99  scoped_ptr<bluetooth_socket::Update::Params> params_;
100};
101
102class BluetoothSocketSetPausedFunction
103    : public BluetoothSocketAsyncApiFunction {
104 public:
105  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.setPaused",
106                             BLUETOOTHSOCKET_SETPAUSED);
107
108  BluetoothSocketSetPausedFunction();
109
110 protected:
111  virtual ~BluetoothSocketSetPausedFunction();
112
113  // BluetoothSocketAsyncApiFunction:
114  virtual bool Prepare() OVERRIDE;
115  virtual void Work() OVERRIDE;
116
117 private:
118  scoped_ptr<bluetooth_socket::SetPaused::Params> params_;
119  BluetoothSocketEventDispatcher* socket_event_dispatcher_;
120};
121
122class BluetoothSocketListenFunction : public BluetoothSocketAsyncApiFunction {
123 public:
124  BluetoothSocketListenFunction();
125
126  virtual bool CreateParams() = 0;
127  virtual void CreateService(
128      scoped_refptr<device::BluetoothAdapter> adapter,
129      const device::BluetoothUUID& uuid,
130      scoped_ptr<std::string> name,
131      const device::BluetoothAdapter::CreateServiceCallback& callback,
132      const device::BluetoothAdapter::CreateServiceErrorCallback&
133          error_callback) = 0;
134  virtual void CreateResults() = 0;
135
136  virtual int socket_id() const = 0;
137  virtual const std::string& uuid() const = 0;
138
139  // BluetoothSocketAsyncApiFunction:
140  virtual bool Prepare() OVERRIDE;
141  virtual void AsyncWorkStart() OVERRIDE;
142
143 protected:
144  virtual ~BluetoothSocketListenFunction();
145
146  virtual void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
147  virtual void OnCreateService(scoped_refptr<device::BluetoothSocket> socket);
148  virtual void OnCreateServiceError(const std::string& message);
149
150  BluetoothSocketEventDispatcher* socket_event_dispatcher_;
151};
152
153class BluetoothSocketListenUsingRfcommFunction
154    : public BluetoothSocketListenFunction {
155 public:
156  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingRfcomm",
157                             BLUETOOTHSOCKET_LISTENUSINGRFCOMM);
158
159  BluetoothSocketListenUsingRfcommFunction();
160
161  // BluetoothSocketListenFunction:
162  virtual int socket_id() const OVERRIDE;
163  virtual const std::string& uuid() const OVERRIDE;
164
165  virtual bool CreateParams() OVERRIDE;
166  virtual void CreateService(
167      scoped_refptr<device::BluetoothAdapter> adapter,
168      const device::BluetoothUUID& uuid,
169      scoped_ptr<std::string> name,
170      const device::BluetoothAdapter::CreateServiceCallback& callback,
171      const device::BluetoothAdapter::CreateServiceErrorCallback&
172          error_callback) OVERRIDE;
173  virtual void CreateResults() OVERRIDE;
174
175 protected:
176  virtual ~BluetoothSocketListenUsingRfcommFunction();
177
178 private:
179  scoped_ptr<bluetooth_socket::ListenUsingRfcomm::Params> params_;
180};
181
182class BluetoothSocketListenUsingL2capFunction
183    : public BluetoothSocketListenFunction {
184 public:
185  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingL2cap",
186                             BLUETOOTHSOCKET_LISTENUSINGL2CAP);
187
188  BluetoothSocketListenUsingL2capFunction();
189
190  // BluetoothSocketListenFunction:
191  virtual int socket_id() const OVERRIDE;
192  virtual const std::string& uuid() const OVERRIDE;
193
194  virtual bool CreateParams() OVERRIDE;
195  virtual void CreateService(
196      scoped_refptr<device::BluetoothAdapter> adapter,
197      const device::BluetoothUUID& uuid,
198      scoped_ptr<std::string> name,
199      const device::BluetoothAdapter::CreateServiceCallback& callback,
200      const device::BluetoothAdapter::CreateServiceErrorCallback&
201          error_callback) OVERRIDE;
202  virtual void CreateResults() OVERRIDE;
203
204 protected:
205  virtual ~BluetoothSocketListenUsingL2capFunction();
206
207 private:
208  scoped_ptr<bluetooth_socket::ListenUsingL2cap::Params> params_;
209};
210
211class BluetoothSocketAbstractConnectFunction :
212    public BluetoothSocketAsyncApiFunction {
213 public:
214  BluetoothSocketAbstractConnectFunction();
215
216 protected:
217  virtual ~BluetoothSocketAbstractConnectFunction();
218
219  // BluetoothSocketAsyncApiFunction:
220  virtual bool Prepare() OVERRIDE;
221  virtual void AsyncWorkStart() OVERRIDE;
222
223  // Subclasses should implement this method to connect to the service
224  // registered with |uuid| on the |device|.
225  virtual void ConnectToService(device::BluetoothDevice* device,
226                                const device::BluetoothUUID& uuid) = 0;
227
228  virtual void OnConnect(scoped_refptr<device::BluetoothSocket> socket);
229  virtual void OnConnectError(const std::string& message);
230
231 private:
232  virtual void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
233
234  scoped_ptr<bluetooth_socket::Connect::Params> params_;
235  BluetoothSocketEventDispatcher* socket_event_dispatcher_;
236};
237
238class BluetoothSocketConnectFunction :
239    public BluetoothSocketAbstractConnectFunction {
240 public:
241  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.connect",
242                             BLUETOOTHSOCKET_CONNECT);
243
244  BluetoothSocketConnectFunction();
245
246 protected:
247  virtual ~BluetoothSocketConnectFunction();
248
249  // BluetoothSocketAbstractConnectFunction:
250  virtual void ConnectToService(device::BluetoothDevice* device,
251                                const device::BluetoothUUID& uuid) OVERRIDE;
252};
253
254class BluetoothSocketDisconnectFunction
255    : public BluetoothSocketAsyncApiFunction {
256 public:
257  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.disconnect",
258                             BLUETOOTHSOCKET_DISCONNECT);
259
260  BluetoothSocketDisconnectFunction();
261
262 protected:
263  virtual ~BluetoothSocketDisconnectFunction();
264
265  // BluetoothSocketAsyncApiFunction:
266  virtual bool Prepare() OVERRIDE;
267  virtual void AsyncWorkStart() OVERRIDE;
268
269 private:
270  virtual void OnSuccess();
271
272  scoped_ptr<bluetooth_socket::Disconnect::Params> params_;
273};
274
275class BluetoothSocketCloseFunction : public BluetoothSocketAsyncApiFunction {
276 public:
277  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.close", BLUETOOTHSOCKET_CLOSE);
278
279  BluetoothSocketCloseFunction();
280
281 protected:
282  virtual ~BluetoothSocketCloseFunction();
283
284  // BluetoothSocketAsyncApiFunction:
285  virtual bool Prepare() OVERRIDE;
286  virtual void Work() OVERRIDE;
287
288 private:
289  scoped_ptr<bluetooth_socket::Close::Params> params_;
290};
291
292class BluetoothSocketSendFunction : public BluetoothSocketAsyncApiFunction {
293 public:
294  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.send", BLUETOOTHSOCKET_SEND);
295
296  BluetoothSocketSendFunction();
297
298 protected:
299  virtual ~BluetoothSocketSendFunction();
300
301  // BluetoothSocketAsyncApiFunction:
302  virtual bool Prepare() OVERRIDE;
303  virtual void AsyncWorkStart() OVERRIDE;
304
305 private:
306  void OnSuccess(int bytes_sent);
307  void OnError(BluetoothApiSocket::ErrorReason reason,
308               const std::string& message);
309
310  scoped_ptr<bluetooth_socket::Send::Params> params_;
311  scoped_refptr<net::IOBuffer> io_buffer_;
312  size_t io_buffer_size_;
313};
314
315class BluetoothSocketGetInfoFunction : public BluetoothSocketAsyncApiFunction {
316 public:
317  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.getInfo",
318                             BLUETOOTHSOCKET_GETINFO);
319
320  BluetoothSocketGetInfoFunction();
321
322 protected:
323  virtual ~BluetoothSocketGetInfoFunction();
324
325  // BluetoothSocketAsyncApiFunction:
326  virtual bool Prepare() OVERRIDE;
327  virtual void Work() OVERRIDE;
328
329 private:
330  scoped_ptr<bluetooth_socket::GetInfo::Params> params_;
331};
332
333class BluetoothSocketGetSocketsFunction
334    : public BluetoothSocketAsyncApiFunction {
335 public:
336  DECLARE_EXTENSION_FUNCTION("bluetoothSocket.getSockets",
337                             BLUETOOTHSOCKET_GETSOCKETS);
338
339  BluetoothSocketGetSocketsFunction();
340
341 protected:
342  virtual ~BluetoothSocketGetSocketsFunction();
343
344  // BluetoothSocketAsyncApiFunction:
345  virtual bool Prepare() OVERRIDE;
346  virtual void Work() OVERRIDE;
347};
348
349}  // namespace core_api
350}  // namespace extensions
351
352#endif  // EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_
353