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