Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / bluetooth_low_energy / bluetooth_low_energy_api.h
blobd16172854fc9465fb6c33715547bb5d5a1afb770
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 {
23 public:
24 static BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>*
25 GetFactoryInstance();
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;
45 private:
46 friend class BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>;
48 scoped_ptr<BluetoothLowEnergyEventRouter> event_router_;
50 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAPI);
53 namespace api {
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 {
59 public:
60 BluetoothLowEnergyExtensionFunction();
62 protected:
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
71 // BluetoothAdapter.
72 virtual bool DoWork() = 0;
74 private:
75 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyExtensionFunction);
78 class BluetoothLowEnergyConnectFunction
79 : public BluetoothLowEnergyExtensionFunction {
80 public:
81 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.connect",
82 BLUETOOTHLOWENERGY_CONNECT);
84 protected:
85 ~BluetoothLowEnergyConnectFunction() override {}
87 // BluetoothLowEnergyExtensionFunction override.
88 bool DoWork() override;
90 private:
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 {
99 public:
100 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.disconnect",
101 BLUETOOTHLOWENERGY_DISCONNECT);
103 protected:
104 ~BluetoothLowEnergyDisconnectFunction() override {}
106 // BluetoothLowEnergyExtensionFunction override.
107 bool DoWork() override;
109 private:
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 {
118 public:
119 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getService",
120 BLUETOOTHLOWENERGY_GETSERVICE);
122 protected:
123 ~BluetoothLowEnergyGetServiceFunction() override {}
125 // BluetoothLowEnergyExtensionFunction override.
126 bool DoWork() override;
129 class BluetoothLowEnergyGetServicesFunction
130 : public BluetoothLowEnergyExtensionFunction {
131 public:
132 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getServices",
133 BLUETOOTHLOWENERGY_GETSERVICES);
135 protected:
136 ~BluetoothLowEnergyGetServicesFunction() override {}
138 // BluetoothLowEnergyExtensionFunction override.
139 bool DoWork() override;
142 class BluetoothLowEnergyGetCharacteristicFunction
143 : public BluetoothLowEnergyExtensionFunction {
144 public:
145 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristic",
146 BLUETOOTHLOWENERGY_GETCHARACTERISTIC);
148 protected:
149 ~BluetoothLowEnergyGetCharacteristicFunction() override {}
151 // BluetoothLowEnergyExtensionFunction override.
152 bool DoWork() override;
155 class BluetoothLowEnergyGetCharacteristicsFunction
156 : public BluetoothLowEnergyExtensionFunction {
157 public:
158 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristics",
159 BLUETOOTHLOWENERGY_GETCHARACTERISTICS);
161 protected:
162 ~BluetoothLowEnergyGetCharacteristicsFunction() override {}
164 // BluetoothLowEnergyExtensionFunction override.
165 bool DoWork() override;
168 class BluetoothLowEnergyGetIncludedServicesFunction
169 : public BluetoothLowEnergyExtensionFunction {
170 public:
171 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getIncludedServices",
172 BLUETOOTHLOWENERGY_GETINCLUDEDSERVICES);
174 protected:
175 ~BluetoothLowEnergyGetIncludedServicesFunction() override {}
177 // BluetoothLowEnergyExtensionFunction override.
178 bool DoWork() override;
181 class BluetoothLowEnergyGetDescriptorFunction
182 : public BluetoothLowEnergyExtensionFunction {
183 public:
184 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptor",
185 BLUETOOTHLOWENERGY_GETDESCRIPTOR);
187 protected:
188 ~BluetoothLowEnergyGetDescriptorFunction() override {}
190 // BluetoothLowEnergyExtensionFunction override.
191 bool DoWork() override;
194 class BluetoothLowEnergyGetDescriptorsFunction
195 : public BluetoothLowEnergyExtensionFunction {
196 public:
197 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptors",
198 BLUETOOTHLOWENERGY_GETDESCRIPTORS);
200 protected:
201 ~BluetoothLowEnergyGetDescriptorsFunction() override {}
203 // BluetoothLowEnergyExtensionFunction override.
204 bool DoWork() override;
207 class BluetoothLowEnergyReadCharacteristicValueFunction
208 : public BluetoothLowEnergyExtensionFunction {
209 public:
210 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readCharacteristicValue",
211 BLUETOOTHLOWENERGY_READCHARACTERISTICVALUE);
213 protected:
214 ~BluetoothLowEnergyReadCharacteristicValueFunction() override {}
216 // BluetoothLowEnergyExtensionFunction override.
217 bool DoWork() override;
219 private:
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 {
231 public:
232 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeCharacteristicValue",
233 BLUETOOTHLOWENERGY_WRITECHARACTERISTICVALUE);
235 protected:
236 ~BluetoothLowEnergyWriteCharacteristicValueFunction() override {}
238 // BluetoothLowEnergyExtensionFunction override.
239 bool DoWork() override;
241 private:
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 {
253 public:
254 DECLARE_EXTENSION_FUNCTION(
255 "bluetoothLowEnergy.startCharacteristicNotifications",
256 BLUETOOTHLOWENERGY_STARTCHARACTERISTICNOTIFICATIONS);
258 protected:
259 ~BluetoothLowEnergyStartCharacteristicNotificationsFunction() override {}
261 // BluetoothLowEnergyExtensionFunction override.
262 bool DoWork() override;
264 private:
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 {
273 public:
274 DECLARE_EXTENSION_FUNCTION(
275 "bluetoothLowEnergy.stopCharacteristicNotifications",
276 BLUETOOTHLOWENERGY_STOPCHARACTERISTICNOTIFICATIONS);
278 protected:
279 ~BluetoothLowEnergyStopCharacteristicNotificationsFunction() override {}
281 // BluetoothLowEnergyExtensionFunction override.
282 bool DoWork() override;
284 private:
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 {
293 public:
294 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readDescriptorValue",
295 BLUETOOTHLOWENERGY_READDESCRIPTORVALUE);
297 protected:
298 ~BluetoothLowEnergyReadDescriptorValueFunction() override {}
300 // BluetoothLowEnergyExtensionFunction override.
301 bool DoWork() override;
303 private:
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 {
315 public:
316 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeDescriptorValue",
317 BLUETOOTHLOWENERGY_WRITEDESCRIPTORVALUE);
319 protected:
320 ~BluetoothLowEnergyWriteDescriptorValueFunction() override {}
322 // BluetoothLowEnergyExtensionFunction override.
323 bool DoWork() override;
325 private:
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 {
337 public:
338 BluetoothLowEnergyAdvertisementFunction();
340 protected:
341 ~BluetoothLowEnergyAdvertisementFunction() override;
343 // Takes ownership.
344 int AddAdvertisement(BluetoothApiAdvertisement* advertisement);
345 BluetoothApiAdvertisement* GetAdvertisement(int advertisement_id);
346 void RemoveAdvertisement(int advertisement_id);
348 // ExtensionFunction override.
349 bool RunAsync() override;
351 private:
352 void Initialize();
354 ApiResourceManager<BluetoothApiAdvertisement>* advertisements_manager_;
356 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAdvertisementFunction);
359 class BluetoothLowEnergyRegisterAdvertisementFunction
360 : public BluetoothLowEnergyAdvertisementFunction {
361 public:
362 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.registerAdvertisement",
363 BLUETOOTHLOWENERGY_REGISTERADVERTISEMENT);
365 protected:
366 ~BluetoothLowEnergyRegisterAdvertisementFunction() override {}
368 // BluetoothLowEnergyExtensionFunction override.
369 bool DoWork() override;
371 private:
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 {
381 public:
382 DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.unregisterAdvertisement",
383 BLUETOOTHLOWENERGY_UNREGISTERADVERTISEMENT);
385 protected:
386 ~BluetoothLowEnergyUnregisterAdvertisementFunction() override {}
388 // BluetoothLowEnergyExtensionFunction override.
389 bool DoWork() override;
391 private:
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_;
400 } // namespace api
401 } // namespace extensions
403 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_API_H_