Upstream version 7.36.149.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/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"
39
40 #include <v8.h>
41
42 namespace WebCore {
43
44 class ExecutionContext;
45
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.
55 //
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
59 // in such cases.
60 //
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
67 // before resolving.
68 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> {
69     WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver);
70 public:
71     static PassRefPtr<ScriptPromiseResolver> create(ScriptState*);
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     // Returns the underlying Promise.
80     // Note that the underlying Promise is cleared when |resolve| or |reject|
81     // is called.
82     ScriptPromise promise();
83
84     template<typename T>
85     void resolve(const T& value, ExecutionContext* executionContext)
86     {
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);
91     }
92     template<typename T>
93     void reject(const T& value, ExecutionContext* executionContext)
94     {
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.
98         return reject(value);
99     }
100     template<typename T>
101     void resolve(const T& value)
102     {
103         ASSERT(m_scriptState->isolate()->InContext());
104         resolve(toV8Value(value));
105     }
106     template<typename T>
107     void reject(const T& value)
108     {
109         ASSERT(m_scriptState->isolate()->InContext());
110         reject(toV8Value(value));
111     }
112     template<typename T> void resolve(const T& value, v8::Handle<v8::Object> creationContext)
113     {
114         ASSERT(m_scriptState->isolate()->InContext());
115         resolve(toV8Value(value, creationContext));
116     }
117     template<typename T> void reject(const T& value, v8::Handle<v8::Object> creationContext)
118     {
119         ASSERT(m_scriptState->isolate()->InContext());
120         reject(toV8Value(value, creationContext));
121     }
122
123     v8::Isolate* isolate() const { return m_scriptState->isolate(); }
124
125     void resolve(v8::Handle<v8::Value>);
126     void reject(v8::Handle<v8::Value>);
127
128     // Used by ToV8Value<ScriptPromiseResolver, v8::Handle<v8::Object> >.
129     static v8::Handle<v8::Object> getCreationContext(v8::Handle<v8::Object> creationContext)
130     {
131         return creationContext;
132     }
133     // Used by ToV8Value<ScriptPromiseResolver, ScriptState*>.
134     static v8::Handle<v8::Object> getCreationContext(ScriptState* scriptState)
135     {
136         return scriptState->context()->Global();
137     }
138
139 private:
140     template<typename T>
141     v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object> creationContext)
142     {
143         return ToV8Value<ScriptPromiseResolver, v8::Handle<v8::Object> >::toV8Value(value, creationContext, m_scriptState->isolate());
144     }
145
146     template<typename T>
147     v8::Handle<v8::Value> toV8Value(const T& value)
148     {
149         return ToV8Value<ScriptPromiseResolver, ScriptState*>::toV8Value(value, m_scriptState.get(), m_scriptState->isolate());
150     }
151
152     explicit ScriptPromiseResolver(ScriptState*);
153
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;
160 };
161
162 } // namespace WebCore
163
164
165 #endif // ScriptPromiseResolver_h