bluetooth_system.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 SERVICES_DEVICE_BLUETOOTH_BLUETOOTH_SYSTEM_H_
  5. #define SERVICES_DEVICE_BLUETOOTH_BLUETOOTH_SYSTEM_H_
  6. #include <string>
  7. #include "base/memory/weak_ptr.h"
  8. #include "dbus/object_path.h"
  9. #include "device/bluetooth/dbus/bluetooth_adapter_client.h"
  10. #include "mojo/public/cpp/bindings/pending_receiver.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "services/device/public/mojom/bluetooth_system.mojom.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace bluez {
  16. class BluetoothAdapterClient;
  17. class BluetoothDeviceClient;
  18. }
  19. namespace device {
  20. class BluetoothSystem : public mojom::BluetoothSystem,
  21. public bluez::BluetoothAdapterClient::Observer {
  22. public:
  23. static void Create(mojo::PendingReceiver<mojom::BluetoothSystem> receiver,
  24. mojo::PendingRemote<mojom::BluetoothSystemClient> client);
  25. explicit BluetoothSystem(
  26. mojo::PendingRemote<mojom::BluetoothSystemClient> client);
  27. BluetoothSystem(const BluetoothSystem&) = delete;
  28. BluetoothSystem& operator=(const BluetoothSystem&) = delete;
  29. ~BluetoothSystem() override;
  30. // bluez::BluetoothAdapterClient::Observer
  31. void AdapterAdded(const dbus::ObjectPath& object_path) override;
  32. void AdapterRemoved(const dbus::ObjectPath& object_path) override;
  33. void AdapterPropertyChanged(const dbus::ObjectPath& object_path,
  34. const std::string& property_name) override;
  35. // mojom::BluetoothSystem
  36. void GetState(GetStateCallback callback) override;
  37. void SetPowered(bool powered, SetPoweredCallback callback) override;
  38. void GetScanState(GetScanStateCallback callback) override;
  39. void StartScan(StartScanCallback callback) override;
  40. void StopScan(StopScanCallback callback) override;
  41. void GetAvailableDevices(GetAvailableDevicesCallback callback) override;
  42. private:
  43. bluez::BluetoothAdapterClient* GetBluetoothAdapterClient();
  44. bluez::BluetoothDeviceClient* GetBluetoothDeviceClient();
  45. void UpdateStateAndNotifyIfNecessary();
  46. ScanState GetScanStateFromActiveAdapter();
  47. void OnSetPoweredFinished(SetPoweredCallback callback, bool succeeded);
  48. void OnStartDiscovery(
  49. StartScanCallback callback,
  50. const absl::optional<bluez::BluetoothAdapterClient::Error>& error);
  51. void OnStopDiscovery(
  52. StopScanCallback callback,
  53. const absl::optional<bluez::BluetoothAdapterClient::Error>& error);
  54. mojo::Remote<mojom::BluetoothSystemClient> client_;
  55. // The ObjectPath of the adapter being used. Updated as BT adapters are
  56. // added and removed. nullopt if there is no adapter.
  57. absl::optional<dbus::ObjectPath> active_adapter_;
  58. // State of |active_adapter_| or kUnavailable if there is no
  59. // |active_adapter_|.
  60. State state_ = State::kUnavailable;
  61. // Note: This should remain the last member so it'll be destroyed and
  62. // invalidate its weak pointers before any other members are destroyed.
  63. base::WeakPtrFactory<BluetoothSystem> weak_ptr_factory_{this};
  64. };
  65. } // namespace device
  66. #endif // SERVICES_DEVICE_BLUETOOTH_BLUETOOTH_SYSTEM_H_