Chromium Code Reviews| 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_reader_linux.h" | 5 #include "device/generic_sensor/platform_sensor_reader_linux.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 apply_scaling_func_(sensor_device->apply_scaling_func), | 67 apply_scaling_func_(sensor_device->apply_scaling_func), |
| 68 timer_(new base::RepeatingTimer()), | 68 timer_(new base::RepeatingTimer()), |
| 69 weak_factory_(this) {} | 69 weak_factory_(this) {} |
| 70 | 70 |
| 71 PollingSensorReader::~PollingSensorReader() { | 71 PollingSensorReader::~PollingSensorReader() { |
| 72 polling_task_runner_->DeleteSoon(FROM_HERE, timer_); | 72 polling_task_runner_->DeleteSoon(FROM_HERE, timer_); |
| 73 } | 73 } |
| 74 | 74 |
| 75 bool PollingSensorReader::StartFetchingData( | 75 bool PollingSensorReader::StartFetchingData( |
| 76 const PlatformSensorConfiguration& configuration) { | 76 const PlatformSensorConfiguration& configuration) { |
| 77 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 77 if (is_reading_active_) | 78 if (is_reading_active_) |
| 78 StopFetchingData(); | 79 StopFetchingData(); |
| 79 | 80 |
| 80 return polling_task_runner_->PostTask( | 81 return polling_task_runner_->PostTask( |
| 81 FROM_HERE, base::Bind(&PollingSensorReader::InitializeTimer, | 82 FROM_HERE, base::Bind(&PollingSensorReader::InitializeTimer, |
| 82 weak_factory_.GetWeakPtr(), configuration)); | 83 weak_factory_.GetWeakPtr(), configuration)); |
| 83 } | 84 } |
| 84 | 85 |
| 85 void PollingSensorReader::StopFetchingData() { | 86 void PollingSensorReader::StopFetchingData() { |
| 87 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 86 is_reading_active_ = false; | 88 is_reading_active_ = false; |
| 87 timer_->Stop(); | 89 timer_->Stop(); |
| 88 } | 90 } |
| 89 | 91 |
| 90 void PollingSensorReader::InitializeTimer( | 92 void PollingSensorReader::InitializeTimer( |
| 91 const PlatformSensorConfiguration& configuration) { | 93 const PlatformSensorConfiguration& configuration) { |
| 92 DCHECK(polling_task_runner_->BelongsToCurrentThread()); | 94 DCHECK(polling_task_runner_->BelongsToCurrentThread()); |
| 93 timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds( | 95 timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds( |
| 94 base::Time::kMicrosecondsPerSecond / | 96 base::Time::kMicrosecondsPerSecond / |
| 95 configuration.frequency()), | 97 configuration.frequency()), |
| 96 this, &PollingSensorReader::PollForData); | 98 this, &PollingSensorReader::PollForData); |
| 97 is_reading_active_ = true; | 99 is_reading_active_ = true; |
| 98 } | 100 } |
| 99 | 101 |
| 100 void PollingSensorReader::PollForData() { | 102 void PollingSensorReader::PollForData() { |
| 101 DCHECK(polling_task_runner_->BelongsToCurrentThread()); | 103 DCHECK(polling_task_runner_->BelongsToCurrentThread()); |
| 102 base::ThreadRestrictions::AssertIOAllowed(); | 104 base::ThreadRestrictions::AssertIOAllowed(); |
| 103 | 105 |
| 104 SensorReading readings; | 106 SensorReading readings; |
| 105 DCHECK_LE(sensor_file_paths_.size(), arraysize(readings.values)); | 107 DCHECK_LE(sensor_file_paths_.size(), arraysize(readings.values)); |
| 106 int i = 0; | 108 int i = 0; |
| 107 for (const auto& path : sensor_file_paths_) { | 109 for (const auto& path : sensor_file_paths_) { |
| 108 std::string new_read_value; | 110 std::string new_read_value; |
| 109 if (!base::ReadFileToString(path, &new_read_value)) { | 111 if (!base::ReadFileToString(path, &new_read_value)) { |
| 110 NotifyReadError(); | 112 NotifyReadError(); |
| 111 StopFetchingData(); | 113 task_runner_->PostTask(FROM_HERE, |
| 114 base::Bind(&PollingSensorReader::StopFetchingData, | |
| 115 weak_factory_.GetWeakPtr())); | |
| 112 return; | 116 return; |
| 113 } | 117 } |
| 114 | 118 |
| 115 double new_value = 0; | 119 double new_value = 0; |
| 116 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); | 120 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); |
| 117 if (!base::StringToDouble(new_read_value, &new_value)) { | 121 if (!base::StringToDouble(new_read_value, &new_value)) { |
| 118 NotifyReadError(); | 122 NotifyReadError(); |
| 119 StopFetchingData(); | 123 task_runner_->PostTask(FROM_HERE, |
| 124 base::Bind(&PollingSensorReader::StopFetchingData, | |
| 125 weak_factory_.GetWeakPtr())); | |
|
gab
2016/12/13 18:12:45
WeakPtr is also not thread-safe (can't obtain it o
maksims (do not use this acc)
2016/12/13 18:18:19
Do you mean I need 2 to create two different weak
gab
2016/12/13 18:23:02
classes in Chromium are typically bound to a singl
| |
| 120 return; | 126 return; |
| 121 } | 127 } |
| 122 readings.values[i++] = new_value; | 128 readings.values[i++] = new_value; |
| 123 } | 129 } |
| 124 if (!apply_scaling_func_.is_null()) | 130 if (!apply_scaling_func_.is_null()) |
| 125 apply_scaling_func_.Run(scaling_value_, offset_value_, readings); | 131 apply_scaling_func_.Run(scaling_value_, offset_value_, readings); |
| 126 | 132 |
| 127 if (is_reading_active_) { | 133 if (is_reading_active_) { |
| 128 task_runner_->PostTask( | 134 task_runner_->PostTask( |
| 129 FROM_HERE, base::Bind(&PlatformSensorLinux::UpdatePlatformSensorReading, | 135 FROM_HERE, base::Bind(&PlatformSensorLinux::UpdatePlatformSensorReading, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 154 | 160 |
| 155 void SensorReader::NotifyReadError() { | 161 void SensorReader::NotifyReadError() { |
| 156 if (is_reading_active_) { | 162 if (is_reading_active_) { |
| 157 task_runner_->PostTask( | 163 task_runner_->PostTask( |
| 158 FROM_HERE, base::Bind(&PlatformSensorLinux::NotifyPlatformSensorError, | 164 FROM_HERE, base::Bind(&PlatformSensorLinux::NotifyPlatformSensorError, |
| 159 base::Unretained(sensor_))); | 165 base::Unretained(sensor_))); |
| 160 } | 166 } |
| 161 } | 167 } |
| 162 | 168 |
| 163 } // namespace device | 169 } // namespace device |
| OLD | NEW |