Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / bluetooth / bluetooth_api.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_
7
8 #include <string>
9
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"
24
25 namespace content {
26 class BrowserContext;
27 }
28
29 namespace device {
30 class BluetoothAdapter;
31 struct BluetoothOutOfBandPairingData;
32 }
33
34 namespace net {
35 class IOBuffer;
36 }
37
38 namespace extensions {
39
40 class BluetoothEventRouter;
41
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 {
47  public:
48   typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData;
49
50   struct ConnectionParams {
51     ConnectionParams();
52     ~ConnectionParams();
53
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;
61   };
62
63   // Convenience method to get the BluetoothAPI for a browser context.
64   static BluetoothAPI* Get(content::BrowserContext* context);
65
66   static BrowserContextKeyedAPIFactory<BluetoothAPI>* GetFactoryInstance();
67
68   explicit BluetoothAPI(content::BrowserContext* context);
69   virtual ~BluetoothAPI();
70
71   BluetoothEventRouter* event_router();
72   scoped_refptr<SocketData> socket_data();
73
74   // KeyedService implementation.
75   virtual void Shutdown() OVERRIDE;
76
77   // EventRouter::Observer implementation.
78   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
79   virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
80
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);
87
88  private:
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,
93       int socket_id,
94       scoped_refptr<device::BluetoothAdapter> adapter);
95
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;
101
102   content::BrowserContext* browser_context_;
103
104   // Created lazily on first access.
105   scoped_ptr<BluetoothEventRouter> event_router_;
106   scoped_refptr<SocketData> socket_data_;
107 };
108
109 namespace api {
110
111 class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
112  public:
113   DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
114                              BLUETOOTH_GETADAPTERSTATE)
115
116  protected:
117   virtual ~BluetoothGetAdapterStateFunction();
118
119   // BluetoothExtensionFunction:
120   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
121 };
122
123 class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
124  public:
125   DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
126
127  protected:
128   virtual ~BluetoothGetDevicesFunction();
129
130   // BluetoothExtensionFunction:
131   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
132 };
133
134 class BluetoothGetDeviceFunction : public BluetoothExtensionFunction {
135  public:
136   DECLARE_EXTENSION_FUNCTION("bluetooth.getDevice", BLUETOOTH_GETDEVICE)
137
138   // BluetoothExtensionFunction:
139   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
140
141  protected:
142   virtual ~BluetoothGetDeviceFunction();
143 };
144
145 class BluetoothAddProfileFunction : public AsyncExtensionFunction {
146  public:
147   DECLARE_EXTENSION_FUNCTION("bluetooth.addProfile", BLUETOOTH_ADDPROFILE)
148
149   BluetoothAddProfileFunction();
150
151  protected:
152   virtual ~BluetoothAddProfileFunction();
153   virtual bool RunAsync() OVERRIDE;
154
155   virtual void RegisterProfile(
156       const device::BluetoothProfile::Options& options,
157       const device::BluetoothProfile::ProfileCallback& callback);
158
159  private:
160   void OnProfileRegistered(device::BluetoothProfile* bluetooth_profile);
161
162   device::BluetoothUUID uuid_;
163 };
164
165 class BluetoothRemoveProfileFunction : public SyncExtensionFunction {
166  public:
167   DECLARE_EXTENSION_FUNCTION("bluetooth.removeProfile",
168                              BLUETOOTH_REMOVEPROFILE)
169
170  protected:
171   virtual ~BluetoothRemoveProfileFunction();
172   virtual bool RunSync() OVERRIDE;
173 };
174
175 class BluetoothConnectFunction : public BluetoothExtensionFunction {
176  public:
177   DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT)
178
179  protected:
180   virtual ~BluetoothConnectFunction();
181
182   // BluetoothExtensionFunction:
183   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
184
185  private:
186   void OnConnectedCallback(scoped_refptr<device::BluetoothAdapter> adapter,
187                            const std::string& device_address);
188   void OnMonitorStartedCallback();
189   void OnErrorCallback(const std::string& error);
190 };
191
192 class BluetoothDisconnectFunction : public AsyncExtensionFunction {
193  public:
194   DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT)
195
196  protected:
197   virtual ~BluetoothDisconnectFunction() {}
198
199   // AsyncExtensionFunction:
200   virtual bool RunAsync() OVERRIDE;
201 };
202
203 class BluetoothSendFunction : public AsyncExtensionFunction {
204  public:
205   DECLARE_EXTENSION_FUNCTION("bluetooth.send", BLUETOOTH_WRITE)
206
207  protected:
208   virtual ~BluetoothSendFunction() {}
209
210   // AsyncExtensionFunction:
211   virtual bool RunAsync() OVERRIDE;
212 };
213
214 class BluetoothUpdateSocketFunction : public AsyncExtensionFunction {
215  public:
216   DECLARE_EXTENSION_FUNCTION("bluetooth.updateSocket", BLUETOOTH_UPDATE_SOCKET)
217
218  protected:
219   virtual ~BluetoothUpdateSocketFunction() {}
220
221   // AsyncExtensionFunction:
222   virtual bool RunAsync() OVERRIDE;
223 };
224
225 class BluetoothSetSocketPausedFunction : public AsyncExtensionFunction {
226  public:
227   DECLARE_EXTENSION_FUNCTION("bluetooth.setSocketPaused",
228                              BLUETOOTH_SET_SOCKET_PAUSED)
229
230  protected:
231   virtual ~BluetoothSetSocketPausedFunction() {}
232
233   // AsyncExtensionFunction:
234   virtual bool RunAsync() OVERRIDE;
235 };
236
237 class BluetoothGetSocketFunction : public AsyncExtensionFunction {
238  public:
239   DECLARE_EXTENSION_FUNCTION("bluetooth.getSocket", BLUETOOTH_GET_SOCKET)
240
241  protected:
242   virtual ~BluetoothGetSocketFunction() {}
243
244   // AsyncExtensionFunction:
245   virtual bool RunAsync() OVERRIDE;
246 };
247
248 class BluetoothGetSocketsFunction : public AsyncExtensionFunction {
249  public:
250   DECLARE_EXTENSION_FUNCTION("bluetooth.getSockets", BLUETOOTH_GET_SOCKETS)
251
252  protected:
253   virtual ~BluetoothGetSocketsFunction() {}
254
255   // AsyncExtensionFunction:
256   virtual bool RunAsync() OVERRIDE;
257 };
258
259 class BluetoothGetLocalOutOfBandPairingDataFunction
260     : public BluetoothExtensionFunction {
261  public:
262   DECLARE_EXTENSION_FUNCTION("bluetooth.getLocalOutOfBandPairingData",
263                              BLUETOOTH_GETLOCALOUTOFBANDPAIRINGDATA)
264
265  protected:
266   virtual ~BluetoothGetLocalOutOfBandPairingDataFunction() {}
267
268   void ReadCallback(const device::BluetoothOutOfBandPairingData& data);
269   void ErrorCallback();
270
271   // BluetoothExtensionFunction:
272   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
273 };
274
275 class BluetoothSetOutOfBandPairingDataFunction
276     : public BluetoothExtensionFunction {
277  public:
278   DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData",
279                              BLUETOOTH_SETOUTOFBANDPAIRINGDATA)
280
281  protected:
282   virtual ~BluetoothSetOutOfBandPairingDataFunction() {}
283
284   void OnSuccessCallback();
285   void OnErrorCallback();
286
287   // BluetoothExtensionFunction:
288   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
289 };
290
291 class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction {
292  public:
293   DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery",
294                              BLUETOOTH_STARTDISCOVERY)
295
296  protected:
297   virtual ~BluetoothStartDiscoveryFunction() {}
298
299   // BluetoothExtensionFunction:
300   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
301
302  private:
303   void OnSuccessCallback();
304   void OnErrorCallback();
305 };
306
307 class BluetoothStopDiscoveryFunction : public BluetoothExtensionFunction {
308  public:
309   DECLARE_EXTENSION_FUNCTION("bluetooth.stopDiscovery", BLUETOOTH_STOPDISCOVERY)
310
311  protected:
312   virtual ~BluetoothStopDiscoveryFunction() {}
313
314   // BluetoothExtensionFunction:
315   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
316
317  private:
318   void OnSuccessCallback();
319   void OnErrorCallback();
320 };
321
322 }  // namespace api
323 }  // namespace extensions
324
325 #endif  // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_