bluetooth_devices_observer.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2018 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 ASH_BLUETOOTH_DEVICES_OBSERVER_H_
  5. #define ASH_BLUETOOTH_DEVICES_OBSERVER_H_
  6. #include "ash/ash_export.h"
  7. #include "base/callback.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "device/bluetooth/bluetooth_adapter.h"
  10. #include "ui/events/devices/input_device.h"
  11. namespace ash {
  12. // This class observes the bluetooth devices and sends out notification when
  13. // bluetooth device changes. It's used as a supplementary to the class
  14. // ui::InputDeviceEventObserver as InputDeviceEventObserver does not have
  15. // knowledge about bluetooth device status thus does not send notifications of
  16. // bluetooth device changes.
  17. class ASH_EXPORT BluetoothDevicesObserver
  18. : public device::BluetoothAdapter::Observer {
  19. public:
  20. // Note |device| can be nullptr here if only the bluetooth adapter status
  21. // changes.
  22. using AdapterOrDeviceChangedCallback =
  23. base::RepeatingCallback<void(device::BluetoothDevice* device)>;
  24. explicit BluetoothDevicesObserver(
  25. const AdapterOrDeviceChangedCallback& device_changed_callback);
  26. BluetoothDevicesObserver(const BluetoothDevicesObserver&) = delete;
  27. BluetoothDevicesObserver& operator=(const BluetoothDevicesObserver&) = delete;
  28. ~BluetoothDevicesObserver() override;
  29. // device::BluetoothAdapter::Observer:
  30. void AdapterPresentChanged(device::BluetoothAdapter* adapter,
  31. bool present) override;
  32. void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
  33. bool powered) override;
  34. void DeviceChanged(device::BluetoothAdapter* adapter,
  35. device::BluetoothDevice* device) override;
  36. // Returns true if |input_device| is a connected bluetooth device. Note this
  37. // function may not work well if there are more than one identical Bluetooth
  38. // devices: the function might return true even if it should return false.
  39. // E.g., Connect two idential bluetooth devices (Input device A & input device
  40. // B, thus the same vendor id and same product id) to Chrome OS, and then
  41. // disconnect device A, calling IsConnectedBluetoothDevice(Input device A)
  42. // still returns true although it should return false as Input device B is
  43. // still connected. Unfortunately there is no good map from an InputDevice to
  44. // a BluetoothDevice, thus we can only guess a match.
  45. bool IsConnectedBluetoothDevice(const ui::InputDevice& input_device) const;
  46. private:
  47. void InitializeOnAdapterReady(
  48. scoped_refptr<device::BluetoothAdapter> adapter);
  49. // Reference to the underlying Bluetooth Adapter. Used to listen for bluetooth
  50. // device change event.
  51. scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
  52. // Callback function to be called when the bluetooth adapter's status or a
  53. // bluetooth device's status changes.
  54. AdapterOrDeviceChangedCallback adapter_or_device_changed_callback_;
  55. base::WeakPtrFactory<BluetoothDevicesObserver> weak_factory_{this};
  56. };
  57. } // namespace ash
  58. #endif // ASH_BLUETOOTH_DEVICES_OBSERVER_H_