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

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: add comments to unittest 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();
shalamov 2016/10/19 16:37:11 make private
maksims (do not use this acc) 2016/10/20 10:04:51 Done.
23 ~PlatformSensorProviderIio() override;
shalamov 2016/10/19 16:37:11 ditto
maksims (do not use this acc) 2016/10/20 10:04:51 Done.
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 (!InitSensorData(type, &data)) {
87 callback.Run(nullptr);
88 return;
89 }
90
91 if (!manager_thread_) {
shalamov 2016/10/19 16:37:11 who is stopping this thread? Looks like it will be
Mikhail 2016/10/19 17:25:41 that requires an extra API in generic classes, I'l
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(SensorReader::Create, data),
112 base::Bind(&PlatformSensorProviderIio::SensorReaderFound,
113 base::Unretained(this), type, base::Passed(&mapping), callback,
114 data, base::Passed(&polling_thread)));
115 }
116
117 void PlatformSensorProviderIio::SensorReaderFound(
118 mojom::SensorType type,
119 mojo::ScopedSharedBufferMapping mapping,
120 const PlatformSensorProviderBase::CreateSensorCallback& callback,
121 const SensorDataIio& data,
122 std::unique_ptr<base::Thread> polling_thread,
123 std::unique_ptr<SensorReader> sensor_reader) {
124 DCHECK(CalledOnValidThread());
125
126 if (!sensor_reader) {
127 manager_thread_->task_runner()->DeleteSoon(FROM_HERE,
128 polling_thread.release());
129 callback.Run(nullptr);
130 return;
131 }
132
133 callback.Run(new PlatformSensorIio(type, std::move(mapping), this, data,
134 std::move(polling_thread),
135 std::move(sensor_reader)));
136 }
137
138 } // 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