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.system;
7 import java.nio.ByteBuffer;
11 * Message pipes are bidirectional communication channel for framed data (i.e., messages). Messages
12 * can contain plain data and/or Mojo handles.
14 public interface MessagePipeHandle extends Handle {
17 * Flags for the message pipe creation operation.
19 public static class CreateFlags extends Flags<CreateFlags> {
20 private static final int FLAG_NONE = 0;
23 * Immutable flag with not bit set.
25 public static final CreateFlags NONE = CreateFlags.none().immutable();
28 * Dedicated constructor.
30 * @param flags initial value of the flags.
32 protected CreateFlags(int flags) {
37 * @return flags with no bit set.
39 public static CreateFlags none() {
40 return new CreateFlags(FLAG_NONE);
46 * Used to specify creation parameters for a message pipe to |Core#createMessagePipe()|.
48 public static class CreateOptions {
49 private CreateFlags mFlags = CreateFlags.NONE;
54 public CreateFlags getFlags() {
61 * Flags for the write operations on MessagePipeHandle .
63 public static class WriteFlags extends Flags<WriteFlags> {
64 private static final int FLAG_NONE = 0;
67 * Immutable flag with no bit set.
69 public static final WriteFlags NONE = WriteFlags.none().immutable();
72 * Dedicated constructor.
74 * @param flags initial value of the flag.
76 private WriteFlags(int flags) {
81 * @return a flag with no bit set.
83 public static WriteFlags none() {
84 return new WriteFlags(FLAG_NONE);
89 * Flags for the read operations on MessagePipeHandle.
91 public static class ReadFlags extends Flags<ReadFlags> {
92 private static final int FLAG_NONE = 0;
93 private static final int FLAG_MAY_DISCARD = 1 << 0;
96 * Immutable flag with no bit set.
98 public static final ReadFlags NONE = ReadFlags.none().immutable();
101 * Dedicated constructor.
103 * @param flags initial value of the flag.
105 private ReadFlags(int flags) {
110 * Change the may-discard bit of this flag. If set, if the message is unable to be read for
111 * whatever reason (e.g., the caller-supplied buffer is too small), discard the message
112 * (i.e., simply dequeue it).
114 * @param mayDiscard the new value of the may-discard bit.
117 public ReadFlags setMayDiscard(boolean mayDiscard) {
118 return setFlag(FLAG_MAY_DISCARD, mayDiscard);
122 * @return a flag with no bit set.
124 public static ReadFlags none() {
125 return new ReadFlags(FLAG_NONE);
131 * Result of the |readMessage| method.
133 public static class ReadMessageResult {
135 * The MojoResult value of the read call.
137 private int mMojoResult;
139 * If a message was read, the size in bytes of the message, otherwise the size in bytes of
142 private int mMessageSize;
144 * If a message was read, the number of handles contained in the message, otherwise the
145 * number of handles contained in the next message.
147 private int mHandlesCount;
149 * If a message was read, the handles contained in the message, undefined otherwise.
151 private List<UntypedHandle> mHandles;
154 * @return the mojoResult
156 public int getMojoResult() {
161 * @param mojoResult the mojoResult to set
163 public void setMojoResult(int mojoResult) {
164 mMojoResult = mojoResult;
168 * @return the messageSize
170 public int getMessageSize() {
175 * @param messageSize the messageSize to set
177 public void setMessageSize(int messageSize) {
178 mMessageSize = messageSize;
182 * @return the handlesCount
184 public int getHandlesCount() {
185 return mHandlesCount;
189 * @param handlesCount the handlesCount to set
191 public void setHandlesCount(int handlesCount) {
192 mHandlesCount = handlesCount;
196 * @return the handles
198 public List<UntypedHandle> getHandles() {
203 * @param handles the handles to set
205 public void setHandles(List<UntypedHandle> handles) {
211 * @see org.chromium.mojo.system.Handle#pass()
214 public MessagePipeHandle pass();
217 * Writes a message to the message pipe endpoint, with message data specified by |bytes| and
218 * attached handles specified by |handles|, and options specified by |flags|. If there is no
219 * message data, |bytes| may be null, otherwise it must be a direct ByteBuffer. If there are no
220 * attached handles, |handles| may be null.
222 * If handles are attached, on success the handles will no longer be valid (the receiver will
223 * receive equivalent, but logically different, handles). Handles to be sent should not be in
224 * simultaneous use (e.g., on another thread).
226 void writeMessage(ByteBuffer bytes, List<? extends Handle> handles, WriteFlags flags);
229 * Reads a message from the message pipe endpoint; also usable to query the size of the next
230 * message or discard the next message. |bytes| indicate the buffer/buffer size to receive the
231 * message data (if any) and |maxNumberOfHandles| indicate the maximum handle count to receive
232 * the attached handles (if any). |bytes| is optional. If null, |maxNumberOfHandles| must be
233 * zero, and the return value will indicate the size of the next message. If non-null, it must
234 * be a direct ByteBuffer and the return value will indicate if the message was read or not. If
235 * the message was read its content will be in |bytes|, and the attached handles will be in the
236 * return value. Partial reads are NEVER done. Either a full read is done and |wasMessageRead|
237 * will be true, or the read is NOT done and |wasMessageRead| will be false (if |mayDiscard| was
238 * set, the message is also discarded in this case).
240 ReadMessageResult readMessage(ByteBuffer bytes, int maxNumberOfHandles,