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

Unified Diff: Source/modules/battery/BatteryDispatcher.cpp

Issue 1229143006: Enforce restricted precision of the Battery Status API level attribute (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: no-find-copies Created 5 years, 5 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
« no previous file with comments | « LayoutTests/battery-status/restricted-level-precision-expected.txt ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/battery/BatteryDispatcher.cpp
diff --git a/Source/modules/battery/BatteryDispatcher.cpp b/Source/modules/battery/BatteryDispatcher.cpp
index cbffcf430d4d5ef968548fddf316068490a0b9d6..7a71753a2ceb9280b7d7cdde69a49963ec79f032 100644
--- a/Source/modules/battery/BatteryDispatcher.cpp
+++ b/Source/modules/battery/BatteryDispatcher.cpp
@@ -10,6 +10,23 @@
namespace blink {
+namespace {
+
+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 round(level * 100) / 100.f;
+}
+
+} // namespace
+
+
BatteryDispatcher& BatteryDispatcher::instance()
{
DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new BatteryDispatcher()));
@@ -32,7 +49,9 @@ DEFINE_TRACE(BatteryDispatcher)
void BatteryDispatcher::updateBatteryStatus(const WebBatteryStatus& batteryStatus)
{
- m_batteryStatus = BatteryStatus::create(batteryStatus.charging, batteryStatus.chargingTime, batteryStatus.dischargingTime, batteryStatus.level);
+ m_batteryStatus = BatteryStatus::create(
+ batteryStatus.charging, batteryStatus.chargingTime, batteryStatus.dischargingTime,
+ ensureTwoSignificantDigits(batteryStatus.level));
notifyControllers();
}
« no previous file with comments | « LayoutTests/battery-status/restricted-level-precision-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698