platform_sensor_accelerometer_mac.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_ACCELEROMETER_MAC_H_
  5. #define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_ACCELEROMETER_MAC_H_
  6. #include <memory>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/task/sequenced_task_runner.h"
  9. #include "base/timer/timer.h"
  10. #include "services/device/generic_sensor/platform_sensor.h"
  11. #include "services/device/public/cpp/generic_sensor/sensor_reading.h"
  12. namespace device {
  13. // Implementation of PlatformSensor for macOS to query the accelerometer
  14. // sensor.
  15. // This is a single instance object per browser process which is created by
  16. // PlatformSensorProviderMac. If there are no clients, this instance is not
  17. // created.
  18. class PlatformSensorAccelerometerMac : public PlatformSensor {
  19. public:
  20. // Construct a platform sensor of type ACCELEROMETER, given a buffer |mapping|
  21. // where readings will be written.
  22. PlatformSensorAccelerometerMac(SensorReadingSharedBuffer* reading_buffer,
  23. PlatformSensorProvider* provider);
  24. PlatformSensorAccelerometerMac(const PlatformSensorAccelerometerMac&) =
  25. delete;
  26. PlatformSensorAccelerometerMac& operator=(
  27. const PlatformSensorAccelerometerMac&) = delete;
  28. mojom::ReportingMode GetReportingMode() override;
  29. // Can only be called once, the first time or after a StopSensor call.
  30. bool StartSensor(const PlatformSensorConfiguration& configuration) override;
  31. void StopSensor() override;
  32. protected:
  33. ~PlatformSensorAccelerometerMac() override;
  34. bool CheckSensorConfiguration(
  35. const PlatformSensorConfiguration& configuration) override;
  36. PlatformSensorConfiguration GetDefaultConfiguration() override;
  37. private:
  38. class BlockingTaskRunnerHelper;
  39. void OnReadingAvailable(SensorReading reading);
  40. scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  41. std::unique_ptr<BlockingTaskRunnerHelper, base::OnTaskRunnerDeleter>
  42. blocking_task_helper_;
  43. SensorReading reading_;
  44. bool is_reading_active_ = false;
  45. base::WeakPtrFactory<PlatformSensorAccelerometerMac> weak_factory_{this};
  46. };
  47. } // namespace device
  48. #endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_ACCELEROMETER_MAC_H_