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 29 matching lines...) Expand all Loading... | |
40 // This class provides an easy way to convert from a Script-exposed | 40 // This class provides an easy way to convert from a Script-exposed |
41 // class (i.e. a class that has a toV8() overload) that uses Promises | 41 // class (i.e. a class that has a toV8() overload) that uses Promises |
42 // to a WebKit API class that uses WebCallbacks. You can define | 42 // to a WebKit API class that uses WebCallbacks. You can define |
43 // seperate Success and Error classes, but this example just uses one | 43 // seperate Success and Error classes, but this example just uses one |
44 // object for both. | 44 // object for both. |
45 // | 45 // |
46 // To use: | 46 // To use: |
47 // | 47 // |
48 // class MyClass ... { | 48 // class MyClass ... { |
49 // typedef blink::WebMyClass WebType; | 49 // typedef blink::WebMyClass WebType; |
50 // static PassRefPtr<MyClass> from(blink::WebMyClass* webInstance) { | 50 // static PassRefPtr<MyClass> from(NewScriptState*, |
51 // blink::WebMyClass* webInstance) { | |
51 // // convert/create as appropriate, but often it's just: | 52 // // convert/create as appropriate, but often it's just: |
52 // return MyClass::create(adoptPtr(webInstance)); | 53 // return MyClass::create(adoptPtr(webInstance)); |
53 // } | 54 // } |
54 // | 55 // |
55 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyC lass, blink::WebMyClass>*: | 56 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyC lass, blink::WebMyClass>*: |
56 // | 57 // |
57 // // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callback s) | 58 // // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callback s) |
58 // webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(res olver, scriptExecutionContext)); | 59 // webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(res olver, scriptExecutionContext)); |
59 // | 60 // |
60 // Note that this class does not manage its own lifetime. In this | 61 // Note that this class does not manage its own lifetime. In this |
61 // example that ownership of the WebCallbacks instance is being passed | 62 // example that ownership of the WebCallbacks instance is being passed |
62 // in and it is up to the callee to free the WebCallbacks instace. | 63 // in and it is up to the callee to free the WebCallbacks instace. |
63 template<typename S, typename T> | 64 template<typename S, typename T> |
64 class CallbackPromiseAdapter FINAL : public blink::WebCallbacks<typename S::WebT ype, typename T::WebType> { | 65 class CallbackPromiseAdapter FINAL : public blink::WebCallbacks<typename S::WebT ype, typename T::WebType> { |
65 public: | 66 public: |
66 CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolver> resolver, Execution Context* context) | 67 CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolver> resolver, Execution Context* context) |
67 : m_resolver(resolver) | 68 : m_resolver(resolver) |
68 , m_scriptState(NewScriptState::current(toIsolate(context))) | 69 , m_scriptState(NewScriptState::current(toIsolate(context))) |
69 { | 70 { |
70 } | 71 } |
71 virtual ~CallbackPromiseAdapter() { } | 72 virtual ~CallbackPromiseAdapter() { } |
72 | 73 |
73 virtual void onSuccess(typename S::WebType* result) OVERRIDE | 74 virtual void onSuccess(typename S::WebType* result) OVERRIDE |
74 { | 75 { |
75 NewScriptState::Scope scope(m_scriptState.get()); | 76 NewScriptState::Scope scope(m_scriptState.get()); |
76 m_resolver->resolve(S::from(result)); | 77 m_resolver->resolve(S::from(m_scriptState.get(), result)); |
77 } | 78 } |
78 virtual void onError(typename T::WebType* error) OVERRIDE | 79 virtual void onError(typename T::WebType* error) OVERRIDE |
79 { | 80 { |
80 NewScriptState::Scope scope(m_scriptState.get()); | 81 NewScriptState::Scope scope(m_scriptState.get()); |
81 m_resolver->reject(T::from(error)); | 82 m_resolver->reject(T::from(error)); |
michaeln1
2014/04/11 23:46:29
Since this is a generic template, should it also a
| |
82 } | 83 } |
83 private: | 84 private: |
84 RefPtr<ScriptPromiseResolver> m_resolver; | 85 RefPtr<ScriptPromiseResolver> m_resolver; |
85 RefPtr<NewScriptState> m_scriptState; | 86 RefPtr<NewScriptState> m_scriptState; |
86 WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter); | 87 WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter); |
87 }; | 88 }; |
88 | 89 |
89 } // namespace WebCore | 90 } // namespace WebCore |
90 | 91 |
91 #endif | 92 #endif |
OLD | NEW |