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