1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.mojo.bindings;
7 import org.chromium.mojo.system.Core;
8 import org.chromium.mojo.system.MessagePipeHandle;
9 import org.chromium.mojo.system.Pair;
12 * Base class for mojo generated interfaces that have a client.
14 * @param <CI> the type of the client interface.
16 public interface InterfaceWithClient<CI extends Interface> extends Interface {
19 * Proxy class for interfaces with a client.
21 interface Proxy<CI extends Interface> extends Interface.Proxy, InterfaceWithClient<CI> {
25 * Base implementation of Proxy.
27 * @param <CI> the type of the client interface.
29 abstract class AbstractProxy<CI extends Interface> extends Interface.AbstractProxy
30 implements Proxy<CI> {
35 * @param core the Core implementation used to create pipes and access the async waiter.
36 * @param messageReceiver the message receiver to send message to.
38 public AbstractProxy(Core core, MessageReceiverWithResponder messageReceiver) {
39 super(core, messageReceiver);
43 * @see InterfaceWithClient#setClient(Interface)
46 public void setClient(CI client) {
47 throw new UnsupportedOperationException(
48 "Setting the client on a proxy is not supported");
53 * Base manager implementation for interfaces that have a client.
55 * @param <I> the type of the interface the manager can handle.
56 * @param <P> the type of the proxy the manager can handle. To be noted, P always extends I.
57 * @param <CI> the type of the client interface.
59 abstract class Manager<I extends InterfaceWithClient<CI>, P extends Proxy<CI>,
60 CI extends Interface> extends Interface.Manager<I, P> {
63 * @see Interface.Manager#bind(Interface, MessagePipeHandle)
66 public final void bind(I impl, MessagePipeHandle handle) {
67 Router router = new RouterImpl(handle);
68 super.bind(handle.getCore(), impl, router);
69 @SuppressWarnings("unchecked")
70 CI client = (CI) getClientManager().attachProxy(handle.getCore(), router);
71 impl.setClient(client);
76 * Returns a Proxy that will send messages to the given |handle|. This implies that the
77 * other end of the handle must be connected to an implementation of the interface. |client|
78 * is the implementation of the client interface.
80 public P attachProxy(MessagePipeHandle handle, CI client) {
81 Router router = new RouterImpl(handle);
82 DelegatingConnectionErrorHandler handlers = new DelegatingConnectionErrorHandler();
83 handlers.addConnectionErrorHandler(client);
84 router.setErrorHandler(handlers);
85 getClientManager().bind(handle.getCore(), client, router);
86 P proxy = super.attachProxy(handle.getCore(), router);
87 handlers.addConnectionErrorHandler(proxy);
93 * Constructs a new |InterfaceRequest| for the interface. This method returns a Pair where
94 * the first element is a proxy, and the second element is the request. The proxy can be
97 * @param client the implementation of the client interface.
99 public final Pair<P, InterfaceRequest<I>> getInterfaceRequest(Core core, CI client) {
100 Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(null);
101 P proxy = attachProxy(handles.first, client);
102 return Pair.create(proxy, new InterfaceRequest<I>(handles.second));
106 * Returns a manager for the client inetrafce.
108 protected abstract Interface.Manager<CI, ?> getClientManager();
112 * Set the client implementation for this interface.
114 public void setClient(CI client);