OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MojoPromiseAdapter_h |
| 6 #define MojoPromiseAdapter_h |
| 7 |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 |
| 10 namespace blink { |
| 11 |
| 12 class ExecutionContext; |
| 13 |
| 14 // This class wraps a ScriptPromiseResolver. |
| 15 // |
| 16 // When the callback() method is called ownership of this object is passed to |
| 17 // a new mojo::Callback. Subclasses must implement a Run() method matching the |
| 18 // callback type and override the destructor to either resolve or reject with |
| 19 // an appropriate value to handle closure of the Mojo pipe. |
| 20 template <typename Callback> |
| 21 class MojoPromiseAdapter : public Callback::Runnable { |
| 22 WTF_MAKE_NONCOPYABLE(MojoPromiseAdapter); |
| 23 |
| 24 public: |
| 25 MojoPromiseAdapter(ScriptPromiseResolver* resolver) : m_resolver(resolver) |
| 26 { |
| 27 } |
| 28 |
| 29 ~MojoPromiseAdapter() override |
| 30 { |
| 31 // Before this object is destroyed a subclass must call resolve() or |
| 32 // reject() unless the execution context is inactive. |
| 33 ASSERT(!active()); |
| 34 } |
| 35 |
| 36 void resolve() |
| 37 { |
| 38 ASSERT(active()); |
| 39 m_resolver->resolve(); |
| 40 m_resolver = nullptr; |
| 41 } |
| 42 |
| 43 template <typename T> |
| 44 void resolve(T value) |
| 45 { |
| 46 ASSERT(active()); |
| 47 m_resolver->resolve(value); |
| 48 m_resolver = nullptr; |
| 49 } |
| 50 |
| 51 void reject() |
| 52 { |
| 53 ASSERT(active()); |
| 54 m_resolver->reject(); |
| 55 m_resolver = nullptr; |
| 56 } |
| 57 |
| 58 template <typename T> |
| 59 void reject(T value) |
| 60 { |
| 61 ASSERT(active()); |
| 62 m_resolver->reject(value); |
| 63 m_resolver = nullptr; |
| 64 } |
| 65 |
| 66 bool active() const |
| 67 { |
| 68 return m_resolver && executionContext() && !executionContext()->activeDO
MObjectsAreStopped(); |
| 69 } |
| 70 |
| 71 Callback callback() |
| 72 { |
| 73 #if ENABLE(ASSERT) |
| 74 // This method passes ownership of |this| to the callback it returns so |
| 75 // it may only be called once. |
| 76 ASSERT(!m_isCallbackCalled); |
| 77 m_isCallbackCalled = true; |
| 78 #endif |
| 79 return Callback(this); |
| 80 } |
| 81 |
| 82 ExecutionContext* executionContext() const { return m_resolver->getExecution
Context(); } |
| 83 |
| 84 private: |
| 85 Persistent<ScriptPromiseResolver> m_resolver; |
| 86 #if ENABLE(ASSERT) |
| 87 bool m_isCallbackCalled = false; |
| 88 #endif |
| 89 }; |
| 90 |
| 91 } |
| 92 |
| 93 #endif // MojoPromiseAdapter_h |
OLD | NEW |