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 "base/bind_to_current_loop.h" | |
7 #include "device/generic_sensor/linux/sensor_data_linux.h" | 8 #include "device/generic_sensor/linux/sensor_data_linux.h" |
8 #include "device/generic_sensor/platform_sensor_reader_linux.h" | 9 #include "device/generic_sensor/platform_sensor_reader_linux.h" |
9 | 10 |
10 namespace device { | 11 namespace device { |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 // Checks if at least one value has been changed. | 15 // Checks if at least one value has been changed. |
15 bool HaveValuesChanged(const SensorReading& lhs, const SensorReading& rhs) { | 16 bool HaveValuesChanged(const SensorReading& lhs, const SensorReading& rhs) { |
16 return lhs.values[0] != rhs.values[0] || lhs.values[1] != rhs.values[1] || | 17 return lhs.values[0] != rhs.values[0] || lhs.values[1] != rhs.values[1] || |
17 lhs.values[2] != rhs.values[2]; | 18 lhs.values[2] != rhs.values[2]; |
18 } | 19 } |
19 | 20 |
20 } // namespace | 21 } // namespace |
21 | 22 |
22 PlatformSensorLinux::PlatformSensorLinux( | 23 PlatformSensorLinux::PlatformSensorLinux( |
23 mojom::SensorType type, | 24 mojom::SensorType type, |
24 mojo::ScopedSharedBufferMapping mapping, | 25 mojo::ScopedSharedBufferMapping mapping, |
25 PlatformSensorProvider* provider, | 26 PlatformSensorProvider* provider, |
26 const SensorInfoLinux* sensor_device, | 27 const SensorInfoLinux* sensor_device, |
27 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) | 28 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) |
28 : PlatformSensor(type, std::move(mapping), provider), | 29 : PlatformSensor(type, std::move(mapping), provider), |
29 default_configuration_( | 30 default_configuration_( |
30 PlatformSensorConfiguration(sensor_device->device_frequency)), | 31 PlatformSensorConfiguration(sensor_device->device_frequency)), |
31 reporting_mode_(sensor_device->reporting_mode), | 32 reporting_mode_(sensor_device->reporting_mode), |
33 polling_thread_task_runner_(std::move(polling_thread_task_runner)), | |
32 weak_factory_(this) { | 34 weak_factory_(this) { |
33 sensor_reader_ = | 35 SensorReader::UpdateSensorReadingCallback update_reading_cb = |
34 SensorReader::Create(sensor_device, this, polling_thread_task_runner); | 36 base::BindToCurrentLoop( |
37 base::Bind(&PlatformSensorLinux::UpdatePlatformSensorReading, | |
38 weak_factory_.GetWeakPtr())); | |
39 SensorReader::NotifyReadErrorCallback notify_read_error_cb = | |
40 base::BindToCurrentLoop( | |
41 base::Bind(&PlatformSensorLinux::NotifyPlatformSensorError, | |
42 weak_factory_.GetWeakPtr())); | |
43 | |
44 sensor_reader_ = SensorReader::Create( | |
45 sensor_device, task_runner_, update_reading_cb, notify_read_error_cb); | |
Reilly Grant (use Gerrit)
2016/12/15 18:51:31
Since these callbacks are bound to the current mes
maksims (do not use this acc)
2016/12/16 08:39:22
I think passing a callback with binded weakPtr to
| |
35 } | 46 } |
36 | 47 |
37 PlatformSensorLinux::~PlatformSensorLinux() = default; | 48 PlatformSensorLinux::~PlatformSensorLinux() { |
49 DCHECK(task_runner_->BelongsToCurrentThread()); | |
50 polling_thread_task_runner_->DeleteSoon(FROM_HERE, sensor_reader_.release()); | |
51 } | |
38 | 52 |
39 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { | 53 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { |
40 DCHECK(task_runner_->BelongsToCurrentThread()); | 54 DCHECK(task_runner_->BelongsToCurrentThread()); |
41 return reporting_mode_; | 55 return reporting_mode_; |
42 } | 56 } |
43 | 57 |
44 void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) { | 58 void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) { |
45 DCHECK(task_runner_->BelongsToCurrentThread()); | 59 DCHECK(task_runner_->BelongsToCurrentThread()); |
46 bool notifyNeeded = false; | 60 bool notifyNeeded = false; |
47 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { | 61 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { |
48 if (!HaveValuesChanged(reading, old_values_)) | 62 if (!HaveValuesChanged(reading, old_values_)) |
49 return; | 63 return; |
50 notifyNeeded = true; | 64 notifyNeeded = true; |
51 } | 65 } |
52 old_values_ = reading; | 66 old_values_ = reading; |
53 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | 67 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
54 UpdateSensorReading(reading, notifyNeeded); | 68 UpdateSensorReading(reading, notifyNeeded); |
55 } | 69 } |
56 | 70 |
57 void PlatformSensorLinux::NotifyPlatformSensorError() { | 71 void PlatformSensorLinux::NotifyPlatformSensorError() { |
58 DCHECK(task_runner_->BelongsToCurrentThread()); | 72 DCHECK(task_runner_->BelongsToCurrentThread()); |
59 NotifySensorError(); | 73 NotifySensorError(); |
60 } | 74 } |
61 | 75 |
62 bool PlatformSensorLinux::StartSensor( | 76 bool PlatformSensorLinux::StartSensor( |
63 const PlatformSensorConfiguration& configuration) { | 77 const PlatformSensorConfiguration& configuration) { |
64 DCHECK(task_runner_->BelongsToCurrentThread()); | 78 DCHECK(task_runner_->BelongsToCurrentThread()); |
65 if (!sensor_reader_) | 79 polling_thread_task_runner_->PostTask( |
66 return false; | 80 FROM_HERE, |
67 return sensor_reader_->StartFetchingData(configuration); | 81 base::Bind(&SensorReader::StartFetchingData, |
82 base::Unretained(sensor_reader_.get()), configuration)); | |
83 return true; | |
68 } | 84 } |
69 | 85 |
70 void PlatformSensorLinux::StopSensor() { | 86 void PlatformSensorLinux::StopSensor() { |
71 DCHECK(task_runner_->BelongsToCurrentThread()); | 87 DCHECK(task_runner_->BelongsToCurrentThread()); |
72 DCHECK(sensor_reader_); | 88 polling_thread_task_runner_->PostTask( |
73 sensor_reader_->StopFetchingData(); | 89 FROM_HERE, base::Bind(&SensorReader::StopFetchingData, |
90 base::Unretained(sensor_reader_.get()))); | |
74 } | 91 } |
75 | 92 |
76 bool PlatformSensorLinux::CheckSensorConfiguration( | 93 bool PlatformSensorLinux::CheckSensorConfiguration( |
77 const PlatformSensorConfiguration& configuration) { | 94 const PlatformSensorConfiguration& configuration) { |
78 DCHECK(task_runner_->BelongsToCurrentThread()); | 95 DCHECK(task_runner_->BelongsToCurrentThread()); |
79 return configuration.frequency() > 0 && | 96 return configuration.frequency() > 0 && |
80 configuration.frequency() <= default_configuration_.frequency(); | 97 configuration.frequency() <= default_configuration_.frequency(); |
81 } | 98 } |
82 | 99 |
83 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { | 100 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { |
84 DCHECK(task_runner_->BelongsToCurrentThread()); | 101 DCHECK(task_runner_->BelongsToCurrentThread()); |
85 return default_configuration_; | 102 return default_configuration_; |
86 } | 103 } |
87 | 104 |
88 } // namespace device | 105 } // namespace device |
OLD | NEW |