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