Chromium Code Reviews| 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 "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/events/Event.h" | 9 #include "core/events/Event.h" |
| 10 #include "modules/battery/BatteryDispatcher.h" | 10 #include "modules/battery/BatteryDispatcher.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 { | 24 { |
| 25 #if !ENABLE(OILPAN) | 25 #if !ENABLE(OILPAN) |
| 26 stopUpdating(); | 26 stopUpdating(); |
| 27 #endif | 27 #endif |
| 28 } | 28 } |
| 29 | 29 |
| 30 BatteryManager::BatteryManager(ExecutionContext* context) | 30 BatteryManager::BatteryManager(ExecutionContext* context) |
| 31 : ActiveDOMObject(context) | 31 : ActiveDOMObject(context) |
| 32 , PlatformEventController(toDocument(context)->page()) | 32 , PlatformEventController(toDocument(context)->page()) |
| 33 , m_batteryStatus(BatteryStatus::create()) | 33 , m_batteryStatus(BatteryStatus::create()) |
| 34 , m_state(NotStarted) | 34 , m_isResolved(false) |
| 35 { | 35 { |
| 36 } | 36 } |
| 37 | 37 |
| 38 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState) | 38 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState) |
| 39 { | 39 { |
| 40 if (m_state == Pending) | 40 if (!m_batteryProperty) { |
| 41 return m_resolver->promise(); | 41 m_batteryProperty = new BatteryProperty(scriptState->executionContext(), this, BatteryProperty::Ready); |
| 42 | 42 |
| 43 m_resolver = ScriptPromiseResolver::create(scriptState); | 43 // If the context is in a stopped state already, do not start updating. |
| 44 ScriptPromise promise = m_resolver->promise(); | 44 if (m_isResolved || !executionContext() || executionContext()->activeDOM ObjectsAreStopped()) { |
| 45 | 45 m_batteryProperty->resolve(this); |
| 46 // If the context is in a stopped state already, do not start updating. | 46 } else { |
| 47 if (m_state == Resolved || !executionContext() || executionContext()->active DOMObjectsAreStopped()) { | 47 m_hasEventListener = true; |
| 48 // FIXME: Consider returning the same promise in this case. See crbug.co m/385025. | 48 startUpdating(); |
| 49 m_state = Resolved; | 49 } |
| 50 m_resolver->resolve(this); | |
| 51 } else if (m_state == NotStarted) { | |
| 52 m_state = Pending; | |
| 53 m_hasEventListener = true; | |
| 54 startUpdating(); | |
| 55 } | 50 } |
| 56 | 51 |
| 57 return promise; | 52 return m_batteryProperty->promise(scriptState->world()); |
| 58 } | 53 } |
| 59 | 54 |
| 60 bool BatteryManager::charging() | 55 bool BatteryManager::charging() |
| 61 { | 56 { |
| 62 return m_batteryStatus->charging(); | 57 return m_batteryStatus->charging(); |
| 63 } | 58 } |
| 64 | 59 |
| 65 double BatteryManager::chargingTime() | 60 double BatteryManager::chargingTime() |
| 66 { | 61 { |
| 67 return m_batteryStatus->chargingTime(); | 62 return m_batteryStatus->chargingTime(); |
| 68 } | 63 } |
| 69 | 64 |
| 70 double BatteryManager::dischargingTime() | 65 double BatteryManager::dischargingTime() |
| 71 { | 66 { |
| 72 return m_batteryStatus->dischargingTime(); | 67 return m_batteryStatus->dischargingTime(); |
| 73 } | 68 } |
| 74 | 69 |
| 75 double BatteryManager::level() | 70 double BatteryManager::level() |
| 76 { | 71 { |
| 77 return m_batteryStatus->level(); | 72 return m_batteryStatus->level(); |
| 78 } | 73 } |
| 79 | 74 |
| 80 void BatteryManager::didUpdateData() | 75 void BatteryManager::didUpdateData() |
| 81 { | 76 { |
| 82 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); | 77 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
| 83 ASSERT(m_state != NotStarted); | 78 ASSERT(m_batteryProperty); |
| 84 | 79 |
| 85 BatteryStatus* oldStatus = m_batteryStatus; | 80 BatteryStatus* oldStatus = m_batteryStatus; |
| 86 m_batteryStatus = BatteryDispatcher::instance().latestData(); | 81 m_batteryStatus = BatteryDispatcher::instance().latestData(); |
| 87 | 82 |
| 88 if (m_state == Pending) { | 83 if (!m_isResolved) { |
|
mlamouri (slow - plz ping)
2015/03/07 14:54:42
Instead of m_isResolved, could you use m_batteryPr
timvolodine
2015/03/12 13:53:11
Done.
| |
| 89 ASSERT(m_resolver); | 84 m_isResolved = true; |
| 90 m_state = Resolved; | 85 m_batteryProperty->resolve(this); |
| 91 m_resolver->resolve(this); | |
| 92 return; | 86 return; |
| 93 } | 87 } |
| 94 | 88 |
| 95 Document* document = toDocument(executionContext()); | 89 Document* document = toDocument(executionContext()); |
| 96 ASSERT(document); | 90 ASSERT(document); |
| 97 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped()) | 91 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped()) |
| 98 return; | 92 return; |
| 99 | 93 |
| 100 ASSERT(oldStatus); | 94 ASSERT(oldStatus); |
| 101 | 95 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 132 | 126 |
| 133 void BatteryManager::resume() | 127 void BatteryManager::resume() |
| 134 { | 128 { |
| 135 m_hasEventListener = true; | 129 m_hasEventListener = true; |
| 136 startUpdating(); | 130 startUpdating(); |
| 137 } | 131 } |
| 138 | 132 |
| 139 void BatteryManager::stop() | 133 void BatteryManager::stop() |
| 140 { | 134 { |
| 141 m_hasEventListener = false; | 135 m_hasEventListener = false; |
| 142 m_state = NotStarted; | 136 m_isResolved = false; |
| 137 m_batteryProperty.clear(); | |
| 143 stopUpdating(); | 138 stopUpdating(); |
| 144 } | 139 } |
| 145 | 140 |
| 146 bool BatteryManager::hasPendingActivity() const | 141 bool BatteryManager::hasPendingActivity() const |
| 147 { | 142 { |
| 148 // Prevent V8 from garbage collecting the wrapper object if there are | 143 // Prevent V8 from garbage collecting the wrapper object if there are |
| 149 // event listeners attached to it. | 144 // event listeners attached to it. |
| 150 return m_state == Resolved && hasEventListeners(); | 145 return m_isResolved && hasEventListeners(); |
|
mlamouri (slow - plz ping)
2015/03/07 14:54:42
Why |m_isResolved|? instead of |m_batteryProperty|
timvolodine
2015/03/12 13:53:11
yes changed to hasEventListeners() seems sufficien
| |
| 151 } | 146 } |
| 152 | 147 |
| 153 DEFINE_TRACE(BatteryManager) | 148 DEFINE_TRACE(BatteryManager) |
| 154 { | 149 { |
| 155 visitor->trace(m_resolver); | 150 visitor->trace(m_batteryProperty); |
| 156 visitor->trace(m_batteryStatus); | 151 visitor->trace(m_batteryStatus); |
| 157 PlatformEventController::trace(visitor); | 152 PlatformEventController::trace(visitor); |
| 158 RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(v isitor); | 153 RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(v isitor); |
| 159 ActiveDOMObject::trace(visitor); | 154 ActiveDOMObject::trace(visitor); |
| 160 } | 155 } |
| 161 | 156 |
| 162 } // namespace blink | 157 } // namespace blink |
| OLD | NEW |