Home | History | Annotate | Download | only in v8
      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/v8/ScriptPromiseResolverWithContext.h"
     35 #include "public/platform/WebCallbacks.h"
     36 
     37 namespace WebCore {
     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> from(ScriptPromiseResolverWithContext* 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 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyClass, blink::WebMyClass>*:
     64 //
     65 //    // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callbacks)
     66 //    webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(resolver, scriptExecutionContext));
     67 //
     68 // Note that this class does not manage its own lifetime. In this
     69 // example that ownership of the WebCallbacks instance is being passed
     70 // in and it is up to the callee to free the WebCallbacks instace.
     71 template<typename S, typename T>
     72 class CallbackPromiseAdapter FINAL : public blink::WebCallbacks<typename S::WebType, typename T::WebType> {
     73 public:
     74     CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolverWithContext> resolver)
     75         : m_resolver(resolver)
     76     {
     77     }
     78     virtual ~CallbackPromiseAdapter() { }
     79 
     80     virtual void onSuccess(typename S::WebType* result) OVERRIDE
     81     {
     82         m_resolver->resolve(S::from(m_resolver.get(), result));
     83     }
     84     virtual void onError(typename T::WebType* error) OVERRIDE
     85     {
     86         m_resolver->reject(T::from(m_resolver.get(), error));
     87     }
     88 private:
     89     RefPtr<ScriptPromiseResolverWithContext> m_resolver;
     90     WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter);
     91 };
     92 
     93 } // namespace WebCore
     94 
     95 #endif
     96