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 "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 { |
| 25 stopUpdating(); | |
| 22 } | 26 } |
| 23 | 27 |
| 24 BatteryManager::BatteryManager(ExecutionContext* context) | 28 BatteryManager::BatteryManager(ExecutionContext* context) |
| 25 : ContextLifecycleObserver(context) | 29 : ActiveDOMObject(context) |
| 26 , m_batteryStatus(nullptr) | 30 , DeviceSensorEventController(*toDocument(context)) |
| 27 { | 31 { |
| 32 // Need to figure out a way to startUpdating only afters EventListeners are added: didAddEventListener? | |
|
timvolodine
2014/04/14 17:51:57
remove this line, we actually need to start updati
| |
| 33 m_hasEventListener = true; | |
| 34 startUpdating(); | |
| 28 } | 35 } |
| 29 | 36 |
| 30 bool BatteryManager::charging() | 37 bool BatteryManager::charging() |
| 31 { | 38 { |
| 32 return m_batteryStatus ? m_batteryStatus->charging() : true; | 39 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
| 40 return lastData->charging(); | |
| 41 | |
| 42 return true; | |
| 33 } | 43 } |
| 34 | 44 |
| 35 double BatteryManager::chargingTime() | 45 double BatteryManager::chargingTime() |
| 36 { | 46 { |
| 37 if (!m_batteryStatus) | 47 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
| 38 return 0; | 48 return lastData->chargingTime(); |
| 39 | 49 |
| 40 if (!m_batteryStatus->charging()) | 50 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 } | 51 } |
| 49 | 52 |
| 50 double BatteryManager::dischargingTime() | 53 double BatteryManager::dischargingTime() |
| 51 { | 54 { |
| 52 if (!m_batteryStatus || m_batteryStatus->charging()) | 55 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
| 53 return std::numeric_limits<double>::infinity(); | 56 return lastData->dischargingTime(); |
| 54 | 57 |
| 55 return m_batteryStatus->dischargingTime(); | 58 return std::numeric_limits<double>::infinity(); |
| 56 } | 59 } |
| 57 | 60 |
| 58 double BatteryManager::level() | 61 double BatteryManager::level() |
| 59 { | 62 { |
| 60 return m_batteryStatus ? m_batteryStatus->level() : 1; | 63 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
| 64 return lastData->level(); | |
| 65 | |
| 66 return 1; | |
| 61 } | 67 } |
| 62 | 68 |
| 63 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event, PassOwnPtr<BatteryStatus> batteryStatus) | 69 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event) |
| 64 { | 70 { |
| 65 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); | 71 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
| 66 | 72 |
| 67 m_batteryStatus = batteryStatus; | |
| 68 dispatchEvent(event); | 73 dispatchEvent(event); |
| 69 } | 74 } |
| 70 | 75 |
| 76 void BatteryManager::registerWithDispatcher() | |
| 77 { | |
| 78 BatteryDispatcher::instance().addClient(this); | |
| 79 } | |
| 80 | |
| 81 void BatteryManager::unregisterWithDispatcher() | |
| 82 { | |
| 83 BatteryDispatcher::instance().removeClient(this); | |
| 84 } | |
| 85 | |
| 86 bool BatteryManager::hasLastData() | |
| 87 { | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 PassRefPtrWillBeRawPtr<Event> BatteryManager::getLastEvent() | |
| 92 { | |
| 93 // We would fire events directly. | |
|
timvolodine
2014/04/14 17:51:57
could you add that this happens through BatteryMan
Srini
2014/04/14 19:53:11
Sure.
| |
| 94 return nullptr; | |
| 95 } | |
| 96 | |
| 97 bool BatteryManager::isNullEvent(Event*) | |
| 98 { | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 void BatteryManager::suspend() | |
| 103 { | |
|
timvolodine
2014/04/14 17:51:57
how does this work with page visibility?
should th
Srini
2014/04/14 19:53:11
Yes, we should have it false here and true on resu
| |
| 104 stopUpdating(); | |
| 105 } | |
| 106 | |
| 107 void BatteryManager::resume() | |
| 108 { | |
| 109 startUpdating(); | |
| 110 } | |
| 111 | |
| 112 void BatteryManager::stop() | |
| 113 { | |
|
timvolodine
2014/04/14 17:51:57
is it possible to resume() after stop()? do we nee
| |
| 114 stopUpdating(); | |
| 115 } | |
| 116 | |
| 71 } // namespace WebCore | 117 } // namespace WebCore |
| OLD | NEW |