Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / mojo / public / java / bindings / src / org / chromium / mojo / bindings / InterfaceWithClient.java
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.
4
5 package org.chromium.mojo.bindings;
6
7 import org.chromium.mojo.system.Core;
8 import org.chromium.mojo.system.MessagePipeHandle;
9 import org.chromium.mojo.system.Pair;
10
11 /**
12  * Base class for mojo generated interfaces that have a client.
13  *
14  * @param <CI> the type of the client interface.
15  */
16 public interface InterfaceWithClient<CI extends Interface> extends Interface {
17
18     /**
19      * Proxy class for interfaces with a client.
20      */
21     interface Proxy<CI extends Interface> extends Interface.Proxy, InterfaceWithClient<CI> {
22     }
23
24     /**
25      * Base implementation of Proxy.
26      *
27      * @param <CI> the type of the client interface.
28      */
29     abstract class AbstractProxy<CI extends Interface> extends Interface.AbstractProxy
30             implements Proxy<CI> {
31
32         /**
33          * Constructor.
34          *
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.
37          */
38         public AbstractProxy(Core core, MessageReceiverWithResponder messageReceiver) {
39             super(core, messageReceiver);
40         }
41
42         /**
43          * @see InterfaceWithClient#setClient(Interface)
44          */
45         @Override
46         public void setClient(CI client) {
47             throw new UnsupportedOperationException(
48                     "Setting the client on a proxy is not supported");
49         }
50     }
51
52     /**
53      * Base manager implementation for interfaces that have a client.
54      *
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.
58      */
59     abstract class Manager<I extends InterfaceWithClient<CI>, P extends Proxy<CI>,
60             CI extends Interface> extends Interface.Manager<I, P> {
61
62         /**
63          * @see Interface.Manager#bind(Interface, MessagePipeHandle)
64          */
65         @Override
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);
72             router.start();
73         }
74
75         /**
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.
79          */
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);
88             router.start();
89             return proxy;
90         }
91
92         /**
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
95          * used immediately.
96          *
97          * @param client the implementation of the client interface.
98          */
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));
103         }
104
105         /**
106          * Returns a manager for the client inetrafce.
107          */
108         protected abstract Interface.Manager<CI, ?> getClientManager();
109     }
110
111     /**
112      * Set the client implementation for this interface.
113      */
114     public void setClient(CI client);
115 }