Chromium Code Reviews| Index: mojo/public/java/bindings/src/org/chromium/mojo/bindings/Interface.java |
| diff --git a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Interface.java b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Interface.java |
| index 0b8592d52a4ecbd40ca8f57f622100302eee0ac7..a905550faca6a073dfa8df50440c36b653412212 100644 |
| --- a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Interface.java |
| +++ b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Interface.java |
| @@ -4,9 +4,239 @@ |
| package org.chromium.mojo.bindings; |
| +import org.chromium.mojo.system.Core; |
| +import org.chromium.mojo.system.MessagePipeHandle; |
| +import org.chromium.mojo.system.MojoException; |
| +import org.chromium.mojo.system.Pair; |
| + |
| +import java.io.Closeable; |
| + |
| /** |
| * Base class for mojo generated interfaces. |
| */ |
| -public interface Interface { |
| +public interface Interface extends ConnectionErrorHandler { |
| + |
| + /** |
| + * A proxy to the a mojo interface. This is base class for all generated proxies. It implements |
| + * the Interface and each time a method is called, the parameters are serialized and sent to the |
| + * {@link MessageReceiverWithResponder}, passing it the response callback if needed. |
| + */ |
| + public interface Proxy extends Interface, Closeable { |
| + |
| + /** |
| + * Set the {@link ConnectionErrorHandler} that will be notified of errors. |
| + */ |
| + public void setErrorHandler(ConnectionErrorHandler errorHandler); |
| + |
| + /** |
| + * @see java.io.Closeable#close() |
| + */ |
| + @Override |
| + public void close(); |
| + } |
| + |
| + /** |
| + * Base implementation of {@link Proxy}. |
| + */ |
| + abstract class AbstractProxy implements Proxy { |
| + |
| + /** |
| + * The {@link Core} implementation to use. |
| + */ |
| + private final Core mCore; |
| + |
| + /** |
| + * The {@link MessageReceiverWithResponder} that will receive a serialized message for each |
| + * method call. |
| + */ |
| + private final MessageReceiverWithResponder mMessageReceiver; |
| + |
| + /** |
| + * The {@link ConnectionErrorHandler} that will be notified of errors. |
| + */ |
| + private ConnectionErrorHandler mErrorHandler = null; |
| + |
| + /** |
| + * Constructor. |
| + * |
| + * @param core the Core implementation used to create pipes and access the async waiter. |
| + * @param messageReceiver the message receiver to send message to. |
| + */ |
| + protected AbstractProxy(Core core, MessageReceiverWithResponder messageReceiver) { |
| + this.mCore = core; |
| + this.mMessageReceiver = messageReceiver; |
| + } |
| + |
| + /** |
| + * Returns the message receiver to send message to. |
| + */ |
| + protected MessageReceiverWithResponder getMessageReceiver() { |
| + return mMessageReceiver; |
| + } |
| + |
| + /** |
| + * Returns the Core implementation. |
| + */ |
| + protected Core getCore() { |
| + return mCore; |
| + } |
| + |
| + /** |
| + * @see Proxy#setErrorHandler(ConnectionErrorHandler) |
| + */ |
| + @Override |
| + public void setErrorHandler(ConnectionErrorHandler errorHandler) { |
| + this.mErrorHandler = errorHandler; |
| + } |
| + |
| + /** |
| + * @see ConnectionErrorHandler#onConnectionError(MojoException) |
| + */ |
| + @Override |
| + public void onConnectionError(MojoException e) { |
| + if (mErrorHandler != null) { |
| + mErrorHandler.onConnectionError(e); |
| + } |
| + } |
| + |
| + /** |
| + * @see Closeable#close() |
| + */ |
| + @Override |
| + public void close() { |
| + mMessageReceiver.close(); |
| + } |
| + } |
| + |
| + /** |
| + * Base implementation of Stubs. Stubs are message receiver that will deserialize the payload |
| + * and call the appropriate method on the implementation, serialize the response and send it |
| + * back. |
| + * |
| + * @param <I> the type of the interface to delegate calls to. |
| + */ |
| + abstract class Stub<I extends Interface> extends SideEffectFreeCloseable |
| + implements MessageReceiverWithResponder { |
| + |
| + /** |
| + * The {@link Core} implementation to use. |
| + */ |
| + private final Core mCore; |
| + |
| + /** |
| + * The implementation to delegate calls to. |
| + */ |
| + private final I mImpl; |
| + |
| + /** |
| + * Constructor. |
| + * |
| + * @param core the {@link Core} implementation to use. |
| + * @param impl the implementation to delegate calls to. |
| + */ |
| + public Stub(Core core, I impl) { |
| + mCore = core; |
| + mImpl = impl; |
| + } |
| + |
| + /** |
| + * Returns the Core implementation. |
| + */ |
| + protected Core getCore() { |
| + return mCore; |
| + } |
| + |
| + /** |
| + * Returns the implementation to delegate calls to. |
| + */ |
| + protected I getImpl() { |
| + return mImpl; |
| + } |
| + |
| + } |
| + |
| + /** |
| + * The |Builder| object allows to build proxies and stubs for a given interface. It also allows |
|
rmcilroy
2014/08/21 14:43:52
"allows to build"->"enables building of" (and in n
qsr
2014/08/22 08:11:18
Done.
|
| + * to build proxy from a |MessagePipeHandle| or bind an implementation to a given |
| + * |MessagePipeHandle| through an implicitly created stub. |
| + * |
| + * @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. |
| + */ |
| + abstract class Builder<I extends Interface, P extends Proxy> { |
|
ppi
2014/08/21 14:47:14
Initially I was confused about this because I expe
qsr
2014/08/22 08:11:18
Changed it to Binder. It is unfortunately the same
|
| + |
| + /** |
| + * Creates a new array of the given |size|. |
| + */ |
| + public abstract I[] newArray(int size); |
| + |
| + /** |
| + * Constructs a Stub delegating to the given implementation. |
| + */ |
| + public abstract Stub<I> buildStub(Core core, I impl); |
| + |
| + /** |
| + * Constructs a Proxy forwarding the calls to the given message receiver. |
| + */ |
| + public abstract P buildProxy(Core core, MessageReceiverWithResponder messageReceiver); |
| + |
| + /** |
| + * Binds the given implementation to the handle. |
| + */ |
| + public void bind(MessagePipeHandle handle, I impl) { |
| + // The handle is intentionally leaked. It will be closed when the connected handle |
| + // is closed. |
| + Router router = new RouterImpl(handle); |
| + router.setErrorHandler(impl); |
| + bind(handle.getCore(), router, impl); |
| + router.start(); |
| + } |
| + |
| + /** |
| + * Binds the given implementation to the InterfaceRequest. |
| + */ |
| + public final void bind(InterfaceRequest<P> request, I impl) { |
| + bind(request.passHandle(), impl); |
| + } |
| + |
| + /** |
| + * 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. |
| + */ |
| + public final P connect(MessagePipeHandle handle) { |
| + RouterImpl router = new RouterImpl(handle); |
| + P proxy = connect(handle.getCore(), router); |
| + DelegatingConnectionErrorHandler handlers = new DelegatingConnectionErrorHandler(); |
| + handlers.addConnectionErrorHandler(proxy); |
| + router.setErrorHandler(handlers); |
| + router.start(); |
| + return proxy; |
| + } |
| + |
| + /** |
| + * Construct a new |InterfaceRequest| for the interface. This method returns a Pair where |
| + * the first element is a proxy, and the second element is the request. The proxy can be |
| + * used immediately. |
| + */ |
| + public final Pair<P, InterfaceRequest<P>> getInterfaceRequest(Core core) { |
| + Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(null); |
| + P proxy = connect(handles.first); |
| + return Pair.create(proxy, new InterfaceRequest<P>(handles.second)); |
| + } |
| + |
| + /** |
| + * Binds the implementation to the given |router|. |
| + */ |
| + final void bind(Core core, Router router, I impl) { |
| + router.setIncomingMessageReceiver(buildStub(core, impl)); |
| + } |
| + |
| + /** |
| + * Returns a Proxy that will send messages to the given |router|. |
| + */ |
| + final P connect(Core core, Router router) { |
| + return buildProxy(core, new AutoCloseableRouter(core, router)); |
| + } |
| + } |
| } |