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