1 // Copyright (c) 2012 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_BLUETOOTH_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h"
13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h"
14 #include "chrome/common/extensions/api/bluetooth.h"
15 #include "device/bluetooth/bluetooth_device.h"
16 #include "device/bluetooth/bluetooth_profile.h"
17 #include "device/bluetooth/bluetooth_socket.h"
18 #include "device/bluetooth/bluetooth_uuid.h"
19 #include "extensions/browser/api/api_resource_manager.h"
20 #include "extensions/browser/api/async_api_function.h"
21 #include "extensions/browser/browser_context_keyed_api_factory.h"
22 #include "extensions/browser/event_router.h"
23 #include "extensions/browser/extension_function.h"
30 class BluetoothAdapter;
31 struct BluetoothOutOfBandPairingData;
38 namespace extensions {
40 class BluetoothEventRouter;
42 // The profile-keyed service that manages the bluetooth extension API.
43 // All methods of this class must be called on the UI thread.
44 // TODO(rpaquay): Rename this and move to separate file.
45 class BluetoothAPI : public BrowserContextKeyedAPI,
46 public EventRouter::Observer {
48 typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData;
50 struct ConnectionParams {
54 content::BrowserThread::ID thread_id;
55 void* browser_context_id;
56 std::string extension_id;
57 std::string device_address;
58 device::BluetoothUUID uuid;
59 scoped_refptr<device::BluetoothSocket> socket;
60 scoped_refptr<SocketData> socket_data;
63 // Convenience method to get the BluetoothAPI for a browser context.
64 static BluetoothAPI* Get(content::BrowserContext* context);
66 static BrowserContextKeyedAPIFactory<BluetoothAPI>* GetFactoryInstance();
68 explicit BluetoothAPI(content::BrowserContext* context);
69 virtual ~BluetoothAPI();
71 BluetoothEventRouter* event_router();
72 scoped_refptr<SocketData> socket_data();
74 // KeyedService implementation.
75 virtual void Shutdown() OVERRIDE;
77 // EventRouter::Observer implementation.
78 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
79 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
81 // Dispatch an event that takes a connection socket as a parameter to the
82 // extension that registered the profile that the socket has connected to.
83 void DispatchConnectionEvent(const std::string& extension_id,
84 const device::BluetoothUUID& profile_uuid,
85 const device::BluetoothDevice* device,
86 scoped_refptr<device::BluetoothSocket> socket);
89 static void RegisterSocket(const ConnectionParams& params);
90 static void RegisterSocketUI(const ConnectionParams& params, int socket_id);
91 static void RegisterSocketWithAdapterUI(
92 const ConnectionParams& params,
94 scoped_refptr<device::BluetoothAdapter> adapter);
96 // BrowserContextKeyedAPI implementation.
97 friend class BrowserContextKeyedAPIFactory<BluetoothAPI>;
98 static const char* service_name() { return "BluetoothAPI"; }
99 static const bool kServiceRedirectedInIncognito = true;
100 static const bool kServiceIsNULLWhileTesting = true;
102 content::BrowserContext* browser_context_;
104 // Created lazily on first access.
105 scoped_ptr<BluetoothEventRouter> event_router_;
106 scoped_refptr<SocketData> socket_data_;
111 class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
113 DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
114 BLUETOOTH_GETADAPTERSTATE)
117 virtual ~BluetoothGetAdapterStateFunction();
119 // BluetoothExtensionFunction:
120 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
123 class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
125 DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
128 virtual ~BluetoothGetDevicesFunction();
130 // BluetoothExtensionFunction:
131 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
134 class BluetoothGetDeviceFunction : public BluetoothExtensionFunction {
136 DECLARE_EXTENSION_FUNCTION("bluetooth.getDevice", BLUETOOTH_GETDEVICE)
138 // BluetoothExtensionFunction:
139 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
142 virtual ~BluetoothGetDeviceFunction();
145 class BluetoothAddProfileFunction : public AsyncExtensionFunction {
147 DECLARE_EXTENSION_FUNCTION("bluetooth.addProfile", BLUETOOTH_ADDPROFILE)
149 BluetoothAddProfileFunction();
152 virtual ~BluetoothAddProfileFunction();
153 virtual bool RunAsync() OVERRIDE;
155 virtual void RegisterProfile(
156 const device::BluetoothProfile::Options& options,
157 const device::BluetoothProfile::ProfileCallback& callback);
160 void OnProfileRegistered(device::BluetoothProfile* bluetooth_profile);
162 device::BluetoothUUID uuid_;
165 class BluetoothRemoveProfileFunction : public SyncExtensionFunction {
167 DECLARE_EXTENSION_FUNCTION("bluetooth.removeProfile",
168 BLUETOOTH_REMOVEPROFILE)
171 virtual ~BluetoothRemoveProfileFunction();
172 virtual bool RunSync() OVERRIDE;
175 class BluetoothConnectFunction : public BluetoothExtensionFunction {
177 DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT)
180 virtual ~BluetoothConnectFunction();
182 // BluetoothExtensionFunction:
183 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
186 void OnConnectedCallback(scoped_refptr<device::BluetoothAdapter> adapter,
187 const std::string& device_address);
188 void OnMonitorStartedCallback();
189 void OnErrorCallback(const std::string& error);
192 class BluetoothDisconnectFunction : public AsyncExtensionFunction {
194 DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT)
197 virtual ~BluetoothDisconnectFunction() {}
199 // AsyncExtensionFunction:
200 virtual bool RunAsync() OVERRIDE;
203 class BluetoothSendFunction : public AsyncExtensionFunction {
205 DECLARE_EXTENSION_FUNCTION("bluetooth.send", BLUETOOTH_WRITE)
208 virtual ~BluetoothSendFunction() {}
210 // AsyncExtensionFunction:
211 virtual bool RunAsync() OVERRIDE;
214 class BluetoothUpdateSocketFunction : public AsyncExtensionFunction {
216 DECLARE_EXTENSION_FUNCTION("bluetooth.updateSocket", BLUETOOTH_UPDATE_SOCKET)
219 virtual ~BluetoothUpdateSocketFunction() {}
221 // AsyncExtensionFunction:
222 virtual bool RunAsync() OVERRIDE;
225 class BluetoothSetSocketPausedFunction : public AsyncExtensionFunction {
227 DECLARE_EXTENSION_FUNCTION("bluetooth.setSocketPaused",
228 BLUETOOTH_SET_SOCKET_PAUSED)
231 virtual ~BluetoothSetSocketPausedFunction() {}
233 // AsyncExtensionFunction:
234 virtual bool RunAsync() OVERRIDE;
237 class BluetoothGetSocketFunction : public AsyncExtensionFunction {
239 DECLARE_EXTENSION_FUNCTION("bluetooth.getSocket", BLUETOOTH_GET_SOCKET)
242 virtual ~BluetoothGetSocketFunction() {}
244 // AsyncExtensionFunction:
245 virtual bool RunAsync() OVERRIDE;
248 class BluetoothGetSocketsFunction : public AsyncExtensionFunction {
250 DECLARE_EXTENSION_FUNCTION("bluetooth.getSockets", BLUETOOTH_GET_SOCKETS)
253 virtual ~BluetoothGetSocketsFunction() {}
255 // AsyncExtensionFunction:
256 virtual bool RunAsync() OVERRIDE;
259 class BluetoothGetLocalOutOfBandPairingDataFunction
260 : public BluetoothExtensionFunction {
262 DECLARE_EXTENSION_FUNCTION("bluetooth.getLocalOutOfBandPairingData",
263 BLUETOOTH_GETLOCALOUTOFBANDPAIRINGDATA)
266 virtual ~BluetoothGetLocalOutOfBandPairingDataFunction() {}
268 void ReadCallback(const device::BluetoothOutOfBandPairingData& data);
269 void ErrorCallback();
271 // BluetoothExtensionFunction:
272 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
275 class BluetoothSetOutOfBandPairingDataFunction
276 : public BluetoothExtensionFunction {
278 DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData",
279 BLUETOOTH_SETOUTOFBANDPAIRINGDATA)
282 virtual ~BluetoothSetOutOfBandPairingDataFunction() {}
284 void OnSuccessCallback();
285 void OnErrorCallback();
287 // BluetoothExtensionFunction:
288 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
291 class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction {
293 DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery",
294 BLUETOOTH_STARTDISCOVERY)
297 virtual ~BluetoothStartDiscoveryFunction() {}
299 // BluetoothExtensionFunction:
300 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
303 void OnSuccessCallback();
304 void OnErrorCallback();
307 class BluetoothStopDiscoveryFunction : public BluetoothExtensionFunction {
309 DECLARE_EXTENSION_FUNCTION("bluetooth.stopDiscovery", BLUETOOTH_STOPDISCOVERY)
312 virtual ~BluetoothStopDiscoveryFunction() {}
314 // BluetoothExtensionFunction:
315 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
318 void OnSuccessCallback();
319 void OnErrorCallback();
323 } // namespace extensions
325 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_