Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / mojo / public / dart / src / data_pipe.dart
blob665285de4a953d5b8a3287300240e5ebe31e13ce
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 part of core;
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;
37   RawMojoHandle handle;
38   MojoResult status;
39   final int element_bytes;
41   MojoDataPipeProducer(this.handle,
42                        this.status,
43                        this.element_bytes);
45   int write(ByteData data, [int num_bytes = -1, int flags = 0]) {
46     if (handle == null) {
47       status = MojoResult.INVALID_ARGUMENT;
48       return status;
49     }
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);
54     if (result == null) {
55       status = MojoResult.INVALID_ARGUMENT;
56       return status;
57     }
59     assert((result is List) && (result.length == 2));
60     status = new MojoResult(result[0]);
61     return result[1];
62   }
64   ByteData beginWrite(int buffer_bytes, [int flags = 0]) {
65     if (handle == null) {
66       status = MojoResult.INVALID_ARGUMENT;
67       return null;
68     }
70     List result = _MojoDataPipeNatives.MojoBeginWriteData(
71         handle.h, buffer_bytes, flags);
72     if (result == null) {
73       status = MojoResult.INVALID_ARGUMENT;
74       return null;
75     }
77     assert((result is List) && (result.length == 2));
78     status = new MojoResult(result[0]);
79     return result[1];
80   }
82   MojoResult endWrite(int bytes_written) {
83     if (handle == null) {
84       status = MojoResult.INVALID_ARGUMENT;
85       return status;
86     }
87     int result = _MojoDataPipeNatives.MojoEndWriteData(handle.h, bytes_written);
88     status = new MojoResult(result);
89     return status;
90   }
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;
101   MojoResult status;
102   final int element_bytes;
104   MojoDataPipeConsumer(this.handle,
105                        this.status,
106                        this.element_bytes);
108   int read(ByteData data, [int num_bytes = -1, int flags = 0]) {
109     if (handle == null) {
110       status = MojoResult.INVALID_ARGUMENT;
111       return status;
112     }
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;
119       return status;
120     }
121     assert((result is List) && (result.length == 2));
122     status = new MojoResult(result[0]);
123     return result[1];
124   }
126   ByteData beginRead(int buffer_bytes, [int flags = 0]) {
127     if (handle == null) {
128       status = MojoResult.INVALID_ARGUMENT;
129       return null;
130     }
132     List result = _MojoDataPipeNatives.MojoBeginReadData(
133         handle.h, buffer_bytes, flags);
134     if (result == null) {
135       status = MojoResult.INVALID_ARGUMENT;
136       return null;
137     }
139     assert((result is List) && (result.length == 2));
140     status = new MojoResult(result[0]);
141     return result[1];
142   }
144   MojoResult endRead(int bytes_read) {
145     if (handle == null) {
146       status = MojoResult.INVALID_ARGUMENT;
147       return status;
148     }
149     int result = _MojoDataPipeNatives.MojoEndReadData(handle.h, bytes_read);
150     status = new MojoResult(result);
151     return status;
152   }
156 class MojoDataPipe {
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;
164   MojoResult status;
166   MojoDataPipe._internal() {
167     producer = null;
168     consumer = null;
169     status = MojoResult.OK;
170   }
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) {
178       return null;
179     }
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]);
189     return pipe;
190   }