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.
8 class _MojoDataPipeNatives {
9 static List MojoCreateDataPipe(
10 int element_bytes, int capacity_bytes, int flags)
11 native "MojoDataPipe_Create";
13 static List MojoWriteData(int handle, ByteData data, int num_bytes, int flags)
14 native "MojoDataPipe_WriteData";
16 static List MojoBeginWriteData(int handle, int buffer_bytes, int flags)
17 native "MojoDataPipe_BeginWriteData";
19 static int MojoEndWriteData(int handle, int bytes_written)
20 native "MojoDataPipe_EndWriteData";
22 static List MojoReadData(int handle, ByteData data, int num_bytes, int flags)
23 native "MojoDataPipe_ReadData";
25 static List MojoBeginReadData(int handle, int buffer_bytes, int flags)
26 native "MojoDataPipe_BeginReadData";
28 static int MojoEndReadData(int handle, int bytes_read)
29 native "MojoDataPipe_EndReadData";
33 class MojoDataPipeProducer {
34 static const int FLAG_NONE = 0;
35 static const int FLAG_ALL_OR_NONE = 1 << 0;
39 final int element_bytes;
41 MojoDataPipeProducer(this.handle,
45 int write(ByteData data, [int num_bytes = -1, int flags = 0]) {
47 status = MojoResult.INVALID_ARGUMENT;
51 int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes;
52 List result = _MojoDataPipeNatives.MojoWriteData(
53 handle.h, data, data_num_bytes, flags);
55 status = MojoResult.INVALID_ARGUMENT;
59 assert((result is List) && (result.length == 2));
60 status = new MojoResult(result[0]);
64 ByteData beginWrite(int buffer_bytes, [int flags = 0]) {
66 status = MojoResult.INVALID_ARGUMENT;
70 List result = _MojoDataPipeNatives.MojoBeginWriteData(
71 handle.h, buffer_bytes, flags);
73 status = MojoResult.INVALID_ARGUMENT;
77 assert((result is List) && (result.length == 2));
78 status = new MojoResult(result[0]);
82 MojoResult endWrite(int bytes_written) {
84 status = MojoResult.INVALID_ARGUMENT;
87 int result = _MojoDataPipeNatives.MojoEndWriteData(handle.h, bytes_written);
88 status = new MojoResult(result);
94 class MojoDataPipeConsumer {
95 static const int FLAG_NONE = 0;
96 static const int FLAG_ALL_OR_NONE = 1 << 0;
97 static const int FLAG_MAY_DISCARD = 1 << 1;
98 static const int FLAG_QUERY = 1 << 2;
100 RawMojoHandle handle;
102 final int element_bytes;
104 MojoDataPipeConsumer(this.handle,
108 int read(ByteData data, [int num_bytes = -1, int flags = 0]) {
109 if (handle == null) {
110 status = MojoResult.INVALID_ARGUMENT;
114 int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes;
115 List result = _MojoDataPipeNatives.MojoReadData(
116 handle.h, data, data_num_bytes, flags);
117 if (result == null) {
118 status = MojoResult.INVALID_ARGUMENT;
121 assert((result is List) && (result.length == 2));
122 status = new MojoResult(result[0]);
126 ByteData beginRead(int buffer_bytes, [int flags = 0]) {
127 if (handle == null) {
128 status = MojoResult.INVALID_ARGUMENT;
132 List result = _MojoDataPipeNatives.MojoBeginReadData(
133 handle.h, buffer_bytes, flags);
134 if (result == null) {
135 status = MojoResult.INVALID_ARGUMENT;
139 assert((result is List) && (result.length == 2));
140 status = new MojoResult(result[0]);
144 MojoResult endRead(int bytes_read) {
145 if (handle == null) {
146 status = MojoResult.INVALID_ARGUMENT;
149 int result = _MojoDataPipeNatives.MojoEndReadData(handle.h, bytes_read);
150 status = new MojoResult(result);
157 static const int FLAG_NONE = 0;
158 static const int FLAG_MAY_DISCARD = 1 << 0;
159 static const int DEFAULT_ELEMENT_SIZE = 1;
160 static const int DEFAULT_CAPACITY = 0;
162 MojoDataPipeProducer producer;
163 MojoDataPipeConsumer consumer;
166 MojoDataPipe._internal() {
169 status = MojoResult.OK;
172 factory MojoDataPipe([int element_bytes = DEFAULT_ELEMENT_SIZE,
173 int capacity_bytes = DEFAULT_CAPACITY,
174 int flags = FLAG_NONE]) {
175 List result = _MojoDataPipeNatives.MojoCreateDataPipe(
176 element_bytes, capacity_bytes, flags);
177 if (result == null) {
180 assert((result is List) && (result.length == 3));
181 RawMojoHandle producer_handle = new RawMojoHandle(result[1]);
182 RawMojoHandle consumer_handle = new RawMojoHandle(result[2]);
183 MojoDataPipe pipe = new MojoDataPipe._internal();
184 pipe.producer = new MojoDataPipeProducer(
185 producer_handle, new MojoResult(result[0]), element_bytes);
186 pipe.consumer = new MojoDataPipeConsumer(
187 consumer_handle, new MojoResult(result[0]), element_bytes);
188 pipe.status = new MojoResult(result[0]);