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

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: 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 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 12 matching lines...) Expand all
23 { 23 {
24 #if !ENABLE(OILPAN) 24 #if !ENABLE(OILPAN)
25 stopUpdating(); 25 stopUpdating();
26 #endif 26 #endif
27 } 27 }
28 28
29 BatteryManager::BatteryManager(ExecutionContext* context) 29 BatteryManager::BatteryManager(ExecutionContext* context)
30 : ActiveDOMObject(context) 30 : ActiveDOMObject(context)
31 , PlatformEventController(toDocument(context)->page()) 31 , PlatformEventController(toDocument(context)->page())
32 , m_batteryStatus(BatteryStatus::create()) 32 , m_batteryStatus(BatteryStatus::create())
33 , m_state(NotStarted)
34 { 33 {
35 } 34 }
36 35
37 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState) 36 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState)
38 { 37 {
39 if (m_state == Pending) 38 if (!m_batteryProperty) {
40 return m_resolver->promise(); 39 m_batteryProperty = new BatteryProperty(scriptState->executionContext(), this, BatteryProperty::Ready);
41 40
42 m_resolver = ScriptPromiseResolver::create(scriptState); 41 // If the context is in a stopped state already, do not start updating.
43 ScriptPromise promise = m_resolver->promise(); 42 if (!executionContext() || executionContext()->activeDOMObjectsAreStoppe d()) {
44 43 m_batteryProperty->resolve(this);
45 // If the context is in a stopped state already, do not start updating. 44 } else {
46 if (m_state == Resolved || !executionContext() || executionContext()->active DOMObjectsAreStopped()) { 45 m_hasEventListener = true;
47 // FIXME: Consider returning the same promise in this case. See crbug.co m/385025. 46 startUpdating();
48 m_state = Resolved; 47 }
49 m_resolver->resolve(this);
50 } else if (m_state == NotStarted) {
51 m_state = Pending;
52 m_hasEventListener = true;
53 startUpdating();
54 } 48 }
55 49
56 return promise; 50 return m_batteryProperty->promise(scriptState->world());
57 } 51 }
58 52
59 bool BatteryManager::charging() 53 bool BatteryManager::charging()
60 { 54 {
61 return m_batteryStatus->charging(); 55 return m_batteryStatus->charging();
62 } 56 }
63 57
64 double BatteryManager::chargingTime() 58 double BatteryManager::chargingTime()
65 { 59 {
66 return m_batteryStatus->chargingTime(); 60 return m_batteryStatus->chargingTime();
67 } 61 }
68 62
69 double BatteryManager::dischargingTime() 63 double BatteryManager::dischargingTime()
70 { 64 {
71 return m_batteryStatus->dischargingTime(); 65 return m_batteryStatus->dischargingTime();
72 } 66 }
73 67
74 double BatteryManager::level() 68 double BatteryManager::level()
75 { 69 {
76 return m_batteryStatus->level(); 70 return m_batteryStatus->level();
77 } 71 }
78 72
79 void BatteryManager::didUpdateData() 73 void BatteryManager::didUpdateData()
80 { 74 {
81 ASSERT(m_state != NotStarted); 75 ASSERT(m_batteryProperty);
82 76
83 BatteryStatus* oldStatus = m_batteryStatus; 77 BatteryStatus* oldStatus = m_batteryStatus;
84 m_batteryStatus = BatteryDispatcher::instance().latestData(); 78 m_batteryStatus = BatteryDispatcher::instance().latestData();
85 79
86 if (m_state == Pending) { 80 if (m_batteryProperty->state() == ScriptPromisePropertyBase::Pending) {
87 ASSERT(m_resolver); 81 m_batteryProperty->resolve(this);
88 m_state = Resolved;
89 m_resolver->resolve(this);
90 return; 82 return;
91 } 83 }
92 84
93 Document* document = toDocument(executionContext()); 85 Document* document = toDocument(executionContext());
94 ASSERT(document); 86 ASSERT(document);
95 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped()) 87 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped())
96 return; 88 return;
97 89
98 ASSERT(oldStatus); 90 ASSERT(oldStatus);
99 91
(...skipping 30 matching lines...) Expand all
130 122
131 void BatteryManager::resume() 123 void BatteryManager::resume()
132 { 124 {
133 m_hasEventListener = true; 125 m_hasEventListener = true;
134 startUpdating(); 126 startUpdating();
135 } 127 }
136 128
137 void BatteryManager::stop() 129 void BatteryManager::stop()
138 { 130 {
139 m_hasEventListener = false; 131 m_hasEventListener = false;
140 m_state = NotStarted; 132 m_batteryProperty.clear();
141 stopUpdating(); 133 stopUpdating();
142 } 134 }
143 135
144 bool BatteryManager::hasPendingActivity() const 136 bool BatteryManager::hasPendingActivity() const
145 { 137 {
146 // Prevent V8 from garbage collecting the wrapper object if there are 138 // Prevent V8 from garbage collecting the wrapper object if there are
147 // event listeners attached to it. 139 // event listeners attached to it.
148 return m_state == Resolved && hasEventListeners(); 140 return hasEventListeners();
149 } 141 }
150 142
151 DEFINE_TRACE(BatteryManager) 143 DEFINE_TRACE(BatteryManager)
152 { 144 {
153 visitor->trace(m_resolver); 145 visitor->trace(m_batteryProperty);
154 visitor->trace(m_batteryStatus); 146 visitor->trace(m_batteryStatus);
155 PlatformEventController::trace(visitor); 147 PlatformEventController::trace(visitor);
156 RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(v isitor); 148 RefCountedGarbageCollectedEventTargetWithInlineData<BatteryManager>::trace(v isitor);
157 ActiveDOMObject::trace(visitor); 149 ActiveDOMObject::trace(visitor);
158 } 150 }
159 151
160 } // namespace blink 152 } // 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