Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Unified Diff: device/generic_sensor/platform_sensor_provider_iio.cc

Issue 2370343002: [sensors] Ambient light sensor implementation for ChromeOS and Linux. (Closed)
Patch Set: change SetTaskRunner to SetFileTaskRunner as per offline discussion with Mikhail Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/generic_sensor/platform_sensor_provider_iio.cc
diff --git a/device/generic_sensor/platform_sensor_provider_iio.cc b/device/generic_sensor/platform_sensor_provider_iio.cc
new file mode 100644
index 0000000000000000000000000000000000000000..956a18f777f19bda7cd5bc59b043a2d121611ab0
--- /dev/null
+++ b/device/generic_sensor/platform_sensor_provider_iio.cc
@@ -0,0 +1,97 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "device/generic_sensor/platform_sensor_provider_iio.h"
+
+#include "base/memory/singleton.h"
+#include "base/task_runner_util.h"
+#include "base/threading/thread.h"
+#include "device/generic_sensor/iio/platform_sensor_utils_iio.h"
+#include "device/generic_sensor/iio/sensor_data_iio.h"
+#include "device/generic_sensor/platform_sensor_iio.h"
+
+namespace device {
+
+// static
+PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() {
+ return base::Singleton<
+ PlatformSensorProviderIio,
+ base::LeakySingletonTraits<PlatformSensorProviderIio>>::get();
+}
+
+PlatformSensorProviderIio::PlatformSensorProviderIio() = default;
+
+PlatformSensorProviderIio::~PlatformSensorProviderIio() = default;
+
+void PlatformSensorProviderIio::CreateSensorInternal(
+ mojom::SensorType type,
+ mojo::ScopedSharedBufferMapping mapping,
+ const CreateSensorCallback& callback) {
+ SensorDataIio data;
+ if (!InitSensorData(type, &data)) {
+ callback.Run(nullptr);
+ return;
+ }
+
+ if (!polling_thread_)
+ polling_thread_.reset(new base::Thread("Linux/CrOS manager thread"));
timvolodine 2016/10/31 18:28:17 If the file is chrome/linux specific it seems to b
maksims (do not use this acc) 2016/10/31 19:21:48 thanks. forgot to change that.
+
+ if (!polling_thread_->IsRunning()) {
+ if (!polling_thread_->StartWithOptions(
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
+ callback.Run(nullptr);
+ return;
+ }
+ polling_thread_task_runner_ = polling_thread_->task_runner();
+ }
+
+ base::PostTaskAndReplyWithResult(
+ polling_thread_task_runner_.get(), FROM_HERE,
+ base::Bind(SensorReader::Create, data),
+ base::Bind(&PlatformSensorProviderIio::SensorReaderFound,
+ base::Unretained(this), type, base::Passed(&mapping), callback,
+ data));
+}
+
+void PlatformSensorProviderIio::SensorReaderFound(
+ mojom::SensorType type,
+ mojo::ScopedSharedBufferMapping mapping,
+ const PlatformSensorProviderBase::CreateSensorCallback& callback,
+ const SensorDataIio& data,
+ std::unique_ptr<SensorReader> sensor_reader) {
+ DCHECK(CalledOnValidThread());
+
+ if (!sensor_reader) {
+ // If there are no sensors, stop polling thread.
+ if (!HasSensors())
+ AllSensorsRemoved();
+ callback.Run(nullptr);
+ return;
+ }
+
+ callback.Run(new PlatformSensorIio(type, std::move(mapping), this, data,
+ std::move(sensor_reader),
+ polling_thread_task_runner_));
+}
+
+void PlatformSensorProviderIio::SetFileTaskRunner(
+ scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) {
+ // This task runner mustn't belong to current IO thread.
+ DCHECK(!file_task_runner->BelongsToCurrentThread());
Mikhail 2016/10/31 12:50:32 think this DCHECK is unnecessary.
maksims (do not use this acc) 2016/11/01 08:10:31 Done.
+ file_task_runner_ = file_task_runner;
Mikhail 2016/10/31 12:50:32 Should be only set once. And on the valid thread.
maksims (do not use this acc) 2016/11/01 08:10:31 Done.
+}
+
+void PlatformSensorProviderIio::AllSensorsRemoved() {
+ DCHECK(CalledOnValidThread());
+ file_task_runner_->PostTask(
Mikhail 2016/10/31 12:50:32 pls add a short comment explaining why this delega
Mikhail 2016/10/31 12:50:32 DCHECK(file_task_runner_);
maksims (do not use this acc) 2016/11/01 08:10:31 Done.
maksims (do not use this acc) 2016/11/01 08:10:31 Done.
+ FROM_HERE, base::Bind(&PlatformSensorProviderIio::StopPollingThread,
+ base::Unretained(this)));
+}
+
+void PlatformSensorProviderIio::StopPollingThread() {
+ DCHECK(file_task_runner_->BelongsToCurrentThread());
Mikhail 2016/10/31 12:50:32 DCHECK(file_task_runner_);
maksims (do not use this acc) 2016/11/01 08:10:31 Done.
+ polling_thread_->Stop();
+}
+
+} // namespace device

Powered by Google App Engine
This is Rietveld 408576698