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 static List MojoCreateDataPipe( |
10 int element_bytes, int capacity_bytes, int flags) | 10 int elementBytes, int capacityBytes, int flags) |
11 native "MojoDataPipe_Create"; | 11 native "MojoDataPipe_Create"; |
12 | 12 |
13 static List MojoWriteData(int handle, ByteData data, int num_bytes, int flags) | 13 static List MojoWriteData(int handle, ByteData data, int numBytes, int flags) |
14 native "MojoDataPipe_WriteData"; | 14 native "MojoDataPipe_WriteData"; |
15 | 15 |
16 static List MojoBeginWriteData(int handle, int buffer_bytes, int flags) | 16 static List MojoBeginWriteData(int handle, int bufferBytes, int flags) |
17 native "MojoDataPipe_BeginWriteData"; | 17 native "MojoDataPipe_BeginWriteData"; |
18 | 18 |
19 static int MojoEndWriteData(int handle, int bytes_written) | 19 static int MojoEndWriteData(int handle, int bytesWritten) |
20 native "MojoDataPipe_EndWriteData"; | 20 native "MojoDataPipe_EndWriteData"; |
21 | 21 |
22 static List MojoReadData(int handle, ByteData data, int num_bytes, int flags) | 22 static List MojoReadData(int handle, ByteData data, int numBytes, int flags) |
23 native "MojoDataPipe_ReadData"; | 23 native "MojoDataPipe_ReadData"; |
24 | 24 |
25 static List MojoBeginReadData(int handle, int buffer_bytes, int flags) | 25 static List MojoBeginReadData(int handle, int bufferBytes, int flags) |
26 native "MojoDataPipe_BeginReadData"; | 26 native "MojoDataPipe_BeginReadData"; |
27 | 27 |
28 static int MojoEndReadData(int handle, int bytes_read) | 28 static int MojoEndReadData(int handle, int bytesRead) |
29 native "MojoDataPipe_EndReadData"; | 29 native "MojoDataPipe_EndReadData"; |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 class MojoDataPipeProducer { | 33 class MojoDataPipeProducer { |
34 static const int FLAG_NONE = 0; | 34 static const int FLAG_NONE = 0; |
35 static const int FLAG_ALL_OR_NONE = 1 << 0; | 35 static const int FLAG_ALL_OR_NONE = 1 << 0; |
36 | 36 |
37 MojoHandle handle; | 37 MojoHandle handle; |
38 MojoResult status; | 38 MojoResult status; |
39 final int element_bytes; | 39 final int elementBytes; |
40 | 40 |
41 MojoDataPipeProducer(this.handle, | 41 MojoDataPipeProducer( |
42 this.status, | 42 this.handle, [this.status = MojoResult.OK, this.elementBytes = 1]); |
43 this.element_bytes); | |
44 | 43 |
45 int write(ByteData data, [int num_bytes = -1, int flags = 0]) { | 44 int write(ByteData data, [int numBytes = -1, int flags = 0]) { |
46 if (handle == null) { | 45 if (handle == null) { |
47 status = MojoResult.INVALID_ARGUMENT; | 46 status = MojoResult.INVALID_ARGUMENT; |
48 return status; | 47 return status; |
49 } | 48 } |
50 | 49 |
51 int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes; | 50 int data_numBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; |
52 List result = _MojoDataPipeNatives.MojoWriteData( | 51 List result = _MojoDataPipeNatives.MojoWriteData( |
53 handle.h, data, data_num_bytes, flags); | 52 handle.h, data, data_numBytes, flags); |
54 if (result == null) { | 53 if (result == null) { |
55 status = MojoResult.INVALID_ARGUMENT; | 54 status = MojoResult.INVALID_ARGUMENT; |
56 return status; | 55 return status; |
57 } | 56 } |
58 | 57 |
59 assert((result is List) && (result.length == 2)); | 58 assert((result is List) && (result.length == 2)); |
60 status = new MojoResult(result[0]); | 59 status = new MojoResult(result[0]); |
61 return result[1]; | 60 return result[1]; |
62 } | 61 } |
63 | 62 |
64 ByteData beginWrite(int buffer_bytes, [int flags = 0]) { | 63 ByteData beginWrite(int bufferBytes, [int flags = 0]) { |
65 if (handle == null) { | 64 if (handle == null) { |
66 status = MojoResult.INVALID_ARGUMENT; | 65 status = MojoResult.INVALID_ARGUMENT; |
67 return null; | 66 return null; |
68 } | 67 } |
69 | 68 |
70 List result = _MojoDataPipeNatives.MojoBeginWriteData( | 69 List result = _MojoDataPipeNatives.MojoBeginWriteData( |
71 handle.h, buffer_bytes, flags); | 70 handle.h, bufferBytes, flags); |
72 if (result == null) { | 71 if (result == null) { |
73 status = MojoResult.INVALID_ARGUMENT; | 72 status = MojoResult.INVALID_ARGUMENT; |
74 return null; | 73 return null; |
75 } | 74 } |
76 | 75 |
77 assert((result is List) && (result.length == 2)); | 76 assert((result is List) && (result.length == 2)); |
78 status = new MojoResult(result[0]); | 77 status = new MojoResult(result[0]); |
79 return result[1]; | 78 return result[1]; |
80 } | 79 } |
81 | 80 |
82 MojoResult endWrite(int bytes_written) { | 81 MojoResult endWrite(int bytesWritten) { |
83 if (handle == null) { | 82 if (handle == null) { |
84 status = MojoResult.INVALID_ARGUMENT; | 83 status = MojoResult.INVALID_ARGUMENT; |
85 return status; | 84 return status; |
86 } | 85 } |
87 int result = _MojoDataPipeNatives.MojoEndWriteData(handle.h, bytes_written); | 86 int result = _MojoDataPipeNatives.MojoEndWriteData(handle.h, bytesWritten); |
88 status = new MojoResult(result); | 87 status = new MojoResult(result); |
89 return status; | 88 return status; |
90 } | 89 } |
91 } | 90 } |
92 | 91 |
93 | 92 |
94 class MojoDataPipeConsumer { | 93 class MojoDataPipeConsumer { |
95 static const int FLAG_NONE = 0; | 94 static const int FLAG_NONE = 0; |
96 static const int FLAG_ALL_OR_NONE = 1 << 0; | 95 static const int FLAG_ALL_OR_NONE = 1 << 0; |
97 static const int FLAG_MAY_DISCARD = 1 << 1; | 96 static const int FLAG_MAY_DISCARD = 1 << 1; |
98 static const int FLAG_QUERY = 1 << 2; | 97 static const int FLAG_QUERY = 1 << 2; |
99 static const int FLAG_PEEK = 1 << 3; | 98 static const int FLAG_PEEK = 1 << 3; |
100 | 99 |
101 MojoHandle handle; | 100 MojoHandle handle; |
102 MojoResult status; | 101 MojoResult status; |
103 final int element_bytes; | 102 final int elementBytes; |
104 | 103 |
105 MojoDataPipeConsumer( | 104 MojoDataPipeConsumer( |
106 this.handle, [this.status = MojoResult.OK, this.element_bytes = 1]); | 105 this.handle, [this.status = MojoResult.OK, this.elementBytes = 1]); |
107 | 106 |
108 int read(ByteData data, [int num_bytes = -1, int flags = 0]) { | 107 int read(ByteData data, [int numBytes = -1, int flags = 0]) { |
109 if (handle == null) { | 108 if (handle == null) { |
110 status = MojoResult.INVALID_ARGUMENT; | 109 status = MojoResult.INVALID_ARGUMENT; |
111 return status; | 110 return status; |
112 } | 111 } |
113 | 112 |
114 int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes; | 113 int data_numBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; |
115 List result = _MojoDataPipeNatives.MojoReadData( | 114 List result = _MojoDataPipeNatives.MojoReadData( |
116 handle.h, data, data_num_bytes, flags); | 115 handle.h, data, data_numBytes, flags); |
117 if (result == null) { | 116 if (result == null) { |
118 status = MojoResult.INVALID_ARGUMENT; | 117 status = MojoResult.INVALID_ARGUMENT; |
119 return status; | 118 return status; |
120 } | 119 } |
121 assert((result is List) && (result.length == 2)); | 120 assert((result is List) && (result.length == 2)); |
122 status = new MojoResult(result[0]); | 121 status = new MojoResult(result[0]); |
123 return result[1]; | 122 return result[1]; |
124 } | 123 } |
125 | 124 |
126 ByteData beginRead([int buffer_bytes = 0, int flags = 0]) { | 125 ByteData beginRead([int bufferBytes = 0, int flags = 0]) { |
127 if (handle == null) { | 126 if (handle == null) { |
128 status = MojoResult.INVALID_ARGUMENT; | 127 status = MojoResult.INVALID_ARGUMENT; |
129 return null; | 128 return null; |
130 } | 129 } |
131 | 130 |
132 List result = _MojoDataPipeNatives.MojoBeginReadData( | 131 List result = _MojoDataPipeNatives.MojoBeginReadData( |
133 handle.h, buffer_bytes, flags); | 132 handle.h, bufferBytes, flags); |
134 if (result == null) { | 133 if (result == null) { |
135 status = MojoResult.INVALID_ARGUMENT; | 134 status = MojoResult.INVALID_ARGUMENT; |
136 return null; | 135 return null; |
137 } | 136 } |
138 | 137 |
139 assert((result is List) && (result.length == 2)); | 138 assert((result is List) && (result.length == 2)); |
140 status = new MojoResult(result[0]); | 139 status = new MojoResult(result[0]); |
141 return result[1]; | 140 return result[1]; |
142 } | 141 } |
143 | 142 |
144 MojoResult endRead(int bytes_read) { | 143 MojoResult endRead(int bytesRead) { |
145 if (handle == null) { | 144 if (handle == null) { |
146 status = MojoResult.INVALID_ARGUMENT; | 145 status = MojoResult.INVALID_ARGUMENT; |
147 return status; | 146 return status; |
148 } | 147 } |
149 int result = _MojoDataPipeNatives.MojoEndReadData(handle.h, bytes_read); | 148 int result = _MojoDataPipeNatives.MojoEndReadData(handle.h, bytesRead); |
150 status = new MojoResult(result); | 149 status = new MojoResult(result); |
151 return status; | 150 return status; |
152 } | 151 } |
153 | 152 |
154 int query() => read(null, 0, FLAG_QUERY); | 153 int query() => read(null, 0, FLAG_QUERY); |
155 } | 154 } |
156 | 155 |
157 | 156 |
158 class MojoDataPipe { | 157 class MojoDataPipe { |
159 static const int FLAG_NONE = 0; | 158 static const int FLAG_NONE = 0; |
160 static const int FLAG_MAY_DISCARD = 1 << 0; | 159 static const int FLAG_MAY_DISCARD = 1 << 0; |
161 static const int DEFAULT_ELEMENT_SIZE = 1; | 160 static const int DEFAULT_ELEMENT_SIZE = 1; |
162 static const int DEFAULT_CAPACITY = 0; | 161 static const int DEFAULT_CAPACITY = 0; |
163 | 162 |
164 MojoDataPipeProducer producer; | 163 MojoDataPipeProducer producer; |
165 MojoDataPipeConsumer consumer; | 164 MojoDataPipeConsumer consumer; |
166 MojoResult status; | 165 MojoResult status; |
167 | 166 |
168 MojoDataPipe._internal() { | 167 MojoDataPipe._internal() { |
169 producer = null; | 168 producer = null; |
170 consumer = null; | 169 consumer = null; |
171 status = MojoResult.OK; | 170 status = MojoResult.OK; |
172 } | 171 } |
173 | 172 |
174 factory MojoDataPipe([int element_bytes = DEFAULT_ELEMENT_SIZE, | 173 factory MojoDataPipe([int elementBytes = DEFAULT_ELEMENT_SIZE, |
175 int capacity_bytes = DEFAULT_CAPACITY, | 174 int capacityBytes = DEFAULT_CAPACITY, |
176 int flags = FLAG_NONE]) { | 175 int flags = FLAG_NONE]) { |
177 List result = _MojoDataPipeNatives.MojoCreateDataPipe( | 176 List result = _MojoDataPipeNatives.MojoCreateDataPipe( |
178 element_bytes, capacity_bytes, flags); | 177 elementBytes, capacityBytes, flags); |
179 if (result == null) { | 178 if (result == null) { |
180 return null; | 179 return null; |
181 } | 180 } |
182 assert((result is List) && (result.length == 3)); | 181 assert((result is List) && (result.length == 3)); |
183 MojoHandle producer_handle = new MojoHandle(result[1]); | 182 MojoHandle producerHandle = new MojoHandle(result[1]); |
184 MojoHandle consumer_handle = new MojoHandle(result[2]); | 183 MojoHandle consumerHandle = new MojoHandle(result[2]); |
185 MojoDataPipe pipe = new MojoDataPipe._internal(); | 184 MojoDataPipe pipe = new MojoDataPipe._internal(); |
186 pipe.producer = new MojoDataPipeProducer( | 185 pipe.producer = new MojoDataPipeProducer( |
187 producer_handle, new MojoResult(result[0]), element_bytes); | 186 producerHandle, new MojoResult(result[0]), elementBytes); |
188 pipe.consumer = new MojoDataPipeConsumer( | 187 pipe.consumer = new MojoDataPipeConsumer( |
189 consumer_handle, new MojoResult(result[0]), element_bytes); | 188 consumerHandle, new MojoResult(result[0]), elementBytes); |
190 pipe.status = new MojoResult(result[0]); | 189 pipe.status = new MojoResult(result[0]); |
191 return pipe; | 190 return pipe; |
192 } | 191 } |
193 } | 192 } |
OLD | NEW |