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 CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_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 "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h"
14 #include "chrome/common/extensions/api/bluetooth_socket.h"
15 #include "device/bluetooth/bluetooth_adapter.h"
16 #include "extensions/browser/api/api_resource_manager.h"
17 #include "extensions/browser/api/async_api_function.h"
18 #include "extensions/browser/extension_function.h"
19 #include "extensions/browser/extension_function_histogram_value.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 virtual ~BluetoothSocketAsyncApiFunction();
46 // AsyncExtensionFunction:
47 virtual 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 virtual ~BluetoothSocketCreateFunction();
77 // BluetoothSocketAsyncApiFunction:
78 virtual bool Prepare() OVERRIDE;
79 virtual void Work() 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 virtual ~BluetoothSocketUpdateFunction();
94 // BluetoothSocketAsyncApiFunction:
95 virtual bool Prepare() OVERRIDE;
96 virtual void Work() 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 virtual ~BluetoothSocketSetPausedFunction();
113 // BluetoothSocketAsyncApiFunction:
114 virtual bool Prepare() OVERRIDE;
115 virtual void Work() OVERRIDE;
118 scoped_ptr<bluetooth_socket::SetPaused::Params> params_;
119 BluetoothSocketEventDispatcher* socket_event_dispatcher_;
122 class BluetoothSocketListenUsingRfcommFunction : public AsyncExtensionFunction {
124 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingRfcomm",
125 BLUETOOTHSOCKET_LISTENUSINGRFCOMM);
128 virtual ~BluetoothSocketListenUsingRfcommFunction() {}
130 // AsyncExtensionFunction override:
131 virtual bool RunAsync() OVERRIDE;
134 class BluetoothSocketListenUsingInsecureRfcommFunction
135 : public AsyncExtensionFunction {
137 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingInsecureRfcomm",
138 BLUETOOTHSOCKET_LISTENUSINGINSECURERFCOMM);
141 virtual ~BluetoothSocketListenUsingInsecureRfcommFunction() {}
143 // AsyncExtensionFunction override:
144 virtual bool RunAsync() OVERRIDE;
147 class BluetoothSocketListenUsingL2capFunction : public AsyncExtensionFunction {
149 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingL2cap",
150 BLUETOOTHSOCKET_LISTENUSINGL2CAP);
153 virtual ~BluetoothSocketListenUsingL2capFunction() {}
155 // AsyncExtensionFunction override:
156 virtual bool RunAsync() OVERRIDE;
159 class BluetoothSocketConnectFunction : public BluetoothSocketAsyncApiFunction {
161 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.connect",
162 BLUETOOTHSOCKET_CONNECT);
164 BluetoothSocketConnectFunction();
167 virtual ~BluetoothSocketConnectFunction();
169 // BluetoothSocketAsyncApiFunction:
170 virtual bool Prepare() OVERRIDE;
171 virtual void AsyncWorkStart() OVERRIDE;
174 virtual void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
175 virtual void OnConnect(scoped_refptr<device::BluetoothSocket> socket);
176 virtual void OnConnectError(const std::string& message);
178 scoped_ptr<bluetooth_socket::Connect::Params> params_;
179 BluetoothSocketEventDispatcher* socket_event_dispatcher_;
182 class BluetoothSocketDisconnectFunction
183 : public BluetoothSocketAsyncApiFunction {
185 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.disconnect",
186 BLUETOOTHSOCKET_DISCONNECT);
188 BluetoothSocketDisconnectFunction();
191 virtual ~BluetoothSocketDisconnectFunction();
193 // BluetoothSocketAsyncApiFunction:
194 virtual bool Prepare() OVERRIDE;
195 virtual void AsyncWorkStart() OVERRIDE;
198 virtual void OnSuccess();
200 scoped_ptr<bluetooth_socket::Disconnect::Params> params_;
203 class BluetoothSocketCloseFunction : public BluetoothSocketAsyncApiFunction {
205 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.close", BLUETOOTHSOCKET_CLOSE);
207 BluetoothSocketCloseFunction();
210 virtual ~BluetoothSocketCloseFunction();
212 // BluetoothSocketAsyncApiFunction:
213 virtual bool Prepare() OVERRIDE;
214 virtual void Work() OVERRIDE;
217 scoped_ptr<bluetooth_socket::Close::Params> params_;
220 class BluetoothSocketSendFunction : public BluetoothSocketAsyncApiFunction {
222 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.send", BLUETOOTHSOCKET_SEND);
224 BluetoothSocketSendFunction();
227 virtual ~BluetoothSocketSendFunction();
229 // BluetoothSocketAsyncApiFunction:
230 virtual bool Prepare() OVERRIDE;
231 virtual void AsyncWorkStart() OVERRIDE;
234 void OnSuccess(int bytes_sent);
235 void OnError(BluetoothApiSocket::ErrorReason reason,
236 const std::string& message);
238 scoped_ptr<bluetooth_socket::Send::Params> params_;
239 scoped_refptr<net::IOBuffer> io_buffer_;
240 size_t io_buffer_size_;
243 class BluetoothSocketGetInfoFunction : public BluetoothSocketAsyncApiFunction {
245 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.getInfo",
246 BLUETOOTHSOCKET_GETINFO);
248 BluetoothSocketGetInfoFunction();
251 virtual ~BluetoothSocketGetInfoFunction();
253 // BluetoothSocketAsyncApiFunction:
254 virtual bool Prepare() OVERRIDE;
255 virtual void Work() OVERRIDE;
258 scoped_ptr<bluetooth_socket::GetInfo::Params> params_;
261 class BluetoothSocketGetSocketsFunction
262 : public BluetoothSocketAsyncApiFunction {
264 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.getSockets",
265 BLUETOOTHSOCKET_GETSOCKETS);
267 BluetoothSocketGetSocketsFunction();
270 virtual ~BluetoothSocketGetSocketsFunction();
272 // BluetoothSocketAsyncApiFunction:
273 virtual bool Prepare() OVERRIDE;
274 virtual void Work() OVERRIDE;
278 } // namespace extensions
280 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_