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 "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_event_router.h"
10 #include "extensions/browser/browser_context_keyed_api_factory.h"
11 #include "extensions/browser/extension_function.h"
12 #include "extensions/browser/extension_function_histogram_value.h"
14 namespace extensions
{
16 class BluetoothLowEnergyEventRouter
;
18 // The profile-keyed service that manages the bluetoothLowEnergy extension API.
19 class BluetoothLowEnergyAPI
: public BrowserContextKeyedAPI
{
21 static BrowserContextKeyedAPIFactory
<BluetoothLowEnergyAPI
>*
24 // Convenience method to get the BluetoothLowEnergy API for a browser context.
25 static BluetoothLowEnergyAPI
* Get(content::BrowserContext
* context
);
27 explicit BluetoothLowEnergyAPI(content::BrowserContext
* context
);
28 ~BluetoothLowEnergyAPI() override
;
30 // KeyedService implementation..
31 void Shutdown() override
;
33 BluetoothLowEnergyEventRouter
* event_router() const {
34 return event_router_
.get();
37 // BrowserContextKeyedAPI implementation.
38 static const char* service_name() { return "BluetoothLowEnergyAPI"; }
39 static const bool kServiceRedirectedInIncognito
= true;
40 static const bool kServiceIsNULLWhileTesting
= true;
43 friend class BrowserContextKeyedAPIFactory
<BluetoothLowEnergyAPI
>;
45 scoped_ptr
<BluetoothLowEnergyEventRouter
> event_router_
;
47 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAPI
);
52 // Base class for bluetoothLowEnergy API functions. This class handles some of
53 // the common logic involved in all API functions, such as checking for
54 // platform support and returning the correct error.
55 class BluetoothLowEnergyExtensionFunction
: public AsyncExtensionFunction
{
57 BluetoothLowEnergyExtensionFunction();
60 ~BluetoothLowEnergyExtensionFunction() override
;
62 // ExtensionFunction override.
63 bool RunAsync() override
;
65 // Implemented by individual bluetoothLowEnergy extension functions to perform
66 // the body of the function. This invoked asynchonously after RunAsync after
67 // the BluetoothLowEnergyEventRouter has obtained a handle on the
69 virtual bool DoWork() = 0;
72 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyExtensionFunction
);
75 class BluetoothLowEnergyConnectFunction
76 : public BluetoothLowEnergyExtensionFunction
{
78 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.connect",
79 BLUETOOTHLOWENERGY_CONNECT
);
82 ~BluetoothLowEnergyConnectFunction() override
{}
84 // BluetoothLowEnergyExtensionFunction override.
85 bool DoWork() override
;
88 // Success and error callbacks, called by
89 // BluetoothLowEnergyEventRouter::Connect.
90 void SuccessCallback();
91 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
94 class BluetoothLowEnergyDisconnectFunction
95 : public BluetoothLowEnergyExtensionFunction
{
97 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.disconnect",
98 BLUETOOTHLOWENERGY_DISCONNECT
);
101 ~BluetoothLowEnergyDisconnectFunction() override
{}
103 // BluetoothLowEnergyExtensionFunction override.
104 bool DoWork() override
;
107 // Success and error callbacks, called by
108 // BluetoothLowEnergyEventRouter::Disconnect.
109 void SuccessCallback();
110 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
113 class BluetoothLowEnergyGetServiceFunction
114 : public BluetoothLowEnergyExtensionFunction
{
116 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getService",
117 BLUETOOTHLOWENERGY_GETSERVICE
);
120 ~BluetoothLowEnergyGetServiceFunction() override
{}
122 // BluetoothLowEnergyExtensionFunction override.
123 bool DoWork() override
;
126 class BluetoothLowEnergyGetServicesFunction
127 : public BluetoothLowEnergyExtensionFunction
{
129 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getServices",
130 BLUETOOTHLOWENERGY_GETSERVICES
);
133 ~BluetoothLowEnergyGetServicesFunction() override
{}
135 // BluetoothLowEnergyExtensionFunction override.
136 bool DoWork() override
;
139 class BluetoothLowEnergyGetCharacteristicFunction
140 : public BluetoothLowEnergyExtensionFunction
{
142 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristic",
143 BLUETOOTHLOWENERGY_GETCHARACTERISTIC
);
146 ~BluetoothLowEnergyGetCharacteristicFunction() override
{}
148 // BluetoothLowEnergyExtensionFunction override.
149 bool DoWork() override
;
152 class BluetoothLowEnergyGetCharacteristicsFunction
153 : public BluetoothLowEnergyExtensionFunction
{
155 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristics",
156 BLUETOOTHLOWENERGY_GETCHARACTERISTICS
);
159 ~BluetoothLowEnergyGetCharacteristicsFunction() override
{}
161 // BluetoothLowEnergyExtensionFunction override.
162 bool DoWork() override
;
165 class BluetoothLowEnergyGetIncludedServicesFunction
166 : public BluetoothLowEnergyExtensionFunction
{
168 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getIncludedServices",
169 BLUETOOTHLOWENERGY_GETINCLUDEDSERVICES
);
172 ~BluetoothLowEnergyGetIncludedServicesFunction() override
{}
174 // BluetoothLowEnergyExtensionFunction override.
175 bool DoWork() override
;
178 class BluetoothLowEnergyGetDescriptorFunction
179 : public BluetoothLowEnergyExtensionFunction
{
181 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptor",
182 BLUETOOTHLOWENERGY_GETDESCRIPTOR
);
185 ~BluetoothLowEnergyGetDescriptorFunction() override
{}
187 // BluetoothLowEnergyExtensionFunction override.
188 bool DoWork() override
;
191 class BluetoothLowEnergyGetDescriptorsFunction
192 : public BluetoothLowEnergyExtensionFunction
{
194 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptors",
195 BLUETOOTHLOWENERGY_GETDESCRIPTORS
);
198 ~BluetoothLowEnergyGetDescriptorsFunction() override
{}
200 // BluetoothLowEnergyExtensionFunction override.
201 bool DoWork() override
;
204 class BluetoothLowEnergyReadCharacteristicValueFunction
205 : public BluetoothLowEnergyExtensionFunction
{
207 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readCharacteristicValue",
208 BLUETOOTHLOWENERGY_READCHARACTERISTICVALUE
);
211 ~BluetoothLowEnergyReadCharacteristicValueFunction() override
{}
213 // BluetoothLowEnergyExtensionFunction override.
214 bool DoWork() override
;
217 // Success and error callbacks, called by
218 // BluetoothLowEnergyEventRouter::ReadCharacteristicValue.
219 void SuccessCallback();
220 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
222 // The instance ID of the requested characteristic.
223 std::string instance_id_
;
226 class BluetoothLowEnergyWriteCharacteristicValueFunction
227 : public BluetoothLowEnergyExtensionFunction
{
229 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeCharacteristicValue",
230 BLUETOOTHLOWENERGY_WRITECHARACTERISTICVALUE
);
233 ~BluetoothLowEnergyWriteCharacteristicValueFunction() override
{}
235 // BluetoothLowEnergyExtensionFunction override.
236 bool DoWork() override
;
239 // Success and error callbacks, called by
240 // BluetoothLowEnergyEventRouter::WriteCharacteristicValue.
241 void SuccessCallback();
242 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
244 // The instance ID of the requested characteristic.
245 std::string instance_id_
;
248 class BluetoothLowEnergyStartCharacteristicNotificationsFunction
249 : public BluetoothLowEnergyExtensionFunction
{
251 DECLARE_EXTENSION_FUNCTION(
252 "bluetoothLowEnergy.startCharacteristicNotifications",
253 BLUETOOTHLOWENERGY_STARTCHARACTERISTICNOTIFICATIONS
);
256 ~BluetoothLowEnergyStartCharacteristicNotificationsFunction() override
{}
258 // BluetoothLowEnergyExtensionFunction override.
259 bool DoWork() override
;
262 // Success and error callbacks, called by
263 // BluetoothLowEnergyEventRouter::StartCharacteristicNotifications.
264 void SuccessCallback();
265 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
268 class BluetoothLowEnergyStopCharacteristicNotificationsFunction
269 : public BluetoothLowEnergyExtensionFunction
{
271 DECLARE_EXTENSION_FUNCTION(
272 "bluetoothLowEnergy.stopCharacteristicNotifications",
273 BLUETOOTHLOWENERGY_STOPCHARACTERISTICNOTIFICATIONS
);
276 ~BluetoothLowEnergyStopCharacteristicNotificationsFunction() override
{}
278 // BluetoothLowEnergyExtensionFunction override.
279 bool DoWork() override
;
282 // Success and error callbacks, called by
283 // BluetoothLowEnergyEventRouter::StopCharacteristicNotifications.
284 void SuccessCallback();
285 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
288 class BluetoothLowEnergyReadDescriptorValueFunction
289 : public BluetoothLowEnergyExtensionFunction
{
291 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readDescriptorValue",
292 BLUETOOTHLOWENERGY_READDESCRIPTORVALUE
);
295 ~BluetoothLowEnergyReadDescriptorValueFunction() override
{}
297 // BluetoothLowEnergyExtensionFunction override.
298 bool DoWork() override
;
301 // Success and error callbacks, called by
302 // BluetoothLowEnergyEventRouter::ReadDescriptorValue.
303 void SuccessCallback();
304 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
306 // The instance ID of the requested descriptor.
307 std::string instance_id_
;
310 class BluetoothLowEnergyWriteDescriptorValueFunction
311 : public BluetoothLowEnergyExtensionFunction
{
313 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeDescriptorValue",
314 BLUETOOTHLOWENERGY_WRITEDESCRIPTORVALUE
);
317 ~BluetoothLowEnergyWriteDescriptorValueFunction() override
{}
319 // BluetoothLowEnergyExtensionFunction override.
320 bool DoWork() override
;
323 // Success and error callbacks, called by
324 // BluetoothLowEnergyEventRouter::WriteDescriptorValue.
325 void SuccessCallback();
326 void ErrorCallback(BluetoothLowEnergyEventRouter::Status status
);
328 // The instance ID of the requested descriptor.
329 std::string instance_id_
;
332 } // namespace core_api
333 } // namespace extensions
335 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_API_H_