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 class _MojoSharedBufferNatives { | |
8 static List Create(int numBytes, int flags) | |
9 native "MojoSharedBuffer_Create"; | |
10 | |
11 static List Duplicate(int bufferHandle, int flags) | |
12 native "MojoSharedBuffer_Duplicate"; | |
13 | |
14 static List Map(MojoSharedBuffer buffer, | |
15 int bufferHandle, | |
16 int offset, | |
17 int numBytes, | |
18 int flags) | |
19 native "MojoSharedBuffer_Map"; | |
20 | |
21 static int Unmap(ByteData buffer) | |
22 native "MojoSharedBuffer_Unmap"; | |
23 } | |
24 | |
25 | |
26 class MojoSharedBuffer { | 7 class MojoSharedBuffer { |
27 static const int CREATE_FLAG_NONE = 0; | 8 static const int CREATE_FLAG_NONE = 0; |
28 static const int DUPLICATE_FLAG_NONE = 0; | 9 static const int DUPLICATE_FLAG_NONE = 0; |
29 static const int MAP_FLAG_NONE = 0; | 10 static const int MAP_FLAG_NONE = 0; |
30 | 11 |
31 MojoHandle handle; | 12 MojoHandle handle; |
32 MojoResult status; | 13 MojoResult status; |
33 ByteData mapping; | 14 ByteData mapping; |
34 | 15 |
35 MojoSharedBuffer( | 16 MojoSharedBuffer(this.handle, |
36 this.handle, [this.status = MojoResult.OK, this.mapping = null]); | 17 [this.status = MojoResult.OK, this.mapping = null]); |
37 | 18 |
38 factory MojoSharedBuffer.create(int numBytes, [int flags = 0]) { | 19 factory MojoSharedBuffer.create(int numBytes, [int flags = 0]) { |
39 List result = _MojoSharedBufferNatives.Create(numBytes, flags); | 20 List result = MojoSharedBufferNatives.Create(numBytes, flags); |
40 if (result == null) { | 21 if (result == null) { |
41 return null; | 22 return null; |
42 } | 23 } |
43 assert((result is List) && (result.length == 2)); | 24 assert((result is List) && (result.length == 2)); |
44 var r = new MojoResult(result[0]); | 25 var r = new MojoResult(result[0]); |
45 if (!r.isOk) { | 26 if (!r.isOk) { |
46 return null; | 27 return null; |
47 } | 28 } |
48 | 29 |
49 MojoSharedBuffer buf = | 30 MojoSharedBuffer buf = |
50 new MojoSharedBuffer(new MojoHandle(result[1]), r, null); | 31 new MojoSharedBuffer(new MojoHandle(result[1]), r, null); |
51 return buf; | 32 return buf; |
52 } | 33 } |
53 | 34 |
54 factory MojoSharedBuffer.duplicate(MojoSharedBuffer msb, [int flags = 0]) { | 35 factory MojoSharedBuffer.duplicate(MojoSharedBuffer msb, [int flags = 0]) { |
55 List result = _MojoSharedBufferNatives.Duplicate(msb.handle.h, flags); | 36 List result = MojoSharedBufferNatives.Duplicate(msb.handle.h, flags); |
56 if (result == null) { | 37 if (result == null) { |
57 return null; | 38 return null; |
58 } | 39 } |
59 assert((result is List) && (result.length == 2)); | 40 assert((result is List) && (result.length == 2)); |
60 var r = new MojoResult(result[0]); | 41 var r = new MojoResult(result[0]); |
61 if(!r.isOk) { | 42 if (!r.isOk) { |
62 return null; | 43 return null; |
63 } | 44 } |
64 | 45 |
65 MojoSharedBuffer dupe = | 46 MojoSharedBuffer dupe = |
66 new MojoSharedBuffer(new MojoHandle(result[1]), r, null); | 47 new MojoSharedBuffer(new MojoHandle(result[1]), r, null); |
67 return dupe; | 48 return dupe; |
68 } | 49 } |
69 | 50 |
70 MojoResult close() { | 51 MojoResult close() { |
71 if (handle == null) { | 52 if (handle == null) { |
72 status = MojoResult.INVALID_ARGUMENT; | 53 status = MojoResult.INVALID_ARGUMENT; |
73 return status; | 54 return status; |
74 } | 55 } |
75 MojoResult r = handle.close(); | 56 MojoResult r = handle.close(); |
76 status = r; | 57 status = r; |
77 mapping = null; | 58 mapping = null; |
78 return status; | 59 return status; |
79 } | 60 } |
80 | 61 |
81 MojoResult map(int offset, int numBytes, [int flags = 0]) { | 62 MojoResult map(int offset, int numBytes, [int flags = 0]) { |
82 if (handle == null) { | 63 if (handle == null) { |
83 status = MojoResult.INVALID_ARGUMENT; | 64 status = MojoResult.INVALID_ARGUMENT; |
84 return status; | 65 return status; |
85 } | 66 } |
86 List result = _MojoSharedBufferNatives.Map( | 67 List result = |
87 this, handle.h, offset, numBytes, flags); | 68 MojoSharedBufferNatives.Map(this, handle.h, offset, numBytes, flags); |
88 if (result == null) { | 69 if (result == null) { |
89 status = MojoResult.INVALID_ARGUMENT; | 70 status = MojoResult.INVALID_ARGUMENT; |
90 return status; | 71 return status; |
91 } | 72 } |
92 assert((result is List) && (result.length == 2)); | 73 assert((result is List) && (result.length == 2)); |
93 status = new MojoResult(result[0]); | 74 status = new MojoResult(result[0]); |
94 mapping = result[1]; | 75 mapping = result[1]; |
95 return status; | 76 return status; |
96 } | 77 } |
97 | 78 |
98 MojoResult unmap() { | 79 MojoResult unmap() { |
99 int r = _MojoSharedBufferNatives.Unmap(mapping); | 80 int r = MojoSharedBufferNatives.Unmap(mapping); |
100 status = new MojoResult(r); | 81 status = new MojoResult(r); |
101 mapping = null; | 82 mapping = null; |
102 return status; | 83 return status; |
103 } | 84 } |
104 } | 85 } |
OLD | NEW |