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 CallbackPromiseAdapter_h
32 #define CallbackPromiseAdapter_h
34 #include "bindings/core/v8/ScriptPromiseResolver.h"
35 #include "public/platform/WebCallbacks.h"
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
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));
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(...);
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;
70 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyClass, blink::WebMyClass>*:
72 // // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callbacks)
73 // webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(resolver, scriptExecutionContext));
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> {
81 CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolver> resolver)
82 : m_resolver(resolver)
86 virtual ~CallbackPromiseAdapter() { }
88 virtual void onSuccess(typename S::WebType* result) OVERRIDE
90 if (!m_resolver->executionContext() || m_resolver->executionContext()->activeDOMObjectsAreStopped()) {
94 m_resolver->resolve(S::take(m_resolver.get(), result));
97 virtual void onError(typename T::WebType* error) OVERRIDE
99 if (!m_resolver->executionContext() || m_resolver->executionContext()->activeDOMObjectsAreStopped()) {
103 m_resolver->reject(T::take(m_resolver.get(), error));
107 RefPtr<ScriptPromiseResolver> m_resolver;
108 WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter);