Index: Source/modules/battery/BatteryManager.cpp |
diff --git a/Source/modules/battery/BatteryManager.cpp b/Source/modules/battery/BatteryManager.cpp |
index aa473753d2be64beff8fe1e66d389d25c920a1fe..552fc8b9baea36f6d84c8826aa0176685e37e8d4 100644 |
--- a/Source/modules/battery/BatteryManager.cpp |
+++ b/Source/modules/battery/BatteryManager.cpp |
@@ -7,6 +7,7 @@ |
#include "RuntimeEnabledFeatures.h" |
#include "core/events/Event.h" |
+#include "modules/battery/BatteryDispatcher.h" |
#include "modules/battery/BatteryStatus.h" |
#include <limits> |
@@ -14,58 +15,108 @@ namespace WebCore { |
PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) |
{ |
- return adoptRefWillBeRefCountedGarbageCollected(new BatteryManager(context)); |
+ RefPtrWillBeRawPtr<BatteryManager> batteryManager(adoptRefWillBeRefCountedGarbageCollected(new BatteryManager(context))); |
+ batteryManager->suspendIfNeeded(); |
+ return batteryManager.release(); |
} |
BatteryManager::~BatteryManager() |
{ |
+ stopUpdating(); |
} |
BatteryManager::BatteryManager(ExecutionContext* context) |
- : ContextLifecycleObserver(context) |
- , m_batteryStatus(nullptr) |
+ : ActiveDOMObject(context) |
+ , DeviceSensorEventController(*toDocument(context)) |
{ |
+ // Need to figure out a way to startUpdating only afters EventListeners are added: didAddEventListener? |
timvolodine
2014/04/09 18:59:02
also, we need to think about this as it might be a
|
+ m_hasEventListener = true; |
+ startUpdating(); |
} |
bool BatteryManager::charging() |
{ |
- return m_batteryStatus ? m_batteryStatus->charging() : true; |
+ if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestData()) |
+ return lastData->charging(); |
+ |
+ return true; |
} |
double BatteryManager::chargingTime() |
{ |
- if (!m_batteryStatus) |
- return 0; |
- |
- if (!m_batteryStatus->charging()) |
- return std::numeric_limits<double>::infinity(); |
+ if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestData()) { |
+ // The spec requires that if level == 1.0, chargingTime == 0 but this has to |
+ // be implement by the backend. Adding this assert will help enforcing it. |
+ ASSERT(lastData->level() != 1.0 && lastData->chargingTime() == 0.0); |
timvolodine
2014/04/09 15:19:18
please remove this assert, as you mention in the c
Srini
2014/04/09 16:09:57
Ok.
|
- // The spec requires that if level == 1.0, chargingTime == 0 but this has to |
- // be implement by the backend. Adding this assert will help enforcing it. |
- ASSERT(level() != 1.0 && m_batteryStatus->chargingTime() == 0.0); |
+ return lastData->chargingTime(); |
+ } |
- return m_batteryStatus->chargingTime(); |
+ return 0; |
timvolodine
2014/04/09 15:19:18
should this be infinity as below?
Srini
2014/04/09 16:09:57
I think that default is 0 when the charging defaul
timvolodine
2014/04/09 18:59:02
I thought the default was +inf, but looking at the
|
} |
double BatteryManager::dischargingTime() |
{ |
- if (!m_batteryStatus || m_batteryStatus->charging()) |
- return std::numeric_limits<double>::infinity(); |
+ if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestData()) |
+ return lastData->dischargingTime(); |
- return m_batteryStatus->dischargingTime(); |
+ return std::numeric_limits<double>::infinity(); |
} |
double BatteryManager::level() |
{ |
- return m_batteryStatus ? m_batteryStatus->level() : 1; |
+ if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestData()) |
+ return lastData->level(); |
+ |
+ return 1; |
} |
-void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event, PassOwnPtr<BatteryStatus> batteryStatus) |
+void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event) |
{ |
ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
- m_batteryStatus = batteryStatus; |
dispatchEvent(event); |
} |
+void BatteryManager::registerWithDispatcher() |
+{ |
+ BatteryDispatcher::instance().addClient(this); |
+} |
+ |
+void BatteryManager::unregisterWithDispatcher() |
+{ |
+ BatteryDispatcher::instance().removeClient(this); |
+} |
+ |
+bool BatteryManager::hasLastData() |
+{ |
+ return false; |
timvolodine
2014/04/09 15:19:18
call to dispatcher.
I've uploaded https://coderevi
Srini
2014/04/09 16:09:57
Ill add this once your patch lands. Thanks for thi
|
+} |
+ |
+PassRefPtrWillBeRawPtr<Event> BatteryManager::getLastEvent() |
+{ |
+ // We would fire events directly. |
+ return nullptr; |
timvolodine
2014/04/09 15:19:18
call to dispatcher + return create event
Srini
2014/04/09 16:09:57
I think I need some help here. Now that I was tryi
timvolodine
2014/04/09 18:59:02
yeah, I see the problem. The DeviceSensorEventCont
|
+} |
+ |
+bool BatteryManager::isNullEvent(Event*) |
+{ |
+ return true; |
timvolodine
2014/04/09 15:19:18
return false;
I don't think we have the concept of
Srini
2014/04/09 16:09:57
Ok.
|
+} |
+ |
+void BatteryManager::suspend() |
+{ |
+ stopUpdating(); |
+} |
+ |
+void BatteryManager::resume() |
+{ |
+ startUpdating(); |
+} |
+ |
+void BatteryManager::stop() |
+{ |
+ stopUpdating(); |
+} |
+ |
} // namespace WebCore |