OLD | NEW |
---|---|
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 "config.h" | 5 #include "config.h" |
6 #include "modules/battery/BatteryManager.h" | 6 #include "modules/battery/BatteryManager.h" |
7 | 7 |
8 #include "RuntimeEnabledFeatures.h" | 8 #include "RuntimeEnabledFeatures.h" |
9 #include "core/events/Event.h" | 9 #include "core/events/Event.h" |
10 #include "modules/battery/BatteryDispatcher.h" | |
10 #include "modules/battery/BatteryStatus.h" | 11 #include "modules/battery/BatteryStatus.h" |
11 #include <limits> | 12 #include <limits> |
12 | 13 |
13 namespace WebCore { | 14 namespace WebCore { |
14 | 15 |
15 PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) | 16 PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) |
16 { | 17 { |
17 return adoptRefWillBeRefCountedGarbageCollected(new BatteryManager(context)) ; | 18 RefPtrWillBeRawPtr<BatteryManager> batteryManager(adoptRefWillBeRefCountedGa rbageCollected(new BatteryManager(context))); |
19 batteryManager->suspendIfNeeded(); | |
20 return batteryManager.release(); | |
18 } | 21 } |
19 | 22 |
20 BatteryManager::~BatteryManager() | 23 BatteryManager::~BatteryManager() |
21 { | 24 { |
25 stopUpdating(); | |
abarth-chromium
2014/04/17 23:02:15
Does a test fail if you remove this line?
| |
22 } | 26 } |
23 | 27 |
24 BatteryManager::BatteryManager(ExecutionContext* context) | 28 BatteryManager::BatteryManager(ExecutionContext* context) |
25 : ContextLifecycleObserver(context) | 29 : ActiveDOMObject(context) |
26 , m_batteryStatus(nullptr) | 30 , DeviceSensorEventController(*toDocument(context)) |
27 { | 31 { |
32 m_hasEventListener = true; | |
33 startUpdating(); | |
28 } | 34 } |
29 | 35 |
30 bool BatteryManager::charging() | 36 bool BatteryManager::charging() |
31 { | 37 { |
32 return m_batteryStatus ? m_batteryStatus->charging() : true; | 38 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
39 return lastData->charging(); | |
40 | |
41 return true; | |
33 } | 42 } |
34 | 43 |
35 double BatteryManager::chargingTime() | 44 double BatteryManager::chargingTime() |
36 { | 45 { |
37 if (!m_batteryStatus) | 46 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
38 return 0; | 47 return lastData->chargingTime(); |
39 | 48 |
40 if (!m_batteryStatus->charging()) | 49 return 0; |
41 return std::numeric_limits<double>::infinity(); | |
42 | |
43 // The spec requires that if level == 1.0, chargingTime == 0 but this has to | |
44 // be implement by the backend. Adding this assert will help enforcing it. | |
45 ASSERT(level() != 1.0 && m_batteryStatus->chargingTime() == 0.0); | |
46 | |
47 return m_batteryStatus->chargingTime(); | |
48 } | 50 } |
49 | 51 |
50 double BatteryManager::dischargingTime() | 52 double BatteryManager::dischargingTime() |
51 { | 53 { |
52 if (!m_batteryStatus || m_batteryStatus->charging()) | 54 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
53 return std::numeric_limits<double>::infinity(); | 55 return lastData->dischargingTime(); |
54 | 56 |
55 return m_batteryStatus->dischargingTime(); | 57 return std::numeric_limits<double>::infinity(); |
56 } | 58 } |
57 | 59 |
58 double BatteryManager::level() | 60 double BatteryManager::level() |
59 { | 61 { |
60 return m_batteryStatus ? m_batteryStatus->level() : 1; | 62 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
63 return lastData->level(); | |
64 | |
65 return 1; | |
61 } | 66 } |
62 | 67 |
63 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event, PassOwnPtr<BatteryStatus> batteryStatus) | 68 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event) |
64 { | 69 { |
65 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); | 70 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
66 | 71 |
67 m_batteryStatus = batteryStatus; | |
68 dispatchEvent(event); | 72 dispatchEvent(event); |
69 } | 73 } |
70 | 74 |
75 void BatteryManager::registerWithDispatcher() | |
76 { | |
77 BatteryDispatcher::instance().addClient(this); | |
78 } | |
79 | |
80 void BatteryManager::unregisterWithDispatcher() | |
81 { | |
82 BatteryDispatcher::instance().removeClient(this); | |
83 } | |
84 | |
85 bool BatteryManager::hasLastData() | |
86 { | |
87 return false; | |
88 } | |
89 | |
90 PassRefPtrWillBeRawPtr<Event> BatteryManager::getLastEvent() | |
91 { | |
92 // Events are dispached via BatteryManager::didChangeBatteryStatus() | |
93 return nullptr; | |
94 } | |
95 | |
96 bool BatteryManager::isNullEvent(Event*) | |
97 { | |
98 return false; | |
99 } | |
100 | |
101 void BatteryManager::suspend() | |
102 { | |
103 m_hasEventListener = false; | |
104 stopUpdating(); | |
105 } | |
106 | |
107 void BatteryManager::resume() | |
108 { | |
109 m_hasEventListener = true; | |
110 startUpdating(); | |
111 } | |
112 | |
113 void BatteryManager::stop() | |
114 { | |
115 m_hasEventListener = false; | |
116 stopUpdating(); | |
117 } | |
118 | |
71 } // namespace WebCore | 119 } // namespace WebCore |
OLD | NEW |