1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #ifndef ScriptPromiseResolver_h
32 #define ScriptPromiseResolver_h
33
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"
42
43 #include <v8.h>
44
45 namespace WebCore {
46
47 class ExecutionContext;
48
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.
58 //
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.
62 //
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.
66 //
67 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> {
68 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver);
69 public:
70 static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise, ExecutionContext*);
71 static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise);
72
73 // A ScriptPromiseResolver should be resolved / rejected before
74 // its destruction.
75 // A ScriptPromiseResolver can be destructed safely without
76 // entering a v8 context.
77 ~ScriptPromiseResolver();
78
79 // Return true if the promise object is in pending state.
80 bool isPending() const;
81
promise()82 ScriptPromise promise()
83 {
84 ASSERT(m_promise.isolate()->InContext());
85 return m_promise;
86 }
87
88 // To use following template methods, T must be a DOM class.
89
90 // This method will be implemented by the code generator.
91 template<typename T>
resolve(T * value,v8::Handle<v8::Object> creationContext)92 void resolve(T* value, v8::Handle<v8::Object> creationContext) { resolve(toV8NoInline(value, creationContext, m_isolate)); }
93 template<typename T>
reject(T * value,v8::Handle<v8::Object> creationContext)94 void reject(T* value, v8::Handle<v8::Object> creationContext) { reject(toV8NoInline(value, creationContext, m_isolate)); }
95
96 template<typename T>
resolve(PassRefPtr<T> value,v8::Handle<v8::Object> creationContext)97 void resolve(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { resolve(value.get(), creationContext); }
98 template<typename T>
reject(PassRefPtr<T> value,v8::Handle<v8::Object> creationContext)99 void reject(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { reject(value.get(), creationContext); }
100
101 template<typename T>
102 inline void resolve(T* value, ExecutionContext*);
103 template<typename T>
104 inline void reject(T* value, ExecutionContext*);
105
106 template<typename T>
resolve(PassRefPtr<T> value,ExecutionContext * context)107 void resolve(PassRefPtr<T> value, ExecutionContext* context) { resolve(value.get(), context); }
108 template<typename T>
reject(PassRefPtr<T> value,ExecutionContext * context)109 void reject(PassRefPtr<T> value, ExecutionContext* context) { reject(value.get(), context); }
110
111 template<typename T>
112 inline void resolve(T* value);
113 template<typename T>
114 inline void reject(T* value);
115
116 template<typename T>
resolve(PassRefPtr<T> value)117 void resolve(PassRefPtr<T> value) { resolve(value.get()); }
118 template<typename T>
reject(PassRefPtr<T> value)119 void reject(PassRefPtr<T> value) { reject(value.get()); }
120
121 void resolve(ScriptValue);
122 void reject(ScriptValue);
123
124 private:
125 ScriptPromiseResolver(ScriptPromise);
126 void resolve(v8::Handle<v8::Value>);
127 void reject(v8::Handle<v8::Value>);
128
129 v8::Isolate* m_isolate;
130 ScriptPromise m_promise;
131 };
132
133 template<typename T>
resolve(T * value,ExecutionContext * context)134 void ScriptPromiseResolver::resolve(T* value, ExecutionContext* context)
135 {
136 ASSERT(m_isolate->InContext());
137 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current());
138 resolve(value, v8Context->Global());
139 }
140
141 template<typename T>
reject(T * value,ExecutionContext * context)142 void ScriptPromiseResolver::reject(T* value, ExecutionContext* context)
143 {
144 ASSERT(m_isolate->InContext());
145 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current());
146 reject(value, v8Context->Global());
147 }
148
149 template<typename T>
resolve(T * value)150 void ScriptPromiseResolver::resolve(T* value)
151 {
152 ASSERT(m_isolate->InContext());
153 resolve(value, v8::Object::New(m_isolate));
154 }
155
156 template<typename T>
reject(T * value)157 void ScriptPromiseResolver::reject(T* value)
158 {
159 ASSERT(m_isolate->InContext());
160 reject(value, v8::Object::New(m_isolate));
161 }
162
163 } // namespace WebCore
164
165
166 #endif // ScriptPromiseResolver_h
167