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