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

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: changed implementation 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 "base/threading/thread_task_runner_handle.h"
11 #include "device/generic_sensor/platform_sensor_ambient_light_iio.h"
12
13 namespace base {
14 class SingleThreadTaskRunner;
15 }
16
17 namespace device {
18
19 namespace {
20
21 using mojom::SensorType;
22
23 } // namespace
24
25 class PlatformSensorProviderIio : public PlatformSensorProvider {
26 public:
27 PlatformSensorProviderIio();
28 ~PlatformSensorProviderIio() override;
29
30 static PlatformSensorProviderIio* GetInstance();
31
32 protected:
33 void CreateSensorInternal(mojom::SensorType type,
34 mojo::ScopedSharedBufferMapping mapping,
35 const CreateSensorCallback& callback) override;
36
37 private:
38 void DoCreateSensor(
39 mojom::SensorType type,
40 mojo::ScopedSharedBufferMapping mapping,
41 const PlatformSensorProviderBase::CreateSensorCallback& callback,
42 std::unique_ptr<SensorReader> sensor_reader);
43
44 std::unique_ptr<SensorReader> GetSensorReader(mojom::SensorType type);
45
46 std::unique_ptr<base::Thread> polling_thread_;
47
48 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderIio);
49 };
50
51 PlatformSensorProvider* PlatformSensorProvider::GetInstance() {
52 return PlatformSensorProviderIio::GetInstance();
53 }
54
55 // static
56 PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() {
57 return base::Singleton<
58 PlatformSensorProviderIio,
59 base::LeakySingletonTraits<PlatformSensorProviderIio>>::get();
60 }
61
62 PlatformSensorProviderIio::PlatformSensorProviderIio() = default;
63
64 PlatformSensorProviderIio::~PlatformSensorProviderIio() {
65 if (polling_thread_)
66 polling_thread_->Stop();
Mikhail 2016/10/11 14:46:47 won't it be stopped automatically when deleted? W
67 }
68
69 void PlatformSensorProviderIio::CreateSensorInternal(
70 mojom::SensorType type,
71 mojo::ScopedSharedBufferMapping mapping,
72 const CreateSensorCallback& callback) {
73 if (!polling_thread_) {
74 polling_thread_.reset(
75 new base::Thread("Sensors polling thread CrOS/Linux"));
76 if (!polling_thread_->StartWithOptions(
77 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
78 callback.Run(nullptr);
79 return;
80 }
81 }
82
83 base::PostTaskAndReplyWithResult(
84 polling_thread_->message_loop()->task_runner().get(), FROM_HERE,
85 base::Bind(&PlatformSensorProviderIio::GetSensorReader,
86 base::Unretained(this), type),
87 base::Bind(&PlatformSensorProviderIio::DoCreateSensor,
Mikhail 2016/10/11 14:46:47 s/DoCreateSensor/SensorReaderFound
maksims (do not use this acc) 2016/10/14 12:31:45 Done.
88 base::Unretained(this), type, base::Passed(&mapping),
89 callback));
90 }
91
92 void PlatformSensorProviderIio::DoCreateSensor(
93 mojom::SensorType type,
94 mojo::ScopedSharedBufferMapping mapping,
95 const PlatformSensorProviderBase::CreateSensorCallback& callback,
96 std::unique_ptr<SensorReader> sensor_reader) {
97 DCHECK(CalledOnValidThread());
98 if (!sensor_reader) {
99 callback.Run(nullptr);
100 return;
101 }
102
103 scoped_refptr<PlatformSensor> sensor = nullptr;
104 switch (type) {
105 case SensorType::AMBIENT_LIGHT:
106 sensor = new PlatformSensorAmbientLightIio(
107 type, move(mapping), this,
108 polling_thread_->message_loop()->task_runner(),
109 std::move(sensor_reader));
110 break;
111 default:
112 NOTIMPLEMENTED();
113 break;
114 }
115
116 callback.Run(sensor);
117 }
118
119 std::unique_ptr<SensorReader> PlatformSensorProviderIio::GetSensorReader(
120 mojom::SensorType type) {
Mikhail 2016/10/11 14:46:47 thread check is missing
121 return SensorReader::Create(type);
122 }
123
124 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698