• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.system;
6 
7 import java.nio.ByteBuffer;
8 
9 /**
10  * Interface for data pipes. A data pipe is a unidirectional communication channel for unframed
11  * data. Data is unframed, but must come as (multiples of) discrete elements, of the size given at
12  * creation time.
13  */
14 public interface DataPipe {
15 
16     /**
17      * Flags for the data pipe creation operation.
18      */
19     public static class CreateFlags extends Flags<CreateFlags> {
20         private static final int FLAG_NONE = 0;
21 
22         /**
23          * Immutable flag with not bit set.
24          */
25         public static final CreateFlags NONE = CreateFlags.none().immutable();
26 
27         /**
28          * Dedicated constructor.
29          *
30          * @param flags initial value of the flags.
31          */
CreateFlags(int flags)32         protected CreateFlags(int flags) {
33             super(flags);
34         }
35 
36         /**
37          * @return flags with no bit set.
38          */
none()39         public static CreateFlags none() {
40             return new CreateFlags(FLAG_NONE);
41         }
42 
43     }
44 
45     /**
46      * Used to specify creation parameters for a data pipe to |Core.createDataPipe()|.
47      */
48     public static class CreateOptions {
49 
50         /**
51          * Used to specify different modes of operation, see |DataPipe.CreateFlags|.
52          */
53         private CreateFlags mFlags = CreateFlags.none();
54         /**
55          * The size of an element, in bytes. All transactions and buffers will consist of an
56          * integral number of elements. Must be nonzero.
57          */
58         private int mElementNumBytes;
59         /**
60          * The capacity of the data pipe, in number of bytes; must be a multiple of
61          * |element_num_bytes|. The data pipe will always be able to queue AT LEAST this much data.
62          * Set to zero to opt for a system-dependent automatically-calculated capacity (which will
63          * always be at least one element).
64          */
65         private int mCapacityNumBytes;
66 
67         /**
68          * @return the flags
69          */
getFlags()70         public CreateFlags getFlags() {
71             return mFlags;
72         }
73 
74         /**
75          * @return the elementNumBytes
76          */
getElementNumBytes()77         public int getElementNumBytes() {
78             return mElementNumBytes;
79         }
80 
81         /**
82          * @param elementNumBytes the elementNumBytes to set
83          */
setElementNumBytes(int elementNumBytes)84         public void setElementNumBytes(int elementNumBytes) {
85             mElementNumBytes = elementNumBytes;
86         }
87 
88         /**
89          * @return the capacityNumBytes
90          */
getCapacityNumBytes()91         public int getCapacityNumBytes() {
92             return mCapacityNumBytes;
93         }
94 
95         /**
96          * @param capacityNumBytes the capacityNumBytes to set
97          */
setCapacityNumBytes(int capacityNumBytes)98         public void setCapacityNumBytes(int capacityNumBytes) {
99             mCapacityNumBytes = capacityNumBytes;
100         }
101 
102     }
103 
104     /**
105      * Flags for the write operations on MessagePipeHandle .
106      */
107     public static class WriteFlags extends Flags<WriteFlags> {
108         private static final int FLAG_NONE = 0;
109         private static final int FLAG_ALL_OR_NONE = 1 << 0;
110 
111         /**
112          * Immutable flag with not bit set.
113          */
114         public static final WriteFlags NONE = WriteFlags.none().immutable();
115 
116         /**
117          * Dedicated constructor.
118          *
119          * @param flags initial value of the flags.
120          */
WriteFlags(int flags)121         private WriteFlags(int flags) {
122             super(flags);
123         }
124 
125         /**
126          * Change the all-or-none bit of those flags. If set, write either all the elements
127          * requested or none of them.
128          *
129          * @param allOrNone the new value of all-or-none bit.
130          * @return this.
131          */
setAllOrNone(boolean allOrNone)132         public WriteFlags setAllOrNone(boolean allOrNone) {
133             return setFlag(FLAG_ALL_OR_NONE, allOrNone);
134         }
135 
136         /**
137          * @return a flag with no bit set.
138          */
none()139         public static WriteFlags none() {
140             return new WriteFlags(FLAG_NONE);
141         }
142     }
143 
144     /**
145      * Flags for the read operations on MessagePipeHandle.
146      */
147     public static class ReadFlags extends Flags<ReadFlags> {
148         private static final int FLAG_NONE = 0;
149         private static final int FLAG_ALL_OR_NONE = 1 << 0;
150         private static final int FLAG_QUERY = 1 << 2;
151         private static final int FLAG_PEEK = 1 << 3;
152 
153         /**
154          * Immutable flag with not bit set.
155          */
156         public static final ReadFlags NONE = ReadFlags.none().immutable();
157 
158         /**
159          * Dedicated constructor.
160          *
161          * @param flags initial value of the flag.
162          */
ReadFlags(int flags)163         private ReadFlags(int flags) {
164             super(flags);
165         }
166 
167         /**
168          * Change the all-or-none bit of this flag. If set, read (or discard) either the requested
169          * number of elements or none.
170          *
171          * @param allOrNone the new value of the all-or-none bit.
172          * @return this.
173          */
setAllOrNone(boolean allOrNone)174         public ReadFlags setAllOrNone(boolean allOrNone) {
175             return setFlag(FLAG_ALL_OR_NONE, allOrNone);
176         }
177 
178         /**
179          * Change the query bit of this flag. If set query the number of elements available to read.
180          * Mutually exclusive with |discard| and |allOrNone| is ignored if this flag is set.
181          *
182          * @param query the new value of the query bit.
183          * @return this.
184          */
query(boolean query)185         public ReadFlags query(boolean query) {
186             return setFlag(FLAG_QUERY, query);
187         }
188 
189         /**
190          * Change the peek bit of this flag. If set, read the requested number of elements, and
191          * leave those elements in the pipe. A later read will return the same data.
192          * Mutually exclusive with |discard| and |query|.
193          *
194          * @param peek the new value of the peek bit.
195          * @return this.
196          */
peek(boolean peek)197         public ReadFlags peek(boolean peek) {
198             return setFlag(FLAG_PEEK, peek);
199         }
200 
201         /**
202          * @return a flag with no bit set.
203          */
none()204         public static ReadFlags none() {
205             return new ReadFlags(FLAG_NONE);
206         }
207 
208     }
209 
210     /**
211      * Handle for the producer part of a data pipe.
212      */
213     public static interface ProducerHandle extends Handle {
214 
215         /**
216          * @see org.chromium.mojo.system.Handle#pass()
217          */
218         @Override
pass()219         public ProducerHandle pass();
220 
221         /**
222          * Writes the given data to the data pipe producer. |elements| points to data; the buffer
223          * must be a direct ByteBuffer and the limit should be a multiple of the data pipe's element
224          * size. If |allOrNone| is set in |flags|, either all the data will be written or none is.
225          * <p>
226          * On success, returns the amount of data that was actually written.
227          * <p>
228          * Note: If the data pipe has the "may discard" option flag (specified on creation), this
229          * will discard as much data as required to write the given data, starting with the earliest
230          * written data that has not been consumed. However, even with "may discard", if the buffer
231          * limit is greater than the data pipe's capacity (and |allOrNone| is not set), this will
232          * write the maximum amount possible (namely, the data pipe's capacity) and return that
233          * amount. It will *not* discard data from |elements|.
234          *
235          * @return number of written bytes.
236          */
writeData(ByteBuffer elements, WriteFlags flags)237         public ResultAnd<Integer> writeData(ByteBuffer elements, WriteFlags flags);
238 
239         /**
240          * Begins a two-phase write to the data pipe producer . On success, returns a |ByteBuffer|
241          * to which the caller can write. If flags has |allOrNone| set, then the buffer capacity
242          * will be at least as large as |numBytes|, which must also be a multiple of the element
243          * size (if |allOrNone| is not set, |numBytes| is ignored and the caller must check the
244          * capacity of the buffer).
245          * <p>
246          * During a two-phase write, this handle is *not* writable. E.g., if another thread tries to
247          * write to it, it will throw a |MojoException| with code |MojoResult.BUSY|; that thread can
248          * then wait for this handle to become writable again.
249          * <p>
250          * Once the caller has finished writing data to the buffer, it should call |endWriteData()|
251          * to specify the amount written and to complete the two-phase write.
252          * <p>
253          * Note: If the data pipe has the "may discard" option flag (specified on creation) and
254          * |flags| has |allOrNone| set, this may discard some data.
255          *
256          * @return The buffer to write to.
257          */
beginWriteData(int numBytes, WriteFlags flags)258         public ByteBuffer beginWriteData(int numBytes, WriteFlags flags);
259 
260         /**
261          * Ends a two-phase write to the data pipe producer that was begun by a call to
262          * |beginWriteData()| on the same handle. |numBytesWritten| should indicate the amount of
263          * data actually written; it must be less than or equal to the capacity of the buffer
264          * returned by |beginWriteData()| and must be a multiple of the element size. The buffer
265          * returned from |beginWriteData()| must have been filled with exactly |numBytesWritten|
266          * bytes of data.
267          * <p>
268          * On failure, the two-phase write (if any) is ended (so the handle may become writable
269          * again, if there's space available) but no data written to the buffer is "put into" the
270          * data pipe.
271          */
endWriteData(int numBytesWritten)272         public void endWriteData(int numBytesWritten);
273     }
274 
275     /**
276      * Handle for the consumer part of a data pipe.
277      */
278     public static interface ConsumerHandle extends Handle {
279         /**
280          * @see org.chromium.mojo.system.Handle#pass()
281          */
282         @Override
pass()283         public ConsumerHandle pass();
284 
285        /**
286          * Discards data on the data pie consumer. This method discards up to |numBytes| (which
287          * again be a multiple of the element size) bytes of data, returning the amount actually
288          * discarded. if |flags| has |allOrNone|, it will either discard exactly |numBytes| bytes of
289          * data or none. In this case, |query| must not be set.
290          */
discardData(int numBytes, ReadFlags flags)291         public int discardData(int numBytes, ReadFlags flags);
292 
293         /**
294          * Reads data from the data pipe consumer. May also be used to query the amount of data
295          * available. If |flags| has not |query| set, this tries to read up to |elements| capacity
296          * (which must be a multiple of the data pipe's element size) bytes of data to |elements|
297          * and returns the amount actually read. |elements| must be a direct ByteBuffer. If flags
298          * has |allOrNone| set, it will either read exactly |elements| capacity bytes of data or
299          * none.
300          * <p>
301          * If flags has |query| set, it queries the amount of data available, returning the number
302          * of bytes available. In this case |allOrNone| is ignored, as are |elements|.
303          */
readData(ByteBuffer elements, ReadFlags flags)304         public ResultAnd<Integer> readData(ByteBuffer elements, ReadFlags flags);
305 
306         /**
307          * Begins a two-phase read from the data pipe consumer. On success, returns a |ByteBuffer|
308          * from which the caller can read up to its limit bytes of data. If flags has |allOrNone|
309          * set, then the limit will be at least as large as |numBytes|, which must also be a
310          * multiple of the element size (if |allOrNone| is not set, |numBytes| is ignored). |flags|
311          * must not have |query| set.
312          * <p>
313          * During a two-phase read, this handle is *not* readable. E.g., if another thread tries to
314          * read from it, it will throw a |MojoException| with code |MojoResult.BUSY|; that thread
315          * can then wait for this handle to become readable again.
316          * <p>
317          * Once the caller has finished reading data from the buffer, it should call |endReadData()|
318          * to specify the amount read and to complete the two-phase read.
319          */
beginReadData(int numBytes, ReadFlags flags)320         public ByteBuffer beginReadData(int numBytes, ReadFlags flags);
321 
322         /**
323          * Ends a two-phase read from the data pipe consumer that was begun by a call to
324          * |beginReadData()| on the same handle. |numBytesRead| should indicate the amount of data
325          * actually read; it must be less than or equal to the limit of the buffer returned by
326          * |beginReadData()| and must be a multiple of the element size.
327          * <p>
328          * On failure, the two-phase read (if any) is ended (so the handle may become readable
329          * again) but no data is "removed" from the data pipe.
330          */
endReadData(int numBytesRead)331         public void endReadData(int numBytesRead);
332     }
333 
334 }
335