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/ScopedPersistent.h"
35 #include "bindings/v8/ScriptObject.h"
36 #include "bindings/v8/ScriptPromise.h"
37 #include "bindings/v8/ScriptState.h"
38 #include "bindings/v8/ScriptValue.h"
39 #include "wtf/RefPtr.h"
45 class ExecutionContext;
47 // ScriptPromiseResolver is a class for performing operations on Promise
48 // (resolve / reject) from C++ world.
49 // ScriptPromiseResolver holds a PromiseResolver.
50 // Here is a typical usage:
51 // 1. Create a ScriptPromiseResolver from a ScriptPromise.
52 // 2. Pass the promise object of the holder to a JavaScript program
53 // (such as XMLHttpRequest return value).
54 // 3. Call resolve or reject when the operation completes or
55 // the operation fails respectively.
57 // Most methods including constructors must be called within a v8 context.
58 // To use ScriptPromiseResolver out of a v8 context the caller must
59 // enter a v8 context, for example by using ScriptScope and ScriptState.
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.
65 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> {
66 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver);
68 static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise, ExecutionContext*);
69 static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise);
71 // A ScriptPromiseResolver should be resolved / rejected before
73 // A ScriptPromiseResolver can be destructed safely without
74 // entering a v8 context.
75 ~ScriptPromiseResolver();
77 // Return true if the promise object is in pending state.
78 bool isPending() const;
80 ScriptPromise promise()
82 ASSERT(v8::Context::InContext());
86 // Resolve with a C++ object which can be converted to a v8 object by toV8.
88 inline void resolve(PassRefPtr<T>);
89 // Reject with a C++ object which can be converted to a v8 object by toV8.
91 inline void reject(PassRefPtr<T>);
93 void resolve(ScriptValue);
94 void reject(ScriptValue);
97 ScriptPromiseResolver(ScriptPromise, v8::Isolate*);
98 void resolve(v8::Handle<v8::Value>);
99 void reject(v8::Handle<v8::Value>);
101 v8::Isolate* m_isolate;
102 ScriptPromise m_promise;
106 void ScriptPromiseResolver::resolve(PassRefPtr<T> value)
108 ASSERT(v8::Context::InContext());
109 resolve(toV8(value.get(), v8::Object::New(), m_isolate));
113 void ScriptPromiseResolver::reject(PassRefPtr<T> value)
115 ASSERT(v8::Context::InContext());
116 reject(toV8(value.get(), v8::Object::New(), m_isolate));
119 } // namespace WebCore
122 #endif // ScriptPromiseResolver_h