| 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 "modules/battery/BatteryStatus.h" | 7 #include "wtf/PassOwnPtr.h" |
| 8 #include "public/platform/Platform.h" | |
| 9 | 8 |
| 10 namespace blink { | 9 namespace blink { |
| 11 | 10 |
| 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 | |
| 29 BatteryDispatcher& BatteryDispatcher::instance() | 11 BatteryDispatcher& BatteryDispatcher::instance() |
| 30 { | 12 { |
| 31 DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new B
atteryDispatcher())); | 13 DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new B
atteryDispatcher())); |
| 32 return *batteryDispatcher; | 14 return *batteryDispatcher; |
| 33 } | 15 } |
| 34 | 16 |
| 35 BatteryDispatcher::BatteryDispatcher() | 17 BatteryDispatcher::BatteryDispatcher() |
| 18 : m_hasLatestData(false) |
| 19 , m_batteryDispatcherProxy(adoptPtr(new BatteryDispatcherProxy(this))) |
| 36 { | 20 { |
| 37 } | 21 } |
| 38 | 22 |
| 39 BatteryDispatcher::~BatteryDispatcher() | 23 BatteryDispatcher::~BatteryDispatcher() |
| 40 { | 24 { |
| 41 } | 25 } |
| 42 | 26 |
| 43 DEFINE_TRACE(BatteryDispatcher) | 27 void BatteryDispatcher::OnUpdateBatteryStatus(const BatteryStatus& batteryStatus
) |
| 44 { | 28 { |
| 45 visitor->trace(m_batteryStatus); | 29 m_batteryStatus = batteryStatus; |
| 46 PlatformEventDispatcher::trace(visitor); | 30 m_hasLatestData = true; |
| 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)); | |
| 54 notifyControllers(); | 31 notifyControllers(); |
| 55 } | 32 } |
| 56 | 33 |
| 57 BatteryStatus* BatteryDispatcher::latestData() | |
| 58 { | |
| 59 return m_batteryStatus.get(); | |
| 60 } | |
| 61 | |
| 62 void BatteryDispatcher::startListening() | 34 void BatteryDispatcher::startListening() |
| 63 { | 35 { |
| 64 Platform::current()->startListening(WebPlatformEventTypeBattery, this); | 36 m_batteryDispatcherProxy->StartListening(); |
| 65 } | 37 } |
| 66 | 38 |
| 67 void BatteryDispatcher::stopListening() | 39 void BatteryDispatcher::stopListening() |
| 68 { | 40 { |
| 69 Platform::current()->stopListening(WebPlatformEventTypeBattery); | 41 m_batteryDispatcherProxy->StopListening(); |
| 70 m_batteryStatus.clear(); | 42 m_hasLatestData = false; |
| 71 } | 43 } |
| 72 | 44 |
| 73 } // namespace blink | 45 } // namespace blink |
| OLD | NEW |