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

Unified Diff: device/generic_sensor/platform_sensor_provider_iio.cc

Issue 2370343002: [sensors] Ambient light sensor implementation for ChromeOS and Linux. (Closed)
Patch Set: Newest version. and some comments 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..9a4989d4e5703d22eda458636c734234f7c3b39f
--- /dev/null
+++ b/device/generic_sensor/platform_sensor_provider_iio.cc
@@ -0,0 +1,146 @@
+// 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.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 base {
+class SingleThreadTaskRunner;
+}
+
+namespace device {
+
+class PlatformSensorProviderIio : public PlatformSensorProvider {
+ public:
+ PlatformSensorProviderIio();
+ ~PlatformSensorProviderIio() override;
+
+ static PlatformSensorProviderIio* GetInstance();
+
+ protected:
+ void CreateSensorInternal(mojom::SensorType type,
+ mojo::ScopedSharedBufferMapping mapping,
+ const CreateSensorCallback& callback) override;
+
+ private:
+ void SensorReaderFound(
+ mojom::SensorType type,
+ mojo::ScopedSharedBufferMapping mapping,
+ const PlatformSensorProviderBase::CreateSensorCallback& callback,
+ const SensorDataIio& data,
+ std::unique_ptr<base::Thread> polling_thread,
+ std::unique_ptr<SensorReader> sensor_reader);
+
+ // Helper function that gets a new sensor reader with a sensor type specific
+ // options taken from SensorDataIio.
+ std::unique_ptr<SensorReader> GetSensorReader(
+ const SensorDataIio& data,
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner);
+
+ // TODO(maksims): make this as a separate class Manager that will
+ // create threads for new sensors, check sensors existence and notify provider
+ // if a new sensor has appeared and it can be created if a request comes
+ // again for the same sensor.
+ // A use case example: a request for a sensor X comes, manager checks if the
+ // sensor exists on a platform and notifies a provider it is not found.
+ // The provider stores this information into its cache and doesn't try to
+ // create this specific sensor if a request comes. But when, for example,
+ // the sensor X is plugged into a usb port, the manager notices that and
+ // notifies the provider, which updates its cache and starts handling requests
+ // for the sensor X.
+ //
+ // Right now, it is only used to delete polling threads if a new sensor
+ // is not created and the thread cannot be passed to it.
+ std::unique_ptr<base::Thread> manager_thread_;
+
+ DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderIio);
+};
+
+PlatformSensorProvider* PlatformSensorProvider::GetInstance() {
+ return PlatformSensorProviderIio::GetInstance();
+}
+
+// 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 (!CreateSensorData(type, &data)) {
+ callback.Run(nullptr);
+ return;
+ }
+
+ if (!manager_thread_) {
+ manager_thread_.reset(new base::Thread("Linux/CrOS manager thread"));
+ if (!manager_thread_->StartWithOptions(
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
+ callback.Run(nullptr);
+ return;
+ }
+ }
+
+ std::unique_ptr<base::Thread> polling_thread(
+ new base::Thread("Linux/CrOS polling sensor thread."));
+
+ if (!polling_thread->StartWithOptions(
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
+ callback.Run(nullptr);
+ return;
+ }
+
+ base::PostTaskAndReplyWithResult(
+ polling_thread->task_runner().get(), FROM_HERE,
+ base::Bind(&PlatformSensorProviderIio::GetSensorReader,
+ base::Unretained(this), data, polling_thread->task_runner()),
+ base::Bind(&PlatformSensorProviderIio::SensorReaderFound,
+ base::Unretained(this), type, base::Passed(&mapping), callback,
+ data, base::Passed(&polling_thread)));
+}
+
+void PlatformSensorProviderIio::SensorReaderFound(
+ mojom::SensorType type,
+ mojo::ScopedSharedBufferMapping mapping,
+ const PlatformSensorProviderBase::CreateSensorCallback& callback,
+ const SensorDataIio& data,
+ std::unique_ptr<base::Thread> polling_thread,
+ std::unique_ptr<SensorReader> sensor_reader) {
+ DCHECK(CalledOnValidThread());
+
+ if (!sensor_reader) {
+ manager_thread_->task_runner()->DeleteSoon(FROM_HERE,
+ polling_thread.release());
+ callback.Run(nullptr);
+ return;
+ }
+
+ callback.Run(new PlatformSensorIio(type, std::move(mapping), this, data,
+ std::move(polling_thread),
+ std::move(sensor_reader)));
+}
+
+std::unique_ptr<SensorReader> PlatformSensorProviderIio::GetSensorReader(
Mikhail 2016/10/14 13:59:05 can we do without this method? (call SensorReader:
maksims (do not use this acc) 2016/10/17 06:06:59 Done.
+ const SensorDataIio& data,
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
+ DCHECK(task_runner->BelongsToCurrentThread());
+ return SensorReader::Create(data);
+}
+
+} // namespace device
« device/generic_sensor/platform_sensor_iio.cc ('K') | « device/generic_sensor/platform_sensor_iio.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698