bluetooth_low_energy_device_watcher_mac.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_DEVICE_WATCHER_MAC_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_DEVICE_WATCHER_MAC_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/files/file_path.h"
  11. #include "base/files/file_path_watcher.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/task/cancelable_task_tracker.h"
  15. #include "base/task/sequenced_task_runner.h"
  16. #include "base/task/thread_pool.h"
  17. #include "device/bluetooth/bluetooth_export.h"
  18. @class NSDictionary;
  19. namespace device {
  20. // Manages watching and reading system bluetooth property list file in
  21. // background thread to obtain a list of known Bluetooth low energy devices.
  22. class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyDeviceWatcherMac
  23. : public base::RefCountedThreadSafe<BluetoothLowEnergyDeviceWatcherMac> {
  24. public:
  25. using LowEnergyDeviceListUpdatedCallback =
  26. base::RepeatingCallback<void(std::map<std::string, std::string>)>;
  27. static scoped_refptr<BluetoothLowEnergyDeviceWatcherMac>
  28. CreateAndStartWatching(
  29. scoped_refptr<base::SequencedTaskRunner> main_thread_task_runner,
  30. LowEnergyDeviceListUpdatedCallback
  31. update_low_energy_device_list_callback);
  32. BluetoothLowEnergyDeviceWatcherMac(
  33. const BluetoothLowEnergyDeviceWatcherMac&) = delete;
  34. BluetoothLowEnergyDeviceWatcherMac& operator=(
  35. const BluetoothLowEnergyDeviceWatcherMac&) = delete;
  36. BluetoothLowEnergyDeviceWatcherMac(
  37. scoped_refptr<base::SequencedTaskRunner> main_thread_task_runner,
  38. LowEnergyDeviceListUpdatedCallback
  39. update_low_energy_device_list_callback);
  40. protected:
  41. virtual ~BluetoothLowEnergyDeviceWatcherMac();
  42. // Read system bluetooth property list file for change and fetches
  43. // identifier and device address of system paired bluetooth devices.
  44. void OnPropertyListFileChangedOnFileThread(const base::FilePath& path,
  45. bool error);
  46. // Overriden in tests.
  47. virtual void Init();
  48. virtual void ReadBluetoothPropertyListFile();
  49. std::map<std::string, std::string> ParseBluetoothDevicePropertyListData(
  50. NSDictionary* data);
  51. LowEnergyDeviceListUpdatedCallback low_energy_device_list_updated_callback() {
  52. return low_energy_device_list_updated_callback_;
  53. }
  54. scoped_refptr<base::SequencedTaskRunner> ui_thread_task_runner() {
  55. return ui_thread_task_runner_;
  56. }
  57. private:
  58. friend class BluetoothAdapterMac;
  59. friend class base::RefCountedThreadSafe<BluetoothLowEnergyDeviceWatcherMac>;
  60. void AddBluetoothPropertyListFileWatcher();
  61. static const base::FilePath& BluetoothPlistFilePath();
  62. // Thread runner to watch, read, and parse bluetooth property list file.
  63. scoped_refptr<base::SequencedTaskRunner> file_thread_task_runner_ =
  64. base::ThreadPool::CreateSequencedTaskRunner(
  65. {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
  66. base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
  67. scoped_refptr<base::SequencedTaskRunner> ui_thread_task_runner_;
  68. LowEnergyDeviceListUpdatedCallback low_energy_device_list_updated_callback_;
  69. std::unique_ptr<base::FilePathWatcher> property_list_watcher_ =
  70. std::make_unique<base::FilePathWatcher>();
  71. SEQUENCE_CHECKER(sequence_checker_);
  72. };
  73. } // namespace device
  74. #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_DEVICE_WATCHER_MAC_H_