• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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.bindings.Callbacks.Callback1;
8 import org.chromium.mojo.bindings.Interface.Manager;
9 import org.chromium.mojo.bindings.Interface.Proxy;
10 import org.chromium.mojo.system.Core;
11 
12 /**
13  * Helper class to handle interface control messages. See
14  * mojo/public/interfaces/bindings/interface_control_messages.mojom.
15  */
16 public class InterfaceControlMessagesHelper {
17     /**
18      * MessageReceiver that forwards a message containing a {@link RunResponseMessageParams} to a
19      * callback.
20      */
21     private static class RunResponseForwardToCallback
22             extends SideEffectFreeCloseable implements MessageReceiver {
23         private final Callback1<RunResponseMessageParams> mCallback;
24 
RunResponseForwardToCallback(Callback1<RunResponseMessageParams> callback)25         RunResponseForwardToCallback(Callback1<RunResponseMessageParams> callback) {
26             mCallback = callback;
27         }
28 
29         /**
30          * @see MessageReceiver#accept(Message)
31          */
32         @Override
accept(Message message)33         public boolean accept(Message message) {
34             RunResponseMessageParams response =
35                     RunResponseMessageParams.deserialize(message.asServiceMessage().getPayload());
36             mCallback.call(response);
37             return true;
38         }
39     }
40 
41     /**
42      * Sends the given run message through the receiver, registering the callback.
43      */
sendRunMessage(Core core, MessageReceiverWithResponder receiver, RunMessageParams params, Callback1<RunResponseMessageParams> callback)44     public static void sendRunMessage(Core core, MessageReceiverWithResponder receiver,
45             RunMessageParams params, Callback1<RunResponseMessageParams> callback) {
46         Message message = params.serializeWithHeader(
47                 core, new MessageHeader(InterfaceControlMessagesConstants.RUN_MESSAGE_ID,
48                         MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG, 0));
49         receiver.acceptWithResponder(message, new RunResponseForwardToCallback(callback));
50     }
51 
52     /**
53      * Sends the given run or close pipe message through the receiver.
54      */
sendRunOrClosePipeMessage( Core core, MessageReceiverWithResponder receiver, RunOrClosePipeMessageParams params)55     public static void sendRunOrClosePipeMessage(
56             Core core, MessageReceiverWithResponder receiver, RunOrClosePipeMessageParams params) {
57         Message message = params.serializeWithHeader(core,
58                 new MessageHeader(InterfaceControlMessagesConstants.RUN_OR_CLOSE_PIPE_MESSAGE_ID));
59         receiver.accept(message);
60     }
61 
62     /**
63      * Handles a received run message.
64      */
handleRun( Core core, Manager<I, P> manager, ServiceMessage message, MessageReceiver responder)65     public static <I extends Interface, P extends Proxy> boolean handleRun(
66             Core core, Manager<I, P> manager, ServiceMessage message, MessageReceiver responder) {
67         RunResponseMessageParams response = new RunResponseMessageParams();
68         response.reserved0 = 16;
69         response.reserved1 = 0;
70         response.queryVersionResult = new QueryVersionResult();
71         response.queryVersionResult.version = manager.getVersion();
72 
73         return responder.accept(response.serializeWithHeader(
74                 core, new MessageHeader(InterfaceControlMessagesConstants.RUN_MESSAGE_ID,
75                         MessageHeader.MESSAGE_IS_RESPONSE_FLAG,
76                         message.getHeader().getRequestId())));
77     }
78 
79     /**
80      * Handles a received run or close pipe message. Closing the pipe is handled by returning
81      * |false|.
82      */
handleRunOrClosePipe( Manager<I, P> manager, ServiceMessage message)83     public static <I extends Interface, P extends Proxy> boolean handleRunOrClosePipe(
84             Manager<I, P> manager, ServiceMessage message) {
85         Message payload = message.getPayload();
86         RunOrClosePipeMessageParams query = RunOrClosePipeMessageParams.deserialize(payload);
87         return query.requireVersion.version <= manager.getVersion();
88     }
89 }
90