| OLD | NEW |
| (Empty) |
| 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 SKY_ENGINE_BINDINGS_CORE_V8_CALLBACKPROMISEADAPTER_H_ | |
| 32 #define SKY_ENGINE_BINDINGS_CORE_V8_CALLBACKPROMISEADAPTER_H_ | |
| 33 | |
| 34 #include "sky/engine/bindings/core/v8/ScriptPromiseResolver.h" | |
| 35 #include "sky/engine/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::WebMyC
lass, blink::WebMyClass>*: | |
| 71 // | |
| 72 // // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callback
s) | |
| 73 // webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(res
olver, 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::WebT
ype, typename T::WebType> { | |
| 80 public: | |
| 81 explicit 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()->a
ctiveDOMObjectsAreStopped()) { | |
| 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()->a
ctiveDOMObjectsAreStopped()) { | |
| 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 template<typename T> | |
| 112 class CallbackPromiseAdapter<void, T> final : public blink::WebCallbacks<void, t
ypename T::WebType> { | |
| 113 public: | |
| 114 explicit CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolver> resolver) | |
| 115 : m_resolver(resolver) | |
| 116 { | |
| 117 ASSERT(m_resolver); | |
| 118 } | |
| 119 virtual ~CallbackPromiseAdapter() { } | |
| 120 | |
| 121 virtual void onSuccess() override | |
| 122 { | |
| 123 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) { | |
| 124 return; | |
| 125 } | |
| 126 m_resolver->resolve(V8UndefinedType()); | |
| 127 } | |
| 128 | |
| 129 virtual void onError(typename T::WebType* error) override | |
| 130 { | |
| 131 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) { | |
| 132 T::dispose(error); | |
| 133 return; | |
| 134 } | |
| 135 m_resolver->reject(T::take(m_resolver.get(), error)); | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 RefPtr<ScriptPromiseResolver> m_resolver; | |
| 140 WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter); | |
| 141 }; | |
| 142 | |
| 143 } // namespace blink | |
| 144 | |
| 145 #endif // SKY_ENGINE_BINDINGS_CORE_V8_CALLBACKPROMISEADAPTER_H_ | |
| OLD | NEW |