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