- add third_party src.
[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/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"
40
41 #include <v8.h>
42
43 namespace WebCore {
44
45 class ExecutionContext;
46
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.
56 //
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.
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 //
65 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> {
66     WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver);
67 public:
68     static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise, ExecutionContext*);
69     static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise);
70
71     // A ScriptPromiseResolver should be resolved / rejected before
72     // its destruction.
73     // A ScriptPromiseResolver can be destructed safely without
74     // entering a v8 context.
75     ~ScriptPromiseResolver();
76
77     // Return true if the promise object is in pending state.
78     bool isPending() const;
79
80     ScriptPromise promise()
81     {
82         ASSERT(v8::Context::InContext());
83         return m_promise;
84     }
85
86     // Resolve with a C++ object which can be converted to a v8 object by toV8.
87     template<typename T>
88     inline void resolve(PassRefPtr<T>);
89     // Reject with a C++ object which can be converted to a v8 object by toV8.
90     template<typename T>
91     inline void reject(PassRefPtr<T>);
92
93     void resolve(ScriptValue);
94     void reject(ScriptValue);
95
96 private:
97     ScriptPromiseResolver(ScriptPromise, v8::Isolate*);
98     void resolve(v8::Handle<v8::Value>);
99     void reject(v8::Handle<v8::Value>);
100
101     v8::Isolate* m_isolate;
102     ScriptPromise m_promise;
103 };
104
105 template<typename T>
106 void ScriptPromiseResolver::resolve(PassRefPtr<T> value)
107 {
108     ASSERT(v8::Context::InContext());
109     resolve(toV8(value.get(), v8::Object::New(), m_isolate));
110 }
111
112 template<typename T>
113 void ScriptPromiseResolver::reject(PassRefPtr<T> value)
114 {
115     ASSERT(v8::Context::InContext());
116     reject(toV8(value.get(), v8::Object::New(), m_isolate));
117 }
118
119 } // namespace WebCore
120
121
122 #endif // ScriptPromiseResolver_h