platform_sensor_reader_win.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2016 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_GENERIC_SENSOR_PLATFORM_SENSOR_READER_WIN_H_
  5. #define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_WIN_H_
  6. #include <SensorsApi.h>
  7. #include <wrl/client.h>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/synchronization/lock.h"
  10. #include "base/thread_annotations.h"
  11. #include "services/device/generic_sensor/platform_sensor_reader_win_base.h"
  12. #include "services/device/public/mojom/sensor.mojom.h"
  13. namespace base {
  14. class TimeDelta;
  15. }
  16. namespace device {
  17. class PlatformSensorConfiguration;
  18. struct ReaderInitParams;
  19. union SensorReading;
  20. // Generic class that uses ISensor interface to fetch sensor data. Used
  21. // by PlatformSensorWin and delivers notifications via Client interface.
  22. // Instances of this class must be created and destructed on the same thread.
  23. class PlatformSensorReaderWin32 final : public PlatformSensorReaderWinBase {
  24. public:
  25. static std::unique_ptr<PlatformSensorReaderWinBase> Create(
  26. mojom::SensorType type,
  27. Microsoft::WRL::ComPtr<ISensorManager> sensor_manager);
  28. // Following methods are thread safe.
  29. void SetClient(Client* client) override;
  30. base::TimeDelta GetMinimalReportingInterval() const override;
  31. [[nodiscard]] bool StartSensor(
  32. const PlatformSensorConfiguration& configuration) override;
  33. void StopSensor() override;
  34. PlatformSensorReaderWin32(const PlatformSensorReaderWin32&) = delete;
  35. PlatformSensorReaderWin32& operator=(const PlatformSensorReaderWin32&) =
  36. delete;
  37. // Must be destructed on the same thread that was used during construction.
  38. ~PlatformSensorReaderWin32() override;
  39. private:
  40. PlatformSensorReaderWin32(Microsoft::WRL::ComPtr<ISensor> sensor,
  41. std::unique_ptr<ReaderInitParams> params);
  42. static Microsoft::WRL::ComPtr<ISensor> GetSensorForType(
  43. REFSENSOR_TYPE_ID sensor_type,
  44. Microsoft::WRL::ComPtr<ISensorManager> sensor_manager);
  45. [[nodiscard]] bool SetReportingInterval(
  46. const PlatformSensorConfiguration& configuration);
  47. void ListenSensorEvent();
  48. [[nodiscard]] HRESULT SensorReadingChanged(ISensorDataReport* report,
  49. SensorReading* reading);
  50. void SensorError();
  51. private:
  52. friend class EventListener;
  53. const std::unique_ptr<ReaderInitParams> init_params_;
  54. scoped_refptr<base::SingleThreadTaskRunner> com_sta_task_runner_;
  55. // Following class members are protected by lock, because SetClient,
  56. // StartSensor and StopSensor are called from another thread by
  57. // PlatformSensorWin that can modify internal state of the object.
  58. base::Lock lock_;
  59. bool sensor_active_ GUARDED_BY(lock_);
  60. raw_ptr<Client> client_ GUARDED_BY(lock_);
  61. Microsoft::WRL::ComPtr<ISensor> sensor_;
  62. Microsoft::WRL::ComPtr<ISensorEvents> event_listener_;
  63. base::WeakPtrFactory<PlatformSensorReaderWin32> weak_factory_{this};
  64. };
  65. } // namespace device
  66. #endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_WIN_H_