Chromium Code Reviews| 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 // | 53 // |
| 54 // // Since promise resolving is done as an async task, it's not | 54 // // Since promise resolving is done as an async task, it's not |
| 55 // // guaranteed that the script context has seen the promise resolve | 55 // // guaranteed that the script context has seen the promise resolve |
| 56 // // immediately after calling onSuccess/onError. You can use the | 56 // // immediately after calling onSuccess/onError. You can use the |
| 57 // // ScriptPromise from the resolver to schedule a task that executes | 57 // // ScriptPromise from the resolver to schedule a task that executes |
| 58 // // after resolving: | 58 // // after resolving: |
| 59 // ScriptState::Scope scope(resolver->scriptState()); | 59 // ScriptState::Scope scope(resolver->scriptState()); |
| 60 // resolver->promise().then(...); | 60 // resolver->promise().then(...); |
| 61 // } | 61 // } |
| 62 // | 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 // | |
| 63 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyC lass, blink::WebMyClass>*: | 70 // Now when calling into a WebKit API that requires a WebCallbacks<blink::WebMyC lass, blink::WebMyClass>*: |
| 64 // | 71 // |
| 65 // // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callback s) | 72 // // call signature: callSomeMethod(WebCallbacks<MyClass, MyClass>* callback s) |
| 66 // webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(res olver, scriptExecutionContext)); | 73 // webObject->callSomeMethod(new CallbackPromiseAdapter<MyClass, MyClass>(res olver, scriptExecutionContext)); |
| 67 // | 74 // |
| 68 // Note that this class does not manage its own lifetime. In this | 75 // Note that this class does not manage its own lifetime. In this |
| 69 // example that ownership of the WebCallbacks instance is being passed | 76 // example that ownership of the WebCallbacks instance is being passed |
| 70 // in and it is up to the callee to free the WebCallbacks instace. | 77 // in and it is up to the callee to free the WebCallbacks instace. |
| 71 template<typename S, typename T> | 78 template<typename S, typename T> |
| 72 class CallbackPromiseAdapter FINAL : public blink::WebCallbacks<typename S::WebT ype, typename T::WebType> { | 79 class CallbackPromiseAdapter FINAL : public blink::WebCallbacks<typename S::WebT ype, typename T::WebType> { |
| 73 public: | 80 public: |
| 74 CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolver> resolver) | 81 CallbackPromiseAdapter(PassRefPtr<ScriptPromiseResolver> resolver) |
| 75 : m_resolver(resolver) | 82 : m_resolver(resolver) |
| 76 { | 83 { |
| 77 } | 84 } |
| 78 virtual ~CallbackPromiseAdapter() { } | 85 virtual ~CallbackPromiseAdapter() { } |
| 79 | 86 |
| 80 virtual void onSuccess(typename S::WebType* result) OVERRIDE | 87 virtual void onSuccess(typename S::WebType* result) OVERRIDE |
| 81 { | 88 { |
| 89 if (!m_resolver || !m_resolver->executionContext()) { | |
|
yhirano
2014/07/18 09:00:02
m_resolver cannot be null (you can add ASSERT(m_re
nhiroki
2014/07/18 10:28:56
Done.
| |
| 90 S::dispose(result); | |
| 91 return; | |
| 92 } | |
| 82 m_resolver->resolve(S::from(m_resolver.get(), result)); | 93 m_resolver->resolve(S::from(m_resolver.get(), result)); |
| 83 } | 94 } |
| 95 | |
| 84 virtual void onError(typename T::WebType* error) OVERRIDE | 96 virtual void onError(typename T::WebType* error) OVERRIDE |
| 85 { | 97 { |
| 98 if (!m_resolver || !m_resolver->executionContext()) { | |
|
yhirano
2014/07/18 09:00:02
ditto
nhiroki
2014/07/18 10:28:56
Done.
| |
| 99 T::dispose(error); | |
| 100 return; | |
| 101 } | |
| 86 m_resolver->reject(T::from(m_resolver.get(), error)); | 102 m_resolver->reject(T::from(m_resolver.get(), error)); |
| 87 } | 103 } |
| 104 | |
| 88 private: | 105 private: |
| 89 RefPtr<ScriptPromiseResolver> m_resolver; | 106 RefPtr<ScriptPromiseResolver> m_resolver; |
| 90 WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter); | 107 WTF_MAKE_NONCOPYABLE(CallbackPromiseAdapter); |
| 91 }; | 108 }; |
| 92 | 109 |
| 93 } // namespace WebCore | 110 } // namespace WebCore |
| 94 | 111 |
| 95 #endif | 112 #endif |
| OLD | NEW |