| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/generic_sensor/platform_sensor_linux.h" | 5 #include "device/generic_sensor/platform_sensor_linux.h" |
| 6 | 6 |
| 7 #include "device/generic_sensor/linux/sensor_data_linux.h" | 7 #include "device/generic_sensor/linux/sensor_data_linux.h" |
| 8 #include "device/generic_sensor/platform_sensor_reader_linux.h" | 8 #include "device/generic_sensor/platform_sensor_reader_linux.h" |
| 9 | 9 |
| 10 namespace device { | 10 namespace device { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 PlatformSensorLinux::PlatformSensorLinux( | 22 PlatformSensorLinux::PlatformSensorLinux( |
| 23 mojom::SensorType type, | 23 mojom::SensorType type, |
| 24 mojo::ScopedSharedBufferMapping mapping, | 24 mojo::ScopedSharedBufferMapping mapping, |
| 25 PlatformSensorProvider* provider, | 25 PlatformSensorProvider* provider, |
| 26 const SensorInfoLinux* sensor_device, | 26 const SensorInfoLinux* sensor_device, |
| 27 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) | 27 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) |
| 28 : PlatformSensor(type, std::move(mapping), provider), | 28 : PlatformSensor(type, std::move(mapping), provider), |
| 29 default_configuration_( | 29 default_configuration_( |
| 30 PlatformSensorConfiguration(sensor_device->device_frequency)), | 30 PlatformSensorConfiguration(sensor_device->device_frequency)), |
| 31 reporting_mode_(sensor_device->reporting_mode), | 31 reporting_mode_(sensor_device->reporting_mode), |
| 32 polling_thread_task_runner_(std::move(polling_thread_task_runner)), |
| 32 weak_factory_(this) { | 33 weak_factory_(this) { |
| 33 sensor_reader_ = | 34 sensor_reader_ = SensorReader::Create( |
| 34 SensorReader::Create(sensor_device, this, polling_thread_task_runner); | 35 sensor_device, weak_factory_.GetWeakPtr(), task_runner_); |
| 35 } | 36 } |
| 36 | 37 |
| 37 PlatformSensorLinux::~PlatformSensorLinux() = default; | 38 PlatformSensorLinux::~PlatformSensorLinux() { |
| 39 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 40 polling_thread_task_runner_->DeleteSoon(FROM_HERE, sensor_reader_.release()); |
| 41 } |
| 38 | 42 |
| 39 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { | 43 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { |
| 40 DCHECK(task_runner_->BelongsToCurrentThread()); | 44 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 41 return reporting_mode_; | 45 return reporting_mode_; |
| 42 } | 46 } |
| 43 | 47 |
| 44 void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) { | 48 void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) { |
| 45 DCHECK(task_runner_->BelongsToCurrentThread()); | 49 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 46 bool notifyNeeded = false; | 50 bool notifyNeeded = false; |
| 47 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { | 51 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { |
| 48 if (!HaveValuesChanged(reading, old_values_)) | 52 if (!HaveValuesChanged(reading, old_values_)) |
| 49 return; | 53 return; |
| 50 notifyNeeded = true; | 54 notifyNeeded = true; |
| 51 } | 55 } |
| 52 old_values_ = reading; | 56 old_values_ = reading; |
| 53 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | 57 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| 54 UpdateSensorReading(reading, notifyNeeded); | 58 UpdateSensorReading(reading, notifyNeeded); |
| 55 } | 59 } |
| 56 | 60 |
| 57 void PlatformSensorLinux::NotifyPlatformSensorError() { | 61 void PlatformSensorLinux::NotifyPlatformSensorError() { |
| 58 DCHECK(task_runner_->BelongsToCurrentThread()); | 62 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 59 NotifySensorError(); | 63 NotifySensorError(); |
| 60 } | 64 } |
| 61 | 65 |
| 62 bool PlatformSensorLinux::StartSensor( | 66 bool PlatformSensorLinux::StartSensor( |
| 63 const PlatformSensorConfiguration& configuration) { | 67 const PlatformSensorConfiguration& configuration) { |
| 64 DCHECK(task_runner_->BelongsToCurrentThread()); | 68 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 65 if (!sensor_reader_) | 69 polling_thread_task_runner_->PostTask( |
| 66 return false; | 70 FROM_HERE, |
| 67 return sensor_reader_->StartFetchingData(configuration); | 71 base::Bind(&SensorReader::StartFetchingData, |
| 72 base::Unretained(sensor_reader_.get()), configuration)); |
| 73 return true; |
| 68 } | 74 } |
| 69 | 75 |
| 70 void PlatformSensorLinux::StopSensor() { | 76 void PlatformSensorLinux::StopSensor() { |
| 71 DCHECK(task_runner_->BelongsToCurrentThread()); | 77 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 72 DCHECK(sensor_reader_); | 78 polling_thread_task_runner_->PostTask( |
| 73 sensor_reader_->StopFetchingData(); | 79 FROM_HERE, base::Bind(&SensorReader::StopFetchingData, |
| 80 base::Unretained(sensor_reader_.get()))); |
| 74 } | 81 } |
| 75 | 82 |
| 76 bool PlatformSensorLinux::CheckSensorConfiguration( | 83 bool PlatformSensorLinux::CheckSensorConfiguration( |
| 77 const PlatformSensorConfiguration& configuration) { | 84 const PlatformSensorConfiguration& configuration) { |
| 78 DCHECK(task_runner_->BelongsToCurrentThread()); | 85 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 79 return configuration.frequency() > 0 && | 86 return configuration.frequency() > 0 && |
| 80 configuration.frequency() <= default_configuration_.frequency(); | 87 configuration.frequency() <= default_configuration_.frequency(); |
| 81 } | 88 } |
| 82 | 89 |
| 83 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { | 90 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { |
| 84 DCHECK(task_runner_->BelongsToCurrentThread()); | 91 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 85 return default_configuration_; | 92 return default_configuration_; |
| 86 } | 93 } |
| 87 | 94 |
| 88 } // namespace device | 95 } // namespace device |
| OLD | NEW |