OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BatteryStatus_h |
| 6 #define BatteryStatus_h |
| 7 |
| 8 #include "wtf/Assertions.h" |
| 9 |
| 10 #include <cmath> |
| 11 #include <limits> |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 // Simple struct to hold the battery status. This class is copyable. |
| 16 class BatteryStatus final { |
| 17 public: |
| 18 BatteryStatus() |
| 19 : m_charging(true) |
| 20 , m_chargingTime(0) |
| 21 , m_dischargingTime(std::numeric_limits<double>::infinity()) |
| 22 , m_level(1) { } |
| 23 BatteryStatus(bool charging, double chargingTime, double dischargingTime, do
uble level) |
| 24 : m_charging(charging) |
| 25 , m_chargingTime(chargingTime) |
| 26 , m_dischargingTime(dischargingTime) |
| 27 , m_level(ensureTwoSignificantDigits(level)) { } |
| 28 BatteryStatus(const BatteryStatus&) = default; |
| 29 BatteryStatus& operator=(const BatteryStatus&) = default; |
| 30 |
| 31 bool charging() const { return m_charging; } |
| 32 double chargingTime() const { return m_chargingTime; } |
| 33 double dischargingTime() const { return m_dischargingTime; } |
| 34 double level() const { return m_level; } |
| 35 |
| 36 private: |
| 37 double ensureTwoSignificantDigits(double level) |
| 38 { |
| 39 // Convert battery level value which should be in [0, 1] to a value in [
0, 1] |
| 40 // with 2 digits of precision. This is to provide a consistent experienc
e |
| 41 // across platforms (e.g. on Mac and Android the battery changes are gen
erally |
| 42 // reported with 1% granularity). It also serves the purpose of reducing
the |
| 43 // possibility of fingerprinting and triggers less level change events o
n |
| 44 // platforms where the granularity is high. |
| 45 ASSERT(level >= 0 && level <= 1); |
| 46 return std::round(level * 100) / 100.f; |
| 47 } |
| 48 |
| 49 bool m_charging; |
| 50 double m_chargingTime; |
| 51 double m_dischargingTime; |
| 52 double m_level; |
| 53 }; |
| 54 |
| 55 } // namespace blink |
| 56 |
| 57 #endif // BatteryStatus_h |
OLD | NEW |