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

Side by Side Diff: third_party/WebKit/Source/modules/battery/BatteryDispatcher.cpp

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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/battery/BatteryDispatcher.h" 5 #include "modules/battery/BatteryDispatcher.h"
6 6
7 #include "modules/battery/BatteryStatus.h" 7 #include "wtf/PassOwnPtr.h"
8 #include "public/platform/Platform.h"
9 8
10 namespace blink { 9 namespace blink {
11 10
12 namespace {
13
14 double ensureTwoSignificantDigits(double level)
15 {
16 // Convert battery level value which should be in [0, 1] to a value in [0, 1 ]
17 // with 2 digits of precision. This is to provide a consistent experience
18 // across platforms (e.g. on Mac and Android the battery changes are general ly
19 // reported with 1% granularity). It also serves the purpose of reducing the
20 // possibility of fingerprinting and triggers less level change events on
21 // platforms where the granularity is high.
22 ASSERT(level >= 0 && level <= 1);
23 return round(level * 100) / 100.f;
24 }
25
26 } // namespace
27
28
29 BatteryDispatcher& BatteryDispatcher::instance() 11 BatteryDispatcher& BatteryDispatcher::instance()
30 { 12 {
31 DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new B atteryDispatcher())); 13 DEFINE_STATIC_LOCAL(Persistent<BatteryDispatcher>, batteryDispatcher, (new B atteryDispatcher()));
32 return *batteryDispatcher; 14 return *batteryDispatcher;
33 } 15 }
34 16
35 BatteryDispatcher::BatteryDispatcher() 17 BatteryDispatcher::BatteryDispatcher()
18 : m_hasLatestData(false)
36 { 19 {
37 } 20 }
38 21
39 BatteryDispatcher::~BatteryDispatcher() 22 BatteryDispatcher::~BatteryDispatcher()
40 { 23 {
41 } 24 }
42 25
43 DEFINE_TRACE(BatteryDispatcher) 26 void BatteryDispatcher::onUpdateBatteryStatus(const BatteryStatus& batteryStatus )
44 { 27 {
45 visitor->trace(m_batteryStatus); 28 m_batteryStatus = batteryStatus;
46 PlatformEventDispatcher::trace(visitor); 29 m_hasLatestData = true;
47 }
48
49 void BatteryDispatcher::updateBatteryStatus(const WebBatteryStatus& batteryStatu s)
50 {
51 m_batteryStatus = BatteryStatus::create(
52 batteryStatus.charging, batteryStatus.chargingTime, batteryStatus.discha rgingTime,
53 ensureTwoSignificantDigits(batteryStatus.level));
54 notifyControllers(); 30 notifyControllers();
55 } 31 }
56 32
57 BatteryStatus* BatteryDispatcher::latestData()
58 {
59 return m_batteryStatus.get();
60 }
61
62 void BatteryDispatcher::startListening() 33 void BatteryDispatcher::startListening()
63 { 34 {
64 Platform::current()->startListening(WebPlatformEventTypeBattery, this); 35 m_batteryStatusDispatcher = adoptPtr(new BatteryStatusDispatcher(this));
65 } 36 }
66 37
67 void BatteryDispatcher::stopListening() 38 void BatteryDispatcher::stopListening()
68 { 39 {
69 Platform::current()->stopListening(WebPlatformEventTypeBattery); 40 m_batteryStatusDispatcher.clear();
70 m_batteryStatus.clear(); 41 m_hasLatestData = false;
71 } 42 }
72 43
73 } // namespace blink 44 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698