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