| 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 element_bytes, int capacity_bytes, int flags) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 static int MojoEndReadData(int handle, int bytes_read) | 28 static int MojoEndReadData(int handle, int bytes_read) |
| 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 RawMojoHandle handle; | 37 MojoHandle handle; |
| 38 MojoResult status; | 38 MojoResult status; |
| 39 final int element_bytes; | 39 final int element_bytes; |
| 40 | 40 |
| 41 MojoDataPipeProducer(this.handle, | 41 MojoDataPipeProducer(this.handle, |
| 42 this.status, | 42 this.status, |
| 43 this.element_bytes); | 43 this.element_bytes); |
| 44 | 44 |
| 45 int write(ByteData data, [int num_bytes = -1, int flags = 0]) { | 45 int write(ByteData data, [int num_bytes = -1, int flags = 0]) { |
| 46 if (handle == null) { | 46 if (handle == null) { |
| 47 status = MojoResult.INVALID_ARGUMENT; | 47 status = MojoResult.INVALID_ARGUMENT; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 return status; | 89 return status; |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 | 92 |
| 93 | 93 |
| 94 class MojoDataPipeConsumer { | 94 class MojoDataPipeConsumer { |
| 95 static const int FLAG_NONE = 0; | 95 static const int FLAG_NONE = 0; |
| 96 static const int FLAG_ALL_OR_NONE = 1 << 0; | 96 static const int FLAG_ALL_OR_NONE = 1 << 0; |
| 97 static const int FLAG_MAY_DISCARD = 1 << 1; | 97 static const int FLAG_MAY_DISCARD = 1 << 1; |
| 98 static const int FLAG_QUERY = 1 << 2; | 98 static const int FLAG_QUERY = 1 << 2; |
| 99 static const int FLAG_PEEK = 1 << 3; |
| 99 | 100 |
| 100 RawMojoHandle handle; | 101 MojoHandle handle; |
| 101 MojoResult status; | 102 MojoResult status; |
| 102 final int element_bytes; | 103 final int element_bytes; |
| 103 | 104 |
| 104 MojoDataPipeConsumer(this.handle, | 105 MojoDataPipeConsumer( |
| 105 this.status, | 106 this.handle, [this.status = MojoResult.OK, this.element_bytes = 1]); |
| 106 this.element_bytes); | |
| 107 | 107 |
| 108 int read(ByteData data, [int num_bytes = -1, int flags = 0]) { | 108 int read(ByteData data, [int num_bytes = -1, int flags = 0]) { |
| 109 if (handle == null) { | 109 if (handle == null) { |
| 110 status = MojoResult.INVALID_ARGUMENT; | 110 status = MojoResult.INVALID_ARGUMENT; |
| 111 return status; | 111 return status; |
| 112 } | 112 } |
| 113 | 113 |
| 114 int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes; | 114 int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes; |
| 115 List result = _MojoDataPipeNatives.MojoReadData( | 115 List result = _MojoDataPipeNatives.MojoReadData( |
| 116 handle.h, data, data_num_bytes, flags); | 116 handle.h, data, data_num_bytes, flags); |
| 117 if (result == null) { | 117 if (result == null) { |
| 118 status = MojoResult.INVALID_ARGUMENT; | 118 status = MojoResult.INVALID_ARGUMENT; |
| 119 return status; | 119 return status; |
| 120 } | 120 } |
| 121 assert((result is List) && (result.length == 2)); | 121 assert((result is List) && (result.length == 2)); |
| 122 status = new MojoResult(result[0]); | 122 status = new MojoResult(result[0]); |
| 123 return result[1]; | 123 return result[1]; |
| 124 } | 124 } |
| 125 | 125 |
| 126 ByteData beginRead(int buffer_bytes, [int flags = 0]) { | 126 ByteData beginRead([int buffer_bytes = 0, int flags = 0]) { |
| 127 if (handle == null) { | 127 if (handle == null) { |
| 128 status = MojoResult.INVALID_ARGUMENT; | 128 status = MojoResult.INVALID_ARGUMENT; |
| 129 return null; | 129 return null; |
| 130 } | 130 } |
| 131 | 131 |
| 132 List result = _MojoDataPipeNatives.MojoBeginReadData( | 132 List result = _MojoDataPipeNatives.MojoBeginReadData( |
| 133 handle.h, buffer_bytes, flags); | 133 handle.h, buffer_bytes, flags); |
| 134 if (result == null) { | 134 if (result == null) { |
| 135 status = MojoResult.INVALID_ARGUMENT; | 135 status = MojoResult.INVALID_ARGUMENT; |
| 136 return null; | 136 return null; |
| 137 } | 137 } |
| 138 | 138 |
| 139 assert((result is List) && (result.length == 2)); | 139 assert((result is List) && (result.length == 2)); |
| 140 status = new MojoResult(result[0]); | 140 status = new MojoResult(result[0]); |
| 141 return result[1]; | 141 return result[1]; |
| 142 } | 142 } |
| 143 | 143 |
| 144 MojoResult endRead(int bytes_read) { | 144 MojoResult endRead(int bytes_read) { |
| 145 if (handle == null) { | 145 if (handle == null) { |
| 146 status = MojoResult.INVALID_ARGUMENT; | 146 status = MojoResult.INVALID_ARGUMENT; |
| 147 return status; | 147 return status; |
| 148 } | 148 } |
| 149 int result = _MojoDataPipeNatives.MojoEndReadData(handle.h, bytes_read); | 149 int result = _MojoDataPipeNatives.MojoEndReadData(handle.h, bytes_read); |
| 150 status = new MojoResult(result); | 150 status = new MojoResult(result); |
| 151 return status; | 151 return status; |
| 152 } | 152 } |
| 153 |
| 154 int query() => read(null, 0, FLAG_QUERY); |
| 153 } | 155 } |
| 154 | 156 |
| 155 | 157 |
| 156 class MojoDataPipe { | 158 class MojoDataPipe { |
| 157 static const int FLAG_NONE = 0; | 159 static const int FLAG_NONE = 0; |
| 158 static const int FLAG_MAY_DISCARD = 1 << 0; | 160 static const int FLAG_MAY_DISCARD = 1 << 0; |
| 159 static const int DEFAULT_ELEMENT_SIZE = 1; | 161 static const int DEFAULT_ELEMENT_SIZE = 1; |
| 160 static const int DEFAULT_CAPACITY = 0; | 162 static const int DEFAULT_CAPACITY = 0; |
| 161 | 163 |
| 162 MojoDataPipeProducer producer; | 164 MojoDataPipeProducer producer; |
| 163 MojoDataPipeConsumer consumer; | 165 MojoDataPipeConsumer consumer; |
| 164 MojoResult status; | 166 MojoResult status; |
| 165 | 167 |
| 166 MojoDataPipe._internal() { | 168 MojoDataPipe._internal() { |
| 167 producer = null; | 169 producer = null; |
| 168 consumer = null; | 170 consumer = null; |
| 169 status = MojoResult.OK; | 171 status = MojoResult.OK; |
| 170 } | 172 } |
| 171 | 173 |
| 172 factory MojoDataPipe([int element_bytes = DEFAULT_ELEMENT_SIZE, | 174 factory MojoDataPipe([int element_bytes = DEFAULT_ELEMENT_SIZE, |
| 173 int capacity_bytes = DEFAULT_CAPACITY, | 175 int capacity_bytes = DEFAULT_CAPACITY, |
| 174 int flags = FLAG_NONE]) { | 176 int flags = FLAG_NONE]) { |
| 175 List result = _MojoDataPipeNatives.MojoCreateDataPipe( | 177 List result = _MojoDataPipeNatives.MojoCreateDataPipe( |
| 176 element_bytes, capacity_bytes, flags); | 178 element_bytes, capacity_bytes, flags); |
| 177 if (result == null) { | 179 if (result == null) { |
| 178 return null; | 180 return null; |
| 179 } | 181 } |
| 180 assert((result is List) && (result.length == 3)); | 182 assert((result is List) && (result.length == 3)); |
| 181 RawMojoHandle producer_handle = new RawMojoHandle(result[1]); | 183 MojoHandle producer_handle = new MojoHandle(result[1]); |
| 182 RawMojoHandle consumer_handle = new RawMojoHandle(result[2]); | 184 MojoHandle consumer_handle = new MojoHandle(result[2]); |
| 183 MojoDataPipe pipe = new MojoDataPipe._internal(); | 185 MojoDataPipe pipe = new MojoDataPipe._internal(); |
| 184 pipe.producer = new MojoDataPipeProducer( | 186 pipe.producer = new MojoDataPipeProducer( |
| 185 producer_handle, new MojoResult(result[0]), element_bytes); | 187 producer_handle, new MojoResult(result[0]), element_bytes); |
| 186 pipe.consumer = new MojoDataPipeConsumer( | 188 pipe.consumer = new MojoDataPipeConsumer( |
| 187 consumer_handle, new MojoResult(result[0]), element_bytes); | 189 consumer_handle, new MojoResult(result[0]), element_bytes); |
| 188 pipe.status = new MojoResult(result[0]); | 190 pipe.status = new MojoResult(result[0]); |
| 189 return pipe; | 191 return pipe; |
| 190 } | 192 } |
| 191 } | 193 } |
| OLD | NEW |