Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Unified Diff: third_party/WebKit/Source/platform/battery/BatteryStatus.h

Issue 1538803002: Migrates battery_status from content/renderer/ to WebKit/platform/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/battery/BatteryStatus.h
diff --git a/third_party/WebKit/Source/platform/battery/BatteryStatus.h b/third_party/WebKit/Source/platform/battery/BatteryStatus.h
new file mode 100644
index 0000000000000000000000000000000000000000..05a8545e8532b6d34013d16a0c92f0d1f62a7c14
--- /dev/null
+++ b/third_party/WebKit/Source/platform/battery/BatteryStatus.h
@@ -0,0 +1,57 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BatteryStatus_h
+#define BatteryStatus_h
+
+#include "wtf/Assertions.h"
+
+#include <cmath>
+#include <limits>
+
+namespace blink {
+
+// Simple struct to hold the battery status. This class is copyable.
+class BatteryStatus final {
+public:
+ BatteryStatus()
+ : m_charging(true)
+ , m_chargingTime(0)
+ , m_dischargingTime(std::numeric_limits<double>::infinity())
+ , m_level(1) { }
+ BatteryStatus(bool charging, double chargingTime, double dischargingTime, double level)
+ : m_charging(charging)
+ , m_chargingTime(chargingTime)
+ , m_dischargingTime(dischargingTime)
+ , m_level(ensureTwoSignificantDigits(level)) { }
+ BatteryStatus(const BatteryStatus&) = default;
+ BatteryStatus& operator=(const BatteryStatus&) = default;
+
+ bool charging() const { return m_charging; }
+ double chargingTime() const { return m_chargingTime; }
+ double dischargingTime() const { return m_dischargingTime; }
+ double level() const { return m_level; }
+
+private:
+ double ensureTwoSignificantDigits(double level)
+ {
+ // Convert battery level value which should be in [0, 1] to a value in [0, 1]
+ // with 2 digits of precision. This is to provide a consistent experience
+ // across platforms (e.g. on Mac and Android the battery changes are generally
+ // reported with 1% granularity). It also serves the purpose of reducing the
+ // possibility of fingerprinting and triggers less level change events on
+ // platforms where the granularity is high.
+ ASSERT(level >= 0 && level <= 1);
+ return std::round(level * 100) / 100.f;
+ }
+
+ bool m_charging;
+ double m_chargingTime;
+ double m_dischargingTime;
+ double m_level;
+};
+
+} // namespace blink
+
+#endif // BatteryStatus_h

Powered by Google App Engine
This is Rietveld 408576698