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 "config.h" | 5 #include "config.h" |
6 #include "modules/battery/BatteryDispatcher.h" | 6 #include "modules/battery/BatteryDispatcher.h" |
7 | 7 |
8 #include "modules/battery/BatteryStatus.h" | 8 #include "modules/battery/BatteryStatus.h" |
9 #include "public/platform/Platform.h" | 9 #include "public/platform/Platform.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
| 13 namespace { |
| 14 |
| 15 double ensureTwoSignificantDigits(double level) |
| 16 { |
| 17 // Convert battery level value which should be in [0, 1] to a value in [0, 1
] |
| 18 // with 2 digits of precision. This is to provide a consistent experience |
| 19 // across platforms (e.g. on Mac and Android the battery changes are general
ly |
| 20 // reported with 1% granularity). It also serves the purpose of reducing the |
| 21 // possibility of fingerprinting and triggers less level change events on |
| 22 // platforms where the granularity is high. |
| 23 ASSERT(level >= 0 && level <= 1); |
| 24 return round(level * 100) / 100.f; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 |
13 BatteryDispatcher& BatteryDispatcher::instance() | 30 BatteryDispatcher& BatteryDispatcher::instance() |
14 { | 31 { |
15 DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new B
atteryDispatcher())); | 32 DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new B
atteryDispatcher())); |
16 return *batteryDispatcher; | 33 return *batteryDispatcher; |
17 } | 34 } |
18 | 35 |
19 BatteryDispatcher::BatteryDispatcher() | 36 BatteryDispatcher::BatteryDispatcher() |
20 { | 37 { |
21 } | 38 } |
22 | 39 |
23 BatteryDispatcher::~BatteryDispatcher() | 40 BatteryDispatcher::~BatteryDispatcher() |
24 { | 41 { |
25 } | 42 } |
26 | 43 |
27 DEFINE_TRACE(BatteryDispatcher) | 44 DEFINE_TRACE(BatteryDispatcher) |
28 { | 45 { |
29 visitor->trace(m_batteryStatus); | 46 visitor->trace(m_batteryStatus); |
30 PlatformEventDispatcher::trace(visitor); | 47 PlatformEventDispatcher::trace(visitor); |
31 } | 48 } |
32 | 49 |
33 void BatteryDispatcher::updateBatteryStatus(const WebBatteryStatus& batteryStatu
s) | 50 void BatteryDispatcher::updateBatteryStatus(const WebBatteryStatus& batteryStatu
s) |
34 { | 51 { |
35 m_batteryStatus = BatteryStatus::create(batteryStatus.charging, batteryStatu
s.chargingTime, batteryStatus.dischargingTime, batteryStatus.level); | 52 m_batteryStatus = BatteryStatus::create( |
| 53 batteryStatus.charging, batteryStatus.chargingTime, batteryStatus.discha
rgingTime, |
| 54 ensureTwoSignificantDigits(batteryStatus.level)); |
36 notifyControllers(); | 55 notifyControllers(); |
37 } | 56 } |
38 | 57 |
39 BatteryStatus* BatteryDispatcher::latestData() | 58 BatteryStatus* BatteryDispatcher::latestData() |
40 { | 59 { |
41 return m_batteryStatus.get(); | 60 return m_batteryStatus.get(); |
42 } | 61 } |
43 | 62 |
44 void BatteryDispatcher::startListening() | 63 void BatteryDispatcher::startListening() |
45 { | 64 { |
46 Platform::current()->startListening(WebPlatformEventBattery, this); | 65 Platform::current()->startListening(WebPlatformEventBattery, this); |
47 } | 66 } |
48 | 67 |
49 void BatteryDispatcher::stopListening() | 68 void BatteryDispatcher::stopListening() |
50 { | 69 { |
51 Platform::current()->stopListening(WebPlatformEventBattery); | 70 Platform::current()->stopListening(WebPlatformEventBattery); |
52 m_batteryStatus.clear(); | 71 m_batteryStatus.clear(); |
53 } | 72 } |
54 | 73 |
55 } // namespace blink | 74 } // namespace blink |
OLD | NEW |