1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ScriptPromiseResolver_h
#define ScriptPromiseResolver_h
#include "bindings/v8/DOMWrapperWorld.h"
#include "bindings/v8/ScopedPersistent.h"
#include "bindings/v8/ScriptObject.h"
#include "bindings/v8/ScriptPromise.h"
#include "bindings/v8/ScriptState.h"
#include "bindings/v8/ScriptValue.h"
#include "bindings/v8/V8Binding.h"
#include "wtf/RefPtr.h"
#include <v8.h>
namespace WebCore {
class ExecutionContext;
// ScriptPromiseResolver is a class for performing operations on Promise
// (resolve / reject) from C++ world.
// ScriptPromiseResolver holds a PromiseResolver.
// Here is a typical usage:
// 1. Create a ScriptPromiseResolver from a ScriptPromise.
// 2. Pass the promise object of the holder to a JavaScript program
// (such as XMLHttpRequest return value).
// 3. Call resolve or reject when the operation completes or
// the operation fails respectively.
//
// Most methods including constructors must be called within a v8 context.
// To use ScriptPromiseResolver out of a v8 context the caller must
// enter a v8 context, for example by using ScriptScope and ScriptState.
//
// To prevent memory leaks, you should release the reference manually
// by calling resolve or reject.
// Destroying the object will also release the reference.
//
class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> {
WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver);
public:
static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise, ExecutionContext*);
static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise);
// A ScriptPromiseResolver should be resolved / rejected before
// its destruction.
// A ScriptPromiseResolver can be destructed safely without
// entering a v8 context.
~ScriptPromiseResolver();
// Return true if the promise object is in pending state.
bool isPending() const;
ScriptPromise promise()
{
ASSERT(m_promise.isolate()->InContext());
return m_promise;
}
// To use following template methods, T must be a DOM class.
// This method will be implemented by the code generator.
template<typename T>
void resolve(T* value, v8::Handle<v8::Object> creationContext) { resolve(toV8NoInline(value, creationContext, m_isolate)); }
template<typename T>
void reject(T* value, v8::Handle<v8::Object> creationContext) { reject(toV8NoInline(value, creationContext, m_isolate)); }
template<typename T>
void resolve(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { resolve(value.get(), creationContext); }
template<typename T>
void reject(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { reject(value.get(), creationContext); }
template<typename T>
inline void resolve(T* value, ExecutionContext*);
template<typename T>
inline void reject(T* value, ExecutionContext*);
template<typename T>
void resolve(PassRefPtr<T> value, ExecutionContext* context) { resolve(value.get(), context); }
template<typename T>
void reject(PassRefPtr<T> value, ExecutionContext* context) { reject(value.get(), context); }
template<typename T>
inline void resolve(T* value);
template<typename T>
inline void reject(T* value);
template<typename T>
void resolve(PassRefPtr<T> value) { resolve(value.get()); }
template<typename T>
void reject(PassRefPtr<T> value) { reject(value.get()); }
void resolve(ScriptValue);
void reject(ScriptValue);
private:
ScriptPromiseResolver(ScriptPromise);
void resolve(v8::Handle<v8::Value>);
void reject(v8::Handle<v8::Value>);
v8::Isolate* m_isolate;
ScriptPromise m_promise;
};
template<typename T>
void ScriptPromiseResolver::resolve(T* value, ExecutionContext* context)
{
ASSERT(m_isolate->InContext());
v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current());
resolve(value, v8Context->Global());
}
template<typename T>
void ScriptPromiseResolver::reject(T* value, ExecutionContext* context)
{
ASSERT(m_isolate->InContext());
v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current());
reject(value, v8Context->Global());
}
template<typename T>
void ScriptPromiseResolver::resolve(T* value)
{
ASSERT(m_isolate->InContext());
resolve(value, v8::Object::New(m_isolate));
}
template<typename T>
void ScriptPromiseResolver::reject(T* value)
{
ASSERT(m_isolate->InContext());
reject(value, v8::Object::New(m_isolate));
}
} // namespace WebCore
#endif // ScriptPromiseResolver_h
|