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