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_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_API_H_
6 #define EXTENSIONS_BROWSER_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_API_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "device/bluetooth/bluetooth_advertisement.h"
10 #include "extensions/browser/api/api_resource_manager.h"
11 #include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_event_router.h"
12 #include "extensions/browser/browser_context_keyed_api_factory.h"
13 #include "extensions/browser/extension_function.h"
14 #include "extensions/browser/extension_function_histogram_value.h"
16 namespace extensions
{
18 class BluetoothApiAdvertisement
;
19 class BluetoothLowEnergyEventRouter
;
21 // The profile-keyed service that manages the bluetoothLowEnergy extension API.
22 class BluetoothLowEnergyAPI
: public BrowserContextKeyedAPI
{
24 static BrowserContextKeyedAPIFactory
<BluetoothLowEnergyAPI
>*
27 // Convenience method to get the BluetoothLowEnergy API for a browser context.
28 static BluetoothLowEnergyAPI
* Get(content::BrowserContext
* context
);
30 explicit BluetoothLowEnergyAPI(content::BrowserContext
* context
);
31 ~BluetoothLowEnergyAPI() override
;
33 // KeyedService implementation..
34 void Shutdown() override
;
36 BluetoothLowEnergyEventRouter
* event_router() const {
37 return event_router_
.get();
40 // BrowserContextKeyedAPI implementation.
41 static const char* service_name() { return "BluetoothLowEnergyAPI"; }
42 static const bool kServiceRedirectedInIncognito
= true;
43 static const bool kServiceIsNULLWhileTesting
= true;
46 friend class BrowserContextKeyedAPIFactory
<BluetoothLowEnergyAPI
>;
48 scoped_ptr
<BluetoothLowEnergyEventRouter
> event_router_
;
50 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAPI
);
55 // Base class for bluetoothLowEnergy API functions. This class handles some of
56 // the common logic involved in all API functions, such as checking for
57 // platform support and returning the correct error.
58 class BluetoothLowEnergyExtensionFunction
: public AsyncExtensionFunction
{
60 BluetoothLowEnergyExtensionFunction();
63 ~BluetoothLowEnergyExtensionFunction() override
;
65 // ExtensionFunction override.
66 bool RunAsync() override
;
68 // Implemented by individual bluetoothLowEnergy extension functions to perform
69 // the body of the function. This invoked asynchonously after RunAsync after
70 // the BluetoothLowEnergyEventRouter has obtained a handle on the
72 virtual bool DoWork() = 0;
75 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyExtensionFunction
);
78 class BluetoothLowEnergyConnectFunction
79 : public BluetoothLowEnergyExtensionFunction
{
81 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.connect",
82 BLUETOOTHLOWENERGY_CONNECT
);
85 ~BluetoothLowEnergyConnectFunction() override
{}
87 // BluetoothLowEnergyExtensionFunction override.
88 bool DoWork() override
;
91 // Success and error callbacks, called by
92 // BluetoothLowEnergyEventRouter::Connect.
93 void SuccessCallback();
94 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
97 class BluetoothLowEnergyDisconnectFunction
98 : public BluetoothLowEnergyExtensionFunction
{
100 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.disconnect",
101 BLUETOOTHLOWENERGY_DISCONNECT
);
104 ~BluetoothLowEnergyDisconnectFunction() override
{}
106 // BluetoothLowEnergyExtensionFunction override.
107 bool DoWork() override
;
110 // Success and error callbacks, called by
111 // BluetoothLowEnergyEventRouter::Disconnect.
112 void SuccessCallback();
113 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
116 class BluetoothLowEnergyGetServiceFunction
117 : public BluetoothLowEnergyExtensionFunction
{
119 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getService",
120 BLUETOOTHLOWENERGY_GETSERVICE
);
123 ~BluetoothLowEnergyGetServiceFunction() override
{}
125 // BluetoothLowEnergyExtensionFunction override.
126 bool DoWork() override
;
129 class BluetoothLowEnergyGetServicesFunction
130 : public BluetoothLowEnergyExtensionFunction
{
132 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getServices",
133 BLUETOOTHLOWENERGY_GETSERVICES
);
136 ~BluetoothLowEnergyGetServicesFunction() override
{}
138 // BluetoothLowEnergyExtensionFunction override.
139 bool DoWork() override
;
142 class BluetoothLowEnergyGetCharacteristicFunction
143 : public BluetoothLowEnergyExtensionFunction
{
145 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristic",
146 BLUETOOTHLOWENERGY_GETCHARACTERISTIC
);
149 ~BluetoothLowEnergyGetCharacteristicFunction() override
{}
151 // BluetoothLowEnergyExtensionFunction override.
152 bool DoWork() override
;
155 class BluetoothLowEnergyGetCharacteristicsFunction
156 : public BluetoothLowEnergyExtensionFunction
{
158 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristics",
159 BLUETOOTHLOWENERGY_GETCHARACTERISTICS
);
162 ~BluetoothLowEnergyGetCharacteristicsFunction() override
{}
164 // BluetoothLowEnergyExtensionFunction override.
165 bool DoWork() override
;
168 class BluetoothLowEnergyGetIncludedServicesFunction
169 : public BluetoothLowEnergyExtensionFunction
{
171 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getIncludedServices",
172 BLUETOOTHLOWENERGY_GETINCLUDEDSERVICES
);
175 ~BluetoothLowEnergyGetIncludedServicesFunction() override
{}
177 // BluetoothLowEnergyExtensionFunction override.
178 bool DoWork() override
;
181 class BluetoothLowEnergyGetDescriptorFunction
182 : public BluetoothLowEnergyExtensionFunction
{
184 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptor",
185 BLUETOOTHLOWENERGY_GETDESCRIPTOR
);
188 ~BluetoothLowEnergyGetDescriptorFunction() override
{}
190 // BluetoothLowEnergyExtensionFunction override.
191 bool DoWork() override
;
194 class BluetoothLowEnergyGetDescriptorsFunction
195 : public BluetoothLowEnergyExtensionFunction
{
197 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptors",
198 BLUETOOTHLOWENERGY_GETDESCRIPTORS
);
201 ~BluetoothLowEnergyGetDescriptorsFunction() override
{}
203 // BluetoothLowEnergyExtensionFunction override.
204 bool DoWork() override
;
207 class BluetoothLowEnergyReadCharacteristicValueFunction
208 : public BluetoothLowEnergyExtensionFunction
{
210 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readCharacteristicValue",
211 BLUETOOTHLOWENERGY_READCHARACTERISTICVALUE
);
214 ~BluetoothLowEnergyReadCharacteristicValueFunction() override
{}
216 // BluetoothLowEnergyExtensionFunction override.
217 bool DoWork() override
;
220 // Success and error callbacks, called by
221 // BluetoothLowEnergyEventRouter::ReadCharacteristicValue.
222 void SuccessCallback();
223 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
225 // The instance ID of the requested characteristic.
226 std::string instance_id_
;
229 class BluetoothLowEnergyWriteCharacteristicValueFunction
230 : public BluetoothLowEnergyExtensionFunction
{
232 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeCharacteristicValue",
233 BLUETOOTHLOWENERGY_WRITECHARACTERISTICVALUE
);
236 ~BluetoothLowEnergyWriteCharacteristicValueFunction() override
{}
238 // BluetoothLowEnergyExtensionFunction override.
239 bool DoWork() override
;
242 // Success and error callbacks, called by
243 // BluetoothLowEnergyEventRouter::WriteCharacteristicValue.
244 void SuccessCallback();
245 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
247 // The instance ID of the requested characteristic.
248 std::string instance_id_
;
251 class BluetoothLowEnergyStartCharacteristicNotificationsFunction
252 : public BluetoothLowEnergyExtensionFunction
{
254 DECLARE_EXTENSION_FUNCTION(
255 "bluetoothLowEnergy.startCharacteristicNotifications",
256 BLUETOOTHLOWENERGY_STARTCHARACTERISTICNOTIFICATIONS
);
259 ~BluetoothLowEnergyStartCharacteristicNotificationsFunction() override
{}
261 // BluetoothLowEnergyExtensionFunction override.
262 bool DoWork() override
;
265 // Success and error callbacks, called by
266 // BluetoothLowEnergyEventRouter::StartCharacteristicNotifications.
267 void SuccessCallback();
268 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
271 class BluetoothLowEnergyStopCharacteristicNotificationsFunction
272 : public BluetoothLowEnergyExtensionFunction
{
274 DECLARE_EXTENSION_FUNCTION(
275 "bluetoothLowEnergy.stopCharacteristicNotifications",
276 BLUETOOTHLOWENERGY_STOPCHARACTERISTICNOTIFICATIONS
);
279 ~BluetoothLowEnergyStopCharacteristicNotificationsFunction() override
{}
281 // BluetoothLowEnergyExtensionFunction override.
282 bool DoWork() override
;
285 // Success and error callbacks, called by
286 // BluetoothLowEnergyEventRouter::StopCharacteristicNotifications.
287 void SuccessCallback();
288 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
291 class BluetoothLowEnergyReadDescriptorValueFunction
292 : public BluetoothLowEnergyExtensionFunction
{
294 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readDescriptorValue",
295 BLUETOOTHLOWENERGY_READDESCRIPTORVALUE
);
298 ~BluetoothLowEnergyReadDescriptorValueFunction() override
{}
300 // BluetoothLowEnergyExtensionFunction override.
301 bool DoWork() override
;
304 // Success and error callbacks, called by
305 // BluetoothLowEnergyEventRouter::ReadDescriptorValue.
306 void SuccessCallback();
307 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
309 // The instance ID of the requested descriptor.
310 std::string instance_id_
;
313 class BluetoothLowEnergyWriteDescriptorValueFunction
314 : public BluetoothLowEnergyExtensionFunction
{
316 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeDescriptorValue",
317 BLUETOOTHLOWENERGY_WRITEDESCRIPTORVALUE
);
320 ~BluetoothLowEnergyWriteDescriptorValueFunction() override
{}
322 // BluetoothLowEnergyExtensionFunction override.
323 bool DoWork() override
;
326 // Success and error callbacks, called by
327 // BluetoothLowEnergyEventRouter::WriteDescriptorValue.
328 void SuccessCallback();
329 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
331 // The instance ID of the requested descriptor.
332 std::string instance_id_
;
335 class BluetoothLowEnergyAdvertisementFunction
336 : public BluetoothLowEnergyExtensionFunction
{
338 BluetoothLowEnergyAdvertisementFunction();
341 ~BluetoothLowEnergyAdvertisementFunction() override
;
344 int AddAdvertisement(BluetoothApiAdvertisement
* advertisement
);
345 BluetoothApiAdvertisement
* GetAdvertisement(int advertisement_id
);
346 void RemoveAdvertisement(int advertisement_id
);
348 // ExtensionFunction override.
349 bool RunAsync() override
;
354 ApiResourceManager
<BluetoothApiAdvertisement
>* advertisements_manager_
;
356 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAdvertisementFunction
);
359 class BluetoothLowEnergyRegisterAdvertisementFunction
360 : public BluetoothLowEnergyAdvertisementFunction
{
362 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.registerAdvertisement",
363 BLUETOOTHLOWENERGY_REGISTERADVERTISEMENT
);
366 ~BluetoothLowEnergyRegisterAdvertisementFunction() override
{}
368 // BluetoothLowEnergyExtensionFunction override.
369 bool DoWork() override
;
372 void SuccessCallback(scoped_refptr
<device::BluetoothAdvertisement
>);
373 void ErrorCallback(device::BluetoothAdvertisement::ErrorCode status
);
375 // The instance ID of the requested descriptor.
376 std::string instance_id_
;
379 class BluetoothLowEnergyUnregisterAdvertisementFunction
380 : public BluetoothLowEnergyAdvertisementFunction
{
382 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.unregisterAdvertisement",
383 BLUETOOTHLOWENERGY_UNREGISTERADVERTISEMENT
);
386 ~BluetoothLowEnergyUnregisterAdvertisementFunction() override
{}
388 // BluetoothLowEnergyExtensionFunction override.
389 bool DoWork() override
;
392 void SuccessCallback(int advertisement_id
);
393 void ErrorCallback(int advertisement_id
,
394 device::BluetoothAdvertisement::ErrorCode status
);
396 // The instance ID of the requested descriptor.
397 std::string instance_id_
;
401 } // namespace extensions
403 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_API_H_