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_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_
6 #define EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_
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"
22 class BluetoothSocket
;
29 namespace extensions
{
33 class BluetoothSocketEventDispatcher
;
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.
39 class BluetoothSocketAsyncApiFunction
: public AsyncExtensionFunction
{
41 BluetoothSocketAsyncApiFunction();
44 ~BluetoothSocketAsyncApiFunction() override
;
46 // AsyncExtensionFunction:
47 bool RunAsync() override
;
51 void AsyncWorkCompleted();
53 virtual bool Prepare() = 0;
55 virtual void AsyncWorkStart();
57 content::BrowserThread::ID
work_thread_id() const;
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();
65 ApiResourceManager
<BluetoothApiSocket
>* manager_
;
68 class BluetoothSocketCreateFunction
: public BluetoothSocketAsyncApiFunction
{
70 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.create", BLUETOOTHSOCKET_CREATE
);
72 BluetoothSocketCreateFunction();
75 ~BluetoothSocketCreateFunction() override
;
77 // BluetoothSocketAsyncApiFunction:
78 bool Prepare() override
;
82 scoped_ptr
<bluetooth_socket::Create::Params
> params_
;
85 class BluetoothSocketUpdateFunction
: public BluetoothSocketAsyncApiFunction
{
87 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.update", BLUETOOTHSOCKET_UPDATE
);
89 BluetoothSocketUpdateFunction();
92 ~BluetoothSocketUpdateFunction() override
;
94 // BluetoothSocketAsyncApiFunction:
95 bool Prepare() override
;
99 scoped_ptr
<bluetooth_socket::Update::Params
> params_
;
102 class BluetoothSocketSetPausedFunction
103 : public BluetoothSocketAsyncApiFunction
{
105 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.setPaused",
106 BLUETOOTHSOCKET_SETPAUSED
);
108 BluetoothSocketSetPausedFunction();
111 ~BluetoothSocketSetPausedFunction() override
;
113 // BluetoothSocketAsyncApiFunction:
114 bool Prepare() override
;
115 void Work() override
;
118 scoped_ptr
<bluetooth_socket::SetPaused::Params
> params_
;
119 BluetoothSocketEventDispatcher
* socket_event_dispatcher_
;
122 class BluetoothSocketListenFunction
: public BluetoothSocketAsyncApiFunction
{
124 BluetoothSocketListenFunction();
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
&
134 virtual void CreateResults() = 0;
136 virtual int socket_id() const = 0;
137 virtual const std::string
& uuid() const = 0;
139 // BluetoothSocketAsyncApiFunction:
140 bool Prepare() override
;
141 void AsyncWorkStart() override
;
144 ~BluetoothSocketListenFunction() override
;
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
);
150 BluetoothSocketEventDispatcher
* socket_event_dispatcher_
;
153 class BluetoothSocketListenUsingRfcommFunction
154 : public BluetoothSocketListenFunction
{
156 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingRfcomm",
157 BLUETOOTHSOCKET_LISTENUSINGRFCOMM
);
159 BluetoothSocketListenUsingRfcommFunction();
161 // BluetoothSocketListenFunction:
162 int socket_id() const override
;
163 const std::string
& uuid() const override
;
165 bool CreateParams() override
;
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 void CreateResults() override
;
176 ~BluetoothSocketListenUsingRfcommFunction() override
;
179 scoped_ptr
<bluetooth_socket::ListenUsingRfcomm::Params
> params_
;
182 class BluetoothSocketListenUsingL2capFunction
183 : public BluetoothSocketListenFunction
{
185 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingL2cap",
186 BLUETOOTHSOCKET_LISTENUSINGL2CAP
);
188 BluetoothSocketListenUsingL2capFunction();
190 // BluetoothSocketListenFunction:
191 int socket_id() const override
;
192 const std::string
& uuid() const override
;
194 bool CreateParams() override
;
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 void CreateResults() override
;
205 ~BluetoothSocketListenUsingL2capFunction() override
;
208 scoped_ptr
<bluetooth_socket::ListenUsingL2cap::Params
> params_
;
211 class BluetoothSocketAbstractConnectFunction
:
212 public BluetoothSocketAsyncApiFunction
{
214 BluetoothSocketAbstractConnectFunction();
217 ~BluetoothSocketAbstractConnectFunction() override
;
219 // BluetoothSocketAsyncApiFunction:
220 bool Prepare() override
;
221 void AsyncWorkStart() override
;
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;
228 virtual void OnConnect(scoped_refptr
<device::BluetoothSocket
> socket
);
229 virtual void OnConnectError(const std::string
& message
);
232 virtual void OnGetAdapter(scoped_refptr
<device::BluetoothAdapter
> adapter
);
234 scoped_ptr
<bluetooth_socket::Connect::Params
> params_
;
235 BluetoothSocketEventDispatcher
* socket_event_dispatcher_
;
238 class BluetoothSocketConnectFunction
:
239 public BluetoothSocketAbstractConnectFunction
{
241 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.connect",
242 BLUETOOTHSOCKET_CONNECT
);
244 BluetoothSocketConnectFunction();
247 ~BluetoothSocketConnectFunction() override
;
249 // BluetoothSocketAbstractConnectFunction:
250 void ConnectToService(device::BluetoothDevice
* device
,
251 const device::BluetoothUUID
& uuid
) override
;
254 class BluetoothSocketDisconnectFunction
255 : public BluetoothSocketAsyncApiFunction
{
257 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.disconnect",
258 BLUETOOTHSOCKET_DISCONNECT
);
260 BluetoothSocketDisconnectFunction();
263 ~BluetoothSocketDisconnectFunction() override
;
265 // BluetoothSocketAsyncApiFunction:
266 bool Prepare() override
;
267 void AsyncWorkStart() override
;
270 virtual void OnSuccess();
272 scoped_ptr
<bluetooth_socket::Disconnect::Params
> params_
;
275 class BluetoothSocketCloseFunction
: public BluetoothSocketAsyncApiFunction
{
277 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.close", BLUETOOTHSOCKET_CLOSE
);
279 BluetoothSocketCloseFunction();
282 ~BluetoothSocketCloseFunction() override
;
284 // BluetoothSocketAsyncApiFunction:
285 bool Prepare() override
;
286 void Work() override
;
289 scoped_ptr
<bluetooth_socket::Close::Params
> params_
;
292 class BluetoothSocketSendFunction
: public BluetoothSocketAsyncApiFunction
{
294 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.send", BLUETOOTHSOCKET_SEND
);
296 BluetoothSocketSendFunction();
299 ~BluetoothSocketSendFunction() override
;
301 // BluetoothSocketAsyncApiFunction:
302 bool Prepare() override
;
303 void AsyncWorkStart() override
;
306 void OnSuccess(int bytes_sent
);
307 void OnError(BluetoothApiSocket::ErrorReason reason
,
308 const std::string
& message
);
310 scoped_ptr
<bluetooth_socket::Send::Params
> params_
;
311 scoped_refptr
<net::IOBuffer
> io_buffer_
;
312 size_t io_buffer_size_
;
315 class BluetoothSocketGetInfoFunction
: public BluetoothSocketAsyncApiFunction
{
317 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.getInfo",
318 BLUETOOTHSOCKET_GETINFO
);
320 BluetoothSocketGetInfoFunction();
323 ~BluetoothSocketGetInfoFunction() override
;
325 // BluetoothSocketAsyncApiFunction:
326 bool Prepare() override
;
327 void Work() override
;
330 scoped_ptr
<bluetooth_socket::GetInfo::Params
> params_
;
333 class BluetoothSocketGetSocketsFunction
334 : public BluetoothSocketAsyncApiFunction
{
336 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.getSockets",
337 BLUETOOTHSOCKET_GETSOCKETS
);
339 BluetoothSocketGetSocketsFunction();
342 ~BluetoothSocketGetSocketsFunction() override
;
344 // BluetoothSocketAsyncApiFunction:
345 bool Prepare() override
;
346 void Work() override
;
350 } // namespace extensions
352 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_