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, | |
25 double charging_time, | |
26 double discharging_time, | |
27 double level) | |
28 : charging_(charging), | |
29 charging_time_(charging_time), | |
30 discharging_time_(discharging_time), | |
31 level_(EnsureTwoSignificantDigits(level)) {} | |
32 BatteryStatus(const BatteryStatus&) = default; | |
33 BatteryStatus& operator=(const BatteryStatus&) = default; | |
34 | |
35 bool charging() const { return charging_; } | |
36 double charging_time() const { return charging_time_; } | |
37 double discharging_time() const { return discharging_time_; } | |
38 double level() const { return level_; } | |
39 | |
40 private: | |
41 double EnsureTwoSignificantDigits(double level) { | |
42 // Convert battery level value which should be in [0, 1] to a value in | |
43 // [0, 1] with 2 digits of precision. This is to provide a consistent | |
44 // experience across platforms (e.g. on Mac and Android the battery changes | |
45 // are generally reported with 1% granularity). It also serves the purpose | |
46 // of reducing the possibility of fingerprinting and triggers less level | |
47 // change events on platforms where the granularity is high. | |
48 ASSERT(level >= 0 && level <= 1); | |
49 return std::round(level * 100) / 100.f; | |
50 } | |
51 | |
52 bool charging_; | |
53 double charging_time_; | |
54 double discharging_time_; | |
55 double level_; | |
56 }; | |
57 | |
58 } // namespace blink | |
59 | |
60 #endif // BLINK_PLATFORM_BATTERY_BATTERY_STATUS_H_ | |
OLD | NEW |