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

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

Issue 686883005: Provides a way to use bindings that doesn't require subclassing (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: review feedback 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/interface_impl.h
diff --git a/mojo/public/cpp/bindings/interface_impl.h b/mojo/public/cpp/bindings/interface_impl.h
index da1055de535bb71445541320ab274be536bd5fe1..1fd430edf808fe501cd7075741cf29930e51593a 100644
--- a/mojo/public/cpp/bindings/interface_impl.h
+++ b/mojo/public/cpp/bindings/interface_impl.h
@@ -5,6 +5,7 @@
#ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
#define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
+#include "mojo/public/cpp/bindings/interface_binding.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/lib/interface_impl_internal.h"
#include "mojo/public/cpp/environment/environment.h"
@@ -12,47 +13,60 @@
namespace mojo {
+// Mojo provides two ways to bind an implementation of a particular interface to
+// a message pipe. The distinction lies in whether the binding logic is
+// inherited from a superclass (InterfaceImpl) or whether the binding is managed
+// separately from the class (InterfaceBinding). InterfaceImpl is generally
+// simpler to use at the cost of flexibility.
+
// InterfaceImpl<..> is designed to be the base class of an interface
// implementation. It may be bound to a pipe or a proxy, see BindToPipe and
// BindToProxy.
+// See also InterfaceBinding.
template <typename Interface>
-class InterfaceImpl : public internal::InterfaceImplBase<Interface> {
+class InterfaceImpl : public InterfaceBindingDelegate, public Interface {
public:
typedef typename Interface::Client Client;
typedef Interface ImplementedInterface;
- InterfaceImpl() : internal_state_(this) {}
- virtual ~InterfaceImpl() {}
+ InterfaceImpl() : interface_binding_(nullptr) {}
+ virtual ~InterfaceImpl() { delete interface_binding_; }
// Returns a proxy to the client interface. This is null upon construction,
// and becomes non-null after OnClientConnected. NOTE: It remains non-null
// until this instance is deleted.
- Client* client() { return internal_state_.client(); }
+ Client* client() { return internal_state()->client(); }
// Blocks the current thread for the first incoming method call, i.e., either
// a call to a method or a client callback method. Returns |true| if a method
// has been called, |false| in case of error. It must only be called on a
// bound object.
bool WaitForIncomingMethodCall() {
- return internal_state_.WaitForIncomingMethodCall();
+ return internal_state()->WaitForIncomingMethodCall();
}
- // Called when the client has connected to this instance.
- virtual void OnConnectionEstablished() {}
-
// Called when the client is no longer connected to this instance. NOTE: The
// client() method continues to return a non-null pointer after this method
// is called. After this method is called, any method calls made on client()
// will be silently ignored.
- virtual void OnConnectionError() {}
+ void OnConnectionError() override {}
// DO NOT USE. Exposed only for internal use and for testing.
internal::InterfaceImplState<Interface>* internal_state() {
- return &internal_state_;
+ return &interface_binding_->internal_state_;
+ }
+
+ // TODO(sky): move to private and friend functions that use.
+ void CreateInterfaceBinding() {
+ MOJO_DCHECK(!interface_binding_);
+ interface_binding_ = new InterfaceBinding<Interface>(this, this);
+ interface_binding_->set_owns_interface(false);
+ interface_binding_->set_delete_interface_on_error(true);
}
private:
- internal::InterfaceImplState<Interface> internal_state_;
+ InterfaceBinding<Interface>* interface_binding_;
+
MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl);
};
@@ -69,21 +83,25 @@ class InterfaceImpl : public internal::InterfaceImplBase<Interface> {
// method will be called.
//
// Before returning, the instance's OnConnectionEstablished method is called.
-template <typename Impl>
+template <typename Impl,
+ typename Interface = typename Impl::ImplementedInterface>
Impl* BindToPipe(
Impl* instance,
ScopedMessagePipeHandle handle,
const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
+ instance->CreateInterfaceBinding();
instance->internal_state()->Bind(handle.Pass(), true, waiter);
return instance;
}
// Like BindToPipe but does not delete the instance after a channel error.
-template <typename Impl>
+template <typename Impl,
+ typename Interface = typename Impl::ImplementedInterface>
Impl* WeakBindToPipe(
Impl* instance,
ScopedMessagePipeHandle handle,
const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
+ instance->CreateInterfaceBinding();
instance->internal_state()->Bind(handle.Pass(), false, waiter);
return instance;
}
@@ -104,6 +122,7 @@ Impl* BindToProxy(
Impl* instance,
InterfacePtr<Interface>* ptr,
const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
+ instance->CreateInterfaceBinding();
instance->internal_state()->BindProxy(ptr, true, waiter);
return instance;
}
@@ -114,6 +133,7 @@ Impl* WeakBindToProxy(
Impl* instance,
InterfacePtr<Interface>* ptr,
const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
+ instance->CreateInterfaceBinding();
instance->internal_state()->BindProxy(ptr, false, waiter);
return instance;
}
« no previous file with comments | « mojo/public/cpp/bindings/interface_binding_delegate.h ('k') | mojo/public/cpp/bindings/lib/interface_impl_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698