2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef ScriptPromiseResolver_h
32 #define ScriptPromiseResolver_h
34 #include "bindings/v8/ScriptPromise.h"
35 #include "bindings/v8/ScriptState.h"
36 #include "bindings/v8/ScriptValue.h"
37 #include "bindings/v8/V8Binding.h"
38 #include "wtf/RefPtr.h"
44 class ExecutionContext;
46 // ScriptPromiseResolver is a class for performing operations on Promise
47 // (resolve / reject) from C++ world.
48 // ScriptPromiseResolver holds a PromiseResolver.
49 // Here is a typical usage:
50 // 1. Create a ScriptPromiseResolver.
51 // 2. Pass the associated promise object to a JavaScript program
52 // (such as XMLHttpRequest return value).
53 // 3. Call resolve or reject when the operation completes or
54 // the operation fails respectively.
56 // Most methods including constructors must be called within a v8 context.
57 // To use ScriptPromiseResolver out of a v8 context the caller must
58 // enter a v8 context. Please use ScriptPromiseResolverWithContext
61 // To prevent memory leaks, you should release the reference manually
62 // by calling resolve or reject.
63 // Destroying the object will also release the reference.
64 // Note that ScriptPromiseResolver::promise returns an empty value when the
65 // resolver is already resolved or rejected. If you want to resolve a resolver
66 // immediately and return the associated promise, you should get the promise
68 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> {
69 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver);
71 static PassRefPtr<ScriptPromiseResolver> create(ScriptState*);
73 // A ScriptPromiseResolver should be resolved / rejected before
75 // A ScriptPromiseResolver can be destructed safely without
76 // entering a v8 context.
77 ~ScriptPromiseResolver();
79 // Returns the underlying Promise.
80 // Note that the underlying Promise is cleared when |resolve| or |reject|
82 ScriptPromise promise();
85 void resolve(const T& value, ExecutionContext* executionContext)
87 ASSERT(m_scriptState->isolate()->InContext());
88 // You should use ScriptPromiseResolverWithContext when you want
89 // to resolve a Promise in a non-original V8 context.
90 return resolve(value);
93 void reject(const T& value, ExecutionContext* executionContext)
95 ASSERT(m_scriptState->isolate()->InContext());
96 // You should use ScriptPromiseResolverWithContext when you want
97 // to reject a Promise in a non-original V8 context.
101 void resolve(const T& value)
103 ASSERT(m_scriptState->isolate()->InContext());
104 resolve(toV8Value(value));
107 void reject(const T& value)
109 ASSERT(m_scriptState->isolate()->InContext());
110 reject(toV8Value(value));
112 template<typename T> void resolve(const T& value, v8::Handle<v8::Object> creationContext)
114 ASSERT(m_scriptState->isolate()->InContext());
115 resolve(toV8Value(value, creationContext));
117 template<typename T> void reject(const T& value, v8::Handle<v8::Object> creationContext)
119 ASSERT(m_scriptState->isolate()->InContext());
120 reject(toV8Value(value, creationContext));
123 v8::Isolate* isolate() const { return m_scriptState->isolate(); }
125 void resolve(v8::Handle<v8::Value>);
126 void reject(v8::Handle<v8::Value>);
128 // Used by ToV8Value<ScriptPromiseResolver, v8::Handle<v8::Object> >.
129 static v8::Handle<v8::Object> getCreationContext(v8::Handle<v8::Object> creationContext)
131 return creationContext;
133 // Used by ToV8Value<ScriptPromiseResolver, ScriptState*>.
134 static v8::Handle<v8::Object> getCreationContext(ScriptState* scriptState)
136 return scriptState->context()->Global();
141 v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object> creationContext)
143 return ToV8Value<ScriptPromiseResolver, v8::Handle<v8::Object> >::toV8Value(value, creationContext, m_scriptState->isolate());
147 v8::Handle<v8::Value> toV8Value(const T& value)
149 return ToV8Value<ScriptPromiseResolver, ScriptState*>::toV8Value(value, m_scriptState.get(), m_scriptState->isolate());
152 explicit ScriptPromiseResolver(ScriptState*);
154 // FIXME: Remove this once ScriptValue::scriptState() becomes available.
155 RefPtr<ScriptState> m_scriptState;
156 // Used when scriptPromiseOnV8Promise is disabled.
157 ScriptPromise m_promise;
158 // Used when scriptPromiseOnV8Promise is enabled.
159 ScriptValue m_resolver;
162 } // namespace WebCore
165 #endif // ScriptPromiseResolver_h