OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 part of core; | 5 part of core; |
6 | 6 |
7 | 7 |
8 class _MojoDataPipeNatives { | 8 class _MojoDataPipeNatives { |
9 static List MojoCreateDataPipe( | 9 external static List MojoCreateDataPipe(int elementBytes, int capacityBytes, |
10 int elementBytes, int capacityBytes, int flags) | 10 int flags); |
11 native "MojoDataPipe_Create"; | |
12 | 11 |
13 static List MojoWriteData(int handle, ByteData data, int numBytes, int flags) | 12 external static List MojoWriteData(int handle, ByteData data, int numBytes, |
14 native "MojoDataPipe_WriteData"; | 13 int flags); |
15 | 14 |
16 static List MojoBeginWriteData(int handle, int bufferBytes, int flags) | 15 external static List MojoBeginWriteData(int handle, int bufferBytes, |
17 native "MojoDataPipe_BeginWriteData"; | 16 int flags); |
18 | 17 |
19 static int MojoEndWriteData(int handle, int bytesWritten) | 18 external static int MojoEndWriteData(int handle, int bytesWritten); |
20 native "MojoDataPipe_EndWriteData"; | |
21 | 19 |
22 static List MojoReadData(int handle, ByteData data, int numBytes, int flags) | 20 external static List MojoReadData(int handle, ByteData data, int numBytes, |
23 native "MojoDataPipe_ReadData"; | 21 int flags); |
24 | 22 |
25 static List MojoBeginReadData(int handle, int bufferBytes, int flags) | 23 external static List MojoBeginReadData(int handle, int bufferBytes, |
26 native "MojoDataPipe_BeginReadData"; | 24 int flags); |
27 | 25 |
28 static int MojoEndReadData(int handle, int bytesRead) | 26 external static int MojoEndReadData(int handle, int bytesRead); |
29 native "MojoDataPipe_EndReadData"; | |
30 } | 27 } |
31 | 28 |
32 | 29 |
33 class MojoDataPipeProducer { | 30 class MojoDataPipeProducer { |
34 static const int FLAG_NONE = 0; | 31 static const int FLAG_NONE = 0; |
35 static const int FLAG_ALL_OR_NONE = 1 << 0; | 32 static const int FLAG_ALL_OR_NONE = 1 << 0; |
36 | 33 |
37 MojoHandle handle; | 34 MojoHandle handle; |
38 MojoResult status; | 35 MojoResult status; |
39 final int elementBytes; | 36 final int elementBytes; |
40 | 37 |
41 MojoDataPipeProducer( | 38 MojoDataPipeProducer(this.handle, [this.status = MojoResult.OK, |
42 this.handle, [this.status = MojoResult.OK, this.elementBytes = 1]); | 39 this.elementBytes = 1]); |
43 | 40 |
44 int write(ByteData data, [int numBytes = -1, int flags = 0]) { | 41 int write(ByteData data, [int numBytes = -1, int flags = 0]) { |
45 if (handle == null) { | 42 if (handle == null) { |
46 status = MojoResult.INVALID_ARGUMENT; | 43 status = MojoResult.INVALID_ARGUMENT; |
47 return status; | 44 return status; |
48 } | 45 } |
49 | 46 |
50 int data_numBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; | 47 int data_numBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; |
51 List result = _MojoDataPipeNatives.MojoWriteData( | 48 List result = |
52 handle.h, data, data_numBytes, flags); | 49 _MojoDataPipeNatives.MojoWriteData(handle.h, data, data_numBytes, flags)
; |
53 if (result == null) { | 50 if (result == null) { |
54 status = MojoResult.INVALID_ARGUMENT; | 51 status = MojoResult.INVALID_ARGUMENT; |
55 return status; | 52 return status; |
56 } | 53 } |
57 | 54 |
58 assert((result is List) && (result.length == 2)); | 55 assert((result is List) && (result.length == 2)); |
59 status = new MojoResult(result[0]); | 56 status = new MojoResult(result[0]); |
60 return result[1]; | 57 return result[1]; |
61 } | 58 } |
62 | 59 |
63 ByteData beginWrite(int bufferBytes, [int flags = 0]) { | 60 ByteData beginWrite(int bufferBytes, [int flags = 0]) { |
64 if (handle == null) { | 61 if (handle == null) { |
65 status = MojoResult.INVALID_ARGUMENT; | 62 status = MojoResult.INVALID_ARGUMENT; |
66 return null; | 63 return null; |
67 } | 64 } |
68 | 65 |
69 List result = _MojoDataPipeNatives.MojoBeginWriteData( | 66 List result = |
70 handle.h, bufferBytes, flags); | 67 _MojoDataPipeNatives.MojoBeginWriteData(handle.h, bufferBytes, flags); |
71 if (result == null) { | 68 if (result == null) { |
72 status = MojoResult.INVALID_ARGUMENT; | 69 status = MojoResult.INVALID_ARGUMENT; |
73 return null; | 70 return null; |
74 } | 71 } |
75 | 72 |
76 assert((result is List) && (result.length == 2)); | 73 assert((result is List) && (result.length == 2)); |
77 status = new MojoResult(result[0]); | 74 status = new MojoResult(result[0]); |
78 return result[1]; | 75 return result[1]; |
79 } | 76 } |
80 | 77 |
(...skipping 13 matching lines...) Expand all Loading... |
94 static const int FLAG_NONE = 0; | 91 static const int FLAG_NONE = 0; |
95 static const int FLAG_ALL_OR_NONE = 1 << 0; | 92 static const int FLAG_ALL_OR_NONE = 1 << 0; |
96 static const int FLAG_DISCARD = 1 << 1; | 93 static const int FLAG_DISCARD = 1 << 1; |
97 static const int FLAG_QUERY = 1 << 2; | 94 static const int FLAG_QUERY = 1 << 2; |
98 static const int FLAG_PEEK = 1 << 3; | 95 static const int FLAG_PEEK = 1 << 3; |
99 | 96 |
100 MojoHandle handle; | 97 MojoHandle handle; |
101 MojoResult status; | 98 MojoResult status; |
102 final int elementBytes; | 99 final int elementBytes; |
103 | 100 |
104 MojoDataPipeConsumer( | 101 MojoDataPipeConsumer(this.handle, [this.status = MojoResult.OK, |
105 this.handle, [this.status = MojoResult.OK, this.elementBytes = 1]); | 102 this.elementBytes = 1]); |
106 | 103 |
107 int read(ByteData data, [int numBytes = -1, int flags = 0]) { | 104 int read(ByteData data, [int numBytes = -1, int flags = 0]) { |
108 if (handle == null) { | 105 if (handle == null) { |
109 status = MojoResult.INVALID_ARGUMENT; | 106 status = MojoResult.INVALID_ARGUMENT; |
110 return 0; | 107 return 0; |
111 } | 108 } |
112 | 109 |
113 int data_numBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; | 110 int data_numBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; |
114 List result = _MojoDataPipeNatives.MojoReadData( | 111 List result = |
115 handle.h, data, data_numBytes, flags); | 112 _MojoDataPipeNatives.MojoReadData(handle.h, data, data_numBytes, flags); |
116 if (result == null) { | 113 if (result == null) { |
117 status = MojoResult.INVALID_ARGUMENT; | 114 status = MojoResult.INVALID_ARGUMENT; |
118 return 0; | 115 return 0; |
119 } | 116 } |
120 assert((result is List) && (result.length == 2)); | 117 assert((result is List) && (result.length == 2)); |
121 status = new MojoResult(result[0]); | 118 status = new MojoResult(result[0]); |
122 return result[1]; | 119 return result[1]; |
123 } | 120 } |
124 | 121 |
125 ByteData beginRead([int bufferBytes = 0, int flags = 0]) { | 122 ByteData beginRead([int bufferBytes = 0, int flags = 0]) { |
126 if (handle == null) { | 123 if (handle == null) { |
127 status = MojoResult.INVALID_ARGUMENT; | 124 status = MojoResult.INVALID_ARGUMENT; |
128 return null; | 125 return null; |
129 } | 126 } |
130 | 127 |
131 List result = _MojoDataPipeNatives.MojoBeginReadData( | 128 List result = |
132 handle.h, bufferBytes, flags); | 129 _MojoDataPipeNatives.MojoBeginReadData(handle.h, bufferBytes, flags); |
133 if (result == null) { | 130 if (result == null) { |
134 status = MojoResult.INVALID_ARGUMENT; | 131 status = MojoResult.INVALID_ARGUMENT; |
135 return null; | 132 return null; |
136 } | 133 } |
137 | 134 |
138 assert((result is List) && (result.length == 2)); | 135 assert((result is List) && (result.length == 2)); |
139 status = new MojoResult(result[0]); | 136 status = new MojoResult(result[0]); |
140 return result[1]; | 137 return result[1]; |
141 } | 138 } |
142 | 139 |
(...skipping 20 matching lines...) Expand all Loading... |
163 MojoDataPipeConsumer consumer; | 160 MojoDataPipeConsumer consumer; |
164 MojoResult status; | 161 MojoResult status; |
165 | 162 |
166 MojoDataPipe._internal() { | 163 MojoDataPipe._internal() { |
167 producer = null; | 164 producer = null; |
168 consumer = null; | 165 consumer = null; |
169 status = MojoResult.OK; | 166 status = MojoResult.OK; |
170 } | 167 } |
171 | 168 |
172 factory MojoDataPipe([int elementBytes = DEFAULT_ELEMENT_SIZE, | 169 factory MojoDataPipe([int elementBytes = DEFAULT_ELEMENT_SIZE, |
173 int capacityBytes = DEFAULT_CAPACITY, | 170 int capacityBytes = DEFAULT_CAPACITY, int flags = FLAG_NONE]) { |
174 int flags = FLAG_NONE]) { | 171 List result = |
175 List result = _MojoDataPipeNatives.MojoCreateDataPipe( | 172 _MojoDataPipeNatives.MojoCreateDataPipe(elementBytes, capacityBytes, fla
gs); |
176 elementBytes, capacityBytes, flags); | |
177 if (result == null) { | 173 if (result == null) { |
178 return null; | 174 return null; |
179 } | 175 } |
180 assert((result is List) && (result.length == 3)); | 176 assert((result is List) && (result.length == 3)); |
181 MojoHandle producerHandle = new MojoHandle(result[1]); | 177 MojoHandle producerHandle = new MojoHandle(result[1]); |
182 MojoHandle consumerHandle = new MojoHandle(result[2]); | 178 MojoHandle consumerHandle = new MojoHandle(result[2]); |
183 MojoDataPipe pipe = new MojoDataPipe._internal(); | 179 MojoDataPipe pipe = new MojoDataPipe._internal(); |
184 pipe.producer = new MojoDataPipeProducer( | 180 pipe.producer = new MojoDataPipeProducer( |
185 producerHandle, new MojoResult(result[0]), elementBytes); | 181 producerHandle, |
| 182 new MojoResult(result[0]), |
| 183 elementBytes); |
186 pipe.consumer = new MojoDataPipeConsumer( | 184 pipe.consumer = new MojoDataPipeConsumer( |
187 consumerHandle, new MojoResult(result[0]), elementBytes); | 185 consumerHandle, |
| 186 new MojoResult(result[0]), |
| 187 elementBytes); |
188 pipe.status = new MojoResult(result[0]); | 188 pipe.status = new MojoResult(result[0]); |
189 return pipe; | 189 return pipe; |
190 } | 190 } |
191 } | 191 } |
OLD | NEW |