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 #ifndef ScriptPromiseResolver_h | 5 #ifndef ScriptPromiseResolver_h |
6 #define ScriptPromiseResolver_h | 6 #define ScriptPromiseResolver_h |
7 | 7 |
8 #include "bindings/core/v8/ScopedPersistent.h" | 8 #include "bindings/core/v8/ScopedPersistent.h" |
9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
11 #include "bindings/core/v8/ToV8.h" | 11 #include "bindings/core/v8/ToV8.h" |
12 #include "core/dom/ActiveDOMObject.h" | 12 #include "core/dom/ActiveDOMObject.h" |
13 #include "core/dom/ExecutionContext.h" | 13 #include "core/dom/ExecutionContext.h" |
14 #include "platform/Timer.h" | 14 #include "platform/Timer.h" |
15 #include "wtf/RefCounted.h" | 15 #include "wtf/RefCounted.h" |
16 #include <v8.h> | 16 #include <v8.h> |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 // This class wraps v8::Promise::Resolver and provides the following | 20 // This class wraps v8::Promise::Resolver and provides the following |
21 // functionalities. | 21 // functionalities. |
22 // - A ScriptPromiseResolver retains a ScriptState. A caller | 22 // - A ScriptPromiseResolver retains a ScriptState. A caller |
23 // can call resolve or reject from outside of a V8 context. | 23 // can call resolve or reject from outside of a V8 context. |
24 // - This class is an ActiveDOMObject and keeps track of the associated | 24 // - This class is an ActiveDOMObject and keeps track of the associated |
25 // ExecutionContext state. When the ExecutionContext is suspended, | 25 // ExecutionContext state. When the ExecutionContext is suspended, |
26 // resolve or reject will be delayed. When it is stopped, resolve or reject | 26 // resolve or reject will be delayed. When it is stopped, resolve or reject |
27 // will be ignored. | 27 // will be ignored. |
28 class ScriptPromiseResolver : public ActiveDOMObject, public RefCounted<ScriptPr
omiseResolver> { | 28 class ScriptPromiseResolver : public RefCountedWillBeRefCountedGarbageCollected<
ScriptPromiseResolver>, public ActiveDOMObject { |
29 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 29 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
30 | |
31 public: | 30 public: |
32 static PassRefPtr<ScriptPromiseResolver> create(ScriptState* scriptState) | 31 static PassRefPtrWillBeRawPtr<ScriptPromiseResolver> create(ScriptState* scr
iptState) |
33 { | 32 { |
34 RefPtr<ScriptPromiseResolver> resolver = adoptRef(new ScriptPromiseResol
ver(scriptState)); | 33 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = adoptRefWillBeNoop(
new ScriptPromiseResolver(scriptState)); |
35 resolver->suspendIfNeeded(); | 34 resolver->suspendIfNeeded(); |
36 return resolver.release(); | 35 return resolver.release(); |
37 } | 36 } |
38 | 37 |
39 virtual ~ScriptPromiseResolver() | 38 virtual ~ScriptPromiseResolver() |
40 { | 39 { |
41 // This assertion fails if: | 40 // This assertion fails if: |
42 // - promise() is called at least once and | 41 // - promise() is called at least once and |
43 // - this resolver is destructed before it is resolved, rejected or | 42 // - this resolver is destructed before it is resolved, rejected or |
44 // the associated ExecutionContext is stopped. | 43 // the associated ExecutionContext is stopped. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 77 |
79 // ActiveDOMObject implementation. | 78 // ActiveDOMObject implementation. |
80 virtual void suspend() override; | 79 virtual void suspend() override; |
81 virtual void resume() override; | 80 virtual void resume() override; |
82 virtual void stop() override; | 81 virtual void stop() override; |
83 | 82 |
84 // Once this function is called this resolver stays alive while the | 83 // Once this function is called this resolver stays alive while the |
85 // promise is pending and the associated ExecutionContext isn't stopped. | 84 // promise is pending and the associated ExecutionContext isn't stopped. |
86 void keepAliveWhilePending(); | 85 void keepAliveWhilePending(); |
87 | 86 |
| 87 virtual void trace(Visitor*) { } |
| 88 |
88 protected: | 89 protected: |
89 // You need to call suspendIfNeeded after the construction because | 90 // You need to call suspendIfNeeded after the construction because |
90 // this is an ActiveDOMObject. | 91 // this is an ActiveDOMObject. |
91 explicit ScriptPromiseResolver(ScriptState*); | 92 explicit ScriptPromiseResolver(ScriptState*); |
92 | 93 |
93 private: | 94 private: |
94 typedef ScriptPromise::InternalResolver Resolver; | 95 typedef ScriptPromise::InternalResolver Resolver; |
95 enum ResolutionState { | 96 enum ResolutionState { |
96 Pending, | 97 Pending, |
97 Resolving, | 98 Resolving, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 ScopedPersistent<v8::Value> m_value; | 135 ScopedPersistent<v8::Value> m_value; |
135 #if ENABLE(ASSERT) | 136 #if ENABLE(ASSERT) |
136 // True if promise() is called. | 137 // True if promise() is called. |
137 bool m_isPromiseCalled; | 138 bool m_isPromiseCalled; |
138 #endif | 139 #endif |
139 }; | 140 }; |
140 | 141 |
141 } // namespace blink | 142 } // namespace blink |
142 | 143 |
143 #endif // ScriptPromiseResolver_h | 144 #endif // ScriptPromiseResolver_h |
OLD | NEW |