OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 21 matching lines...) Expand all Loading... |
32 #define CallbackPromiseAdapter_h | 32 #define CallbackPromiseAdapter_h |
33 | 33 |
34 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | 34 #include "bindings/v8/ScriptPromiseResolverWithContext.h" |
35 #include "public/platform/WebCallbacks.h" | 35 #include "public/platform/WebCallbacks.h" |
36 | 36 |
37 namespace WebCore { | 37 namespace WebCore { |
38 | 38 |
39 // This class provides an easy way to convert from a Script-exposed | 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 | 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 | 41 // to a WebKit API class that uses WebCallbacks. You can define |
42 // seperate Success and Error classes, but this example just uses one | 42 // separate Success and Error classes, but this example just uses one |
43 // object for both. | 43 // object for both. |
44 // | 44 // |
45 // To use: | 45 // To use: |
46 // | 46 // |
47 // class MyClass ... { | 47 // class MyClass ... { |
48 // typedef blink::WebMyClass WebType; | 48 // typedef blink::WebMyClass WebType; |
49 // static PassRefPtr<MyClass> from(NewScriptState*, | 49 // static PassRefPtr<MyClass> from(ScriptPromiseResolverWithContext* resolver
, |
50 // blink::WebMyClass* webInstance) { | 50 // blink::WebMyClass* webInstance) { |
51 // // convert/create as appropriate, but often it's just: | 51 // // convert/create as appropriate, but often it's just: |
52 // return MyClass::create(adoptPtr(webInstance)); | 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 // NewScriptState::Scope scope(resolver->scriptState()); |
| 60 // resolver->promise().then(...); |
53 // } | 61 // } |
54 // | 62 // |
55 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyC
lass, blink::WebMyClass>*: | 63 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyC
lass, blink::WebMyClass>*: |
56 // | 64 // |
57 // // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callback
s) | 65 // // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callback
s) |
58 // webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(res
olver, scriptExecutionContext)); | 66 // webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(res
olver, scriptExecutionContext)); |
59 // | 67 // |
60 // Note that this class does not manage its own lifetime. In this | 68 // Note that this class does not manage its own lifetime. In this |
61 // example that ownership of the WebCallbacks instance is being passed | 69 // example that ownership of the WebCallbacks instance is being passed |
62 // in and it is up to the callee to free the WebCallbacks instace. | 70 // in and it is up to the callee to free the WebCallbacks instace. |
63 template<typename S, typename T> | 71 template<typename S, typename T> |
64 class CallbackPromiseAdapter FINAL : public blink::WebCallbacks<typename S::WebT
ype, typename T::WebType> { | 72 class CallbackPromiseAdapter FINAL : public blink::WebCallbacks<typename S::WebT
ype, typename T::WebType> { |
65 public: | 73 public: |
66 CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolverWithContext> resolver
) | 74 CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolverWithContext> resolver
) |
67 : m_resolver(resolver) | 75 : m_resolver(resolver) |
68 { | 76 { |
69 } | 77 } |
70 virtual ~CallbackPromiseAdapter() { } | 78 virtual ~CallbackPromiseAdapter() { } |
71 | 79 |
72 virtual void onSuccess(typename S::WebType* result) OVERRIDE | 80 virtual void onSuccess(typename S::WebType* result) OVERRIDE |
73 { | 81 { |
74 v8::HandleScope handleScope(m_resolver->scriptState()->isolate()); | 82 m_resolver->resolve(S::from(m_resolver.get(), result)); |
75 m_resolver->resolve(S::from(m_resolver->scriptState(), result)); | |
76 } | 83 } |
77 virtual void onError(typename T::WebType* error) OVERRIDE | 84 virtual void onError(typename T::WebType* error) OVERRIDE |
78 { | 85 { |
79 v8::HandleScope handleScope(m_resolver->scriptState()->isolate()); | 86 m_resolver->reject(T::from(m_resolver.get(), error)); |
80 m_resolver->reject(T::from(m_resolver->scriptState(), error)); | |
81 } | 87 } |
82 private: | 88 private: |
83 RefPtr<ScriptPromiseResolverWithContext> m_resolver; | 89 RefPtr<ScriptPromiseResolverWithContext> m_resolver; |
84 WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter); | 90 WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter); |
85 }; | 91 }; |
86 | 92 |
87 } // namespace WebCore | 93 } // namespace WebCore |
88 | 94 |
89 #endif | 95 #endif |
OLD | NEW |