OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/device_light/DeviceLightDispatcher.h" | 5 #include "modules/device_light/DeviceLightDispatcher.h" |
6 | 6 |
7 #include <math.h> | |
8 | |
7 #include "modules/device_light/DeviceLightController.h" | 9 #include "modules/device_light/DeviceLightController.h" |
8 #include "public/platform/Platform.h" | 10 #include "public/platform/Platform.h" |
9 | 11 |
12 namespace { | |
13 double EnsureRoundedLuxValue(double lux) { | |
14 // Make sure to round the lux value to nearest integer, to | |
15 // avoid too precise values and hence reduce fingerprinting risk. | |
16 // The special case when the lux value is infinity (no data can be | |
17 // provided) is simply returned as is. | |
18 return isinf(lux) ? lux : std::round(lux); | |
palmer
2017/03/23 19:00:19
Apologies in advance for the drive-by. :)
A round
timvolodine
2017/04/20 01:34:30
Acknowledged.
| |
19 } | |
20 } // namespace | |
21 | |
10 namespace blink { | 22 namespace blink { |
11 | 23 |
12 DeviceLightDispatcher& DeviceLightDispatcher::instance() { | 24 DeviceLightDispatcher& DeviceLightDispatcher::instance() { |
13 DEFINE_STATIC_LOCAL(DeviceLightDispatcher, deviceLightDispatcher, | 25 DEFINE_STATIC_LOCAL(DeviceLightDispatcher, deviceLightDispatcher, |
14 (new DeviceLightDispatcher)); | 26 (new DeviceLightDispatcher)); |
15 return deviceLightDispatcher; | 27 return deviceLightDispatcher; |
16 } | 28 } |
17 | 29 |
18 DeviceLightDispatcher::DeviceLightDispatcher() : m_lastDeviceLightData(-1) {} | 30 DeviceLightDispatcher::DeviceLightDispatcher() : m_lastDeviceLightData(-1) {} |
19 | 31 |
20 DeviceLightDispatcher::~DeviceLightDispatcher() {} | 32 DeviceLightDispatcher::~DeviceLightDispatcher() {} |
21 | 33 |
22 DEFINE_TRACE(DeviceLightDispatcher) { | 34 DEFINE_TRACE(DeviceLightDispatcher) { |
23 PlatformEventDispatcher::trace(visitor); | 35 PlatformEventDispatcher::trace(visitor); |
24 } | 36 } |
25 | 37 |
26 void DeviceLightDispatcher::startListening() { | 38 void DeviceLightDispatcher::startListening() { |
27 Platform::current()->startListening(WebPlatformEventTypeDeviceLight, this); | 39 Platform::current()->startListening(WebPlatformEventTypeDeviceLight, this); |
28 } | 40 } |
29 | 41 |
30 void DeviceLightDispatcher::stopListening() { | 42 void DeviceLightDispatcher::stopListening() { |
31 Platform::current()->stopListening(WebPlatformEventTypeDeviceLight); | 43 Platform::current()->stopListening(WebPlatformEventTypeDeviceLight); |
32 m_lastDeviceLightData = -1; | 44 m_lastDeviceLightData = -1; |
33 } | 45 } |
34 | 46 |
35 void DeviceLightDispatcher::didChangeDeviceLight(double value) { | 47 void DeviceLightDispatcher::didChangeDeviceLight(double value) { |
36 m_lastDeviceLightData = value; | 48 double newValue = EnsureRoundedLuxValue(value); |
37 notifyControllers(); | 49 if (m_lastDeviceLightData != newValue) { |
50 m_lastDeviceLightData = newValue; | |
51 notifyControllers(); | |
52 } | |
38 } | 53 } |
39 | 54 |
40 double DeviceLightDispatcher::latestDeviceLightData() const { | 55 double DeviceLightDispatcher::latestDeviceLightData() const { |
41 return m_lastDeviceLightData; | 56 return m_lastDeviceLightData; |
42 } | 57 } |
43 | 58 |
44 } // namespace blink | 59 } // namespace blink |
OLD | NEW |