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