blob: 44673157e5c5b34afa0c56ff42184add3ab9d821 [file] [log] [blame]
jamesra9125262014-11-19 01:35:281// 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
5part of core;
6
7class _MojoSharedBufferNatives {
Colin Blundell835d018d32015-01-19 12:17:508 static List Create(int numBytes, int flags)
jamesra9125262014-11-19 01:35:289 native "MojoSharedBuffer_Create";
10
Colin Blundell835d018d32015-01-19 12:17:5011 static List Duplicate(int bufferHandle, int flags)
jamesra9125262014-11-19 01:35:2812 native "MojoSharedBuffer_Duplicate";
13
rockotfa648a322014-12-09 06:41:3714 static List Map(MojoSharedBuffer buffer,
Colin Blundell835d018d32015-01-19 12:17:5015 int bufferHandle,
rockotfa648a322014-12-09 06:41:3716 int offset,
Colin Blundell835d018d32015-01-19 12:17:5017 int numBytes,
rockotfa648a322014-12-09 06:41:3718 int flags)
jamesra9125262014-11-19 01:35:2819 native "MojoSharedBuffer_Map";
20
21 static int Unmap(ByteData buffer)
22 native "MojoSharedBuffer_Unmap";
23}
24
25
26class 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
rockot6dcbf7c2015-01-16 00:41:1331 MojoHandle handle;
jamesra9125262014-11-19 01:35:2832 MojoResult status;
33 ByteData mapping;
34
Colin Blundell835d018d32015-01-19 12:17:5035 MojoSharedBuffer(
36 this.handle, [this.status = MojoResult.OK, this.mapping = null]);
jamesra9125262014-11-19 01:35:2837
Colin Blundell835d018d32015-01-19 12:17:5038 factory MojoSharedBuffer.create(int numBytes, [int flags = 0]) {
39 List result = _MojoSharedBufferNatives.Create(numBytes, flags);
jamesra9125262014-11-19 01:35:2840 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
Colin Blundell835d018d32015-01-19 12:17:5049 MojoSharedBuffer buf =
50 new MojoSharedBuffer(new MojoHandle(result[1]), r, null);
jamesra9125262014-11-19 01:35:2851 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
Colin Blundell835d018d32015-01-19 12:17:5065 MojoSharedBuffer dupe =
66 new MojoSharedBuffer(new MojoHandle(result[1]), r, null);
jamesra9125262014-11-19 01:35:2867 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
Colin Blundell835d018d32015-01-19 12:17:5081 MojoResult map(int offset, int numBytes, [int flags = 0]) {
jamesra9125262014-11-19 01:35:2882 if (handle == null) {
83 status = MojoResult.INVALID_ARGUMENT;
84 return status;
85 }
86 List result = _MojoSharedBufferNatives.Map(
Colin Blundell835d018d32015-01-19 12:17:5087 this, handle.h, offset, numBytes, flags);
jamesra9125262014-11-19 01:35:2888 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}