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 "modules/battery/BatteryManager.h" | 5 #include "modules/battery/BatteryManager.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/events/Event.h" | 8 #include "core/events/Event.h" |
9 #include "modules/battery/BatteryDispatcher.h" | 9 #include "modules/battery/BatteryDispatcher.h" |
10 #include "modules/battery/BatteryStatus.h" | |
11 | 10 |
12 namespace blink { | 11 namespace blink { |
13 | 12 |
14 BatteryManager* BatteryManager::create(ExecutionContext* context) | 13 BatteryManager* BatteryManager::create(ExecutionContext* context) |
15 { | 14 { |
16 BatteryManager* batteryManager = new BatteryManager(context); | 15 BatteryManager* batteryManager = new BatteryManager(context); |
17 batteryManager->suspendIfNeeded(); | 16 batteryManager->suspendIfNeeded(); |
18 return batteryManager; | 17 return batteryManager; |
19 } | 18 } |
20 | 19 |
21 BatteryManager::~BatteryManager() | 20 BatteryManager::~BatteryManager() |
22 { | 21 { |
23 #if !ENABLE(OILPAN) | 22 #if !ENABLE(OILPAN) |
24 stopUpdating(); | 23 stopUpdating(); |
25 #endif | 24 #endif |
26 } | 25 } |
27 | 26 |
28 BatteryManager::BatteryManager(ExecutionContext* context) | 27 BatteryManager::BatteryManager(ExecutionContext* context) |
29 : ActiveDOMObject(context) | 28 : ActiveDOMObject(context) |
30 , PlatformEventController(toDocument(context)->page()) | 29 , PlatformEventController(toDocument(context)->page()) |
31 , m_batteryStatus(BatteryStatus::create()) | |
32 { | 30 { |
33 } | 31 } |
34 | 32 |
35 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState) | 33 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState) |
36 { | 34 { |
37 if (!m_batteryProperty) { | 35 if (!m_batteryProperty) { |
38 m_batteryProperty = new BatteryProperty(scriptState->executionContext(),
this, BatteryProperty::Ready); | 36 m_batteryProperty = new BatteryProperty(scriptState->executionContext(),
this, BatteryProperty::Ready); |
39 | 37 |
40 // If the context is in a stopped state already, do not start updating. | 38 // If the context is in a stopped state already, do not start updating. |
41 if (!executionContext() || executionContext()->activeDOMObjectsAreStoppe
d()) { | 39 if (!executionContext() || executionContext()->activeDOMObjectsAreStoppe
d()) { |
42 m_batteryProperty->resolve(this); | 40 m_batteryProperty->resolve(this); |
43 } else { | 41 } else { |
44 m_hasEventListener = true; | 42 m_hasEventListener = true; |
45 startUpdating(); | 43 startUpdating(); |
46 } | 44 } |
47 } | 45 } |
48 | 46 |
49 return m_batteryProperty->promise(scriptState->world()); | 47 return m_batteryProperty->promise(scriptState->world()); |
50 } | 48 } |
51 | 49 |
52 bool BatteryManager::charging() | 50 bool BatteryManager::charging() |
53 { | 51 { |
54 return m_batteryStatus->charging(); | 52 return m_batteryStatus.charging(); |
55 } | 53 } |
56 | 54 |
57 double BatteryManager::chargingTime() | 55 double BatteryManager::chargingTime() |
58 { | 56 { |
59 return m_batteryStatus->chargingTime(); | 57 return m_batteryStatus.charging_time(); |
60 } | 58 } |
61 | 59 |
62 double BatteryManager::dischargingTime() | 60 double BatteryManager::dischargingTime() |
63 { | 61 { |
64 return m_batteryStatus->dischargingTime(); | 62 return m_batteryStatus.discharging_time(); |
65 } | 63 } |
66 | 64 |
67 double BatteryManager::level() | 65 double BatteryManager::level() |
68 { | 66 { |
69 return m_batteryStatus->level(); | 67 return m_batteryStatus.level(); |
70 } | 68 } |
71 | 69 |
72 void BatteryManager::didUpdateData() | 70 void BatteryManager::didUpdateData() |
73 { | 71 { |
74 ASSERT(m_batteryProperty); | 72 ASSERT(m_batteryProperty); |
75 | 73 |
76 BatteryStatus* oldStatus = m_batteryStatus; | 74 BatteryStatus oldStatus = m_batteryStatus; |
77 m_batteryStatus = BatteryDispatcher::instance().latestData(); | 75 m_batteryStatus = *BatteryDispatcher::instance().latestData(); |
78 | 76 |
79 if (m_batteryProperty->state() == ScriptPromisePropertyBase::Pending) { | 77 if (m_batteryProperty->state() == ScriptPromisePropertyBase::Pending) { |
80 m_batteryProperty->resolve(this); | 78 m_batteryProperty->resolve(this); |
81 return; | 79 return; |
82 } | 80 } |
83 | 81 |
84 Document* document = toDocument(executionContext()); | 82 Document* document = toDocument(executionContext()); |
85 ASSERT(document); | 83 ASSERT(document); |
86 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr
eStopped()) | 84 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr
eStopped()) |
87 return; | 85 return; |
88 | 86 |
89 ASSERT(oldStatus); | 87 if (m_batteryStatus.charging() != oldStatus.charging()) |
90 | |
91 if (m_batteryStatus->charging() != oldStatus->charging()) | |
92 dispatchEvent(Event::create(EventTypeNames::chargingchange)); | 88 dispatchEvent(Event::create(EventTypeNames::chargingchange)); |
93 if (m_batteryStatus->chargingTime() != oldStatus->chargingTime()) | 89 if (m_batteryStatus.charging_time() != oldStatus.charging_time()) |
94 dispatchEvent(Event::create(EventTypeNames::chargingtimechange)); | 90 dispatchEvent(Event::create(EventTypeNames::chargingtimechange)); |
95 if (m_batteryStatus->dischargingTime() != oldStatus->dischargingTime()) | 91 if (m_batteryStatus.discharging_time() != oldStatus.discharging_time()) |
96 dispatchEvent(Event::create(EventTypeNames::dischargingtimechange)); | 92 dispatchEvent(Event::create(EventTypeNames::dischargingtimechange)); |
97 if (m_batteryStatus->level() != oldStatus->level()) | 93 if (m_batteryStatus.level() != oldStatus.level()) |
98 dispatchEvent(Event::create(EventTypeNames::levelchange)); | 94 dispatchEvent(Event::create(EventTypeNames::levelchange)); |
99 } | 95 } |
100 | 96 |
101 void BatteryManager::registerWithDispatcher() | 97 void BatteryManager::registerWithDispatcher() |
102 { | 98 { |
103 BatteryDispatcher::instance().addController(this); | 99 BatteryDispatcher::instance().addController(this); |
104 } | 100 } |
105 | 101 |
106 void BatteryManager::unregisterWithDispatcher() | 102 void BatteryManager::unregisterWithDispatcher() |
107 { | 103 { |
(...skipping 27 matching lines...) Expand all Loading... |
135 bool BatteryManager::hasPendingActivity() const | 131 bool BatteryManager::hasPendingActivity() const |
136 { | 132 { |
137 // Prevent V8 from garbage collecting the wrapper object if there are | 133 // Prevent V8 from garbage collecting the wrapper object if there are |
138 // event listeners attached to it. | 134 // event listeners attached to it. |
139 return hasEventListeners(); | 135 return hasEventListeners(); |
140 } | 136 } |
141 | 137 |
142 DEFINE_TRACE(BatteryManager) | 138 DEFINE_TRACE(BatteryManager) |
143 { | 139 { |
144 visitor->trace(m_batteryProperty); | 140 visitor->trace(m_batteryProperty); |
145 visitor->trace(m_batteryStatus); | |
146 PlatformEventController::trace(visitor); | 141 PlatformEventController::trace(visitor); |
147 RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(v
isitor); | 142 RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(v
isitor); |
148 ActiveDOMObject::trace(visitor); | 143 ActiveDOMObject::trace(visitor); |
149 } | 144 } |
150 | 145 |
151 } // namespace blink | 146 } // namespace blink |
OLD | NEW |