Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1909)

Unified Diff: Source/modules/battery/BatteryManager.cpp

Issue 329723005: Battery Status API: blink promise implementation and layout tests. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: revert to a single resolver at a time Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/modules/battery/BatteryManager.h ('k') | Source/modules/battery/NavigatorBattery.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/battery/BatteryManager.cpp
diff --git a/Source/modules/battery/BatteryManager.cpp b/Source/modules/battery/BatteryManager.cpp
index 4e6d5a94fa6e329c96e74f6ed5f775a3f46a775a..4eaa7539af1d4f40937a1e1d33484439a3bb6aaf 100644
--- a/Source/modules/battery/BatteryManager.cpp
+++ b/Source/modules/battery/BatteryManager.cpp
@@ -27,9 +27,28 @@ BatteryManager::BatteryManager(ExecutionContext* context)
: ActiveDOMObject(context)
, DeviceEventControllerBase(toDocument(context)->page())
, m_batteryStatus(BatteryStatus::create())
+ , m_state(NotStarted)
{
- m_hasEventListener = true;
- startUpdating();
+}
+
+ScriptPromise BatteryManager::startRequest(ScriptState* scriptState)
+{
+ if (m_state == Pending)
+ return m_resolver->promise();
+
+ m_resolver = ScriptPromiseResolverWithContext::create(scriptState);
+ ScriptPromise promise = m_resolver->promise();
+
+ if (m_state == Resolved) {
+ // FIXME: Consider returning the same promise in this case. See crbug.com/385025.
+ m_resolver->resolve(this);
+ } else if (m_state == NotStarted) {
+ m_state = Pending;
+ m_hasEventListener = true;
+ startUpdating();
+ }
+
+ return promise;
}
bool BatteryManager::charging()
@@ -55,6 +74,7 @@ double BatteryManager::level()
void BatteryManager::didUpdateData()
{
ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled());
+ ASSERT(m_state != NotStarted);
RefPtr<BatteryStatus> oldStatus = m_batteryStatus;
m_batteryStatus = BatteryDispatcher::instance().latestData();
@@ -62,6 +82,13 @@ void BatteryManager::didUpdateData()
// BatteryDispatcher also holds a reference to m_batteryStatus.
ASSERT(m_batteryStatus->refCount() > 1);
+ if (m_state == Pending) {
+ ASSERT(m_resolver);
+ m_state = Resolved;
+ m_resolver->resolve(this);
+ return;
+ }
+
Document* document = toDocument(executionContext());
if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAreStopped())
return;
« no previous file with comments | « Source/modules/battery/BatteryManager.h ('k') | Source/modules/battery/NavigatorBattery.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698