Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "device/generic_sensor/platform_sensor_ambient_light_iio.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/synchronization/shared_memory_seqlock_buffer.h" | |
| 9 | |
| 10 namespace device { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 using DeviceLightHardwareBuffer = base::SharedMemorySeqLockBuffer<double>; | |
| 15 | |
| 16 void DestroySensorOnMainThread(scoped_refptr<PlatformSensor> sensor) { | |
| 17 sensor = nullptr; | |
| 18 } | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 22 // static | |
| 23 scoped_refptr<PlatformSensor> PlatformSensorAmbientLightIio::CreateSensor( | |
| 24 mojom::SensorType type, | |
| 25 mojo::ScopedSharedBufferMapping mapping, | |
| 26 uint64_t buffer_size, | |
| 27 PlatformSensorProvider* provider, | |
| 28 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) { | |
| 29 scoped_refptr<PlatformSensorAmbientLightIio> sensor = | |
| 30 new PlatformSensorAmbientLightIio(type, std::move(mapping), buffer_size, | |
| 31 provider, ipc_task_runner); | |
| 32 if (!sensor->sensor_exists()) { | |
| 33 ipc_task_runner->PostTask( | |
| 34 FROM_HERE, base::Bind(&DestroySensorOnMainThread, std::move(sensor))); | |
| 35 return nullptr; | |
| 36 } | |
| 37 return std::move(sensor); | |
| 38 } | |
| 39 | |
| 40 PlatformSensorAmbientLightIio::PlatformSensorAmbientLightIio( | |
| 41 mojom::SensorType type, | |
| 42 mojo::ScopedSharedBufferMapping mapping, | |
| 43 uint64_t buffer_size, | |
| 44 PlatformSensorProvider* provider, | |
| 45 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) | |
| 46 : PollingPlatformSensor(type, | |
| 47 std::move(mapping), | |
| 48 provider, | |
| 49 std::move(ipc_task_runner)), | |
| 50 lux_value_(-1), | |
| 51 sensor_exists_(false), | |
| 52 ambient_light_reader_(new AmbientLightSensorReaderIio()), | |
| 53 weak_factory_(this) { | |
| 54 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); | |
| 55 if (ambient_light_reader_->DetectLightSensor()) { | |
| 56 sensor_exists_ = true; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 PlatformSensorAmbientLightIio::~PlatformSensorAmbientLightIio() = default; | |
| 61 | |
| 62 mojom::ReportingMode PlatformSensorAmbientLightIio::GetReportingMode() { | |
| 63 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); | |
| 64 return mojom::ReportingMode::ON_CHANGE; | |
| 65 } | |
| 66 | |
| 67 PlatformSensorConfiguration | |
| 68 PlatformSensorAmbientLightIio::GetDefaultConfiguration() { | |
| 69 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); | |
| 70 return PlatformSensorConfiguration(5); | |
| 71 } | |
| 72 | |
| 73 bool PlatformSensorAmbientLightIio::CheckSensorConfiguration( | |
| 74 const PlatformSensorConfiguration& configuration) { | |
| 75 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); | |
| 76 return configuration.frequency() > 0 && | |
| 77 configuration.frequency() <= | |
| 78 PlatformSensorConfiguration::kMaxAllowedFrequency; | |
| 79 } | |
| 80 | |
| 81 void PlatformSensorAmbientLightIio::UpdateReading() { | |
| 82 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); | |
| 83 | |
| 84 double update = -1; | |
| 85 if (!ambient_light_reader_->ReadSensorLuxValue(&update)) { | |
| 86 StopSensor(); | |
|
darktears
2016/09/30 21:35:32
Is this the behavior that we should follow?
| |
| 87 ipc_task_runner_->PostTask( | |
| 88 FROM_HERE, base::Bind(&PlatformSensorAmbientLightIio::NotifySensorError, | |
| 89 weak_factory_.GetWeakPtr())); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 if (lux_value_ == update) | |
| 94 return; | |
| 95 | |
| 96 lux_value_ = update; | |
| 97 | |
| 98 DeviceLightHardwareBuffer* light_buffer = | |
| 99 static_cast<DeviceLightHardwareBuffer*>(shared_buffer_mapping_.get()); | |
| 100 light_buffer->seqlock.WriteBegin(); | |
| 101 light_buffer->data = lux_value_; | |
| 102 light_buffer->seqlock.WriteEnd(); | |
| 103 | |
| 104 ipc_task_runner_->PostTask( | |
| 105 FROM_HERE, | |
| 106 base::Bind(&PlatformSensorAmbientLightIio::NotifySensorReadingChanged, | |
| 107 weak_factory_.GetWeakPtr())); | |
| 108 } | |
| 109 | |
| 110 } // namespace device | |
| OLD | NEW |