OLD | NEW |
| (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 "content/browser/device_sensors/data_fetcher_shared_memory.h" | |
6 | |
7 #include <GuidDef.h> | |
8 #include <InitGuid.h> | |
9 #include <PortableDeviceTypes.h> | |
10 #include <Sensors.h> | |
11 | |
12 #include "base/logging.h" | |
13 #include "base/macros.h" | |
14 #include "base/metrics/histogram_macros.h" | |
15 #include "base/win/iunknown_impl.h" | |
16 #include "base/win/windows_version.h" | |
17 | |
18 namespace { | |
19 | |
20 const double kMeanGravity = 9.80665; | |
21 | |
22 void SetLightBuffer(content::DeviceLightHardwareBuffer* buffer, double lux) { | |
23 DCHECK(buffer); | |
24 buffer->seqlock.WriteBegin(); | |
25 buffer->data.value = lux; | |
26 buffer->seqlock.WriteEnd(); | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 | |
32 namespace content { | |
33 | |
34 class DataFetcherSharedMemory::SensorEventSink | |
35 : public ISensorEvents, public base::win::IUnknownImpl { | |
36 public: | |
37 SensorEventSink() {} | |
38 ~SensorEventSink() override {} | |
39 | |
40 // IUnknown interface | |
41 ULONG STDMETHODCALLTYPE AddRef() override { | |
42 return IUnknownImpl::AddRef(); | |
43 } | |
44 | |
45 ULONG STDMETHODCALLTYPE Release() override { | |
46 return IUnknownImpl::Release(); | |
47 } | |
48 | |
49 STDMETHODIMP QueryInterface(REFIID riid, void** ppv) override { | |
50 if (riid == __uuidof(ISensorEvents)) { | |
51 *ppv = static_cast<ISensorEvents*>(this); | |
52 AddRef(); | |
53 return S_OK; | |
54 } | |
55 return IUnknownImpl::QueryInterface(riid, ppv); | |
56 } | |
57 | |
58 // ISensorEvents interface | |
59 STDMETHODIMP OnEvent(ISensor* sensor, | |
60 REFGUID event_id, | |
61 IPortableDeviceValues* event_data) override { | |
62 return S_OK; | |
63 } | |
64 | |
65 STDMETHODIMP OnLeave(REFSENSOR_ID sensor_id) override { | |
66 return S_OK; | |
67 } | |
68 | |
69 STDMETHODIMP OnStateChanged(ISensor* sensor, SensorState state) override { | |
70 return S_OK; | |
71 } | |
72 | |
73 STDMETHODIMP OnDataUpdated(ISensor* sensor, | |
74 ISensorDataReport* new_data) override { | |
75 if (nullptr == new_data || nullptr == sensor) | |
76 return E_INVALIDARG; | |
77 return UpdateSharedMemoryBuffer(sensor, new_data) ? S_OK : E_FAIL; | |
78 } | |
79 | |
80 protected: | |
81 virtual bool UpdateSharedMemoryBuffer( | |
82 ISensor* sensor, ISensorDataReport* new_data) = 0; | |
83 | |
84 void GetSensorValue(REFPROPERTYKEY property, ISensorDataReport* new_data, | |
85 double* value, bool* has_value) { | |
86 PROPVARIANT variant_value = {}; | |
87 if (SUCCEEDED(new_data->GetSensorValue(property, &variant_value))) { | |
88 if (variant_value.vt == VT_R8) | |
89 *value = variant_value.dblVal; | |
90 else if (variant_value.vt == VT_R4) | |
91 *value = variant_value.fltVal; | |
92 *has_value = true; | |
93 } else { | |
94 *value = 0; | |
95 *has_value = false; | |
96 } | |
97 } | |
98 | |
99 private: | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(SensorEventSink); | |
102 }; | |
103 | |
104 class DataFetcherSharedMemory::SensorEventSinkOrientation | |
105 : public DataFetcherSharedMemory::SensorEventSink { | |
106 public: | |
107 explicit SensorEventSinkOrientation( | |
108 DeviceOrientationHardwareBuffer* const buffer) : buffer_(buffer) {} | |
109 ~SensorEventSinkOrientation() override {} | |
110 | |
111 protected: | |
112 bool UpdateSharedMemoryBuffer( | |
113 ISensor* sensor, ISensorDataReport* new_data) override { | |
114 double alpha, beta, gamma; | |
115 bool has_alpha, has_beta, has_gamma; | |
116 | |
117 GetSensorValue(SENSOR_DATA_TYPE_TILT_X_DEGREES, new_data, &beta, | |
118 &has_beta); | |
119 GetSensorValue(SENSOR_DATA_TYPE_TILT_Y_DEGREES, new_data, &gamma, | |
120 &has_gamma); | |
121 GetSensorValue(SENSOR_DATA_TYPE_TILT_Z_DEGREES, new_data, &alpha, | |
122 &has_alpha); | |
123 | |
124 if (buffer_) { | |
125 buffer_->seqlock.WriteBegin(); | |
126 buffer_->data.alpha = alpha; | |
127 buffer_->data.hasAlpha = has_alpha; | |
128 buffer_->data.beta = beta; | |
129 buffer_->data.hasBeta = has_beta; | |
130 buffer_->data.gamma = gamma; | |
131 buffer_->data.hasGamma = has_gamma; | |
132 buffer_->data.absolute = has_alpha || has_beta || has_gamma; | |
133 buffer_->data.allAvailableSensorsAreActive = true; | |
134 buffer_->seqlock.WriteEnd(); | |
135 } | |
136 | |
137 return true; | |
138 } | |
139 | |
140 private: | |
141 DeviceOrientationHardwareBuffer* const buffer_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(SensorEventSinkOrientation); | |
144 }; | |
145 | |
146 class DataFetcherSharedMemory::SensorEventSinkMotion | |
147 : public DataFetcherSharedMemory::SensorEventSink { | |
148 public: | |
149 explicit SensorEventSinkMotion(DeviceMotionHardwareBuffer* const buffer) | |
150 : buffer_(buffer) {} | |
151 ~SensorEventSinkMotion() override {} | |
152 | |
153 protected: | |
154 bool UpdateSharedMemoryBuffer( | |
155 ISensor* sensor, ISensorDataReport* new_data) override { | |
156 | |
157 SENSOR_TYPE_ID sensor_type = GUID_NULL; | |
158 if (!SUCCEEDED(sensor->GetType(&sensor_type))) | |
159 return false; | |
160 | |
161 if (IsEqualIID(sensor_type, SENSOR_TYPE_ACCELEROMETER_3D)) { | |
162 double acceleration_including_gravity_x; | |
163 double acceleration_including_gravity_y; | |
164 double acceleration_including_gravity_z; | |
165 bool has_acceleration_including_gravity_x; | |
166 bool has_acceleration_including_gravity_y; | |
167 bool has_acceleration_including_gravity_z; | |
168 | |
169 GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_X_G, new_data, | |
170 &acceleration_including_gravity_x, | |
171 &has_acceleration_including_gravity_x); | |
172 GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Y_G, new_data, | |
173 &acceleration_including_gravity_y, | |
174 &has_acceleration_including_gravity_y); | |
175 GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Z_G, new_data, | |
176 &acceleration_including_gravity_z, | |
177 &has_acceleration_including_gravity_z); | |
178 | |
179 if (buffer_) { | |
180 buffer_->seqlock.WriteBegin(); | |
181 buffer_->data.accelerationIncludingGravityX = | |
182 -acceleration_including_gravity_x * kMeanGravity; | |
183 buffer_->data.hasAccelerationIncludingGravityX = | |
184 has_acceleration_including_gravity_x; | |
185 buffer_->data.accelerationIncludingGravityY = | |
186 -acceleration_including_gravity_y * kMeanGravity; | |
187 buffer_->data.hasAccelerationIncludingGravityY = | |
188 has_acceleration_including_gravity_y; | |
189 buffer_->data.accelerationIncludingGravityZ = | |
190 -acceleration_including_gravity_z * kMeanGravity; | |
191 buffer_->data.hasAccelerationIncludingGravityZ = | |
192 has_acceleration_including_gravity_z; | |
193 // TODO(timvolodine): consider setting this after all | |
194 // sensors have fired. | |
195 buffer_->data.allAvailableSensorsAreActive = true; | |
196 buffer_->seqlock.WriteEnd(); | |
197 } | |
198 | |
199 } else if (IsEqualIID(sensor_type, SENSOR_TYPE_GYROMETER_3D)) { | |
200 double alpha, beta, gamma; | |
201 bool has_alpha, has_beta, has_gamma; | |
202 | |
203 GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, | |
204 new_data, &alpha, &has_alpha); | |
205 GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, | |
206 new_data, &beta, &has_beta); | |
207 GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, | |
208 new_data, &gamma, &has_gamma); | |
209 | |
210 if (buffer_) { | |
211 buffer_->seqlock.WriteBegin(); | |
212 buffer_->data.rotationRateAlpha = alpha; | |
213 buffer_->data.hasRotationRateAlpha = has_alpha; | |
214 buffer_->data.rotationRateBeta = beta; | |
215 buffer_->data.hasRotationRateBeta = has_beta; | |
216 buffer_->data.rotationRateGamma = gamma; | |
217 buffer_->data.hasRotationRateGamma = has_gamma; | |
218 buffer_->data.allAvailableSensorsAreActive = true; | |
219 buffer_->seqlock.WriteEnd(); | |
220 } | |
221 } | |
222 | |
223 return true; | |
224 } | |
225 | |
226 private: | |
227 DeviceMotionHardwareBuffer* const buffer_; | |
228 | |
229 DISALLOW_COPY_AND_ASSIGN(SensorEventSinkMotion); | |
230 }; | |
231 | |
232 class DataFetcherSharedMemory::SensorEventSinkLight | |
233 : public DataFetcherSharedMemory::SensorEventSink { | |
234 public: | |
235 explicit SensorEventSinkLight(DeviceLightHardwareBuffer* const buffer) | |
236 : buffer_(buffer) {} | |
237 ~SensorEventSinkLight() override {} | |
238 | |
239 protected: | |
240 bool UpdateSharedMemoryBuffer(ISensor* sensor, | |
241 ISensorDataReport* new_data) override { | |
242 double lux; | |
243 bool has_lux; | |
244 | |
245 GetSensorValue(SENSOR_DATA_TYPE_LIGHT_LEVEL_LUX, new_data, &lux, &has_lux); | |
246 | |
247 if(!has_lux) { | |
248 // Could not get lux value. | |
249 return false; | |
250 } | |
251 | |
252 SetLightBuffer(buffer_, lux); | |
253 | |
254 return true; | |
255 } | |
256 | |
257 private: | |
258 DeviceLightHardwareBuffer* const buffer_; | |
259 | |
260 DISALLOW_COPY_AND_ASSIGN(SensorEventSinkLight); | |
261 }; | |
262 | |
263 DataFetcherSharedMemory::DataFetcherSharedMemory() {} | |
264 | |
265 DataFetcherSharedMemory::~DataFetcherSharedMemory() { | |
266 } | |
267 | |
268 DataFetcherSharedMemory::FetcherType DataFetcherSharedMemory::GetType() const { | |
269 return FETCHER_TYPE_SEPARATE_THREAD; | |
270 } | |
271 | |
272 bool DataFetcherSharedMemory::Start(ConsumerType consumer_type, void* buffer) { | |
273 DCHECK(buffer); | |
274 | |
275 switch (consumer_type) { | |
276 case CONSUMER_TYPE_ORIENTATION: | |
277 { | |
278 orientation_buffer_ = | |
279 static_cast<DeviceOrientationHardwareBuffer*>(buffer); | |
280 scoped_refptr<SensorEventSink> sink( | |
281 new SensorEventSinkOrientation(orientation_buffer_)); | |
282 bool inclinometer_available = RegisterForSensor( | |
283 SENSOR_TYPE_INCLINOMETER_3D, sensor_inclinometer_.Receive(), sink); | |
284 UMA_HISTOGRAM_BOOLEAN("InertialSensor.InclinometerWindowsAvailable", | |
285 inclinometer_available); | |
286 if (inclinometer_available) | |
287 return true; | |
288 // if no sensors are available set buffer to ready, to fire null-events. | |
289 SetBufferAvailableState(consumer_type, true); | |
290 } | |
291 break; | |
292 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | |
293 { | |
294 orientation_absolute_buffer_ = | |
295 static_cast<DeviceOrientationHardwareBuffer*>(buffer); | |
296 scoped_refptr<SensorEventSink> sink( | |
297 new SensorEventSinkOrientation(orientation_absolute_buffer_)); | |
298 // Currently we use the same sensor as for orientation which provides | |
299 // absolute angles. | |
300 bool inclinometer_available = RegisterForSensor( | |
301 SENSOR_TYPE_INCLINOMETER_3D, | |
302 sensor_inclinometer_absolute_.Receive(), sink); | |
303 // TODO(timvolodine): consider adding UMA. | |
304 if (inclinometer_available) | |
305 return true; | |
306 // if no sensors are available set buffer to ready, to fire null-events. | |
307 SetBufferAvailableState(consumer_type, true); | |
308 } | |
309 break; | |
310 case CONSUMER_TYPE_MOTION: | |
311 { | |
312 motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer); | |
313 scoped_refptr<SensorEventSink> sink( | |
314 new SensorEventSinkMotion(motion_buffer_)); | |
315 bool accelerometer_available = RegisterForSensor( | |
316 SENSOR_TYPE_ACCELEROMETER_3D, sensor_accelerometer_.Receive(), | |
317 sink); | |
318 bool gyrometer_available = RegisterForSensor( | |
319 SENSOR_TYPE_GYROMETER_3D, sensor_gyrometer_.Receive(), sink); | |
320 UMA_HISTOGRAM_BOOLEAN("InertialSensor.AccelerometerWindowsAvailable", | |
321 accelerometer_available); | |
322 UMA_HISTOGRAM_BOOLEAN("InertialSensor.GyrometerWindowsAvailable", | |
323 gyrometer_available); | |
324 if (accelerometer_available || gyrometer_available) { | |
325 motion_buffer_->seqlock.WriteBegin(); | |
326 motion_buffer_->data.interval = GetInterval().InMilliseconds(); | |
327 motion_buffer_->seqlock.WriteEnd(); | |
328 return true; | |
329 } | |
330 // if no sensors are available set buffer to ready, to fire null-events. | |
331 SetBufferAvailableState(consumer_type, true); | |
332 } | |
333 break; | |
334 case CONSUMER_TYPE_LIGHT: | |
335 { | |
336 light_buffer_ = static_cast<DeviceLightHardwareBuffer*>(buffer); | |
337 scoped_refptr<SensorEventSink> sink( | |
338 new SensorEventSinkLight(light_buffer_)); | |
339 bool sensor_light_available = RegisterForSensor( | |
340 SENSOR_TYPE_AMBIENT_LIGHT, sensor_light_.Receive(), sink); | |
341 if (sensor_light_available) { | |
342 SetLightBuffer(light_buffer_, -1); | |
343 return true; | |
344 } | |
345 | |
346 // if no sensors are available, fire an Infinity event. | |
347 SetLightBuffer(light_buffer_, std::numeric_limits<double>::infinity()); | |
348 } | |
349 break; | |
350 default: | |
351 NOTREACHED(); | |
352 } | |
353 return false; | |
354 } | |
355 | |
356 bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) { | |
357 DisableSensors(consumer_type); | |
358 switch (consumer_type) { | |
359 case CONSUMER_TYPE_ORIENTATION: | |
360 SetBufferAvailableState(consumer_type, false); | |
361 orientation_buffer_ = nullptr; | |
362 return true; | |
363 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | |
364 SetBufferAvailableState(consumer_type, false); | |
365 orientation_absolute_buffer_ = nullptr; | |
366 return true; | |
367 case CONSUMER_TYPE_MOTION: | |
368 SetBufferAvailableState(consumer_type, false); | |
369 motion_buffer_ = nullptr; | |
370 return true; | |
371 case CONSUMER_TYPE_LIGHT: | |
372 SetLightBuffer(light_buffer_, -1); | |
373 light_buffer_ = nullptr; | |
374 return true; | |
375 default: | |
376 NOTREACHED(); | |
377 } | |
378 return false; | |
379 } | |
380 | |
381 bool DataFetcherSharedMemory::RegisterForSensor( | |
382 REFSENSOR_TYPE_ID sensor_type, | |
383 ISensor** sensor, | |
384 scoped_refptr<SensorEventSink> event_sink) { | |
385 if (base::win::GetVersion() < base::win::VERSION_WIN7) | |
386 return false; | |
387 | |
388 base::win::ScopedComPtr<ISensorManager> sensor_manager; | |
389 HRESULT hr = sensor_manager.CreateInstance(CLSID_SensorManager); | |
390 if (FAILED(hr) || !sensor_manager.get()) | |
391 return false; | |
392 | |
393 base::win::ScopedComPtr<ISensorCollection> sensor_collection; | |
394 hr = sensor_manager->GetSensorsByType( | |
395 sensor_type, sensor_collection.Receive()); | |
396 | |
397 if (FAILED(hr) || !sensor_collection.get()) | |
398 return false; | |
399 | |
400 ULONG count = 0; | |
401 hr = sensor_collection->GetCount(&count); | |
402 if (FAILED(hr) || !count) | |
403 return false; | |
404 | |
405 hr = sensor_collection->GetAt(0, sensor); | |
406 if (FAILED(hr) || !(*sensor)) | |
407 return false; | |
408 | |
409 base::win::ScopedComPtr<IPortableDeviceValues> device_values; | |
410 if (SUCCEEDED(device_values.CreateInstance(CLSID_PortableDeviceValues))) { | |
411 if (SUCCEEDED(device_values->SetUnsignedIntegerValue( | |
412 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, | |
413 GetInterval().InMilliseconds()))) { | |
414 base::win::ScopedComPtr<IPortableDeviceValues> return_values; | |
415 (*sensor)->SetProperties(device_values.get(), return_values.Receive()); | |
416 } | |
417 } | |
418 | |
419 base::win::ScopedComPtr<ISensorEvents> sensor_events; | |
420 hr = event_sink->QueryInterface( | |
421 __uuidof(ISensorEvents), sensor_events.ReceiveVoid()); | |
422 if (FAILED(hr) || !sensor_events.get()) | |
423 return false; | |
424 | |
425 hr = (*sensor)->SetEventSink(sensor_events.get()); | |
426 if (FAILED(hr)) | |
427 return false; | |
428 | |
429 return true; | |
430 } | |
431 | |
432 void DataFetcherSharedMemory::DisableSensors(ConsumerType consumer_type) { | |
433 switch(consumer_type) { | |
434 case CONSUMER_TYPE_ORIENTATION: | |
435 if (sensor_inclinometer_.get()) { | |
436 sensor_inclinometer_->SetEventSink(nullptr); | |
437 sensor_inclinometer_.Release(); | |
438 } | |
439 break; | |
440 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | |
441 if (sensor_inclinometer_absolute_.get()) { | |
442 sensor_inclinometer_absolute_->SetEventSink(nullptr); | |
443 sensor_inclinometer_absolute_.Release(); | |
444 } | |
445 break; | |
446 case CONSUMER_TYPE_MOTION: | |
447 if (sensor_accelerometer_.get()) { | |
448 sensor_accelerometer_->SetEventSink(nullptr); | |
449 sensor_accelerometer_.Release(); | |
450 } | |
451 if (sensor_gyrometer_.get()) { | |
452 sensor_gyrometer_->SetEventSink(nullptr); | |
453 sensor_gyrometer_.Release(); | |
454 } | |
455 break; | |
456 case CONSUMER_TYPE_LIGHT: | |
457 if (sensor_light_.get()) { | |
458 sensor_light_->SetEventSink(nullptr); | |
459 sensor_light_.Release(); | |
460 } | |
461 break; | |
462 default: | |
463 NOTREACHED(); | |
464 } | |
465 } | |
466 | |
467 void DataFetcherSharedMemory::SetBufferAvailableState( | |
468 ConsumerType consumer_type, bool enabled) { | |
469 switch(consumer_type) { | |
470 case CONSUMER_TYPE_ORIENTATION: | |
471 if (orientation_buffer_) { | |
472 orientation_buffer_->seqlock.WriteBegin(); | |
473 orientation_buffer_->data.allAvailableSensorsAreActive = enabled; | |
474 orientation_buffer_->seqlock.WriteEnd(); | |
475 } | |
476 break; | |
477 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | |
478 if (orientation_absolute_buffer_) { | |
479 orientation_absolute_buffer_->seqlock.WriteBegin(); | |
480 orientation_absolute_buffer_->data.allAvailableSensorsAreActive = | |
481 enabled; | |
482 orientation_absolute_buffer_->seqlock.WriteEnd(); | |
483 } | |
484 break; | |
485 case CONSUMER_TYPE_MOTION: | |
486 if (motion_buffer_) { | |
487 motion_buffer_->seqlock.WriteBegin(); | |
488 motion_buffer_->data.allAvailableSensorsAreActive = enabled; | |
489 motion_buffer_->seqlock.WriteEnd(); | |
490 } | |
491 break; | |
492 default: | |
493 NOTREACHED(); | |
494 } | |
495 } | |
496 | |
497 } // namespace content | |
OLD | NEW |