Index: mojo/bindings/java/src/org/chromium/mojo/bindings/Interface.java |
diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/Interface.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/Interface.java |
index 0b8592d52a4ecbd40ca8f57f622100302eee0ac7..5165d4c3098762088eb272740e364c85c451b7a2 100644 |
--- a/mojo/bindings/java/src/org/chromium/mojo/bindings/Interface.java |
+++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/Interface.java |
@@ -4,9 +4,207 @@ |
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; |
+ |
/** |
* 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 proxy. It implements |
+ * the Interface and each time a method is called, the parameter are serialized and sent to the |
+ * {@link MessageReceiverWithResponder}, passing it the response callback if needed. |
+ */ |
+ abstract class Proxy implements Interface { |
+ |
+ /** |
+ * The {@link Core} implementation to use. |
+ */ |
+ private final Core mCore; |
+ |
+ /** |
+ * The {@link MessageReceiverWithResponder} that will received serialized message for each |
+ * method call. |
+ */ |
+ private final MessageReceiverWithResponder mMessageReceiver; |
+ |
+ private ConnectionErrorHandler mErrorHandler = null; |
+ |
+ /** |
+ * Constructor. |
+ */ |
+ protected Proxy(Core core, MessageReceiverWithResponder messageReceiver) { |
+ this.mCore = core; |
+ this.mMessageReceiver = messageReceiver; |
+ } |
+ |
+ protected MessageReceiverWithResponder getMessageReceiver() { |
+ return mMessageReceiver; |
+ } |
+ |
+ protected Core getCore() { |
+ return mCore; |
+ } |
+ |
+ /** |
+ * @param mErrorHandler the mErrorHandler to set |
+ */ |
+ public void setErrorHandler(ConnectionErrorHandler mErrorHandler) { |
+ this.mErrorHandler = mErrorHandler; |
+ } |
+ |
+ /** |
+ * @see ConnectionErrorHandler#onConnectionError(MojoException) |
+ */ |
+ @Override |
+ public void onConnectionError(MojoException e) { |
+ if (mErrorHandler != null) { |
+ mErrorHandler.onConnectionError(e); |
+ } |
+ } |
+ } |
+ |
+ /** |
+ * TODO(qsr): Insert description here. |
+ * |
+ * @param <I> |
+ */ |
+ abstract class Stub<I extends Interface> implements MessageReceiverWithResponder { |
+ |
+ private final Core mCore; |
+ private final I mImpl; |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param messageReceiver |
+ */ |
+ public Stub(Core core, I impl) { |
+ mCore = core; |
+ mImpl = impl; |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @return |
+ */ |
+ protected Core getCore() { |
+ return mCore; |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @return |
+ */ |
+ protected I getImpl() { |
+ return mImpl; |
+ } |
+ |
+ } |
+ |
+ /** |
+ * TODO(qsr): Insert description here. |
+ * |
+ * @param <I> |
+ */ |
+ abstract class Builder<I extends Interface, P extends Proxy> { |
+ |
+ public abstract I[] newArray(int size); |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param impl |
+ * @return |
+ */ |
+ public abstract Stub<I> buildStub(Core core, I impl); |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param messageReceiver |
+ * @return |
+ */ |
+ public abstract P buildProxy(Core core, MessageReceiverWithResponder messageReceiver); |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param handle |
+ * @param impl |
+ */ |
+ public void bind(MessagePipeHandle handle, I impl) { |
+ // The handle is intentionally leaked. It will be closed when the connected handle |
+ // will be closed. |
+ Router router = new RouterImpl(handle); |
+ router.setErrorHandler(impl); |
+ router.start(); |
+ bind(handle.getCore(), router, impl); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param request |
+ * @param impl |
+ */ |
+ public void bind(InterfaceRequest<I> request, I impl) { |
+ bind(request.passHandle(), impl); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param handle |
+ * @return |
+ */ |
+ public 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; |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param router |
+ * @param impl |
+ */ |
+ public void bind(Core core, Router router, I impl) { |
+ router.setIncomingMessageReceiver(buildStub(core, impl)); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param handle |
+ * @return |
+ */ |
+ public P connect(Core core, Router router) { |
+ return buildProxy(core, new AutoCloseableRouter(core, router)); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param core |
+ * @return |
+ */ |
+ public Pair<P, InterfaceRequest<I>> getInterfaceRequest(Core core) { |
+ Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(); |
+ P proxy = connect(handles.first); |
+ return Pair.create(proxy, new InterfaceRequest<I>(handles.second)); |
+ } |
+ } |
} |