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

Side by Side 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: fix comments Created 5 years, 9 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 unified diff | Download patch
« no previous file with comments | « Source/modules/battery/BatteryManager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "core/dom/Document.h" 8 #include "core/dom/Document.h"
9 #include "core/events/Event.h" 9 #include "core/events/Event.h"
10 #include "modules/battery/BatteryDispatcher.h" 10 #include "modules/battery/BatteryDispatcher.h"
(...skipping 13 matching lines...) Expand all
24 { 24 {
25 #if !ENABLE(OILPAN) 25 #if !ENABLE(OILPAN)
26 stopUpdating(); 26 stopUpdating();
27 #endif 27 #endif
28 } 28 }
29 29
30 BatteryManager::BatteryManager(ExecutionContext* context) 30 BatteryManager::BatteryManager(ExecutionContext* context)
31 : ActiveDOMObject(context) 31 : ActiveDOMObject(context)
32 , PlatformEventController(toDocument(context)->page()) 32 , PlatformEventController(toDocument(context)->page())
33 , m_batteryStatus(BatteryStatus::create()) 33 , m_batteryStatus(BatteryStatus::create())
34 , m_state(NotStarted)
35 { 34 {
36 } 35 }
37 36
38 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState) 37 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState)
39 { 38 {
40 if (m_state == Pending) 39 if (!m_batteryProperty) {
41 return m_resolver->promise(); 40 m_batteryProperty = new BatteryProperty(scriptState->executionContext(), this, BatteryProperty::Ready);
42 41
43 m_resolver = ScriptPromiseResolver::create(scriptState); 42 // If the context is in a stopped state already, do not start updating.
44 ScriptPromise promise = m_resolver->promise(); 43 if (m_batteryProperty->state() == ScriptPromisePropertyBase::Resolved || !executionContext() || executionContext()->activeDOMObjectsAreStopped()) {
mlamouri (slow - plz ping) 2015/03/13 16:20:00 |m_batteryProperty->state() == ScriptPromiseProper
timvolodine 2015/06/12 17:29:03 right, removed this test
45 44 m_batteryProperty->resolve(this);
46 // If the context is in a stopped state already, do not start updating. 45 } else {
47 if (m_state == Resolved || !executionContext() || executionContext()->active DOMObjectsAreStopped()) { 46 m_hasEventListener = true;
48 // FIXME: Consider returning the same promise in this case. See crbug.co m/385025. 47 startUpdating();
49 m_state = Resolved; 48 }
50 m_resolver->resolve(this);
51 } else if (m_state == NotStarted) {
52 m_state = Pending;
53 m_hasEventListener = true;
54 startUpdating();
55 } 49 }
56 50
57 return promise; 51 return m_batteryProperty->promise(scriptState->world());
58 } 52 }
59 53
60 bool BatteryManager::charging() 54 bool BatteryManager::charging()
61 { 55 {
62 return m_batteryStatus->charging(); 56 return m_batteryStatus->charging();
63 } 57 }
64 58
65 double BatteryManager::chargingTime() 59 double BatteryManager::chargingTime()
66 { 60 {
67 return m_batteryStatus->chargingTime(); 61 return m_batteryStatus->chargingTime();
68 } 62 }
69 63
70 double BatteryManager::dischargingTime() 64 double BatteryManager::dischargingTime()
71 { 65 {
72 return m_batteryStatus->dischargingTime(); 66 return m_batteryStatus->dischargingTime();
73 } 67 }
74 68
75 double BatteryManager::level() 69 double BatteryManager::level()
76 { 70 {
77 return m_batteryStatus->level(); 71 return m_batteryStatus->level();
78 } 72 }
79 73
80 void BatteryManager::didUpdateData() 74 void BatteryManager::didUpdateData()
81 { 75 {
82 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); 76 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled());
83 ASSERT(m_state != NotStarted); 77 ASSERT(m_batteryProperty);
84 78
85 BatteryStatus* oldStatus = m_batteryStatus; 79 BatteryStatus* oldStatus = m_batteryStatus;
86 m_batteryStatus = BatteryDispatcher::instance().latestData(); 80 m_batteryStatus = BatteryDispatcher::instance().latestData();
87 81
88 if (m_state == Pending) { 82 if (m_batteryProperty->state() == ScriptPromisePropertyBase::Pending) {
89 ASSERT(m_resolver); 83 m_batteryProperty->resolve(this);
90 m_state = Resolved;
91 m_resolver->resolve(this);
92 return; 84 return;
93 } 85 }
94 86
95 Document* document = toDocument(executionContext()); 87 Document* document = toDocument(executionContext());
96 ASSERT(document); 88 ASSERT(document);
97 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped()) 89 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped())
98 return; 90 return;
99 91
100 ASSERT(oldStatus); 92 ASSERT(oldStatus);
101 93
(...skipping 30 matching lines...) Expand all
132 124
133 void BatteryManager::resume() 125 void BatteryManager::resume()
134 { 126 {
135 m_hasEventListener = true; 127 m_hasEventListener = true;
136 startUpdating(); 128 startUpdating();
137 } 129 }
138 130
139 void BatteryManager::stop() 131 void BatteryManager::stop()
140 { 132 {
141 m_hasEventListener = false; 133 m_hasEventListener = false;
142 m_state = NotStarted; 134 m_batteryProperty.clear();
143 stopUpdating(); 135 stopUpdating();
144 } 136 }
145 137
146 bool BatteryManager::hasPendingActivity() const 138 bool BatteryManager::hasPendingActivity() const
147 { 139 {
148 // Prevent V8 from garbage collecting the wrapper object if there are 140 // Prevent V8 from garbage collecting the wrapper object if there are
149 // event listeners attached to it. 141 // event listeners attached to it.
150 return m_state == Resolved && hasEventListeners(); 142 return hasEventListeners();
151 } 143 }
152 144
153 DEFINE_TRACE(BatteryManager) 145 DEFINE_TRACE(BatteryManager)
154 { 146 {
155 visitor->trace(m_resolver); 147 visitor->trace(m_batteryProperty);
156 visitor->trace(m_batteryStatus); 148 visitor->trace(m_batteryStatus);
157 PlatformEventController::trace(visitor); 149 PlatformEventController::trace(visitor);
158 RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(v isitor); 150 RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(v isitor);
159 ActiveDOMObject::trace(visitor); 151 ActiveDOMObject::trace(visitor);
160 } 152 }
161 153
162 } // namespace blink 154 } // namespace blink
OLDNEW
« 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