Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / bluetooth_low_energy / bluetooth_low_energy_api.h
blob834cca3fdf38d199e235a83d249d968400cc16e3
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 {
20 public:
21 static BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>*
22 GetFactoryInstance();
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;
42 private:
43 friend class BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>;
45 scoped_ptr<BluetoothLowEnergyEventRouter> event_router_;
47 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAPI);
50 namespace core_api {
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 {
56 public:
57 BluetoothLowEnergyExtensionFunction();
59 protected:
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
68 // BluetoothAdapter.
69 virtual bool DoWork() = 0;
71 private:
72 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyExtensionFunction);
75 class BluetoothLowEnergyConnectFunction
76 : public BluetoothLowEnergyExtensionFunction {
77 public:
78 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.connect",
79 BLUETOOTHLOWENERGY_CONNECT);
81 protected:
82 ~BluetoothLowEnergyConnectFunction() override {}
84 // BluetoothLowEnergyExtensionFunction override.
85 bool DoWork() override;
87 private:
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 {
96 public:
97 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.disconnect",
98 BLUETOOTHLOWENERGY_DISCONNECT);
100 protected:
101 ~BluetoothLowEnergyDisconnectFunction() override {}
103 // BluetoothLowEnergyExtensionFunction override.
104 bool DoWork() override;
106 private:
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 {
115 public:
116 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getService",
117 BLUETOOTHLOWENERGY_GETSERVICE);
119 protected:
120 ~BluetoothLowEnergyGetServiceFunction() override {}
122 // BluetoothLowEnergyExtensionFunction override.
123 bool DoWork() override;
126 class BluetoothLowEnergyGetServicesFunction
127 : public BluetoothLowEnergyExtensionFunction {
128 public:
129 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getServices",
130 BLUETOOTHLOWENERGY_GETSERVICES);
132 protected:
133 ~BluetoothLowEnergyGetServicesFunction() override {}
135 // BluetoothLowEnergyExtensionFunction override.
136 bool DoWork() override;
139 class BluetoothLowEnergyGetCharacteristicFunction
140 : public BluetoothLowEnergyExtensionFunction {
141 public:
142 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristic",
143 BLUETOOTHLOWENERGY_GETCHARACTERISTIC);
145 protected:
146 ~BluetoothLowEnergyGetCharacteristicFunction() override {}
148 // BluetoothLowEnergyExtensionFunction override.
149 bool DoWork() override;
152 class BluetoothLowEnergyGetCharacteristicsFunction
153 : public BluetoothLowEnergyExtensionFunction {
154 public:
155 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristics",
156 BLUETOOTHLOWENERGY_GETCHARACTERISTICS);
158 protected:
159 ~BluetoothLowEnergyGetCharacteristicsFunction() override {}
161 // BluetoothLowEnergyExtensionFunction override.
162 bool DoWork() override;
165 class BluetoothLowEnergyGetIncludedServicesFunction
166 : public BluetoothLowEnergyExtensionFunction {
167 public:
168 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getIncludedServices",
169 BLUETOOTHLOWENERGY_GETINCLUDEDSERVICES);
171 protected:
172 ~BluetoothLowEnergyGetIncludedServicesFunction() override {}
174 // BluetoothLowEnergyExtensionFunction override.
175 bool DoWork() override;
178 class BluetoothLowEnergyGetDescriptorFunction
179 : public BluetoothLowEnergyExtensionFunction {
180 public:
181 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptor",
182 BLUETOOTHLOWENERGY_GETDESCRIPTOR);
184 protected:
185 ~BluetoothLowEnergyGetDescriptorFunction() override {}
187 // BluetoothLowEnergyExtensionFunction override.
188 bool DoWork() override;
191 class BluetoothLowEnergyGetDescriptorsFunction
192 : public BluetoothLowEnergyExtensionFunction {
193 public:
194 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptors",
195 BLUETOOTHLOWENERGY_GETDESCRIPTORS);
197 protected:
198 ~BluetoothLowEnergyGetDescriptorsFunction() override {}
200 // BluetoothLowEnergyExtensionFunction override.
201 bool DoWork() override;
204 class BluetoothLowEnergyReadCharacteristicValueFunction
205 : public BluetoothLowEnergyExtensionFunction {
206 public:
207 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readCharacteristicValue",
208 BLUETOOTHLOWENERGY_READCHARACTERISTICVALUE);
210 protected:
211 ~BluetoothLowEnergyReadCharacteristicValueFunction() override {}
213 // BluetoothLowEnergyExtensionFunction override.
214 bool DoWork() override;
216 private:
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 {
228 public:
229 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeCharacteristicValue",
230 BLUETOOTHLOWENERGY_WRITECHARACTERISTICVALUE);
232 protected:
233 ~BluetoothLowEnergyWriteCharacteristicValueFunction() override {}
235 // BluetoothLowEnergyExtensionFunction override.
236 bool DoWork() override;
238 private:
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 {
250 public:
251 DECLARE_EXTENSION_FUNCTION(
252 "bluetoothLowEnergy.startCharacteristicNotifications",
253 BLUETOOTHLOWENERGY_STARTCHARACTERISTICNOTIFICATIONS);
255 protected:
256 ~BluetoothLowEnergyStartCharacteristicNotificationsFunction() override {}
258 // BluetoothLowEnergyExtensionFunction override.
259 bool DoWork() override;
261 private:
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 {
270 public:
271 DECLARE_EXTENSION_FUNCTION(
272 "bluetoothLowEnergy.stopCharacteristicNotifications",
273 BLUETOOTHLOWENERGY_STOPCHARACTERISTICNOTIFICATIONS);
275 protected:
276 ~BluetoothLowEnergyStopCharacteristicNotificationsFunction() override {}
278 // BluetoothLowEnergyExtensionFunction override.
279 bool DoWork() override;
281 private:
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 {
290 public:
291 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readDescriptorValue",
292 BLUETOOTHLOWENERGY_READDESCRIPTORVALUE);
294 protected:
295 ~BluetoothLowEnergyReadDescriptorValueFunction() override {}
297 // BluetoothLowEnergyExtensionFunction override.
298 bool DoWork() override;
300 private:
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 {
312 public:
313 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeDescriptorValue",
314 BLUETOOTHLOWENERGY_WRITEDESCRIPTORVALUE);
316 protected:
317 ~BluetoothLowEnergyWriteDescriptorValueFunction() override {}
319 // BluetoothLowEnergyExtensionFunction override.
320 bool DoWork() override;
322 private:
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_