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

Side by Side Diff: device/sensors/data_fetcher_shared_memory_base.cc

Issue 2819273006: Move //device/sensor impl to be part of the internal implemenation of the Device Service. (Closed)
Patch Set: Move //device/sensor impl to be part of the internal implemenation of the Device Service Created 3 years, 8 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 2014 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/sensors/data_fetcher_shared_memory_base.h"
6
7 #include <stddef.h>
8 #include <string.h>
9
10 #include "base/bind.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/stl_util.h"
16 #include "base/threading/thread.h"
17 #include "base/timer/timer.h"
18 #include "device/sensors/public/cpp/device_light_hardware_buffer.h"
19 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h"
20 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h"
21
22 namespace device {
23
24 namespace {
25
26 size_t GetConsumerSharedMemoryBufferSize(ConsumerType consumer_type) {
27 switch (consumer_type) {
28 case CONSUMER_TYPE_MOTION:
29 return sizeof(DeviceMotionHardwareBuffer);
30 case CONSUMER_TYPE_ORIENTATION:
31 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE:
32 return sizeof(DeviceOrientationHardwareBuffer);
33 case CONSUMER_TYPE_LIGHT:
34 return sizeof(DeviceLightHardwareBuffer);
35 default:
36 NOTREACHED();
37 }
38 return 0;
39 }
40
41 } // namespace
42
43 class DataFetcherSharedMemoryBase::PollingThread : public base::Thread {
44 public:
45 PollingThread(const char* name, DataFetcherSharedMemoryBase* fetcher);
46 ~PollingThread() override;
47
48 void AddConsumer(ConsumerType consumer_type, void* buffer);
49 void RemoveConsumer(ConsumerType consumer_type);
50
51 unsigned GetConsumersBitmask() const { return consumers_bitmask_; }
52 bool IsTimerRunning() const { return timer_ ? timer_->IsRunning() : false; }
53
54 private:
55 void DoPoll();
56
57 unsigned consumers_bitmask_;
58 DataFetcherSharedMemoryBase* fetcher_;
59 std::unique_ptr<base::RepeatingTimer> timer_;
60
61 DISALLOW_COPY_AND_ASSIGN(PollingThread);
62 };
63
64 // --- PollingThread methods
65
66 DataFetcherSharedMemoryBase::PollingThread::PollingThread(
67 const char* name,
68 DataFetcherSharedMemoryBase* fetcher)
69 : base::Thread(name), consumers_bitmask_(0), fetcher_(fetcher) {}
70
71 DataFetcherSharedMemoryBase::PollingThread::~PollingThread() {}
72
73 void DataFetcherSharedMemoryBase::PollingThread::AddConsumer(
74 ConsumerType consumer_type,
75 void* buffer) {
76 DCHECK(fetcher_);
77 if (!fetcher_->Start(consumer_type, buffer))
78 return;
79
80 consumers_bitmask_ |= consumer_type;
81
82 if (!timer_ && fetcher_->GetType() == FETCHER_TYPE_POLLING_CALLBACK) {
83 timer_.reset(new base::RepeatingTimer());
84 timer_->Start(FROM_HERE, fetcher_->GetInterval(), this,
85 &PollingThread::DoPoll);
86 }
87 }
88
89 void DataFetcherSharedMemoryBase::PollingThread::RemoveConsumer(
90 ConsumerType consumer_type) {
91 DCHECK(fetcher_);
92 if (!fetcher_->Stop(consumer_type))
93 return;
94
95 consumers_bitmask_ &= ~consumer_type;
96
97 if (!consumers_bitmask_)
98 timer_.reset(); // will also stop the timer.
99 }
100
101 void DataFetcherSharedMemoryBase::PollingThread::DoPoll() {
102 DCHECK(fetcher_);
103 DCHECK(consumers_bitmask_);
104 fetcher_->Fetch(consumers_bitmask_);
105 }
106
107 // --- end of PollingThread methods
108
109 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase()
110 : started_consumers_(0) {}
111
112 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() {
113 DCHECK_EQ(0u, started_consumers_);
114
115 // By this point the polling thread should have already been stopped (it's not
116 // safe for it to be running in this class's destructor as tasks are posted to
117 // it that call virtual methods of this class).
118 DCHECK(!polling_thread_ || !polling_thread_->IsRunning());
119 }
120
121 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData(
122 ConsumerType consumer_type) {
123 if (started_consumers_ & consumer_type)
124 return true;
125
126 void* buffer = GetSharedMemoryBuffer(consumer_type);
127 if (!buffer)
128 return false;
129
130 size_t buffer_size = GetConsumerSharedMemoryBufferSize(consumer_type);
131 // buffer size should be strictly positive because buffer is non-zero.
132 DCHECK(buffer_size > 0);
133 // make sure to clear any potentially stale values in the memory buffer.
134 memset(buffer, 0, buffer_size);
135
136 if (GetType() != FETCHER_TYPE_DEFAULT) {
137 if (!InitAndStartPollingThreadIfNecessary())
138 return false;
139 polling_thread_->task_runner()->PostTask(
140 FROM_HERE, base::Bind(&PollingThread::AddConsumer,
141 base::Unretained(polling_thread_.get()),
142 consumer_type, buffer));
143 } else {
144 if (!Start(consumer_type, buffer))
145 return false;
146 }
147
148 started_consumers_ |= consumer_type;
149
150 return true;
151 }
152
153 bool DataFetcherSharedMemoryBase::StopFetchingDeviceData(
154 ConsumerType consumer_type) {
155 if (!(started_consumers_ & consumer_type))
156 return true;
157
158 if (GetType() != FETCHER_TYPE_DEFAULT) {
159 polling_thread_->task_runner()->PostTask(
160 FROM_HERE,
161 base::Bind(&PollingThread::RemoveConsumer,
162 base::Unretained(polling_thread_.get()), consumer_type));
163 } else {
164 if (!Stop(consumer_type))
165 return false;
166 }
167
168 started_consumers_ ^= consumer_type;
169
170 return true;
171 }
172
173 void DataFetcherSharedMemoryBase::Shutdown() {
174 StopFetchingDeviceData(CONSUMER_TYPE_MOTION);
175 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION);
176 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION_ABSOLUTE);
177 StopFetchingDeviceData(CONSUMER_TYPE_LIGHT);
178
179 // Ensure that the polling thread stops before entering the destructor of the
180 // subclass, as the stopping of the polling thread causes tasks to execute
181 // that call virtual methods of this class, which can cause crashes if they
182 // execute while (or after) the subclass is being torn down.
183 if (polling_thread_)
184 polling_thread_->Stop();
185 }
186
187 mojo::ScopedSharedBufferHandle
188 DataFetcherSharedMemoryBase::GetSharedMemoryHandle(ConsumerType consumer_type) {
189 auto it = shared_memory_map_.find(consumer_type);
190 DCHECK(it != shared_memory_map_.end());
191 return it->second.first->Clone();
192 }
193
194 bool DataFetcherSharedMemoryBase::InitAndStartPollingThreadIfNecessary() {
195 if (polling_thread_)
196 return true;
197
198 polling_thread_.reset(new PollingThread("Device Sensor poller", this));
199
200 if (!polling_thread_->Start()) {
201 LOG(ERROR) << "Failed to start sensor data polling thread";
202 return false;
203 }
204 return true;
205 }
206
207 void DataFetcherSharedMemoryBase::Fetch(unsigned consumer_bitmask) {
208 NOTIMPLEMENTED();
209 }
210
211 DataFetcherSharedMemoryBase::FetcherType DataFetcherSharedMemoryBase::GetType()
212 const {
213 return FETCHER_TYPE_DEFAULT;
214 }
215
216 base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const {
217 return base::TimeDelta::FromMicroseconds(kDeviceSensorIntervalMicroseconds);
218 }
219
220 void* DataFetcherSharedMemoryBase::GetSharedMemoryBuffer(
221 ConsumerType consumer_type) {
222 auto it = shared_memory_map_.find(consumer_type);
223 if (it != shared_memory_map_.end())
224 return it->second.second.get();
225
226 size_t buffer_size = GetConsumerSharedMemoryBufferSize(consumer_type);
227 if (buffer_size == 0)
228 return nullptr;
229
230 mojo::ScopedSharedBufferHandle buffer =
231 mojo::SharedBufferHandle::Create(buffer_size);
232 mojo::ScopedSharedBufferMapping mapping = buffer->Map(buffer_size);
233 if (!mapping)
234 return nullptr;
235 void* mem = mapping.get();
236 memset(mem, 0, buffer_size);
237 shared_memory_map_[consumer_type] =
238 std::make_pair(std::move(buffer), std::move(mapping));
239 return mem;
240 }
241
242 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const {
243 return polling_thread_ ? polling_thread_->message_loop() : nullptr;
244 }
245
246 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const {
247 return polling_thread_ ? polling_thread_->IsTimerRunning() : false;
248 }
249
250 } // namespace device
OLDNEW
« no previous file with comments | « device/sensors/data_fetcher_shared_memory_base.h ('k') | device/sensors/data_fetcher_shared_memory_base_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698