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 3 // found in the LICENSE file. 4 5 #include "config.h" 6 #include "modules/battery/BatteryManager.h" 7 8 #include "core/dom/Document.h" 9 #include "core/events/Event.h" 10 #include "modules/battery/BatteryDispatcher.h" 11 #include "modules/battery/BatteryStatus.h" 12 #include "platform/RuntimeEnabledFeatures.h" 13 14 namespace blink { 15 16 BatteryManager* BatteryManager::create(ExecutionContext* context) 17 { 18 BatteryManager* batteryManager = new BatteryManager(context); 19 batteryManager->suspendIfNeeded(); 20 return batteryManager; 21 } 22 23 BatteryManager::~BatteryManager() 24 { 25 stopUpdating(); 26 } 27 28 BatteryManager::BatteryManager(ExecutionContext* context) 29 : ActiveDOMObject(context) 30 , PlatformEventController(toDocument(context)->page()) 31 , m_batteryStatus(BatteryStatus::create()) 32 , m_state(NotStarted) 33 { 34 } 35 36 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState) 37 { 38 if (m_state == Pending) 39 return m_resolver->promise(); 40 41 m_resolver = ScriptPromiseResolver::create(scriptState); 42 ScriptPromise promise = m_resolver->promise(); 43 44 if (m_state == Resolved) { 45 // FIXME: Consider returning the same promise in this case. See crbug.com/385025. 46 m_resolver->resolve(this); 47 } else if (m_state == NotStarted) { 48 m_state = Pending; 49 m_hasEventListener = true; 50 startUpdating(); 51 } 52 53 return promise; 54 } 55 56 bool BatteryManager::charging() 57 { 58 return m_batteryStatus->charging(); 59 } 60 61 double BatteryManager::chargingTime() 62 { 63 return m_batteryStatus->chargingTime(); 64 } 65 66 double BatteryManager::dischargingTime() 67 { 68 return m_batteryStatus->dischargingTime(); 69 } 70 71 double BatteryManager::level() 72 { 73 return m_batteryStatus->level(); 74 } 75 76 void BatteryManager::didUpdateData() 77 { 78 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); 79 ASSERT(m_state != NotStarted); 80 81 BatteryStatus* oldStatus = m_batteryStatus; 82 m_batteryStatus = BatteryDispatcher::instance().latestData(); 83 84 if (m_state == Pending) { 85 ASSERT(m_resolver); 86 m_state = Resolved; 87 m_resolver->resolve(this); 88 return; 89 } 90 91 Document* document = toDocument(executionContext()); 92 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAreStopped()) 93 return; 94 95 ASSERT(oldStatus); 96 97 if (m_batteryStatus->charging() != oldStatus->charging()) 98 dispatchEvent(Event::create(EventTypeNames::chargingchange)); 99 if (m_batteryStatus->chargingTime() != oldStatus->chargingTime()) 100 dispatchEvent(Event::create(EventTypeNames::chargingtimechange)); 101 if (m_batteryStatus->dischargingTime() != oldStatus->dischargingTime()) 102 dispatchEvent(Event::create(EventTypeNames::dischargingtimechange)); 103 if (m_batteryStatus->level() != oldStatus->level()) 104 dispatchEvent(Event::create(EventTypeNames::levelchange)); 105 } 106 107 void BatteryManager::registerWithDispatcher() 108 { 109 BatteryDispatcher::instance().addController(this); 110 } 111 112 void BatteryManager::unregisterWithDispatcher() 113 { 114 BatteryDispatcher::instance().removeController(this); 115 } 116 117 bool BatteryManager::hasLastData() 118 { 119 return BatteryDispatcher::instance().latestData(); 120 } 121 122 void BatteryManager::suspend() 123 { 124 m_hasEventListener = false; 125 stopUpdating(); 126 } 127 128 void BatteryManager::resume() 129 { 130 m_hasEventListener = true; 131 startUpdating(); 132 } 133 134 void BatteryManager::stop() 135 { 136 m_hasEventListener = false; 137 m_state = NotStarted; 138 stopUpdating(); 139 } 140 141 bool BatteryManager::hasPendingActivity() const 142 { 143 // Prevent V8 from garbage collecting the wrapper object if there are 144 // event listeners attached to it. 145 return m_state == Resolved && hasEventListeners(); 146 } 147 148 void BatteryManager::trace(Visitor* visitor) 149 { 150 visitor->trace(m_batteryStatus); 151 EventTargetWithInlineData::trace(visitor); 152 } 153 154 } // namespace blink 155