platform_sensor_win.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include "services/device/generic_sensor/platform_sensor_win.h"
  5. #include "base/bind.h"
  6. #include "base/task/single_thread_task_runner.h"
  7. #include "base/time/time.h"
  8. #include "services/device/public/cpp/generic_sensor/sensor_traits.h"
  9. namespace device {
  10. PlatformSensorWin::PlatformSensorWin(
  11. mojom::SensorType type,
  12. SensorReadingSharedBuffer* reading_buffer,
  13. PlatformSensorProvider* provider,
  14. scoped_refptr<base::SingleThreadTaskRunner> sensor_thread_runner,
  15. std::unique_ptr<PlatformSensorReaderWinBase> sensor_reader)
  16. : PlatformSensor(type, reading_buffer, provider),
  17. sensor_thread_runner_(sensor_thread_runner),
  18. sensor_reader_(sensor_reader.release()) {
  19. DCHECK(sensor_reader_);
  20. sensor_reader_->SetClient(this);
  21. }
  22. PlatformSensorConfiguration PlatformSensorWin::GetDefaultConfiguration() {
  23. return PlatformSensorConfiguration(GetSensorDefaultFrequency(GetType()));
  24. }
  25. mojom::ReportingMode PlatformSensorWin::GetReportingMode() {
  26. // All Windows sensors, even with high accuracy / sensitivity will not report
  27. // reading updates continuously. Therefore, return ON_CHANGE by default.
  28. return mojom::ReportingMode::ON_CHANGE;
  29. }
  30. double PlatformSensorWin::GetMaximumSupportedFrequency() {
  31. base::TimeDelta minimal_reporting_interval_ms =
  32. sensor_reader_->GetMinimalReportingInterval();
  33. if (minimal_reporting_interval_ms.is_zero())
  34. return GetSensorDefaultFrequency(GetType());
  35. return 1.0 / minimal_reporting_interval_ms.InSecondsF();
  36. }
  37. void PlatformSensorWin::OnReadingUpdated(const SensorReading& reading) {
  38. UpdateSharedBufferAndNotifyClients(reading);
  39. }
  40. void PlatformSensorWin::OnSensorError() {
  41. PostTaskToMainSequence(FROM_HERE,
  42. base::BindOnce(&PlatformSensorWin::NotifySensorError,
  43. weak_factory_.GetWeakPtr()));
  44. }
  45. bool PlatformSensorWin::StartSensor(
  46. const PlatformSensorConfiguration& configuration) {
  47. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  48. return sensor_reader_->StartSensor(configuration);
  49. }
  50. void PlatformSensorWin::StopSensor() {
  51. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  52. sensor_reader_->StopSensor();
  53. }
  54. bool PlatformSensorWin::CheckSensorConfiguration(
  55. const PlatformSensorConfiguration& configuration) {
  56. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  57. base::TimeDelta minimal_reporting_interval_ms =
  58. sensor_reader_->GetMinimalReportingInterval();
  59. if (minimal_reporting_interval_ms.is_zero())
  60. return true;
  61. double max_frequency = 1.0 / minimal_reporting_interval_ms.InSecondsF();
  62. return configuration.frequency() <= max_frequency;
  63. }
  64. PlatformSensorWin::~PlatformSensorWin() {
  65. sensor_reader_->SetClient(nullptr);
  66. sensor_thread_runner_->DeleteSoon(FROM_HERE, sensor_reader_.get());
  67. }
  68. } // namespace device