Index: third_party/WebKit/Source/bindings/core/v8/MojoPromiseAdapter.h |
diff --git a/third_party/WebKit/Source/bindings/core/v8/MojoPromiseAdapter.h b/third_party/WebKit/Source/bindings/core/v8/MojoPromiseAdapter.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bf95ff0edbaaec57b54dd8c8306278e049a86c15 |
--- /dev/null |
+++ b/third_party/WebKit/Source/bindings/core/v8/MojoPromiseAdapter.h |
@@ -0,0 +1,93 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MojoPromiseAdapter_h |
+#define MojoPromiseAdapter_h |
+ |
+#include "bindings/core/v8/ScriptPromiseResolver.h" |
+ |
+namespace blink { |
+ |
+class ExecutionContext; |
+ |
+// This class wraps a ScriptPromiseResolver. |
+// |
+// When the callback() method is called ownership of this object is passed to |
+// a new mojo::Callback. Subclasses must implement a Run() method matching the |
+// callback type and override the destructor to either resolve or reject with |
+// an appropriate value to handle closure of the Mojo pipe. |
+template <typename Callback> |
+class MojoPromiseAdapter : public Callback::Runnable { |
+ WTF_MAKE_NONCOPYABLE(MojoPromiseAdapter); |
+ |
+public: |
+ MojoPromiseAdapter(ScriptPromiseResolver* resolver) : m_resolver(resolver) |
+ { |
+ } |
+ |
+ ~MojoPromiseAdapter() override |
+ { |
+ // Before this object is destroyed a subclass must call resolve() or |
+ // reject() unless the execution context is inactive. |
+ ASSERT(!active()); |
+ } |
+ |
+ void resolve() |
+ { |
+ ASSERT(active()); |
+ m_resolver->resolve(); |
+ m_resolver = nullptr; |
+ } |
+ |
+ template <typename T> |
+ void resolve(T value) |
+ { |
+ ASSERT(active()); |
+ m_resolver->resolve(value); |
+ m_resolver = nullptr; |
+ } |
+ |
+ void reject() |
+ { |
+ ASSERT(active()); |
+ m_resolver->reject(); |
+ m_resolver = nullptr; |
+ } |
+ |
+ template <typename T> |
+ void reject(T value) |
+ { |
+ ASSERT(active()); |
+ m_resolver->reject(value); |
+ m_resolver = nullptr; |
+ } |
+ |
+ bool active() const |
+ { |
+ return m_resolver && executionContext() && !executionContext()->activeDOMObjectsAreStopped(); |
+ } |
+ |
+ Callback callback() |
+ { |
+#if ENABLE(ASSERT) |
+ // This method passes ownership of |this| to the callback it returns so |
+ // it may only be called once. |
+ ASSERT(!m_isCallbackCalled); |
+ m_isCallbackCalled = true; |
+#endif |
+ return Callback(this); |
+ } |
+ |
+ ExecutionContext* executionContext() const { return m_resolver->getExecutionContext(); } |
+ |
+private: |
+ Persistent<ScriptPromiseResolver> m_resolver; |
+#if ENABLE(ASSERT) |
+ bool m_isCallbackCalled = false; |
+#endif |
+}; |
+ |
+} |
+ |
+#endif // MojoPromiseAdapter_h |