OLD | NEW |
---|---|
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 "RuntimeEnabledFeatures.h" | 8 #include "RuntimeEnabledFeatures.h" |
9 #include "modules/battery/BatteryDispatcher.h" | 9 #include "modules/battery/BatteryDispatcher.h" |
10 #include "modules/battery/BatteryStatus.h" | 10 #include "modules/battery/BatteryStatus.h" |
11 | 11 |
12 namespace WebCore { | 12 namespace WebCore { |
13 | 13 |
14 PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) | 14 PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) |
15 { | 15 { |
16 RefPtrWillBeRawPtr<BatteryManager> batteryManager(adoptRefWillBeRefCountedGa rbageCollected(new BatteryManager(context))); | 16 RefPtrWillBeRawPtr<BatteryManager> batteryManager(adoptRefWillBeRefCountedGa rbageCollected(new BatteryManager(context))); |
17 batteryManager->suspendIfNeeded(); | 17 batteryManager->suspendIfNeeded(); |
18 return batteryManager.release(); | 18 return batteryManager.release(); |
19 } | 19 } |
20 | 20 |
21 class BatteryManager::ThenFunction FINAL : public ScriptFunction { | |
22 public: | |
23 static PassOwnPtr<ScriptFunction> create(PassRefPtr<BatteryManager> observer ) | |
24 { | |
25 ExecutionContext* executionContext = observer->executionContext(); | |
26 return adoptPtr(new ThenFunction(toIsolate(executionContext), observer)) ; | |
27 } | |
28 private: | |
29 ThenFunction(v8::Isolate* isolate, PassRefPtr<BatteryManager> observer) | |
30 : ScriptFunction(isolate) | |
31 , m_observer(observer) | |
32 { | |
33 } | |
34 | |
35 virtual ScriptValue call(ScriptValue value) OVERRIDE | |
36 { | |
37 m_observer->onPromiseResolved(); | |
38 return value; | |
39 } | |
40 | |
41 RefPtr<BatteryManager> m_observer; | |
42 }; | |
43 | |
21 BatteryManager::~BatteryManager() | 44 BatteryManager::~BatteryManager() |
22 { | 45 { |
23 stopUpdating(); | 46 stopUpdating(); |
24 } | 47 } |
25 | 48 |
26 BatteryManager::BatteryManager(ExecutionContext* context) | 49 BatteryManager::BatteryManager(ExecutionContext* context) |
27 : ActiveDOMObject(context) | 50 : ActiveDOMObject(context) |
28 , DeviceEventControllerBase(toDocument(context)->page()) | 51 , DeviceEventControllerBase(toDocument(context)->page()) |
29 , m_batteryStatus(BatteryStatus::create()) | 52 , m_batteryStatus(BatteryStatus::create()) |
53 , m_state(NotStarted) | |
30 { | 54 { |
55 } | |
56 | |
57 ScriptPromise BatteryManager::startRequest(ScriptState* scriptState) | |
58 { | |
59 if (m_state == Pending) | |
60 return m_resolver->promise(); | |
61 | |
62 m_resolver = ScriptPromiseResolverWithContext::create(scriptState); | |
63 ScriptPromise promise = m_resolver->promise(); | |
64 | |
65 if (m_state == Resolved) { | |
66 m_resolver->resolve(this); | |
67 return promise; | |
68 } | |
abarth-chromium
2014/06/11 19:44:34
Why not just return m_resolver->promise(); at this
timvolodine
2014/06/12 16:38:45
slightly refactored this code. Done.
| |
69 | |
70 m_state = Pending; | |
31 m_hasEventListener = true; | 71 m_hasEventListener = true; |
72 promise.then(ThenFunction::create(this)); | |
abarth-chromium
2014/06/11 19:44:34
Why do we need to create a ThenFunction instead of
timvolodine
2014/06/12 16:38:45
yeah on second thought the "ThenFunction" seems li
| |
32 startUpdating(); | 73 startUpdating(); |
74 return promise; | |
75 } | |
76 | |
77 void BatteryManager::onPromiseResolved() | |
78 { | |
79 ASSERT(m_state == Pending); | |
80 m_state = Resolved; | |
33 } | 81 } |
34 | 82 |
35 bool BatteryManager::charging() | 83 bool BatteryManager::charging() |
36 { | 84 { |
37 return m_batteryStatus->charging(); | 85 return m_batteryStatus->charging(); |
38 } | 86 } |
39 | 87 |
40 double BatteryManager::chargingTime() | 88 double BatteryManager::chargingTime() |
41 { | 89 { |
42 return m_batteryStatus->chargingTime(); | 90 return m_batteryStatus->chargingTime(); |
43 } | 91 } |
44 | 92 |
45 double BatteryManager::dischargingTime() | 93 double BatteryManager::dischargingTime() |
46 { | 94 { |
47 return m_batteryStatus->dischargingTime(); | 95 return m_batteryStatus->dischargingTime(); |
48 } | 96 } |
49 | 97 |
50 double BatteryManager::level() | 98 double BatteryManager::level() |
51 { | 99 { |
52 return m_batteryStatus->level(); | 100 return m_batteryStatus->level(); |
53 } | 101 } |
54 | 102 |
55 void BatteryManager::didUpdateData() | 103 void BatteryManager::didUpdateData() |
56 { | 104 { |
57 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); | 105 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
106 ASSERT(m_state != NotStarted); | |
58 | 107 |
59 RefPtr<BatteryStatus> oldStatus = m_batteryStatus; | 108 RefPtr<BatteryStatus> oldStatus = m_batteryStatus; |
60 m_batteryStatus = BatteryDispatcher::instance().latestData(); | 109 m_batteryStatus = BatteryDispatcher::instance().latestData(); |
61 | 110 |
62 // BatteryDispatcher also holds a reference to m_batteryStatus. | 111 // BatteryDispatcher also holds a reference to m_batteryStatus. |
63 ASSERT(m_batteryStatus->refCount() > 1); | 112 ASSERT(m_batteryStatus->refCount() > 1); |
64 | 113 |
114 if (m_state == Pending && m_resolver) { | |
115 ASSERT(m_resolver); | |
116 m_resolver->resolve(this); | |
117 return; | |
118 } | |
119 | |
65 Document* document = toDocument(executionContext()); | 120 Document* document = toDocument(executionContext()); |
66 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped()) | 121 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped()) |
67 return; | 122 return; |
68 | 123 |
69 ASSERT(oldStatus); | 124 ASSERT(oldStatus); |
70 | 125 |
71 if (m_batteryStatus->charging() != oldStatus->charging()) | 126 if (m_batteryStatus->charging() != oldStatus->charging()) |
72 dispatchEvent(Event::create(EventTypeNames::chargingchange)); | 127 dispatchEvent(Event::create(EventTypeNames::chargingchange)); |
73 if (m_batteryStatus->chargingTime() != oldStatus->chargingTime()) | 128 if (m_batteryStatus->chargingTime() != oldStatus->chargingTime()) |
74 dispatchEvent(Event::create(EventTypeNames::chargingtimechange)); | 129 dispatchEvent(Event::create(EventTypeNames::chargingtimechange)); |
(...skipping 30 matching lines...) Expand all Loading... | |
105 startUpdating(); | 160 startUpdating(); |
106 } | 161 } |
107 | 162 |
108 void BatteryManager::stop() | 163 void BatteryManager::stop() |
109 { | 164 { |
110 m_hasEventListener = false; | 165 m_hasEventListener = false; |
111 stopUpdating(); | 166 stopUpdating(); |
112 } | 167 } |
113 | 168 |
114 } // namespace WebCore | 169 } // namespace WebCore |
OLD | NEW |