/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.h

https://gitlab.com/0072016/Facebook-SDK- · C Header · 405 lines · 253 code · 96 blank · 56 comment · 0 complexity · de76e0ae8ae6211d9a4f27e314d18893 MD5 · raw file

  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.
  4. #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_API_H_
  5. #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_API_H_
  6. #include <memory>
  7. #include "base/macros.h"
  8. #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.h"
  9. #include "chrome/browser/extensions/browser_context_keyed_service_factories.h"
  10. #include "device/bluetooth/bluetooth_advertisement.h"
  11. #include "extensions/browser/api/api_resource_manager.h"
  12. #include "extensions/browser/extension_function.h"
  13. #include "extensions/browser/extension_function_histogram_value.h"
  14. namespace extensions {
  15. class BluetoothApiAdvertisement;
  16. class BluetoothLowEnergyEventRouter;
  17. // The profile-keyed service that manages the bluetoothLowEnergy extension API.
  18. class BluetoothLowEnergyAPI : public BrowserContextKeyedAPI {
  19. public:
  20. static BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>*
  21. GetFactoryInstance();
  22. // Convenience method to get the BluetoothLowEnergy API for a browser context.
  23. static BluetoothLowEnergyAPI* Get(content::BrowserContext* context);
  24. explicit BluetoothLowEnergyAPI(content::BrowserContext* context);
  25. ~BluetoothLowEnergyAPI() override;
  26. // KeyedService implementation..
  27. void Shutdown() override;
  28. BluetoothLowEnergyEventRouter* event_router() const {
  29. return event_router_.get();
  30. }
  31. // BrowserContextKeyedAPI implementation.
  32. static const char* service_name() { return "BluetoothLowEnergyAPI"; }
  33. static const bool kServiceRedirectedInIncognito = true;
  34. static const bool kServiceIsNULLWhileTesting = true;
  35. private:
  36. friend class BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>;
  37. std::unique_ptr<BluetoothLowEnergyEventRouter> event_router_;
  38. DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAPI);
  39. };
  40. namespace api {
  41. // Base class for bluetoothLowEnergy API functions. This class handles some of
  42. // the common logic involved in all API functions, such as checking for
  43. // platform support and returning the correct error.
  44. class BluetoothLowEnergyExtensionFunction : public AsyncExtensionFunction {
  45. public:
  46. BluetoothLowEnergyExtensionFunction();
  47. protected:
  48. ~BluetoothLowEnergyExtensionFunction() override;
  49. // ExtensionFunction override.
  50. bool RunAsync() override;
  51. // Implemented by individual bluetoothLowEnergy extension functions to perform
  52. // the body of the function. This invoked asynchonously after RunAsync after
  53. // the BluetoothLowEnergyEventRouter has obtained a handle on the
  54. // BluetoothAdapter.
  55. virtual bool DoWork() = 0;
  56. private:
  57. DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyExtensionFunction);
  58. };
  59. class BluetoothLowEnergyConnectFunction
  60. : public BluetoothLowEnergyExtensionFunction {
  61. public:
  62. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.connect",
  63. BLUETOOTHLOWENERGY_CONNECT);
  64. protected:
  65. ~BluetoothLowEnergyConnectFunction() override {}
  66. // BluetoothLowEnergyExtensionFunction override.
  67. bool DoWork() override;
  68. private:
  69. // Success and error callbacks, called by
  70. // BluetoothLowEnergyEventRouter::Connect.
  71. void SuccessCallback();
  72. void ErrorCallback(BluetoothLowEnergyEventRouter::Status status);
  73. };
  74. class BluetoothLowEnergyDisconnectFunction
  75. : public BluetoothLowEnergyExtensionFunction {
  76. public:
  77. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.disconnect",
  78. BLUETOOTHLOWENERGY_DISCONNECT);
  79. protected:
  80. ~BluetoothLowEnergyDisconnectFunction() override {}
  81. // BluetoothLowEnergyExtensionFunction override.
  82. bool DoWork() override;
  83. private:
  84. // Success and error callbacks, called by
  85. // BluetoothLowEnergyEventRouter::Disconnect.
  86. void SuccessCallback();
  87. void ErrorCallback(BluetoothLowEnergyEventRouter::Status status);
  88. };
  89. class BluetoothLowEnergyGetServiceFunction
  90. : public BluetoothLowEnergyExtensionFunction {
  91. public:
  92. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getService",
  93. BLUETOOTHLOWENERGY_GETSERVICE);
  94. protected:
  95. ~BluetoothLowEnergyGetServiceFunction() override {}
  96. // BluetoothLowEnergyExtensionFunction override.
  97. bool DoWork() override;
  98. };
  99. class BluetoothLowEnergyGetServicesFunction
  100. : public BluetoothLowEnergyExtensionFunction {
  101. public:
  102. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getServices",
  103. BLUETOOTHLOWENERGY_GETSERVICES);
  104. protected:
  105. ~BluetoothLowEnergyGetServicesFunction() override {}
  106. // BluetoothLowEnergyExtensionFunction override.
  107. bool DoWork() override;
  108. };
  109. class BluetoothLowEnergyGetCharacteristicFunction
  110. : public BluetoothLowEnergyExtensionFunction {
  111. public:
  112. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristic",
  113. BLUETOOTHLOWENERGY_GETCHARACTERISTIC);
  114. protected:
  115. ~BluetoothLowEnergyGetCharacteristicFunction() override {}
  116. // BluetoothLowEnergyExtensionFunction override.
  117. bool DoWork() override;
  118. };
  119. class BluetoothLowEnergyGetCharacteristicsFunction
  120. : public BluetoothLowEnergyExtensionFunction {
  121. public:
  122. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getCharacteristics",
  123. BLUETOOTHLOWENERGY_GETCHARACTERISTICS);
  124. protected:
  125. ~BluetoothLowEnergyGetCharacteristicsFunction() override {}
  126. // BluetoothLowEnergyExtensionFunction override.
  127. bool DoWork() override;
  128. };
  129. class BluetoothLowEnergyGetIncludedServicesFunction
  130. : public BluetoothLowEnergyExtensionFunction {
  131. public:
  132. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getIncludedServices",
  133. BLUETOOTHLOWENERGY_GETINCLUDEDSERVICES);
  134. protected:
  135. ~BluetoothLowEnergyGetIncludedServicesFunction() override {}
  136. // BluetoothLowEnergyExtensionFunction override.
  137. bool DoWork() override;
  138. };
  139. class BluetoothLowEnergyGetDescriptorFunction
  140. : public BluetoothLowEnergyExtensionFunction {
  141. public:
  142. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptor",
  143. BLUETOOTHLOWENERGY_GETDESCRIPTOR);
  144. protected:
  145. ~BluetoothLowEnergyGetDescriptorFunction() override {}
  146. // BluetoothLowEnergyExtensionFunction override.
  147. bool DoWork() override;
  148. };
  149. class BluetoothLowEnergyGetDescriptorsFunction
  150. : public BluetoothLowEnergyExtensionFunction {
  151. public:
  152. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.getDescriptors",
  153. BLUETOOTHLOWENERGY_GETDESCRIPTORS);
  154. protected:
  155. ~BluetoothLowEnergyGetDescriptorsFunction() override {}
  156. // BluetoothLowEnergyExtensionFunction override.
  157. bool DoWork() override;
  158. };
  159. class BluetoothLowEnergyReadCharacteristicValueFunction
  160. : public BluetoothLowEnergyExtensionFunction {
  161. public:
  162. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readCharacteristicValue",
  163. BLUETOOTHLOWENERGY_READCHARACTERISTICVALUE);
  164. protected:
  165. ~BluetoothLowEnergyReadCharacteristicValueFunction() override {}
  166. // BluetoothLowEnergyExtensionFunction override.
  167. bool DoWork() override;
  168. private:
  169. // Success and error callbacks, called by
  170. // BluetoothLowEnergyEventRouter::ReadCharacteristicValue.
  171. void SuccessCallback();
  172. void ErrorCallback(BluetoothLowEnergyEventRouter::Status status);
  173. // The instance ID of the requested characteristic.
  174. std::string instance_id_;
  175. };
  176. class BluetoothLowEnergyWriteCharacteristicValueFunction
  177. : public BluetoothLowEnergyExtensionFunction {
  178. public:
  179. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeCharacteristicValue",
  180. BLUETOOTHLOWENERGY_WRITECHARACTERISTICVALUE);
  181. protected:
  182. ~BluetoothLowEnergyWriteCharacteristicValueFunction() override {}
  183. // BluetoothLowEnergyExtensionFunction override.
  184. bool DoWork() override;
  185. private:
  186. // Success and error callbacks, called by
  187. // BluetoothLowEnergyEventRouter::WriteCharacteristicValue.
  188. void SuccessCallback();
  189. void ErrorCallback(BluetoothLowEnergyEventRouter::Status status);
  190. // The instance ID of the requested characteristic.
  191. std::string instance_id_;
  192. };
  193. class BluetoothLowEnergyStartCharacteristicNotificationsFunction
  194. : public BluetoothLowEnergyExtensionFunction {
  195. public:
  196. DECLARE_EXTENSION_FUNCTION(
  197. "bluetoothLowEnergy.startCharacteristicNotifications",
  198. BLUETOOTHLOWENERGY_STARTCHARACTERISTICNOTIFICATIONS);
  199. protected:
  200. ~BluetoothLowEnergyStartCharacteristicNotificationsFunction() override {}
  201. // BluetoothLowEnergyExtensionFunction override.
  202. bool DoWork() override;
  203. private:
  204. // Success and error callbacks, called by
  205. // BluetoothLowEnergyEventRouter::StartCharacteristicNotifications.
  206. void SuccessCallback();
  207. void ErrorCallback(BluetoothLowEnergyEventRouter::Status status);
  208. };
  209. class BluetoothLowEnergyStopCharacteristicNotificationsFunction
  210. : public BluetoothLowEnergyExtensionFunction {
  211. public:
  212. DECLARE_EXTENSION_FUNCTION(
  213. "bluetoothLowEnergy.stopCharacteristicNotifications",
  214. BLUETOOTHLOWENERGY_STOPCHARACTERISTICNOTIFICATIONS);
  215. protected:
  216. ~BluetoothLowEnergyStopCharacteristicNotificationsFunction() override {}
  217. // BluetoothLowEnergyExtensionFunction override.
  218. bool DoWork() override;
  219. private:
  220. // Success and error callbacks, called by
  221. // BluetoothLowEnergyEventRouter::StopCharacteristicNotifications.
  222. void SuccessCallback();
  223. void ErrorCallback(BluetoothLowEnergyEventRouter::Status status);
  224. };
  225. class BluetoothLowEnergyReadDescriptorValueFunction
  226. : public BluetoothLowEnergyExtensionFunction {
  227. public:
  228. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.readDescriptorValue",
  229. BLUETOOTHLOWENERGY_READDESCRIPTORVALUE);
  230. protected:
  231. ~BluetoothLowEnergyReadDescriptorValueFunction() override {}
  232. // BluetoothLowEnergyExtensionFunction override.
  233. bool DoWork() override;
  234. private:
  235. // Success and error callbacks, called by
  236. // BluetoothLowEnergyEventRouter::ReadDescriptorValue.
  237. void SuccessCallback();
  238. void ErrorCallback(BluetoothLowEnergyEventRouter::Status status);
  239. // The instance ID of the requested descriptor.
  240. std::string instance_id_;
  241. };
  242. class BluetoothLowEnergyWriteDescriptorValueFunction
  243. : public BluetoothLowEnergyExtensionFunction {
  244. public:
  245. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.writeDescriptorValue",
  246. BLUETOOTHLOWENERGY_WRITEDESCRIPTORVALUE);
  247. protected:
  248. ~BluetoothLowEnergyWriteDescriptorValueFunction() override {}
  249. // BluetoothLowEnergyExtensionFunction override.
  250. bool DoWork() override;
  251. private:
  252. // Success and error callbacks, called by
  253. // BluetoothLowEnergyEventRouter::WriteDescriptorValue.
  254. void SuccessCallback();
  255. void ErrorCallback(BluetoothLowEnergyEventRouter::Status status);
  256. // The instance ID of the requested descriptor.
  257. std::string instance_id_;
  258. };
  259. class BluetoothLowEnergyAdvertisementFunction
  260. : public BluetoothLowEnergyExtensionFunction {
  261. public:
  262. BluetoothLowEnergyAdvertisementFunction();
  263. protected:
  264. ~BluetoothLowEnergyAdvertisementFunction() override;
  265. // Takes ownership.
  266. int AddAdvertisement(BluetoothApiAdvertisement* advertisement);
  267. BluetoothApiAdvertisement* GetAdvertisement(int advertisement_id);
  268. void RemoveAdvertisement(int advertisement_id);
  269. // ExtensionFunction override.
  270. bool RunAsync() override;
  271. private:
  272. void Initialize();
  273. ApiResourceManager<BluetoothApiAdvertisement>* advertisements_manager_;
  274. DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAdvertisementFunction);
  275. };
  276. class BluetoothLowEnergyRegisterAdvertisementFunction
  277. : public BluetoothLowEnergyAdvertisementFunction {
  278. public:
  279. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.registerAdvertisement",
  280. BLUETOOTHLOWENERGY_REGISTERADVERTISEMENT);
  281. protected:
  282. ~BluetoothLowEnergyRegisterAdvertisementFunction() override {}
  283. // BluetoothLowEnergyExtensionFunction override.
  284. bool DoWork() override;
  285. private:
  286. void SuccessCallback(scoped_refptr<device::BluetoothAdvertisement>);
  287. void ErrorCallback(device::BluetoothAdvertisement::ErrorCode status);
  288. // The instance ID of the requested descriptor.
  289. std::string instance_id_;
  290. };
  291. class BluetoothLowEnergyUnregisterAdvertisementFunction
  292. : public BluetoothLowEnergyAdvertisementFunction {
  293. public:
  294. DECLARE_EXTENSION_FUNCTION("bluetoothLowEnergy.unregisterAdvertisement",
  295. BLUETOOTHLOWENERGY_UNREGISTERADVERTISEMENT);
  296. protected:
  297. ~BluetoothLowEnergyUnregisterAdvertisementFunction() override {}
  298. // BluetoothLowEnergyExtensionFunction override.
  299. bool DoWork() override;
  300. private:
  301. void SuccessCallback(int advertisement_id);
  302. void ErrorCallback(int advertisement_id,
  303. device::BluetoothAdvertisement::ErrorCode status);
  304. // The instance ID of the requested descriptor.
  305. std::string instance_id_;
  306. };
  307. } // namespace api
  308. } // namespace extensions
  309. #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_API_H_