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