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