platform_sensor_provider_winrt.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2019 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_PROVIDER_WINRT_H_
  5. #define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_WINRT_H_
  6. #include "services/device/generic_sensor/platform_sensor_provider.h"
  7. namespace device {
  8. class PlatformSensorReaderWinBase;
  9. // Helper class used to instantiate new PlatformSensorReaderWinBase instances.
  10. class SensorReaderFactory {
  11. public:
  12. virtual ~SensorReaderFactory() = default;
  13. virtual std::unique_ptr<PlatformSensorReaderWinBase> CreateSensorReader(
  14. mojom::SensorType type);
  15. };
  16. // Implementation of PlatformSensorProvider for Windows platform using the
  17. // Windows.Devices.Sensors WinRT API. PlatformSensorProviderWinrt is
  18. // responsible for the following tasks:
  19. // - Starts sensor thread and stops it when there are no active sensors.
  20. // - Creates sensor reader.
  21. // - Constructs PlatformSensorWin on IPC thread and returns it to requester.
  22. class PlatformSensorProviderWinrt final : public PlatformSensorProvider {
  23. public:
  24. PlatformSensorProviderWinrt();
  25. ~PlatformSensorProviderWinrt() override;
  26. void SetSensorReaderFactoryForTesting(
  27. std::unique_ptr<SensorReaderFactory> sensor_reader_factory);
  28. protected:
  29. // PlatformSensorProvider interface implementation.
  30. void CreateSensorInternal(mojom::SensorType type,
  31. SensorReadingSharedBuffer* reading_buffer,
  32. CreateSensorCallback callback) override;
  33. private:
  34. std::unique_ptr<PlatformSensorReaderWinBase> CreateSensorReader(
  35. mojom::SensorType type);
  36. void SensorReaderCreated(
  37. mojom::SensorType type,
  38. SensorReadingSharedBuffer* reading_buffer,
  39. CreateSensorCallback callback,
  40. std::unique_ptr<PlatformSensorReaderWinBase> sensor_reader);
  41. // The Windows.Devices.Sensors WinRT API supports both STA and MTA
  42. // threads. STA was chosen as PlatformSensorWin can only handle STA.
  43. scoped_refptr<base::SingleThreadTaskRunner> com_sta_task_runner_;
  44. std::unique_ptr<SensorReaderFactory> sensor_reader_factory_;
  45. PlatformSensorProviderWinrt(const PlatformSensorProviderWinrt&) = delete;
  46. PlatformSensorProviderWinrt& operator=(const PlatformSensorProviderWinrt&) =
  47. delete;
  48. };
  49. } // namespace device
  50. #endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_WINRT_H_