tray_bluetooth_helper_legacy.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2017 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_SYSTEM_BLUETOOTH_TRAY_BLUETOOTH_HELPER_LEGACY_H_
  5. #define ASH_SYSTEM_BLUETOOTH_TRAY_BLUETOOTH_HELPER_LEGACY_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/ash_export.h"
  10. #include "ash/system/bluetooth/tray_bluetooth_helper.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/time/time.h"
  14. #include "device/bluetooth/bluetooth_adapter.h"
  15. #include "services/device/public/mojom/bluetooth_system.mojom.h"
  16. namespace base {
  17. class Time;
  18. } // namespace base
  19. namespace device {
  20. class BluetoothDiscoverySession;
  21. } // namespace device
  22. namespace ash {
  23. // Implementation of TrayBluetoothHelper on top of the //device/bluetooth/ APIs.
  24. // Exported for tests.
  25. class ASH_EXPORT TrayBluetoothHelperLegacy
  26. : public TrayBluetoothHelper,
  27. public device::BluetoothAdapter::Observer {
  28. public:
  29. TrayBluetoothHelperLegacy();
  30. TrayBluetoothHelperLegacy(const TrayBluetoothHelperLegacy&) = delete;
  31. TrayBluetoothHelperLegacy& operator=(const TrayBluetoothHelperLegacy&) =
  32. delete;
  33. ~TrayBluetoothHelperLegacy() override;
  34. // Completes initialization after the Bluetooth adapter is ready.
  35. void InitializeOnAdapterReady(
  36. scoped_refptr<device::BluetoothAdapter> adapter);
  37. // TrayBluetoothHelper:
  38. void Initialize() override;
  39. void StartBluetoothDiscovering() override;
  40. void StopBluetoothDiscovering() override;
  41. void ConnectToBluetoothDevice(const BluetoothAddress& address) override;
  42. device::mojom::BluetoothSystem::State GetBluetoothState() override;
  43. void SetBluetoothEnabled(bool enabled) override;
  44. bool HasBluetoothDiscoverySession() override;
  45. void GetBluetoothDevices(GetBluetoothDevicesCallback callback) const override;
  46. // BluetoothAdapter::Observer:
  47. void AdapterPresentChanged(device::BluetoothAdapter* adapter,
  48. bool present) override;
  49. void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
  50. bool powered) override;
  51. void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
  52. bool discovering) override;
  53. private:
  54. void OnStartDiscoverySession(
  55. std::unique_ptr<device::BluetoothDiscoverySession> discovery_session);
  56. bool should_run_discovery_ = false;
  57. scoped_refptr<device::BluetoothAdapter> adapter_;
  58. std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_;
  59. // AdapterPoweredChanged gets called right after AdapterPresentChanged when an
  60. // adapter is added or removed. This causes us to call
  61. // Observer::OnBluetoothStateChanged() a second time without the state
  62. // actually changing. To avoid this, we cache the state whenever
  63. // AdapterPresentChanged and AdapterPoweredChanged get called and only notify
  64. // if the new state is different than the cached state.
  65. device::mojom::BluetoothSystem::State last_state_ =
  66. device::mojom::BluetoothSystem::State::kUnavailable;
  67. // The time at which discovery started, effectively when the user opened the
  68. // System Tray Bluetooth options with Bluetooth on, or when Bluetooth turned
  69. // on while the Bluetooth options were open.
  70. base::Time discovery_start_timestamp_;
  71. // Object could be deleted during a prolonged Bluetooth operation.
  72. base::WeakPtrFactory<TrayBluetoothHelperLegacy> weak_ptr_factory_{this};
  73. };
  74. } // namespace ash
  75. #endif // ASH_SYSTEM_BLUETOOTH_TRAY_BLUETOOTH_HELPER_LEGACY_H_