1 // Copyright 2013 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 DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_ 6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_ 7 8 #include <string> 9 10 #include "base/memory/weak_ptr.h" 11 #include "chromeos/dbus/bluetooth_adapter_client.h" 12 #include "chromeos/dbus/bluetooth_device_client.h" 13 #include "chromeos/dbus/bluetooth_input_client.h" 14 #include "dbus/object_path.h" 15 #include "device/bluetooth/bluetooth_adapter.h" 16 17 namespace device { 18 19 class BluetoothAdapterFactory; 20 21 } // namespace device 22 23 namespace chromeos { 24 25 class BluetoothChromeOSTest; 26 class BluetoothDeviceChromeOS; 27 28 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the 29 // Chrome OS platform. 30 class BluetoothAdapterChromeOS 31 : public device::BluetoothAdapter, 32 private chromeos::BluetoothAdapterClient::Observer, 33 private chromeos::BluetoothDeviceClient::Observer, 34 private chromeos::BluetoothInputClient::Observer { 35 public: 36 // BluetoothAdapter override 37 virtual void AddObserver( 38 device::BluetoothAdapter::Observer* observer) OVERRIDE; 39 virtual void RemoveObserver( 40 device::BluetoothAdapter::Observer* observer) OVERRIDE; 41 virtual std::string GetAddress() const OVERRIDE; 42 virtual std::string GetName() const OVERRIDE; 43 virtual bool IsInitialized() const OVERRIDE; 44 virtual bool IsPresent() const OVERRIDE; 45 virtual bool IsPowered() const OVERRIDE; 46 virtual void SetPowered( 47 bool powered, 48 const base::Closure& callback, 49 const ErrorCallback& error_callback) OVERRIDE; 50 virtual bool IsDiscovering() const OVERRIDE; 51 virtual void StartDiscovering( 52 const base::Closure& callback, 53 const ErrorCallback& error_callback) OVERRIDE; 54 virtual void StopDiscovering( 55 const base::Closure& callback, 56 const ErrorCallback& error_callback) OVERRIDE; 57 virtual void ReadLocalOutOfBandPairingData( 58 const device::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& 59 callback, 60 const ErrorCallback& error_callback) OVERRIDE; 61 62 private: 63 friend class device::BluetoothAdapterFactory; 64 friend class BluetoothChromeOSTest; 65 friend class BluetoothDeviceChromeOS; 66 friend class BluetoothProfileChromeOS; 67 friend class BluetoothProfileChromeOSTest; 68 69 BluetoothAdapterChromeOS(); 70 virtual ~BluetoothAdapterChromeOS(); 71 72 // BluetoothAdapterClient::Observer override. 73 virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE; 74 virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE; 75 virtual void AdapterPropertyChanged( 76 const dbus::ObjectPath& object_path, 77 const std::string& property_name) OVERRIDE; 78 79 // BluetoothDeviceClient::Observer override. 80 virtual void DeviceAdded(const dbus::ObjectPath& object_path) OVERRIDE; 81 virtual void DeviceRemoved(const dbus::ObjectPath& object_path) OVERRIDE; 82 virtual void DevicePropertyChanged(const dbus::ObjectPath& object_path, 83 const std::string& property_name) OVERRIDE; 84 85 // BluetoothInputClient::Observer override. 86 virtual void InputPropertyChanged(const dbus::ObjectPath& object_path, 87 const std::string& property_name) OVERRIDE; 88 89 // Internal method used to locate the device object by object path 90 // (the devices map and BluetoothDevice methods are by address) 91 BluetoothDeviceChromeOS* GetDeviceWithPath( 92 const dbus::ObjectPath& object_path); 93 94 // Set the tracked adapter to the one in |object_path|, this object will 95 // subsequently operate on that adapter until it is removed. 96 void SetAdapter(const dbus::ObjectPath& object_path); 97 98 // Set the adapter name to one chosen from the system information, and method 99 // called by dbus:: on completion of the alias property change. 100 void SetAdapterName(); 101 void OnSetAlias(bool success); 102 103 // Remove the currently tracked adapter. IsPresent() will return false after 104 // this is called. 105 void RemoveAdapter(); 106 107 // Announce to observers a change in the adapter state. 108 void PoweredChanged(bool powered); 109 void DiscoveringChanged(bool discovering); 110 void PresentChanged(bool present); 111 112 // Announce to observers a change in device state that is not reflected by 113 // its D-Bus properties. 114 void NotifyDeviceChanged(BluetoothDeviceChromeOS* device); 115 116 // Called by dbus:: on completion of the powered property change. 117 void OnSetPowered(const base::Closure& callback, 118 const ErrorCallback& error_callback, 119 bool success); 120 121 // Called by dbus:: on completion of the D-Bus method call to start discovery. 122 void OnStartDiscovery(const base::Closure& callback); 123 void OnStartDiscoveryError(const ErrorCallback& error_callback, 124 const std::string& error_name, 125 const std::string& error_message); 126 127 // Called by dbus:: on completion of the D-Bus method call to stop discovery. 128 void OnStopDiscovery(const base::Closure& callback); 129 void OnStopDiscoveryError(const ErrorCallback& error_callback, 130 const std::string& error_name, 131 const std::string& error_message); 132 133 // Object path of the adapter we track. 134 dbus::ObjectPath object_path_; 135 136 // List of observers interested in event notifications from us. 137 ObserverList<device::BluetoothAdapter::Observer> observers_; 138 139 // Note: This should remain the last member so it'll be destroyed and 140 // invalidate its weak pointers before any other members are destroyed. 141 base::WeakPtrFactory<BluetoothAdapterChromeOS> weak_ptr_factory_; 142 143 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS); 144 }; 145 146 } // namespace chromeos 147 148 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_ 149