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