Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / CallbackPromiseAdapter.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 CallbackPromiseAdapter_h
32 #define CallbackPromiseAdapter_h
33
34 #include "bindings/core/v8/ScriptPromiseResolver.h"
35 #include "public/platform/WebCallbacks.h"
36
37 namespace blink {
38
39 // This class provides an easy way to convert from a Script-exposed
40 // class (i.e. a class that has a toV8() overload) that uses Promises
41 // to a WebKit API class that uses WebCallbacks. You can define
42 // separate Success and Error classes, but this example just uses one
43 // object for both.
44 //
45 // To use:
46 //
47 // class MyClass ... {
48 //    typedef blink::WebMyClass WebType;
49 //    static PassRefPtr<MyClass> take(ScriptPromiseResolver* resolver,
50 //                                    blink::WebMyClass* webInstance) {
51 //        // convert/create as appropriate, but often it's just:
52 //        return MyClass::create(adoptPtr(webInstance));
53 //
54 //        // Since promise resolving is done as an async task, it's not
55 //        // guaranteed that the script context has seen the promise resolve
56 //        // immediately after calling onSuccess/onError. You can use the
57 //        // ScriptPromise from the resolver to schedule a task that executes
58 //        // after resolving:
59 //        ScriptState::Scope scope(resolver->scriptState());
60 //        resolver->promise().then(...);
61 //    }
62 //
63 //    // Called when aborting to resolve/reject a promise due to an empty
64 //    // execution context.
65 //    static void dispose(blink::WebMyClass* webInstance) {
66 //        // delete as appropriate, but often it's just:
67 //        delete webInstance;
68 //    }
69 //
70 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyClass, blink::WebMyClass>*:
71 //
72 //    // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callbacks)
73 //    webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(resolver, scriptExecutionContext));
74 //
75 // Note that this class does not manage its own lifetime. In this
76 // example that ownership of the WebCallbacks instance is being passed
77 // in and it is up to the callee to free the WebCallbacks instace.
78 template<typename S, typename T>
79 class CallbackPromiseAdapter FINAL : public blink::WebCallbacks<typename S::WebType, typename T::WebType> {
80 public:
81     CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolver> resolver)
82         : m_resolver(resolver)
83     {
84         ASSERT(m_resolver);
85     }
86     virtual ~CallbackPromiseAdapter() { }
87
88     virtual void onSuccess(typename S::WebType* result) OVERRIDE
89     {
90         if (!m_resolver->executionContext() || m_resolver->executionContext()->activeDOMObjectsAreStopped()) {
91             S::dispose(result);
92             return;
93         }
94         m_resolver->resolve(S::take(m_resolver.get(), result));
95     }
96
97     virtual void onError(typename T::WebType* error) OVERRIDE
98     {
99         if (!m_resolver->executionContext() || m_resolver->executionContext()->activeDOMObjectsAreStopped()) {
100             T::dispose(error);
101             return;
102         }
103         m_resolver->reject(T::take(m_resolver.get(), error));
104     }
105
106 private:
107     RefPtr<ScriptPromiseResolver> m_resolver;
108     WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter);
109 };
110
111 } // namespace blink
112
113 #endif