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/DOMWrapperWorld.h"
35 #include "bindings/v8/ScopedPersistent.h"
36 #include "bindings/v8/ScriptObject.h"
37 #include "bindings/v8/ScriptPromise.h"
38 #include "bindings/v8/ScriptState.h"
39 #include "bindings/v8/ScriptValue.h"
40 #include "bindings/v8/V8Binding.h"
41 #include "wtf/RefPtr.h"
47 class ExecutionContext;
49 // ScriptPromiseResolver is a class for performing operations on Promise
50 // (resolve / reject) from C++ world.
51 // ScriptPromiseResolver holds a PromiseResolver.
52 // Here is a typical usage:
53 // 1. Create a ScriptPromiseResolver from a ScriptPromise.
54 // 2. Pass the promise object of the holder to a JavaScript program
55 // (such as XMLHttpRequest return value).
56 // 3. Call resolve or reject when the operation completes or
57 // the operation fails respectively.
59 // Most methods including constructors must be called within a v8 context.
60 // To use ScriptPromiseResolver out of a v8 context the caller must
61 // enter a v8 context, for example by using ScriptScope and ScriptState.
63 // To prevent memory leaks, you should release the reference manually
64 // by calling resolve or reject.
65 // Destroying the object will also release the reference.
67 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> {
68 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver);
70 static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise, ExecutionContext*);
71 static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise);
73 // A ScriptPromiseResolver should be resolved / rejected before
75 // A ScriptPromiseResolver can be destructed safely without
76 // entering a v8 context.
77 ~ScriptPromiseResolver();
79 // Return true if the promise object is in pending state.
80 bool isPending() const;
82 ScriptPromise promise()
84 ASSERT(m_promise.isolate()->InContext());
88 // To use following template methods, T must be a DOM class.
90 // This method will be implemented by the code generator.
92 void resolve(T* value, v8::Handle<v8::Object> creationContext) { resolve(toV8NoInline(value, creationContext, m_isolate)); }
94 void reject(T* value, v8::Handle<v8::Object> creationContext) { reject(toV8NoInline(value, creationContext, m_isolate)); }
97 void resolve(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { resolve(value.get(), creationContext); }
99 void reject(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { reject(value.get(), creationContext); }
102 inline void resolve(T* value, ExecutionContext*);
104 inline void reject(T* value, ExecutionContext*);
107 void resolve(PassRefPtr<T> value, ExecutionContext* context) { resolve(value.get(), context); }
109 void reject(PassRefPtr<T> value, ExecutionContext* context) { reject(value.get(), context); }
112 inline void resolve(T* value);
114 inline void reject(T* value);
117 void resolve(PassRefPtr<T> value) { resolve(value.get()); }
119 void reject(PassRefPtr<T> value) { reject(value.get()); }
121 void resolve(ScriptValue);
122 void reject(ScriptValue);
125 ScriptPromiseResolver(ScriptPromise);
126 void resolve(v8::Handle<v8::Value>);
127 void reject(v8::Handle<v8::Value>);
129 v8::Isolate* m_isolate;
130 ScriptPromise m_promise;
134 void ScriptPromiseResolver::resolve(T* value, ExecutionContext* context)
136 ASSERT(m_isolate->InContext());
137 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current());
138 resolve(value, v8Context->Global());
142 void ScriptPromiseResolver::reject(T* value, ExecutionContext* context)
144 ASSERT(m_isolate->InContext());
145 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current());
146 reject(value, v8Context->Global());
150 void ScriptPromiseResolver::resolve(T* value)
152 ASSERT(m_isolate->InContext());
153 resolve(value, v8::Object::New(m_isolate));
157 void ScriptPromiseResolver::reject(T* value)
159 ASSERT(m_isolate->InContext());
160 reject(value, v8::Object::New(m_isolate));
163 } // namespace WebCore
166 #endif // ScriptPromiseResolver_h