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

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

Issue 976373002: Return same promise consistently when using navigator.getBattery() (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebase, make compile, fix comment Created 5 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') | no next file » | 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 0c259e34bf141842bc7d48d900c164f0e6cbba2b..4c92f17a9701ce65c865b36a1b92f686c390a887 100644
--- a/Source/modules/battery/BatteryManager.cpp
+++ b/Source/modules/battery/BatteryManager.cpp
@@ -30,30 +30,24 @@ BatteryManager::BatteryManager(ExecutionContext* context)
: ActiveDOMObject(context)
, PlatformEventController(toDocument(context)->page())
, m_batteryStatus(BatteryStatus::create())
- , m_state(NotStarted)
{
}
ScriptPromise BatteryManager::startRequest(ScriptState* scriptState)
{
- if (m_state == Pending)
- return m_resolver->promise();
-
- m_resolver = ScriptPromiseResolver::create(scriptState);
- ScriptPromise promise = m_resolver->promise();
-
- // If the context is in a stopped state already, do not start updating.
- if (m_state == Resolved || !executionContext() || executionContext()->activeDOMObjectsAreStopped()) {
- // FIXME: Consider returning the same promise in this case. See crbug.com/385025.
- m_state = Resolved;
- m_resolver->resolve(this);
- } else if (m_state == NotStarted) {
- m_state = Pending;
- m_hasEventListener = true;
- startUpdating();
+ if (!m_batteryProperty) {
+ m_batteryProperty = new BatteryProperty(scriptState->executionContext(), this, BatteryProperty::Ready);
+
+ // If the context is in a stopped state already, do not start updating.
+ if (!executionContext() || executionContext()->activeDOMObjectsAreStopped()) {
+ m_batteryProperty->resolve(this);
+ } else {
+ m_hasEventListener = true;
+ startUpdating();
+ }
}
- return promise;
+ return m_batteryProperty->promise(scriptState->world());
}
bool BatteryManager::charging()
@@ -78,15 +72,13 @@ double BatteryManager::level()
void BatteryManager::didUpdateData()
{
- ASSERT(m_state != NotStarted);
+ ASSERT(m_batteryProperty);
BatteryStatus* oldStatus = m_batteryStatus;
m_batteryStatus = BatteryDispatcher::instance().latestData();
- if (m_state == Pending) {
- ASSERT(m_resolver);
- m_state = Resolved;
- m_resolver->resolve(this);
+ if (m_batteryProperty->state() == ScriptPromisePropertyBase::Pending) {
+ m_batteryProperty->resolve(this);
return;
}
@@ -137,7 +129,7 @@ void BatteryManager::resume()
void BatteryManager::stop()
{
m_hasEventListener = false;
- m_state = NotStarted;
+ m_batteryProperty.clear();
stopUpdating();
}
@@ -145,12 +137,12 @@ bool BatteryManager::hasPendingActivity() const
{
// Prevent V8 from garbage collecting the wrapper object if there are
// event listeners attached to it.
- return m_state == Resolved && hasEventListeners();
+ return hasEventListeners();
}
DEFINE_TRACE(BatteryManager)
{
- visitor->trace(m_resolver);
+ visitor->trace(m_batteryProperty);
visitor->trace(m_batteryStatus);
PlatformEventController::trace(visitor);
RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(visitor);
« no previous file with comments | « Source/modules/battery/BatteryManager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698