| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 internal; | 5 part of internal; |
| 6 | 6 |
| 7 // Data associated with an open handle. | 7 // Data associated with an open handle. |
| 8 class _OpenHandle { | 8 class _OpenHandle { |
| 9 final StackTrace stack; | 9 final StackTrace stack; |
| 10 String description; | 10 String description; |
| 11 _OpenHandle(this.stack, {this.description}); | 11 _OpenHandle(this.stack, {this.description}); |
| 12 } | 12 } |
| 13 | 13 |
| 14 class MojoCoreNatives { | 14 class MojoCoreNatives { |
| 15 /// Returns the time, in microseconds, since some undefined point in the past. | |
| 16 /// | |
| 17 /// The values are only meaningful relative to other values that were obtained | |
| 18 /// from the same device without an intervening system restart. Such values | |
| 19 /// are guaranteed to be monotonically non-decreasing with the passage of real | |
| 20 /// time. | |
| 21 /// | |
| 22 /// Although the units are microseconds, the resolution of the clock may vary | |
| 23 /// and is typically in the range of ~1-15 ms. | |
| 24 static int getTimeTicksNow() native "Mojo_GetTimeTicksNow"; | 15 static int getTimeTicksNow() native "Mojo_GetTimeTicksNow"; |
| 25 | 16 |
| 26 /// Returns the time, in milliseconds, since some undefined point in the past. | |
| 27 /// | |
| 28 /// This method is equivalent to `getTimeTicksNow() ~/ 1000`. | |
| 29 static int timerMillisecondClock() => getTimeTicksNow() ~/ 1000; | 17 static int timerMillisecondClock() => getTimeTicksNow() ~/ 1000; |
| 30 } | 18 } |
| 31 | 19 |
| 32 class MojoHandleNatives { | 20 class MojoHandleNatives { |
| 33 static HashMap<int, _OpenHandle> _openHandles = new HashMap(); | 21 static HashMap<int, _OpenHandle> _openHandles = new HashMap(); |
| 34 | 22 |
| 35 /// Puts the given [handleToken] with the given [description] into the set of | |
| 36 /// open handles. | |
| 37 /// | |
| 38 /// The [handleToken] is a token that identifies the Mojo handle. | |
| 39 /// | |
| 40 /// This method is only used to report open handles (see [reportOpenHandles]). | |
| 41 static void addOpenHandle(int handleToken, {String description}) { | 23 static void addOpenHandle(int handleToken, {String description}) { |
| 42 var stack; | 24 var stack; |
| 43 // We only remember a stack trace when in checked mode. | 25 // We only remember a stack trace when in checked mode. |
| 44 assert((stack = StackTrace.current) != null); | 26 assert((stack = StackTrace.current) != null); |
| 45 var openHandle = new _OpenHandle(stack, description: description); | 27 var openHandle = new _OpenHandle(stack, description: description); |
| 46 _openHandles[handleToken] = openHandle; | 28 _openHandles[handleToken] = openHandle; |
| 47 } | 29 } |
| 48 | 30 |
| 49 /// Removes the given [handleToken] from the set of open handles. | |
| 50 /// | |
| 51 /// The [handleToken] is a token that identifies the Mojo handle. | |
| 52 /// | |
| 53 /// This method is only used to report open handles (see [reportOpenHandles]). | |
| 54 /// | |
| 55 /// Handles are removed from the set when they are closed, but also, when they | |
| 56 /// are serialized in the mojo encoder [codec.dart]. | |
| 57 static void removeOpenHandle(int handleToken) { | 31 static void removeOpenHandle(int handleToken) { |
| 58 _openHandles.remove(handleToken); | 32 _openHandles.remove(handleToken); |
| 59 } | 33 } |
| 60 | 34 |
| 61 static void _reportOpenHandle(int handle, _OpenHandle openHandle) { | 35 static void _reportOpenHandle(int handle, _OpenHandle openHandle) { |
| 62 StringBuffer sb = new StringBuffer(); | 36 StringBuffer sb = new StringBuffer(); |
| 63 sb.writeln('HANDLE LEAK: handle: $handle'); | 37 sb.writeln('HANDLE LEAK: handle: $handle'); |
| 64 if (openHandle.description != null) { | 38 if (openHandle.description != null) { |
| 65 sb.writeln('HANDLE LEAK: description: ${openHandle.description}'); | 39 sb.writeln('HANDLE LEAK: description: ${openHandle.description}'); |
| 66 } | 40 } |
| 67 if (openHandle.stack != null) { | 41 if (openHandle.stack != null) { |
| 68 sb.writeln('HANDLE LEAK: creation stack trace:'); | 42 sb.writeln('HANDLE LEAK: creation stack trace:'); |
| 69 sb.writeln(openHandle.stack); | 43 sb.writeln(openHandle.stack); |
| 70 } else { | 44 } else { |
| 71 sb.writeln('HANDLE LEAK: creation stack trace available in strict mode.'); | 45 sb.writeln('HANDLE LEAK: creation stack trace available in strict mode.'); |
| 72 } | 46 } |
| 73 print(sb.toString()); | 47 print(sb.toString()); |
| 74 } | 48 } |
| 75 | 49 |
| 76 /// Prints a list of all open handles. | |
| 77 /// | |
| 78 /// Returns `true` if there are no open handles. | |
| 79 /// | |
| 80 /// Prints all handles that have been added with [addOpenHandle] but haven't | |
| 81 /// been removed with [removeOpenHandle]. | |
| 82 /// | |
| 83 /// Programs should not have open handles when the program terminates. | |
| 84 static bool reportOpenHandles() { | 50 static bool reportOpenHandles() { |
| 85 if (_openHandles.length == 0) { | 51 if (_openHandles.length == 0) { |
| 86 return true; | 52 return true; |
| 87 } | 53 } |
| 88 _openHandles.forEach(_reportOpenHandle); | 54 _openHandles.forEach(_reportOpenHandle); |
| 89 return false; | 55 return false; |
| 90 } | 56 } |
| 91 | 57 |
| 92 /// Updates the description of the given [handleToken] in the set of open | |
| 93 /// handles. | |
| 94 /// | |
| 95 /// The [handleToken] is a token that identifies the Mojo handle. | |
| 96 /// | |
| 97 /// Does nothing, if the [handleToken] isn't in the set. | |
| 98 static bool setDescription(int handleToken, String description) { | 58 static bool setDescription(int handleToken, String description) { |
| 99 _OpenHandle openHandle = _openHandles[handleToken]; | 59 _OpenHandle openHandle = _openHandles[handleToken]; |
| 100 if (openHandle != null) { | 60 if (openHandle != null) { |
| 101 openHandle.description = description; | 61 openHandle.description = description; |
| 102 } | 62 } |
| 103 return true; | 63 return true; |
| 104 } | 64 } |
| 105 | 65 |
| 106 /// Registers a finalizer on [eventSubscription] to close the given | |
| 107 /// [handleToken]. | |
| 108 /// | |
| 109 /// Returns an integer, encoding the result as specified in the [MojoResult] | |
| 110 /// class. In particular, a successful operation returns [MojoResult.kOk]. | |
| 111 /// | |
| 112 /// When [eventSubscription] (currently an Instance of the | |
| 113 /// [MojoEventSubscription] class) is garbage-collected, invokes [close] on | |
| 114 /// the [handleToken]. | |
| 115 /// | |
| 116 /// The [handleToken] is a token that identifies the Mojo handle. | |
| 117 /// Since the token can be an integer, it's not possible to install the | |
| 118 /// finalizer directly on the token. | |
| 119 static int registerFinalizer(Object eventSubscription, int handleToken) | 66 static int registerFinalizer(Object eventSubscription, int handleToken) |
| 120 native "MojoHandle_RegisterFinalizer"; | 67 native "MojoHandle_RegisterFinalizer"; |
| 121 | 68 |
| 122 /// Closes the given [handleToken]. | |
| 123 /// | |
| 124 /// Returns an integer, encoding the result as specified in the [MojoResult] | |
| 125 /// class. In particular, a successful operation returns [MojoResult.kOk]. | |
| 126 /// | |
| 127 /// The [handleToken] is a token that identifies the Mojo handle. | |
| 128 static int close(int handleToken) native "MojoHandle_Close"; | 69 static int close(int handleToken) native "MojoHandle_Close"; |
| 129 | 70 |
| 130 /// Waits on the given [handleToken] for a signal. | |
| 131 /// | |
| 132 /// Returns a list of two elements. The first entry is an integer, encoding | |
| 133 /// if the operation was a success or not, as specified in the [MojoResult] | |
| 134 /// class. In particular, a successful operation is signaled by | |
| 135 /// [MojoResult.kOk]. The second entry is itself a list of 2 elements: | |
| 136 /// an integer of satisfied signals, and an integer of satisfiable signals. | |
| 137 /// Both entries are encoded as specified in [MojoHandleSignals]. | |
| 138 /// | |
| 139 /// A signal is satisfiable, if the signal may become true in the future. | |
| 140 /// | |
| 141 /// The [deadline] specifies how long the call should wait (if no signal is | |
| 142 /// triggered). If the deadline passes, the returned result-integer is | |
| 143 /// [MojoResult.kDeadlineExceeded]. If the deadline is 0, then the result | |
| 144 /// is only [MojoResult.kDeadlineExceeded] if no other termination condition | |
| 145 /// is already satisfied (see below). | |
| 146 /// | |
| 147 /// The [signals] integer encodes the signals this method should wait for. | |
| 148 /// The integer is encoded as specified in [MojoHandleSignals]. | |
| 149 /// | |
| 150 /// Waits on the given handle until one of the following happens: | |
| 151 /// - A signal indicated by [signals] is satisfied. | |
| 152 /// - It becomes known that no signal indicated by [signals] will ever be | |
| 153 /// satisfied (for example the handle has been closed on the other side). | |
| 154 /// - The [deadline] has passed. | |
| 155 static List wait(int handleToken, int signals, int deadline) | 71 static List wait(int handleToken, int signals, int deadline) |
| 156 native "MojoHandle_Wait"; | 72 native "MojoHandle_Wait"; |
| 157 | 73 |
| 158 /// Waits on many handles at the same time. | |
| 159 /// | |
| 160 /// Returns a list with exactly 3 elements: | |
| 161 /// - the result integer, encoded as specified in [MojoResult]. In particular, | |
| 162 /// [MojoResult.kOk] signals success. | |
| 163 /// - the index of the handle that caused the return. May be `null` if the | |
| 164 /// operation didn't succeed. | |
| 165 /// - a list of signal states. May be `null` if the operation didn't succeed. | |
| 166 /// Each signal state is represented by a list of 2 elements: an integer of | |
| 167 /// satisfied signals, and an integer of satisfiable signals (see [wait]). | |
| 168 /// | |
| 169 /// Behaves as if [wait] was called on each of the [handleTokens] separately, | |
| 170 /// completing when the first would complete. | |
| 171 static List waitMany(List<int> handleTokens, List<int> signals, int deadline) | 74 static List waitMany(List<int> handleTokens, List<int> signals, int deadline) |
| 172 native "MojoHandle_WaitMany"; | 75 native "MojoHandle_WaitMany"; |
| 173 | 76 |
| 174 // Called from the embedder's unhandled exception callback. | 77 // Called from the embedder's unhandled exception callback. |
| 175 // Returns the number of successfully closed handles. | 78 // Returns the number of successfully closed handles. |
| 176 static int _closeOpenHandles() { | 79 static int _closeOpenHandles() { |
| 177 int count = 0; | 80 int count = 0; |
| 178 _openHandles.forEach((int handle, _) { | 81 _openHandles.forEach((int handle, _) { |
| 179 if (MojoHandleNatives.close(handle) == 0) { | 82 if (MojoHandleNatives.close(handle) == 0) { |
| 180 count++; | 83 count++; |
| 181 } | 84 } |
| 182 }); | 85 }); |
| 183 _openHandles.clear(); | 86 _openHandles.clear(); |
| 184 return count; | 87 return count; |
| 185 } | 88 } |
| 186 } | 89 } |
| 187 | 90 |
| 188 class _MojoHandleWatcherNatives { | 91 class _MojoHandleWatcherNatives { |
| 189 static int sendControlData( | 92 static int sendControlData( |
| 190 int controlHandle, | 93 int controlHandle, |
| 191 int commandCode, | 94 int commandCode, |
| 192 int handleOrDeadline, | 95 int handleOrDeadline, |
| 193 SendPort port, | 96 SendPort port, |
| 194 int data) native "MojoHandleWatcher_SendControlData"; | 97 int data) native "MojoHandleWatcher_SendControlData"; |
| 195 } | 98 } |
| 196 | 99 |
| 197 class MojoMessagePipeNatives { | 100 class MojoMessagePipeNatives { |
| 198 /// Creates a message pipe represented by its two endpoints (handles). | |
| 199 /// | |
| 200 /// Returns a list with exactly 3 elements: | |
| 201 /// - the result integer, encoded as specified in [MojoResult]. In particular, | |
| 202 /// [MojoResult.kOk] signals a successful creation. | |
| 203 /// - the two endpoints of the message pipe. These tokens can be used in the | |
| 204 /// methods of [MojoHandleNatives]. | |
| 205 /// | |
| 206 /// The parameter [flags] is reserved for future use and should currently be | |
| 207 /// set to [MojoMessagePipe.FLAG_NONE] (equal to 0). | |
| 208 static List MojoCreateMessagePipe(int flags) native "MojoMessagePipe_Create"; | 101 static List MojoCreateMessagePipe(int flags) native "MojoMessagePipe_Create"; |
| 209 | 102 |
| 210 /// Writes a message into the endpoint [handleToken]. | |
| 211 /// | |
| 212 /// Returns a result integer, encoded as specified in [MojoResult]. In | |
| 213 /// particular, [MojoResult.kOk] signals a successful write. | |
| 214 /// | |
| 215 /// The [handleToken] is a token that identifies the Mojo handle. | |
| 216 /// | |
| 217 /// A message is composed of [numBytes] bytes of [data], and a list of | |
| 218 /// [handleTokens]. | |
| 219 /// | |
| 220 /// The parameter [flags] is reserved for future use and should currently be | |
| 221 /// set to [MojoMessagePipeEndpoint.WRITE_FLAG_NONE] (equal to 0). | |
| 222 static int MojoWriteMessage(int handleToken, ByteData data, int numBytes, | 103 static int MojoWriteMessage(int handleToken, ByteData data, int numBytes, |
| 223 List<int> handles, int flags) native "MojoMessagePipe_Write"; | 104 List<int> handles, int flags) native "MojoMessagePipe_Write"; |
| 224 | 105 |
| 225 /// Reads a message from the endpoint [handleToken]. | |
| 226 /// | |
| 227 /// Returns `null` if the parameters are invalid. Otherwise returns a list of | |
| 228 /// exactly 3 elements: | |
| 229 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 230 /// particular, [MojoResult.kOk] signals a successful read. | |
| 231 /// 2. the number of bytes read (or bytes available if the message couldn't | |
| 232 /// be read). | |
| 233 /// 3. the number of handles read (or handles available if the message | |
| 234 /// couldn't be read). | |
| 235 /// | |
| 236 /// If no message is available, the result-integer is set to | |
| 237 /// [MojoResult.kShouldWait]. | |
| 238 /// | |
| 239 /// The [handleToken] is a token that identifies the Mojo handle. | |
| 240 /// | |
| 241 /// Both [data], and [handleTokens] may be null. If [data] is null, then | |
| 242 /// [numBytes] must be 0. | |
| 243 /// | |
| 244 /// A message is always read in its entirety. That is, if a message doesn't | |
| 245 /// fit into [data] and/or [handleTokens], then the message is left in the | |
| 246 /// pipe or discarded (see the description of [flags] below). | |
| 247 /// | |
| 248 /// If the message wasn't read because [data] or [handleTokens] was too small, | |
| 249 /// the result integer is set to [MojoResult.kResourceExhausted]. | |
| 250 /// | |
| 251 /// The returned list *always* contains the size of the message (independent | |
| 252 /// if it was actually read into [data] and [handleTokens]). | |
| 253 /// A common pattern thus consists of invoking this method with | |
| 254 /// [data] and [handleTokens] set to `null` to query the size of the next | |
| 255 /// message that is in the pipe. | |
| 256 /// | |
| 257 /// The parameter [flags] may set to either | |
| 258 /// [MojoMessagePipeEndpoint.READ_FLAG_NONE] (equal to 0) or | |
| 259 /// [MojoMessagePipeEndpoint.READ_FLAG_MAY_DISCARD] (equal to 1). In the | |
| 260 /// latter case messages that couldn't be read (for example, because the | |
| 261 /// [data] or [handleTokens] wasn't big enough) are discarded. | |
| 262 static List MojoReadMessage(int handleToken, ByteData data, int numBytes, | 106 static List MojoReadMessage(int handleToken, ByteData data, int numBytes, |
| 263 List<int> handleTokens, int flags) native "MojoMessagePipe_Read"; | 107 List<int> handleTokens, int flags) native "MojoMessagePipe_Read"; |
| 264 | 108 |
| 265 /// Reads a message from the endpoint [handleToken]. | |
| 266 /// | |
| 267 /// The result is returned in the provided list [result], which must have | |
| 268 /// a length of at least 5. | |
| 269 /// | |
| 270 /// The elements in [result] are: | |
| 271 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 272 /// particular, [MojoResult.kOk] signals a successful read. This value is | |
| 273 /// only used as output. | |
| 274 /// 2. the [ByteData] data array. This entry is used both as input and output. | |
| 275 /// If the array is non-null and big enough it is used to store the | |
| 276 /// byte-data of the message. Otherwise a new [ByteData] array of the | |
| 277 /// required length is allocated and stored in this slot. | |
| 278 /// 3. a list, used to store handles. This entry is used both as input and | |
| 279 /// output. If the list is big enough it is filled with the read handles. | |
| 280 /// Otherwise, a new list of the required length is allocated and used | |
| 281 /// instead. | |
| 282 /// 4. the size of the read byte data. Only used as output. | |
| 283 /// 5. the number of read handles. Only used as output. | |
| 284 /// | |
| 285 /// The [handleToken] is a token that identifies the Mojo handle. | |
| 286 /// | |
| 287 /// The parameter [flags] may set to either | |
| 288 /// [MojoMessagePipeEndpoint.READ_FLAG_NONE] (equal to 0) or | |
| 289 /// [MojoMessagePipeEndpoint.READ_FLAG_MAY_DISCARD] (equal to 1). In the | |
| 290 /// latter case messages that couldn't be read are discarded. | |
| 291 /// | |
| 292 /// Also see [MojoReadMessage]. | |
| 293 static void MojoQueryAndReadMessage(int handleToken, int flags, List result) | 109 static void MojoQueryAndReadMessage(int handleToken, int flags, List result) |
| 294 native "MojoMessagePipe_QueryAndRead"; | 110 native "MojoMessagePipe_QueryAndRead"; |
| 295 } | 111 } |
| 296 | 112 |
| 297 class MojoDataPipeNatives { | 113 class MojoDataPipeNatives { |
| 298 /// Creates a (unidirectional) data pipe represented by its two endpoints | |
| 299 /// (handles). | |
| 300 /// | |
| 301 /// Returns a list with exactly 3 elements: | |
| 302 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 303 /// particular, [MojoResult.kOk] signals a successful creation. | |
| 304 /// 2. the producer endpoint. A handle token. | |
| 305 /// 3. the consumer endpoint. A handle token. | |
| 306 /// | |
| 307 /// The parameter [elementBytes] specifies the size of an element in bytes. | |
| 308 /// All transactions and buffers consist of an integral number of elements. | |
| 309 /// The integer [elementBytes] must be non-zero. The default should be | |
| 310 /// [MojoDataPipe.DEFAULT_ELEMENT_SIZE] (equal to 1). | |
| 311 /// | |
| 312 /// The parameter [capacityBytes] specifies the capacity of the data-pipe, in | |
| 313 /// bytes. The parameter must be a multiple of [elementBytes]. The data-pipe | |
| 314 /// will always be able to queue *at least* this much data. If [capacityBytes] | |
| 315 /// is set to zero, a system-dependent automatically-calculated capacity is | |
| 316 /// used. The default should be [MojoDataPipe.DEFAULT_CAPACITY] (equal to 0). | |
| 317 /// | |
| 318 /// The parameter [flags] is reserved for future use and should currently be | |
| 319 /// set to [MojoDataPipe.FLAG_NONE] (equal to 0). | |
| 320 static List MojoCreateDataPipe(int elementBytes, int capacityBytes, int flags) | 114 static List MojoCreateDataPipe(int elementBytes, int capacityBytes, int flags) |
| 321 native "MojoDataPipe_Create"; | 115 native "MojoDataPipe_Create"; |
| 322 | 116 |
| 323 /// Writes [numBytes] bytes from [data] into the producer handle. | |
| 324 /// | |
| 325 /// Returns an integer, encoding the result as specified in the [MojoResult] | |
| 326 /// class. In particular, a successful operation returns [MojoResult.kOk]. | |
| 327 /// | |
| 328 /// The argument [handleToken] must be a producer handle created through | |
| 329 /// [MojoCreateDataPipe]. | |
| 330 /// | |
| 331 /// The argument [numBytes] should be a multiple of the data pipe's | |
| 332 /// element size. | |
| 333 /// | |
| 334 /// The argument [flags] can be | |
| 335 /// - [MojoDataPipeProducer.FLAG_NONE] (equal to 0), or | |
| 336 /// - [MojoDataPipeProducer.FLAG_ALL_OR_NONE] (equal to 1). | |
| 337 /// | |
| 338 /// If [flags] is equal to [MojoDataPipeProducer.FLAG_ALL_OR_NONE], then | |
| 339 /// either all data is written, or none is. If the data can't be written, then | |
| 340 /// the result integer is set to [MojoResult.kOutOfRange]. | |
| 341 /// | |
| 342 /// If no data can currently be written to an open consumer (and [flags] is | |
| 343 /// *not* set to [MojoDataPipeProducer.FLAG_ALL_OR_NONE]), then the | |
| 344 /// result-integer is set to [MojoResult.kShouldWait]. | |
| 345 static List MojoWriteData(int handle, ByteData data, int numBytes, int flags) | 117 static List MojoWriteData(int handle, ByteData data, int numBytes, int flags) |
| 346 native "MojoDataPipe_WriteData"; | 118 native "MojoDataPipe_WriteData"; |
| 347 | 119 |
| 348 /// Starts a two-phase write. | |
| 349 /// | |
| 350 /// Returns a List of exactly 2 elements: | |
| 351 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 352 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 353 /// 2. a [ByteData] object (when successful), or `null` (if unsuccessful). | |
| 354 /// | |
| 355 /// The argument [handleToken] must be a producer handle created through | |
| 356 /// [MojoCreateDataPipe]. | |
| 357 /// | |
| 358 /// A two-phase write consists of requesting a buffer to write to (this | |
| 359 /// function), followed by a call to [MojoEndWriteData] to signal that the | |
| 360 /// buffer has been filled with data and is ready to write. | |
| 361 /// | |
| 362 /// While the system waits for the [MojoEndWriteData], the underlying | |
| 363 /// data pipe is set to non-writable. | |
| 364 /// | |
| 365 /// A two-phase write is only started if the result integer (the first | |
| 366 /// argument of the returned list) is equal to [MojoResult.kOk]. Otherwise, | |
| 367 /// the underlying pipe stays writable (assuming it was before), and does not | |
| 368 /// expect a call to [MojoEndWriteData]. | |
| 369 /// | |
| 370 /// The result integer is equal to [MojoResult.kBusy] if the pipe is already | |
| 371 /// executing a two-phase write. | |
| 372 /// | |
| 373 /// The parameter [flags] is reserved for future use and should currently be | |
| 374 /// set to [MojoDataPipeProducer.FLAG_NONE] (equal to 0). | |
| 375 static List MojoBeginWriteData(int handleToken, int flags) | 120 static List MojoBeginWriteData(int handleToken, int flags) |
| 376 native "MojoDataPipe_BeginWriteData"; | 121 native "MojoDataPipe_BeginWriteData"; |
| 377 | 122 |
| 378 /// Finishes a two-phase write. | |
| 379 /// | |
| 380 /// Returns a result integer, encoded as specified in [MojoResult]. In | |
| 381 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 382 /// | |
| 383 /// The argument [handleToken] must be a producer handle created through | |
| 384 /// [MojoCreateDataPipe] and must be the same that was given to a previous | |
| 385 /// call to [MojoBeginWriteData]. | |
| 386 /// | |
| 387 /// Writes [bytesWritten] bytes of the [ByteData] buffer provided by | |
| 388 /// [MojoBeginWriteData] into the pipe. The parameter [bytesWritten] must be | |
| 389 /// less or equal to the size of the [ByteData] buffer and must be a multiple | |
| 390 /// of the data pipe's element size. | |
| 391 static int MojoEndWriteData(int handleToken, int bytesWritten) | 123 static int MojoEndWriteData(int handleToken, int bytesWritten) |
| 392 native "MojoDataPipe_EndWriteData"; | 124 native "MojoDataPipe_EndWriteData"; |
| 393 | 125 |
| 394 /// Reads up to [numBytes] from the given consumer [handleToken]. | |
| 395 /// | |
| 396 /// Returns a list of exactly two elements: | |
| 397 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 398 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 399 /// 2. an integer `readBytes` (having different semantics depending on the | |
| 400 /// flags. See below for the different cases. | |
| 401 /// | |
| 402 /// The argument [handleToken] must be a consumer handle created through | |
| 403 /// [MojoCreateDataPipe]. | |
| 404 /// | |
| 405 /// The argument [numBytes] must be a multiple of the data pipe's element | |
| 406 /// size. | |
| 407 /// | |
| 408 /// If [flags] has neither [MojoDataPipeConsumer.FLAG_DISCARD] (equal to 2), | |
| 409 /// nor [MojoDataPipeConsumer.FLAG_QUERY] (equal to 4) set, tries to read up | |
| 410 /// to [numBytes] bytes of data into the [data] buffer and set | |
| 411 /// `readBytes` (the second element of the returned list) to the amount | |
| 412 /// actually read. | |
| 413 /// | |
| 414 /// If [flags] has [MojoDataPipeConsumer.FLAG_ALL_OR_NONE] (equal to 1) set, | |
| 415 /// either reads exactly [numBytes] bytes of data or none. Additionally, if | |
| 416 /// [flags] has [MojoDataPipeConsumer.FLAG_PEEK] (equal to 8) set, the data | |
| 417 /// read remains in the pipe and is available to future reads. | |
| 418 /// | |
| 419 /// If [flags] has [MojoDataPipeConsumer.FLAG_DISCARD] (equal to 2) set, it | |
| 420 /// discards up to [numBytes] (which again must be a multiple of the element | |
| 421 /// size) bytes of data, setting `readBytes` to the amount actually | |
| 422 /// discarded. If [flags] has [MojoDataPipeConsumer.FLAG_ALL_OR_NONE] (equal | |
| 423 /// to 1), either discards exactly [numBytes] bytes of data or none. In this | |
| 424 /// case, [MojoDataPipeConsumer.FLAG_QUERY] must not be set, and | |
| 425 /// the [data] buffer is ignored (and should typically be set to | |
| 426 /// null). | |
| 427 /// | |
| 428 /// If flags has [MojoDataPipeConsumer.FLAG_QUERY] set, queries the amount of | |
| 429 /// data available, setting `readBytes` to the number of bytes available. In | |
| 430 /// this case, [MojoDataPipeConsumer.FLAG_DISCARD] must not be set, and | |
| 431 /// [MojoDataPipeConsumer.FLAG_ALL_OR_NONE] is ignored, as are [data] and | |
| 432 /// [numBytes]. | |
| 433 static List MojoReadData(int handleToken, ByteData data, int numBytes, | 126 static List MojoReadData(int handleToken, ByteData data, int numBytes, |
| 434 int flags) native "MojoDataPipe_ReadData"; | 127 int flags) native "MojoDataPipe_ReadData"; |
| 435 | 128 |
| 436 /// Starts a two-phase read. | |
| 437 /// | |
| 438 /// Returns a List of exactly 2 elements: | |
| 439 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 440 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 441 /// 2. a [ByteData] object (when successful), or `null` (if unsuccessful). | |
| 442 /// | |
| 443 /// The argument [handleToken] must be a consumer handle created through | |
| 444 /// [MojoCreateDataPipe]. | |
| 445 /// | |
| 446 /// A two-phase write consists of requesting a buffer to read from (this | |
| 447 /// function), followed by a call to [MojoEndReadData] to signal that the | |
| 448 /// buffer has been read. | |
| 449 /// | |
| 450 /// While the system waits for the [MojoEndReadData], the underlying | |
| 451 /// data pipe is set to non-readable. | |
| 452 /// | |
| 453 /// A two-phase read is only started if the result integer (the first | |
| 454 /// argument of the returned list) is equal to [MojoResult.kOk]. Otherwise, | |
| 455 /// the underlying pipe stays readable (assuming it was before), and does not | |
| 456 /// expect a call to [MojoEndReadData]. | |
| 457 /// | |
| 458 /// The result integer is equal to [MojoResult.kBusy] if the pipe is already | |
| 459 /// executing a two-phase read. | |
| 460 /// | |
| 461 /// The result integer is equal to [MojoResult.kShouldWait] if the pipe has | |
| 462 /// no data available. | |
| 463 /// | |
| 464 /// The parameter [flags] is reserved for future use and should currently be | |
| 465 /// set to [MojoDataPipeConsumer.FLAG_NONE] (equal to 0). | |
| 466 static List MojoBeginReadData(int handleToken, int flags) | 129 static List MojoBeginReadData(int handleToken, int flags) |
| 467 native "MojoDataPipe_BeginReadData"; | 130 native "MojoDataPipe_BeginReadData"; |
| 468 | 131 |
| 469 /// Finishes a two-phase read. | |
| 470 /// | |
| 471 /// Returns a result integer, encoded as specified in [MojoResult]. In | |
| 472 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 473 /// | |
| 474 /// The argument [handleToken] must be a consumer handle created through | |
| 475 /// [MojoCreateDataPipe] and must be the same that was given to a previous | |
| 476 /// call to [MojoBeginReadData]. | |
| 477 /// | |
| 478 /// Consumes [bytesRead] bytes of the [ByteData] buffer provided by | |
| 479 /// [MojoBeginReadData]. The parameter [bytesWritten] must be | |
| 480 /// less or equal to the size of the [ByteData] buffer and must be a multiple | |
| 481 /// of the data pipe's element size. | |
| 482 static int MojoEndReadData(int handleToken, int bytesRead) | 132 static int MojoEndReadData(int handleToken, int bytesRead) |
| 483 native "MojoDataPipe_EndReadData"; | 133 native "MojoDataPipe_EndReadData"; |
| 484 } | 134 } |
| 485 | 135 |
| 486 class MojoSharedBufferNatives { | 136 class MojoSharedBufferNatives { |
| 487 /// Creates a shared buffer of [numBytes] bytes. | |
| 488 /// | |
| 489 /// Returns a List of exactly 2 elements: | |
| 490 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 491 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 492 /// 2. a handle. | |
| 493 /// | |
| 494 /// A shared buffer can be shared between applications (by duplicating the | |
| 495 /// handle -- see [Duplicate] -- and passing it over a message pipe). | |
| 496 /// | |
| 497 /// A shared buffer can be accessed through by invoking [Map]. | |
| 498 /// | |
| 499 /// The parameter [flags] is reserved for future use and should currently be | |
| 500 /// set to [MojoSharedBuffer.createFlagNone] (equal to 0). | |
| 501 static List Create(int numBytes, int flags) native "MojoSharedBuffer_Create"; | 137 static List Create(int numBytes, int flags) native "MojoSharedBuffer_Create"; |
| 502 | 138 |
| 503 /// Duplicates the given [bufferHandleToken] so that it can be shared through | |
| 504 /// a message pipe. | |
| 505 /// | |
| 506 /// Returns a list of exactly 2 elements: | |
| 507 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 508 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 509 /// 2. the duplicated handle. | |
| 510 /// | |
| 511 /// The [bufferHandleToken] must be a handle created by [Create]. | |
| 512 /// | |
| 513 /// Creates another handle (returned as second element in the returned list) | |
| 514 /// which can then be sent to another application over a message pipe, while | |
| 515 /// retaining access to the [bufferHandleToken] (and any mappings that it may | |
| 516 /// have). | |
| 517 /// | |
| 518 /// The parameter [flags] is reserved for future use and should currently be | |
| 519 /// set to [MojoSharedBuffer.duplicateFlagNone] (equal to 0). | |
| 520 static List Duplicate(int bufferHandleToken, int flags) | 139 static List Duplicate(int bufferHandleToken, int flags) |
| 521 native "MojoSharedBuffer_Duplicate"; | 140 native "MojoSharedBuffer_Duplicate"; |
| 522 | 141 |
| 523 /// Maps the given [bufferHandleToken] so that its data can be access through | |
| 524 /// a [ByteData] buffer. | |
| 525 /// | |
| 526 /// Returns a list of exactly 2 elements: | |
| 527 /// 1. the result integer, encoded as specified in [MojoResult]. In | |
| 528 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 529 /// 2. a [ByteData] buffer that maps to the data in the shared buffer. | |
| 530 /// | |
| 531 /// The [bufferHandleToken] must be a handle created by [Create]. | |
| 532 /// | |
| 533 /// Maps [numBytes] of data, starting at offset [offset] into a [ByteData] | |
| 534 /// buffer. | |
| 535 /// | |
| 536 /// Note: there is no `unmap` call, since this is supposed to happen via | |
| 537 /// finalizers. | |
| 538 /// | |
| 539 /// The parameter [flags] is reserved for future use and should currently be | |
| 540 /// set to [MojoSharedBuffer.mapFlagNone] (equal to 0). | |
| 541 static List Map(int bufferHandleToken, int offset, int numBytes, int flags) | 142 static List Map(int bufferHandleToken, int offset, int numBytes, int flags) |
| 542 native "MojoSharedBuffer_Map"; | 143 native "MojoSharedBuffer_Map"; |
| 543 | 144 |
| 544 | |
| 545 /// Returns information about [bufferHandleToken]. | |
| 546 /// | |
| 547 /// Returns a list of exactly 3 elements: | |
| 548 /// 1. The result integer, encoded as specified in [MojoResult]. In | |
| 549 /// particular, [MojoResult.kOk] signals a successful operation. | |
| 550 /// 2. The flags of the buffer (currently unused). | |
| 551 /// 3. The size of the buffer (in bytes). | |
| 552 /// | |
| 553 /// The [bufferHandleToken] must be a handle created by [Create]. | |
| 554 static List GetInformation(int bufferHandleToken) | 145 static List GetInformation(int bufferHandleToken) |
| 555 native "MojoSharedBuffer_GetInformation"; | 146 native "MojoSharedBuffer_GetInformation"; |
| 556 } | 147 } |
| OLD | NEW |