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

Unified Diff: mojo/public/cpp/bindings/connector.h

Issue 718473003: Add mojo::Binding<Interface> for more flexible pipe<->impl binding (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month 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: mojo/public/cpp/bindings/connector.h
diff --git a/mojo/public/cpp/bindings/connector.h b/mojo/public/cpp/bindings/connector.h
new file mode 100644
index 0000000000000000000000000000000000000000..26bbda15ec929238706dd76667d6999162c18f3c
--- /dev/null
+++ b/mojo/public/cpp/bindings/connector.h
@@ -0,0 +1,119 @@
+// Copyright 2014 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 MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_
+#define MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_
+
+#include "mojo/public/c/environment/async_waiter.h"
+#include "mojo/public/cpp/bindings/error_handler.h"
+#include "mojo/public/cpp/bindings/interface_ptr.h"
+#include "mojo/public/cpp/bindings/lib/filter_chain.h"
+#include "mojo/public/cpp/bindings/lib/message_header_validator.h"
+#include "mojo/public/cpp/bindings/lib/router.h"
+#include "mojo/public/cpp/environment/logging.h"
+#include "mojo/public/cpp/system/core.h"
+
+namespace mojo {
+
+// This connects an interface implementation a pipe. Deleting the connector
DaveMoore 2014/11/11 17:18:45 nit: "to a pipe"
+// closes the pipe.
+//
+// Example:
+//
+// #include "foo.mojom.h"
+//
+// class FooImpl : public Foo {
+// public:
+// explicit FooImpl(ScopedMessagePipeHandle handle)
+// : connector_(this, handle.Pass()) {}
+//
+// // Foo implementation here.
+//
+// private:
+// Connector<Foo> connector_;
+// };
+//
+template <typename Interface>
+class Connector : public ErrorHandler {
+ public:
+ using Client = typename Interface::Client;
+
+ explicit Connector(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); }
+
+ Connector(
+ Interface* impl,
+ ScopedMessagePipeHandle handle,
+ const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
+ : Connector(impl) {
+ stub_.set_sink(impl_);
+ Bind(handle.Pass(), waiter);
+ }
+
+ virtual ~Connector() {
+ delete proxy_;
+ if (router_) {
+ router_->set_error_handler(nullptr);
+ delete router_;
+ }
+ }
+
+ void Bind(
+ ScopedMessagePipeHandle handle,
+ const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
+ internal::FilterChain filters;
+ filters.Append<internal::MessageHeaderValidator>();
+ filters.Append<typename Interface::RequestValidator_>();
+ filters.Append<typename Client::ResponseValidator_>();
+
+ router_ = new internal::Router(handle.Pass(), filters.Pass(), waiter);
+ router_->set_incoming_receiver(&stub_);
+ router_->set_error_handler(this);
+
+ proxy_ = new typename Client::Proxy_(router_);
+ }
+
+ void Bind(
+ InterfacePtr<Interface>* ptr,
+ const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
+ MessagePipe pipe;
+ ptr->Bind(pipe.handle0.Pass(), waiter);
+ Bind(pipe.handle1.Pass(), waiter);
+ }
+
+ bool WaitForIncomingMethodCall() {
+ MOJO_DCHECK(router_);
+ return router_->WaitForIncomingMessage();
+ }
+
+ void set_error_handler(ErrorHandler* error_handler) {
+ error_handler_ = error_handler;
+ }
+
+ // ErrorHandler implementation
+ void OnConnectionError() override {
+ if (error_handler_)
+ error_handler_->OnConnectionError();
+ }
+
+ Client* client() { return proxy_; }
+
+ // Exposed for testing, should not generally be used.
+ internal::Router* router() { return router_; }
+
+ protected:
+ Interface* impl() { return impl_; }
+
+ private:
+ internal::Router* router_ = nullptr;
+ typename Client::Proxy_* proxy_ = nullptr;
+ typename Interface::Stub_ stub_;
+ Interface* impl_;
+ ErrorHandler* error_handler_ = nullptr;
+
+ MOJO_DISALLOW_COPY_AND_ASSIGN(Connector);
+};
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_

Powered by Google App Engine
This is Rietveld 408576698