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 class MojoMessagePipeReadResult { | |
8 final MojoResult status; | |
9 final int bytesRead; | |
10 final int handlesRead; | |
11 | |
12 MojoMessagePipeReadResult(this.status, this.bytesRead, this.handlesRead); | |
13 MojoMessagePipeReadResult.fromList(List<int> resultList) | |
14 : this(new MojoResult(resultList[0]), resultList[1], resultList[2]); | |
15 | |
16 String toString() { | |
17 return "MojoMessagePipeReadResult(" | |
18 "status: $status, bytesRead: $bytesRead, handlesRead: $handlesRead)"; | |
19 } | |
20 } | |
21 | |
22 class MojoMessagePipeEndpoint { | |
23 static const int WRITE_FLAG_NONE = 0; | |
24 static const int READ_FLAG_NONE = 0; | |
25 static const int READ_FLAG_MAY_DISCARD = 1 << 0; | |
26 | |
27 MojoHandle handle; | |
28 MojoResult status; | |
29 | |
30 MojoMessagePipeEndpoint(this.handle); | |
31 | |
32 MojoResult write(ByteData data, | |
33 [int numBytes = -1, List<MojoHandle> handles = null, int flags = 0]) { | |
34 if (handle == null) { | |
35 status = MojoResult.INVALID_ARGUMENT; | |
36 return status; | |
37 } | |
38 | |
39 int dataLengthInBytes = (data == null) ? 0 : data.lengthInBytes; | |
40 | |
41 // If numBytes has the default value, use the full length of the data. | |
42 int dataNumBytes = (numBytes == -1) ? dataLengthInBytes : numBytes; | |
43 if (dataNumBytes > dataLengthInBytes) { | |
44 status = MojoResult.INVALID_ARGUMENT; | |
45 return status; | |
46 } | |
47 | |
48 // handles may be null, otherwise convert to ints. | |
49 List<int> mojoHandles = | |
50 (handles != null) ? handles.map((h) => h.h).toList() : null; | |
51 | |
52 // Do the call. | |
53 int result = MojoMessagePipeNatives.MojoWriteMessage( | |
54 handle.h, data, dataNumBytes, mojoHandles, flags); | |
55 | |
56 status = new MojoResult(result); | |
57 return status; | |
58 } | |
59 | |
60 MojoMessagePipeReadResult read(ByteData data, | |
61 [int numBytes = -1, List<MojoHandle> handles = null, int flags = 0]) { | |
62 if (handle == null) { | |
63 status = MojoResult.INVALID_ARGUMENT; | |
64 return null; | |
65 } | |
66 | |
67 // If numBytes has the default value, use the full length of the data. | |
68 int dataNumBytes; | |
69 if (data == null) { | |
70 dataNumBytes = 0; | |
71 } else { | |
72 dataNumBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; | |
73 if (dataNumBytes > data.lengthInBytes) { | |
74 status = MojoResult.INVALID_ARGUMENT; | |
75 return null; | |
76 } | |
77 } | |
78 | |
79 // handles may be null, otherwise make an int list for the handles. | |
80 List<int> mojoHandles; | |
81 if (handles == null) { | |
82 mojoHandles = null; | |
83 } else { | |
84 mojoHandles = new List<int>(handles.length); | |
85 } | |
86 | |
87 // Do the call. | |
88 List result = MojoMessagePipeNatives.MojoReadMessage( | |
89 handle.h, data, dataNumBytes, mojoHandles, flags); | |
90 | |
91 if (result == null) { | |
92 status = MojoResult.INVALID_ARGUMENT; | |
93 return null; | |
94 } | |
95 | |
96 assert((result is List) && (result.length == 3)); | |
97 var readResult = new MojoMessagePipeReadResult.fromList(result); | |
98 | |
99 // Copy out the handles that were read. | |
100 if (handles != null) { | |
101 for (var i = 0; i < readResult.handlesRead; i++) { | |
102 handles[i] = new MojoHandle(mojoHandles[i]); | |
103 } | |
104 } | |
105 | |
106 status = readResult.status; | |
107 return readResult; | |
108 } | |
109 | |
110 MojoMessagePipeReadResult query() => read(null); | |
111 | |
112 void close() { | |
113 handle.close(); | |
114 handle = null; | |
115 } | |
116 | |
117 String toString() => | |
118 "MojoMessagePipeEndpoint(handle: $handle, status: $status)"; | |
119 } | |
120 | |
121 class MojoMessagePipe { | |
122 static const int FLAG_NONE = 0; | |
123 | |
124 List<MojoMessagePipeEndpoint> endpoints; | |
125 MojoResult status; | |
126 | |
127 MojoMessagePipe._() { | |
128 endpoints = null; | |
129 status = MojoResult.OK; | |
130 } | |
131 | |
132 factory MojoMessagePipe([int flags = FLAG_NONE]) { | |
133 List result = MojoMessagePipeNatives.MojoCreateMessagePipe(flags); | |
134 if (result == null) { | |
135 return null; | |
136 } | |
137 assert((result is List) && (result.length == 3)); | |
138 | |
139 MojoHandle end1 = new MojoHandle(result[1]); | |
140 MojoHandle end2 = new MojoHandle(result[2]); | |
141 MojoMessagePipe pipe = new MojoMessagePipe._(); | |
142 pipe.endpoints = new List(2); | |
143 pipe.endpoints[0] = new MojoMessagePipeEndpoint(end1); | |
144 pipe.endpoints[1] = new MojoMessagePipeEndpoint(end2); | |
145 pipe.status = new MojoResult(result[0]); | |
146 return pipe; | |
147 } | |
148 } | |
OLD | NEW |