| 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 <cmath> |
| 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 // TODO(timvolodine): consider reducing the lux value precision further. |
| 19 return std::isinf(lux) ? lux : std::round(lux); |
| 20 } |
| 21 } // namespace |
| 22 |
| 10 namespace blink { | 23 namespace blink { |
| 11 | 24 |
| 12 DeviceLightDispatcher& DeviceLightDispatcher::Instance() { | 25 DeviceLightDispatcher& DeviceLightDispatcher::Instance() { |
| 13 DEFINE_STATIC_LOCAL(DeviceLightDispatcher, device_light_dispatcher, | 26 DEFINE_STATIC_LOCAL(DeviceLightDispatcher, device_light_dispatcher, |
| 14 (new DeviceLightDispatcher)); | 27 (new DeviceLightDispatcher)); |
| 15 return device_light_dispatcher; | 28 return device_light_dispatcher; |
| 16 } | 29 } |
| 17 | 30 |
| 18 DeviceLightDispatcher::DeviceLightDispatcher() : last_device_light_data_(-1) {} | 31 DeviceLightDispatcher::DeviceLightDispatcher() : last_device_light_data_(-1) {} |
| 19 | 32 |
| 20 DeviceLightDispatcher::~DeviceLightDispatcher() {} | 33 DeviceLightDispatcher::~DeviceLightDispatcher() {} |
| 21 | 34 |
| 22 DEFINE_TRACE(DeviceLightDispatcher) { | 35 DEFINE_TRACE(DeviceLightDispatcher) { |
| 23 PlatformEventDispatcher::Trace(visitor); | 36 PlatformEventDispatcher::Trace(visitor); |
| 24 } | 37 } |
| 25 | 38 |
| 26 void DeviceLightDispatcher::StartListening() { | 39 void DeviceLightDispatcher::StartListening() { |
| 27 Platform::Current()->StartListening(kWebPlatformEventTypeDeviceLight, this); | 40 Platform::Current()->StartListening(kWebPlatformEventTypeDeviceLight, this); |
| 28 } | 41 } |
| 29 | 42 |
| 30 void DeviceLightDispatcher::StopListening() { | 43 void DeviceLightDispatcher::StopListening() { |
| 31 Platform::Current()->StopListening(kWebPlatformEventTypeDeviceLight); | 44 Platform::Current()->StopListening(kWebPlatformEventTypeDeviceLight); |
| 32 last_device_light_data_ = -1; | 45 last_device_light_data_ = -1; |
| 33 } | 46 } |
| 34 | 47 |
| 35 void DeviceLightDispatcher::DidChangeDeviceLight(double value) { | 48 void DeviceLightDispatcher::DidChangeDeviceLight(double value) { |
| 36 last_device_light_data_ = value; | 49 double newValue = EnsureRoundedLuxValue(value); |
| 37 NotifyControllers(); | 50 if (last_device_light_data_ != newValue) { |
| 51 last_device_light_data_ = newValue; |
| 52 NotifyControllers(); |
| 53 } |
| 38 } | 54 } |
| 39 | 55 |
| 40 double DeviceLightDispatcher::LatestDeviceLightData() const { | 56 double DeviceLightDispatcher::LatestDeviceLightData() const { |
| 41 return last_device_light_data_; | 57 return last_device_light_data_; |
| 42 } | 58 } |
| 43 | 59 |
| 44 } // namespace blink | 60 } // namespace blink |
| OLD | NEW |