Index: mojo/public/java/bindings/src/org/chromium/mojo/bindings/InterfaceWithClient.java |
diff --git a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/InterfaceWithClient.java b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/InterfaceWithClient.java |
index 69fe6edcb3b5c217c1da54db65d2916e0b15dc2b..1d924c3858e24d15aeb648fe611ccfd00e790e00 100644 |
--- a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/InterfaceWithClient.java |
+++ b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/InterfaceWithClient.java |
@@ -4,6 +4,9 @@ |
package org.chromium.mojo.bindings; |
+import org.chromium.mojo.system.Core; |
+import org.chromium.mojo.system.MessagePipeHandle; |
+ |
/** |
* Base class for mojo generated interfaces that have a client. |
* |
@@ -12,8 +15,88 @@ package org.chromium.mojo.bindings; |
public interface InterfaceWithClient<C extends Interface> extends Interface { |
/** |
- * Set the client associated with this interface. |
+ * Proxy class for interfaces with a client. |
*/ |
- public void setClient(C client); |
+ interface Proxy<C extends Interface> extends Interface.Proxy, InterfaceWithClient<C> { |
+ } |
+ |
+ /** |
+ * Base implementation of Proxy. |
+ * |
+ * @param <C> the type of the client interface. |
+ */ |
+ abstract class AbstractProxy<C extends Interface> extends Interface.AbstractProxy |
+ implements Proxy<C> { |
+ |
+ /** |
+ * Constructor. |
+ * |
+ * @param core the Core implementation used to create pipes and access the async waiter. |
+ * @param messageReceiver the message receiver to send message to. |
+ */ |
+ public AbstractProxy(Core core, MessageReceiverWithResponder messageReceiver) { |
+ super(core, messageReceiver); |
+ } |
+ |
+ /** |
+ * @see InterfaceWithClient#setClient(Interface) |
+ */ |
+ @Override |
+ public void setClient(C client) { |
+ throw new UnsupportedOperationException( |
+ "Setting the client on a proxy is not supported"); |
+ } |
+ } |
+ |
+ /** |
+ * Base builder implementation for interfaces that have a client. |
+ * |
+ * @param <I> the type of the interface the builder can handle. |
+ * @param <P> the type of the proxy the builder can handle. To be noted, P always extends I. |
+ * @param <C> the type of the client interface. |
+ */ |
+ abstract class Builder<I extends InterfaceWithClient<C>, P extends Proxy<C>, |
+ C extends Interface> extends Interface.Builder<I, P> { |
+ |
+ /** |
+ * @see Interface.Builder#bind(org.chromium.mojo.system.MessagePipeHandle, Interface) |
+ */ |
+ @Override |
+ public final void bind(MessagePipeHandle handle, I impl) { |
+ Router router = new RouterImpl(handle); |
+ router.setErrorHandler(impl); |
+ super.bind(handle.getCore(), router, impl); |
+ @SuppressWarnings("unchecked") |
+ C client = (C) getClientBuilder().connect(handle.getCore(), router); |
+ impl.setClient(client); |
+ router.start(); |
+ } |
+ |
+ /** |
+ * Returns a Proxy that will send messages to the given |handle|. This implies that the |
+ * other end of the handle must be connected to an implementation of the interface. |client| |
+ * is the implementation of the client interface. |
+ */ |
+ public P connect(MessagePipeHandle handle, C client) { |
+ Router router = new RouterImpl(handle); |
+ DelegatingConnectionErrorHandler handlers = new DelegatingConnectionErrorHandler(); |
+ router.setErrorHandler(handlers); |
+ handlers.addConnectionErrorHandler(client); |
+ getClientBuilder().bind(handle.getCore(), router, client); |
+ P proxy = super.connect(handle.getCore(), router); |
+ handlers.addConnectionErrorHandler(proxy); |
+ router.start(); |
+ return proxy; |
+ } |
+ /** |
+ * Returns a builder for the client inetrafce. |
+ */ |
+ protected abstract Interface.Builder<C, ?> getClientBuilder(); |
+ } |
+ |
+ /** |
+ * Set the client implementation for this interface. |
+ */ |
+ public void setClient(C client); |
} |