Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptPromiseResolver.h
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
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>
92     void resolve(T* value, v8::Handle<v8::Object> creationContext) { resolve(toV8NoInline(value, creationContext, m_isolate)); }
93     template<typename T>
94     void reject(T* value, v8::Handle<v8::Object> creationContext) { reject(toV8NoInline(value, creationContext, m_isolate)); }
95
96     template<typename T>
97     void resolve(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { resolve(value.get(), creationContext); }
98     template<typename T>
99     void resolve(RawPtr<T> value, v8::Handle<v8::Object> creationContext) { resolve(value.get(), creationContext); }
100     template<typename T>
101     void reject(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { reject(value.get(), creationContext); }
102     template<typename T>
103     void reject(RawPtr<T> value, v8::Handle<v8::Object> creationContext) { reject(value.get(), creationContext); }
104
105     template<typename T>
106     inline void resolve(T* value, ExecutionContext*);
107     template<typename T>
108     inline void reject(T* value, ExecutionContext*);
109
110     template<typename T>
111     void resolve(PassRefPtr<T> value, ExecutionContext* context) { resolve(value.get(), context); }
112     template<typename T>
113     void resolve(RawPtr<T> value, ExecutionContext* context) { resolve(value.get(), context); }
114     template<typename T>
115     void reject(PassRefPtr<T> value, ExecutionContext* context) { reject(value.get(), context); }
116     template<typename T>
117     void reject(RawPtr<T> value, ExecutionContext* context) { reject(value.get(), context); }
118
119     template<typename T>
120     inline void resolve(T* value);
121     template<typename T>
122     inline void reject(T* value);
123
124     template<typename T>
125     void resolve(PassRefPtr<T> value) { resolve(value.get()); }
126     template<typename T>
127     void resolve(RawPtr<T> value) { resolve(value.get()); }
128     template<typename T>
129     void reject(PassRefPtr<T> value) { reject(value.get()); }
130     template<typename T>
131     void reject(RawPtr<T> value) { reject(value.get()); }
132
133     void resolve(ScriptValue);
134     void reject(ScriptValue);
135
136 private:
137     ScriptPromiseResolver(ScriptPromise);
138     void resolve(v8::Handle<v8::Value>);
139     void reject(v8::Handle<v8::Value>);
140
141     v8::Isolate* m_isolate;
142     ScriptPromise m_promise;
143 };
144
145 template<typename T>
146 void ScriptPromiseResolver::resolve(T* value, ExecutionContext* context)
147 {
148     ASSERT(m_isolate->InContext());
149     v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current(m_isolate));
150     resolve(value, v8Context->Global());
151 }
152
153 template<typename T>
154 void ScriptPromiseResolver::reject(T* value, ExecutionContext* context)
155 {
156     ASSERT(m_isolate->InContext());
157     v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current(m_isolate));
158     reject(value, v8Context->Global());
159 }
160
161 template<typename T>
162 void ScriptPromiseResolver::resolve(T* value)
163 {
164     ASSERT(m_isolate->InContext());
165     resolve(value, v8::Object::New(m_isolate));
166 }
167
168 template<typename T>
169 void ScriptPromiseResolver::reject(T* value)
170 {
171     ASSERT(m_isolate->InContext());
172     reject(value, v8::Object::New(m_isolate));
173 }
174
175 } // namespace WebCore
176
177
178 #endif // ScriptPromiseResolver_h