Home | History | Annotate | Download | only in bluetooth
      1 // Copyright (c) 2012 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_BLUETOOTH_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "chrome/browser/extensions/api/api_function.h"
     13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h"
     14 #include "chrome/browser/extensions/event_router.h"
     15 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     16 #include "device/bluetooth/bluetooth_device.h"
     17 #include "device/bluetooth/bluetooth_profile.h"
     18 
     19 namespace device {
     20 
     21 class BluetoothAdapter;
     22 class BluetoothDevice;
     23 class BluetoothSocket;
     24 struct BluetoothOutOfBandPairingData;
     25 
     26 }  // namespace device
     27 
     28 namespace extensions {
     29 
     30 class ExtensionBluetoothEventRouter;
     31 
     32 // The profile-keyed service that manages the bluetooth extension API.
     33 class BluetoothAPI : public BrowserContextKeyedService,
     34                      public EventRouter::Observer {
     35  public:
     36   // Convenience method to get the BluetoothAPI for a profile.
     37   static BluetoothAPI* Get(Profile* profile);
     38 
     39   explicit BluetoothAPI(Profile* profile);
     40   virtual ~BluetoothAPI();
     41 
     42   ExtensionBluetoothEventRouter* bluetooth_event_router();
     43 
     44   // BrowserContextKeyedService implementation.
     45   virtual void Shutdown() OVERRIDE;
     46 
     47   // EventRouter::Observer implementation.
     48   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
     49   virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
     50 
     51  private:
     52   Profile* profile_;
     53 
     54   // Created lazily on first access.
     55   scoped_ptr<ExtensionBluetoothEventRouter> bluetooth_event_router_;
     56 };
     57 
     58 namespace api {
     59 
     60 class BluetoothAddProfileFunction : public AsyncExtensionFunction {
     61  public:
     62   DECLARE_EXTENSION_FUNCTION("bluetooth.addProfile", BLUETOOTH_ADDPROFILE)
     63 
     64   BluetoothAddProfileFunction();
     65 
     66  protected:
     67   virtual ~BluetoothAddProfileFunction() {}
     68   virtual bool RunImpl() OVERRIDE;
     69 
     70   virtual void RegisterProfile(
     71       const device::BluetoothProfile::Options& options,
     72       const device::BluetoothProfile::ProfileCallback& callback);
     73 
     74  private:
     75   void OnProfileRegistered(device::BluetoothProfile* bluetooth_profile);
     76 
     77   std::string uuid_;
     78 };
     79 
     80 class BluetoothRemoveProfileFunction : public SyncExtensionFunction {
     81  public:
     82   DECLARE_EXTENSION_FUNCTION("bluetooth.removeProfile",
     83                              BLUETOOTH_REMOVEPROFILE)
     84 
     85  protected:
     86   virtual ~BluetoothRemoveProfileFunction() {}
     87   virtual bool RunImpl() OVERRIDE;
     88 };
     89 
     90 class BluetoothGetProfilesFunction : public BluetoothExtensionFunction {
     91  public:
     92   DECLARE_EXTENSION_FUNCTION("bluetooth.getProfiles", BLUETOOTH_GETPROFILES)
     93 
     94  protected:
     95   virtual ~BluetoothGetProfilesFunction() {}
     96 
     97   // BluetoothExtensionFunction:
     98   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
     99 };
    100 
    101 class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
    102  public:
    103   DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
    104                              BLUETOOTH_GETADAPTERSTATE)
    105 
    106  protected:
    107   virtual ~BluetoothGetAdapterStateFunction() {}
    108 
    109   // BluetoothExtensionFunction:
    110   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
    111 };
    112 
    113 class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
    114  public:
    115   DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
    116 
    117   BluetoothGetDevicesFunction();
    118 
    119  protected:
    120   virtual ~BluetoothGetDevicesFunction() {}
    121 
    122   // BluetoothExtensionFunction:
    123   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
    124 
    125  private:
    126   void DispatchDeviceSearchResult(const device::BluetoothDevice& device);
    127   void FinishDeviceSearch();
    128 
    129   int device_events_sent_;
    130 };
    131 
    132 class BluetoothGetServicesFunction : public BluetoothExtensionFunction {
    133  public:
    134   DECLARE_EXTENSION_FUNCTION("bluetooth.getServices", BLUETOOTH_GETSERVICES)
    135 
    136  protected:
    137   virtual ~BluetoothGetServicesFunction() {}
    138 
    139   // BluetoothExtensionFunction:
    140   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
    141 
    142  private:
    143   void GetServiceRecordsCallback(
    144       base::ListValue* services,
    145       const device::BluetoothDevice::ServiceRecordList& records);
    146   void OnErrorCallback();
    147 };
    148 
    149 class BluetoothConnectFunction : public BluetoothExtensionFunction {
    150  public:
    151   DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT)
    152 
    153  protected:
    154   virtual ~BluetoothConnectFunction() {}
    155 
    156   // BluetoothExtensionFunction:
    157   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
    158 
    159  private:
    160   void OnSuccessCallback();
    161   void OnErrorCallback();
    162 };
    163 
    164 class BluetoothDisconnectFunction : public SyncExtensionFunction {
    165  public:
    166   DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT)
    167 
    168  protected:
    169   virtual ~BluetoothDisconnectFunction() {}
    170 
    171   // ExtensionFunction:
    172   virtual bool RunImpl() OVERRIDE;
    173 };
    174 
    175 class BluetoothReadFunction : public AsyncApiFunction {
    176  public:
    177   DECLARE_EXTENSION_FUNCTION("bluetooth.read", BLUETOOTH_READ)
    178   BluetoothReadFunction();
    179 
    180  protected:
    181   virtual ~BluetoothReadFunction();
    182 
    183   // AsyncApiFunction:
    184   virtual bool Prepare() OVERRIDE;
    185   virtual bool Respond() OVERRIDE;
    186   virtual void Work() OVERRIDE;
    187 
    188  private:
    189   bool success_;
    190   scoped_refptr<device::BluetoothSocket> socket_;
    191 };
    192 
    193 class BluetoothWriteFunction : public AsyncApiFunction {
    194  public:
    195   DECLARE_EXTENSION_FUNCTION("bluetooth.write", BLUETOOTH_WRITE)
    196   BluetoothWriteFunction();
    197 
    198  protected:
    199   virtual ~BluetoothWriteFunction();
    200 
    201   // AsyncApiFunction:
    202   virtual bool Prepare() OVERRIDE;
    203   virtual bool Respond() OVERRIDE;
    204   virtual void Work() OVERRIDE;
    205 
    206  private:
    207   bool success_;
    208   const base::BinaryValue* data_to_write_;  // memory is owned by args_
    209   scoped_refptr<device::BluetoothSocket> socket_;
    210 };
    211 
    212 class BluetoothSetOutOfBandPairingDataFunction
    213     : public BluetoothExtensionFunction {
    214  public:
    215   DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData",
    216                              BLUETOOTH_SETOUTOFBANDPAIRINGDATA)
    217 
    218  protected:
    219   virtual ~BluetoothSetOutOfBandPairingDataFunction() {}
    220 
    221   void OnSuccessCallback();
    222   void OnErrorCallback();
    223 
    224   // BluetoothExtensionFunction:
    225   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
    226 };
    227 
    228 class BluetoothGetLocalOutOfBandPairingDataFunction
    229     : public BluetoothExtensionFunction {
    230  public:
    231   DECLARE_EXTENSION_FUNCTION("bluetooth.getLocalOutOfBandPairingData",
    232                              BLUETOOTH_GETLOCALOUTOFBANDPAIRINGDATA)
    233 
    234  protected:
    235   virtual ~BluetoothGetLocalOutOfBandPairingDataFunction() {}
    236 
    237   void ReadCallback(
    238       const device::BluetoothOutOfBandPairingData& data);
    239   void ErrorCallback();
    240 
    241   // BluetoothExtensionFunction:
    242   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
    243 };
    244 
    245 class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction {
    246  public:
    247   DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery",
    248                              BLUETOOTH_STARTDISCOVERY)
    249 
    250  protected:
    251   virtual ~BluetoothStartDiscoveryFunction() {}
    252 
    253   // BluetoothExtensionFunction:
    254   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
    255 
    256  private:
    257   void OnSuccessCallback();
    258   void OnErrorCallback();
    259 };
    260 
    261 class BluetoothStopDiscoveryFunction : public BluetoothExtensionFunction {
    262  public:
    263   DECLARE_EXTENSION_FUNCTION("bluetooth.stopDiscovery", BLUETOOTH_STOPDISCOVERY)
    264 
    265  protected:
    266   virtual ~BluetoothStopDiscoveryFunction() {}
    267 
    268   // BluetoothExtensionFunction:
    269   virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
    270 
    271  private:
    272   void OnSuccessCallback();
    273   void OnErrorCallback();
    274 };
    275 
    276 }  // namespace api
    277 }  // namespace extensions
    278 
    279 #endif  // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_
    280