Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(767)

Unified Diff: third_party/WebKit/Source/bindings/core/v8/MojoPromiseAdapter.h

Issue 1850023002: Consume Mojo services directly in Blink's WebUSB implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move promise adapter pattern into a header that can be shared. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698