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