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 { |
22 } | 25 } |
23 | 26 |
24 BatteryManager::BatteryManager(ExecutionContext* context) | 27 BatteryManager::BatteryManager(ExecutionContext* context) |
25 : ContextLifecycleObserver(context) | 28 : ActiveDOMObject(context) |
26 , m_batteryStatus(nullptr) | 29 , DeviceSensorEventController(*toDocument(context)) |
27 { | 30 { |
| 31 m_hasEventListener = true; |
| 32 startUpdating(); |
28 } | 33 } |
29 | 34 |
30 bool BatteryManager::charging() | 35 bool BatteryManager::charging() |
31 { | 36 { |
32 return m_batteryStatus ? m_batteryStatus->charging() : true; | 37 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD
ata()) |
| 38 return lastData->charging(); |
| 39 |
| 40 return true; |
33 } | 41 } |
34 | 42 |
35 double BatteryManager::chargingTime() | 43 double BatteryManager::chargingTime() |
36 { | 44 { |
37 if (!m_batteryStatus) | 45 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD
ata()) |
38 return 0; | 46 return lastData->chargingTime(); |
39 | 47 |
40 if (!m_batteryStatus->charging()) | 48 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 } | 49 } |
49 | 50 |
50 double BatteryManager::dischargingTime() | 51 double BatteryManager::dischargingTime() |
51 { | 52 { |
52 if (!m_batteryStatus || m_batteryStatus->charging()) | 53 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD
ata()) |
53 return std::numeric_limits<double>::infinity(); | 54 return lastData->dischargingTime(); |
54 | 55 |
55 return m_batteryStatus->dischargingTime(); | 56 return std::numeric_limits<double>::infinity(); |
56 } | 57 } |
57 | 58 |
58 double BatteryManager::level() | 59 double BatteryManager::level() |
59 { | 60 { |
60 return m_batteryStatus ? m_batteryStatus->level() : 1; | 61 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD
ata()) |
| 62 return lastData->level(); |
| 63 |
| 64 return 1; |
61 } | 65 } |
62 | 66 |
63 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event,
PassOwnPtr<BatteryStatus> batteryStatus) | 67 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event) |
64 { | 68 { |
65 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); | 69 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
66 | 70 |
67 m_batteryStatus = batteryStatus; | |
68 dispatchEvent(event); | 71 dispatchEvent(event); |
69 } | 72 } |
70 | 73 |
| 74 void BatteryManager::registerWithDispatcher() |
| 75 { |
| 76 BatteryDispatcher::instance().addClient(this); |
| 77 } |
| 78 |
| 79 void BatteryManager::unregisterWithDispatcher() |
| 80 { |
| 81 BatteryDispatcher::instance().removeClient(this); |
| 82 } |
| 83 |
| 84 bool BatteryManager::hasLastData() |
| 85 { |
| 86 return false; |
| 87 } |
| 88 |
| 89 PassRefPtrWillBeRawPtr<Event> BatteryManager::getLastEvent() |
| 90 { |
| 91 // Events are dispached via BatteryManager::didChangeBatteryStatus() |
| 92 return nullptr; |
| 93 } |
| 94 |
| 95 bool BatteryManager::isNullEvent(Event*) |
| 96 { |
| 97 return false; |
| 98 } |
| 99 |
| 100 void BatteryManager::suspend() |
| 101 { |
| 102 m_hasEventListener = false; |
| 103 stopUpdating(); |
| 104 } |
| 105 |
| 106 void BatteryManager::resume() |
| 107 { |
| 108 m_hasEventListener = true; |
| 109 startUpdating(); |
| 110 } |
| 111 |
| 112 void BatteryManager::stop() |
| 113 { |
| 114 m_hasEventListener = false; |
| 115 stopUpdating(); |
| 116 } |
| 117 |
71 } // namespace WebCore | 118 } // namespace WebCore |
OLD | NEW |