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
3 // found in the LICENSE file.
5 #ifndef ScriptPromiseResolverWithContext_h
6 #define ScriptPromiseResolverWithContext_h
8 #include "bindings/v8/ScopedPersistent.h"
9 #include "bindings/v8/ScriptPromise.h"
10 #include "bindings/v8/ScriptPromiseResolver.h"
11 #include "bindings/v8/ScriptState.h"
12 #include "bindings/v8/V8Binding.h"
13 #include "core/dom/ActiveDOMObject.h"
14 #include "core/dom/ExecutionContext.h"
15 #include "platform/Timer.h"
16 #include "wtf/RefCounted.h"
17 #include "wtf/Vector.h"
22 // This class wraps ScriptPromiseResolver and provides the following
23 // functionalities in addition to ScriptPromiseResolver's.
24 // - A ScriptPromiseResolverWithContext retains a ScriptState. A caller
25 // can call resolve or reject from outside of a V8 context.
26 // - This class is an ActiveDOMObject and keeps track of the associated
27 // ExecutionContext state. When the ExecutionContext is suspended,
28 // resolve or reject will be delayed. When it is stopped, resolve or reject
30 class ScriptPromiseResolverWithContext : public ActiveDOMObject, public RefCounted<ScriptPromiseResolverWithContext> {
31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext);
34 static PassRefPtr<ScriptPromiseResolverWithContext> create(ScriptState* scriptState)
36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptPromiseResolverWithContext(scriptState));
37 resolver->suspendIfNeeded();
38 return resolver.release();
41 virtual ~ScriptPromiseResolverWithContext()
43 // This assertion fails if:
44 // - promise() is called at least once and
45 // - this resolver is destructed before it is resolved, rejected or
46 // the associated ExecutionContext is stopped.
47 ASSERT(m_state == ResolvedOrRejected || !m_isPromiseCalled);
50 // Anything that can be passed to toV8Value can be passed to this function.
54 resolveOrReject(value, Resolving);
57 // Anything that can be passed to toV8Value can be passed to this function.
61 resolveOrReject(value, Rejecting);
64 ScriptState* scriptState() { return m_scriptState.get(); }
66 // Note that an empty ScriptPromise will be returned after resolve or
68 ScriptPromise promise()
71 m_isPromiseCalled = true;
73 return m_resolver ? m_resolver->promise() : ScriptPromise();
76 ScriptState* scriptState() const { return m_scriptState.get(); }
78 // ActiveDOMObject implementation.
79 virtual void suspend() OVERRIDE;
80 virtual void resume() OVERRIDE;
81 virtual void stop() OVERRIDE;
83 // Once this function is called this resolver stays alive while the
84 // promise is pending and the associated ExecutionContext isn't stopped.
85 void keepAliveWhilePending();
88 // You need to call suspendIfNeeded after the construction because
89 // this is an ActiveDOMObject.
90 explicit ScriptPromiseResolverWithContext(ScriptState*);
93 enum ResolutionState {
101 KeepAliveWhilePending,
105 v8::Handle<v8::Value> toV8Value(const T& value)
107 return ToV8Value<ScriptPromiseResolverWithContext, v8::Handle<v8::Object> >::toV8Value(value, m_scriptState->context()->Global(), m_scriptState->isolate());
110 template <typename T>
111 void resolveOrReject(T value, ResolutionState newState)
113 if (m_state != Pending || !executionContext() || executionContext()->activeDOMObjectsAreStopped())
115 ASSERT(newState == Resolving || newState == Rejecting);
117 // Retain this object until it is actually resolved or rejected.
118 // |deref| will be called in |clear|.
121 ScriptState::Scope scope(m_scriptState.get());
122 m_value.set(m_scriptState->isolate(), toV8Value(value));
123 if (!executionContext()->activeDOMObjectsAreSuspended())
124 resolveOrRejectImmediately();
127 void resolveOrRejectImmediately();
128 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*);
131 ResolutionState m_state;
132 const RefPtr<ScriptState> m_scriptState;
134 Timer<ScriptPromiseResolverWithContext> m_timer;
135 RefPtr<ScriptPromiseResolver> m_resolver;
136 ScopedPersistent<v8::Value> m_value;
138 // True if promise() is called.
139 bool m_isPromiseCalled;
143 } // namespace WebCore
145 #endif // #ifndef ScriptPromiseResolverWithContext_h