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

Side by Side 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 unified diff | Download patch
OLDNEW
(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_provider.h"
6
7 #include "base/memory/singleton.h"
8 #include "base/task_runner_util.h"
9 #include "base/threading/thread.h"
10 #include "device/generic_sensor/iio/platform_sensor_utils_iio.h"
11 #include "device/generic_sensor/iio/sensor_data_iio.h"
12 #include "device/generic_sensor/platform_sensor_iio.h"
13
14 namespace base {
15 class SingleThreadTaskRunner;
16 }
17
18 namespace device {
19
20 class PlatformSensorProviderIio : public PlatformSensorProvider {
21 public:
22 PlatformSensorProviderIio();
23 ~PlatformSensorProviderIio() override;
24
25 static PlatformSensorProviderIio* GetInstance();
26
27 protected:
28 void CreateSensorInternal(mojom::SensorType type,
29 mojo::ScopedSharedBufferMapping mapping,
30 const CreateSensorCallback& callback) override;
31
32 private:
33 void SensorReaderFound(
34 mojom::SensorType type,
35 mojo::ScopedSharedBufferMapping mapping,
36 const PlatformSensorProviderBase::CreateSensorCallback& callback,
37 const SensorDataIio& data,
38 std::unique_ptr<base::Thread> polling_thread,
39 std::unique_ptr<SensorReader> sensor_reader);
40
41 // Helper function that gets a new sensor reader with a sensor type specific
42 // options taken from SensorDataIio.
43 std::unique_ptr<SensorReader> GetSensorReader(
44 const SensorDataIio& data,
45 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
46
47 // TODO(maksims): make this as a separate class Manager that will
48 // create threads for new sensors, check sensors existence and notify provider
49 // if a new sensor has appeared and it can be created if a request comes
50 // again for the same sensor.
51 // A use case example: a request for a sensor X comes, manager checks if the
52 // sensor exists on a platform and notifies a provider it is not found.
53 // The provider stores this information into its cache and doesn't try to
54 // create this specific sensor if a request comes. But when, for example,
55 // the sensor X is plugged into a usb port, the manager notices that and
56 // notifies the provider, which updates its cache and starts handling requests
57 // for the sensor X.
58 //
59 // Right now, it is only used to delete polling threads if a new sensor
60 // is not created and the thread cannot be passed to it.
61 std::unique_ptr<base::Thread> manager_thread_;
62
63 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderIio);
64 };
65
66 PlatformSensorProvider* PlatformSensorProvider::GetInstance() {
67 return PlatformSensorProviderIio::GetInstance();
68 }
69
70 // static
71 PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() {
72 return base::Singleton<
73 PlatformSensorProviderIio,
74 base::LeakySingletonTraits<PlatformSensorProviderIio>>::get();
75 }
76
77 PlatformSensorProviderIio::PlatformSensorProviderIio() = default;
78
79 PlatformSensorProviderIio::~PlatformSensorProviderIio() = default;
80
81 void PlatformSensorProviderIio::CreateSensorInternal(
82 mojom::SensorType type,
83 mojo::ScopedSharedBufferMapping mapping,
84 const CreateSensorCallback& callback) {
85 SensorDataIio data;
86 if (!CreateSensorData(type, &data)) {
87 callback.Run(nullptr);
88 return;
89 }
90
91 if (!manager_thread_) {
92 manager_thread_.reset(new base::Thread("Linux/CrOS manager thread"));
93 if (!manager_thread_->StartWithOptions(
94 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
95 callback.Run(nullptr);
96 return;
97 }
98 }
99
100 std::unique_ptr<base::Thread> polling_thread(
101 new base::Thread("Linux/CrOS polling sensor thread."));
102
103 if (!polling_thread->StartWithOptions(
104 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
105 callback.Run(nullptr);
106 return;
107 }
108
109 base::PostTaskAndReplyWithResult(
110 polling_thread->task_runner().get(), FROM_HERE,
111 base::Bind(&PlatformSensorProviderIio::GetSensorReader,
112 base::Unretained(this), data, polling_thread->task_runner()),
113 base::Bind(&PlatformSensorProviderIio::SensorReaderFound,
114 base::Unretained(this), type, base::Passed(&mapping), callback,
115 data, base::Passed(&polling_thread)));
116 }
117
118 void PlatformSensorProviderIio::SensorReaderFound(
119 mojom::SensorType type,
120 mojo::ScopedSharedBufferMapping mapping,
121 const PlatformSensorProviderBase::CreateSensorCallback& callback,
122 const SensorDataIio& data,
123 std::unique_ptr<base::Thread> polling_thread,
124 std::unique_ptr<SensorReader> sensor_reader) {
125 DCHECK(CalledOnValidThread());
126
127 if (!sensor_reader) {
128 manager_thread_->task_runner()->DeleteSoon(FROM_HERE,
129 polling_thread.release());
130 callback.Run(nullptr);
131 return;
132 }
133
134 callback.Run(new PlatformSensorIio(type, std::move(mapping), this, data,
135 std::move(polling_thread),
136 std::move(sensor_reader)));
137 }
138
139 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.
140 const SensorDataIio& data,
141 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
142 DCHECK(task_runner->BelongsToCurrentThread());
143 return SensorReader::Create(data);
144 }
145
146 } // namespace device
OLDNEW
« 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