Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: mojo/dart/packages/mojo/lib/src/message_pipe.dart

Issue 1439993003: Dart: Avoid MojoResult and MojoHandleSignals constructors. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Small fixes Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 MojoMessagePipeReadResult { 7 class MojoMessagePipeReadResult {
8 final MojoResult status; 8 final int status;
9 final int bytesRead; 9 final int bytesRead;
10 final int handlesRead; 10 final int handlesRead;
11 11
12 MojoMessagePipeReadResult(this.status, this.bytesRead, this.handlesRead); 12 MojoMessagePipeReadResult(this.status, this.bytesRead, this.handlesRead);
13 MojoMessagePipeReadResult.fromList(List<int> resultList) 13 MojoMessagePipeReadResult.fromList(List<int> resultList)
14 : this(new MojoResult(resultList[0]), resultList[1], resultList[2]); 14 : this(resultList[0], resultList[1], resultList[2]);
15 15
16 String toString() { 16 String toString() {
17 return "MojoMessagePipeReadResult(" 17 return "MojoMessagePipeReadResult("
18 "status: $status, bytesRead: $bytesRead, handlesRead: $handlesRead)"; 18 "status: ${MojoResult.string(status)}, bytesRead: $bytesRead, "
19 "handlesRead: $handlesRead)";
19 } 20 }
20 } 21 }
21 22
22 class MojoMessagePipeQueryAndReadState { 23 class MojoMessagePipeQueryAndReadState {
23 static final List _result = new List(5); 24 static final List _result = new List(5);
24 25
25 List<MojoHandle> _handles; 26 List<MojoHandle> _handles;
26 27
27 MojoResult get status => new MojoResult(_result[0]); 28 int get status => _result[0];
28 ByteData get data => _result[1]; 29 ByteData get data => _result[1];
29 List<MojoHandle> get handles => _handles; 30 List<MojoHandle> get handles => _handles;
30 int get dataLength => _result[3]; 31 int get dataLength => _result[3];
31 int get handlesLength => _result[4]; 32 int get handlesLength => _result[4];
32 33
33 MojoMessagePipeQueryAndReadState(); 34 MojoMessagePipeQueryAndReadState();
34 35
35 void queryAndRead(int handle, int flags) { 36 void queryAndRead(int handle, int flags) {
36 MojoMessagePipeNatives.MojoQueryAndReadMessage(handle, flags, _result); 37 MojoMessagePipeNatives.MojoQueryAndReadMessage(handle, flags, _result);
37 38
38 if (handlesLength == 0) { 39 if (handlesLength == 0) {
39 _handles = null; 40 _handles = null;
40 } else { 41 } else {
41 _handles = new List(handlesLength); 42 _handles = new List(handlesLength);
42 for (int i = 0; i < handlesLength; i++) { 43 for (int i = 0; i < handlesLength; i++) {
43 _handles[i] = new MojoHandle(_result[2][i]); 44 _handles[i] = new MojoHandle(_result[2][i]);
44 } 45 }
45 } 46 }
46 } 47 }
47 48
48 String toString() { 49 String toString() {
49 return "MojoMessagePipeQueryAndReadState(" 50 return "MojoMessagePipeQueryAndReadState("
50 "status: $status, dataLength: $dataLength, " 51 "status: ${MojoResult.string(status)}, dataLength: $dataLength, "
51 "handlesLength: $handlesLength)"; 52 "handlesLength: $handlesLength)";
52 } 53 }
53 } 54 }
54 55
55 class MojoMessagePipeEndpoint { 56 class MojoMessagePipeEndpoint {
56 static const int WRITE_FLAG_NONE = 0; 57 static const int WRITE_FLAG_NONE = 0;
57 static const int READ_FLAG_NONE = 0; 58 static const int READ_FLAG_NONE = 0;
58 static const int READ_FLAG_MAY_DISCARD = 1 << 0; 59 static const int READ_FLAG_MAY_DISCARD = 1 << 0;
59 60
60 static final _queryAndReadState = new MojoMessagePipeQueryAndReadState(); 61 static final _queryAndReadState = new MojoMessagePipeQueryAndReadState();
61 62
62 MojoHandle handle; 63 MojoHandle handle;
63 MojoResult status; 64 int status;
64 65
65 MojoMessagePipeEndpoint(this.handle); 66 MojoMessagePipeEndpoint(this.handle);
66 67
67 MojoResult write(ByteData data, 68 bool get ok => status == MojoResult.kOk;
69
70 int write(ByteData data,
68 [int numBytes = -1, List<MojoHandle> handles = null, int flags = 0]) { 71 [int numBytes = -1, List<MojoHandle> handles = null, int flags = 0]) {
69 if (handle == null) { 72 if (handle == null) {
70 status = MojoResult.INVALID_ARGUMENT; 73 status = MojoResult.kInvalidArgument;
71 return status; 74 return status;
72 } 75 }
73 76
74 int dataLengthInBytes = (data == null) ? 0 : data.lengthInBytes; 77 int dataLengthInBytes = (data == null) ? 0 : data.lengthInBytes;
75 78
76 // If numBytes has the default value, use the full length of the data. 79 // If numBytes has the default value, use the full length of the data.
77 int dataNumBytes = (numBytes == -1) ? dataLengthInBytes : numBytes; 80 int dataNumBytes = (numBytes == -1) ? dataLengthInBytes : numBytes;
78 if (dataNumBytes > dataLengthInBytes) { 81 if (dataNumBytes > dataLengthInBytes) {
79 status = MojoResult.INVALID_ARGUMENT; 82 status = MojoResult.kInvalidArgument;
80 return status; 83 return status;
81 } 84 }
82 85
83 // handles may be null, otherwise convert to ints. 86 // handles may be null, otherwise convert to ints.
84 List<int> mojoHandles = 87 List<int> mojoHandles =
85 (handles != null) ? handles.map((h) => h.h).toList() : null; 88 (handles != null) ? handles.map((h) => h.h).toList() : null;
86 89
87 // Do the call. 90 // Do the call.
88 int result = MojoMessagePipeNatives.MojoWriteMessage( 91 status = MojoMessagePipeNatives.MojoWriteMessage(
89 handle.h, data, dataNumBytes, mojoHandles, flags); 92 handle.h, data, dataNumBytes, mojoHandles, flags);
90
91 status = new MojoResult(result);
92 return status; 93 return status;
93 } 94 }
94 95
95 MojoMessagePipeReadResult read(ByteData data, 96 MojoMessagePipeReadResult read(ByteData data,
96 [int numBytes = -1, List<MojoHandle> handles = null, int flags = 0]) { 97 [int numBytes = -1, List<MojoHandle> handles = null, int flags = 0]) {
97 if (handle == null) { 98 if (handle == null) {
98 status = MojoResult.INVALID_ARGUMENT; 99 status = MojoResult.kInvalidArgument;
99 return null; 100 return null;
100 } 101 }
101 102
102 // If numBytes has the default value, use the full length of the data. 103 // If numBytes has the default value, use the full length of the data.
103 int dataNumBytes; 104 int dataNumBytes;
104 if (data == null) { 105 if (data == null) {
105 dataNumBytes = 0; 106 dataNumBytes = 0;
106 } else { 107 } else {
107 dataNumBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; 108 dataNumBytes = (numBytes == -1) ? data.lengthInBytes : numBytes;
108 if (dataNumBytes > data.lengthInBytes) { 109 if (dataNumBytes > data.lengthInBytes) {
109 status = MojoResult.INVALID_ARGUMENT; 110 status = MojoResult.kInvalidArgument;
110 return null; 111 return null;
111 } 112 }
112 } 113 }
113 114
114 // handles may be null, otherwise make an int list for the handles. 115 // handles may be null, otherwise make an int list for the handles.
115 List<int> mojoHandles; 116 List<int> mojoHandles;
116 if (handles == null) { 117 if (handles == null) {
117 mojoHandles = null; 118 mojoHandles = null;
118 } else { 119 } else {
119 mojoHandles = new List<int>(handles.length); 120 mojoHandles = new List<int>(handles.length);
120 } 121 }
121 122
122 // Do the call. 123 // Do the call.
123 List result = MojoMessagePipeNatives.MojoReadMessage( 124 List result = MojoMessagePipeNatives.MojoReadMessage(
124 handle.h, data, dataNumBytes, mojoHandles, flags); 125 handle.h, data, dataNumBytes, mojoHandles, flags);
125 126
126 if (result == null) { 127 if (result == null) {
127 status = MojoResult.INVALID_ARGUMENT; 128 status = MojoResult.kInvalidArgument;
128 return null; 129 return null;
129 } 130 }
130 131
131 assert((result is List) && (result.length == 3)); 132 assert((result is List) && (result.length == 3));
132 var readResult = new MojoMessagePipeReadResult.fromList(result); 133 var readResult = new MojoMessagePipeReadResult.fromList(result);
133 134
134 // Copy out the handles that were read. 135 // Copy out the handles that were read.
135 if (handles != null) { 136 if (handles != null) {
136 for (var i = 0; i < readResult.handlesRead; i++) { 137 for (var i = 0; i < readResult.handlesRead; i++) {
137 handles[i] = new MojoHandle(mojoHandles[i]); 138 handles[i] = new MojoHandle(mojoHandles[i]);
138 } 139 }
139 } 140 }
140 141
141 status = readResult.status; 142 status = readResult.status;
142 return readResult; 143 return readResult;
143 } 144 }
144 145
145 MojoMessagePipeReadResult query() => read(null); 146 MojoMessagePipeReadResult query() => read(null);
146 147
147 bool setDescription(String description) => 148 bool setDescription(String description) =>
148 MojoHandleNatives.setDescription(handle.h, description); 149 MojoHandleNatives.setDescription(handle.h, description);
149 150
150 /// Warning: The object returned by this function, and the buffers inside of 151 /// Warning: The object returned by this function, and the buffers inside of
151 /// it are only valid until the next call to this function by the same 152 /// it are only valid until the next call to this function by the same
152 /// isolate. 153 /// isolate.
153 MojoMessagePipeQueryAndReadState queryAndRead([int flags = 0]) { 154 MojoMessagePipeQueryAndReadState queryAndRead([int flags = 0]) {
154 if (handle == null) { 155 if (handle == null) {
155 status = MojoResult.INVALID_ARGUMENT; 156 status = MojoResult.kInvalidArgument;
156 return null; 157 return null;
157 } 158 }
158 159
159 _queryAndReadState.queryAndRead(handle.h, flags); 160 _queryAndReadState.queryAndRead(handle.h, flags);
160 status = _queryAndReadState.status; 161 status = _queryAndReadState.status;
161 return _queryAndReadState; 162 return _queryAndReadState;
162 } 163 }
163 164
164 void close() { 165 void close() {
165 handle.close(); 166 handle.close();
166 handle = null; 167 handle = null;
167 } 168 }
168 169
169 String toString() => 170 String toString() => "MojoMessagePipeEndpoint(handle: $handle, "
170 "MojoMessagePipeEndpoint(handle: $handle, status: $status)"; 171 "status: ${MojoResult.string(status)})";
171 } 172 }
172 173
173 class MojoMessagePipe { 174 class MojoMessagePipe {
174 static const int FLAG_NONE = 0; 175 static const int FLAG_NONE = 0;
175 176
176 List<MojoMessagePipeEndpoint> endpoints; 177 List<MojoMessagePipeEndpoint> endpoints;
177 MojoResult status; 178 int status;
178 179
179 MojoMessagePipe._() { 180 MojoMessagePipe._() : status = MojoResult.kOk;
180 endpoints = null;
181 status = MojoResult.OK;
182 }
183 181
184 factory MojoMessagePipe([int flags = FLAG_NONE]) { 182 factory MojoMessagePipe([int flags = FLAG_NONE]) {
185 List result = MojoMessagePipeNatives.MojoCreateMessagePipe(flags); 183 List result = MojoMessagePipeNatives.MojoCreateMessagePipe(flags);
186 if (result == null) { 184 if (result == null) {
187 return null; 185 return null;
188 } 186 }
189 assert((result is List) && (result.length == 3)); 187 assert((result is List) && (result.length == 3));
190 188
191 MojoHandle end1 = new MojoHandle(result[1]); 189 MojoHandle end1 = new MojoHandle(result[1]);
192 MojoHandle end2 = new MojoHandle(result[2]); 190 MojoHandle end2 = new MojoHandle(result[2]);
193 MojoMessagePipe pipe = new MojoMessagePipe._(); 191 MojoMessagePipe pipe = new MojoMessagePipe._();
194 pipe.endpoints = new List(2); 192 pipe.endpoints = new List(2);
195 pipe.endpoints[0] = new MojoMessagePipeEndpoint(end1); 193 pipe.endpoints[0] = new MojoMessagePipeEndpoint(end1);
196 pipe.endpoints[1] = new MojoMessagePipeEndpoint(end2); 194 pipe.endpoints[1] = new MojoMessagePipeEndpoint(end2);
197 pipe.status = new MojoResult(result[0]); 195 pipe.status = result[0];
198 return pipe; 196 return pipe;
199 } 197 }
200 } 198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698