1 // Copyright 2018 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 #include "mojo/public/cpp/base/big_buffer_mojom_traits.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "mojo/public/cpp/bindings/array_data_view.h"
12
13 namespace mojo {
14
15 // static
16 uint32_t StructTraits<mojo_base::mojom::BigBufferSharedMemoryRegionDataView,
17 mojo_base::internal::BigBufferSharedMemoryRegion>::
size(const mojo_base::internal::BigBufferSharedMemoryRegion & region)18 size(const mojo_base::internal::BigBufferSharedMemoryRegion& region) {
19 return base::checked_cast<uint32_t>(region.size());
20 }
21
22 // static
23 ScopedSharedBufferHandle
24 StructTraits<mojo_base::mojom::BigBufferSharedMemoryRegionDataView,
25 mojo_base::internal::BigBufferSharedMemoryRegion>::
buffer_handle(mojo_base::internal::BigBufferSharedMemoryRegion & region)26 buffer_handle(mojo_base::internal::BigBufferSharedMemoryRegion& region) {
27 return region.TakeBufferHandle();
28 }
29
30 // static
31 bool StructTraits<mojo_base::mojom::BigBufferSharedMemoryRegionDataView,
32 mojo_base::internal::BigBufferSharedMemoryRegion>::
Read(mojo_base::mojom::BigBufferSharedMemoryRegionDataView data,mojo_base::internal::BigBufferSharedMemoryRegion * out)33 Read(mojo_base::mojom::BigBufferSharedMemoryRegionDataView data,
34 mojo_base::internal::BigBufferSharedMemoryRegion* out) {
35 *out = mojo_base::internal::BigBufferSharedMemoryRegion(
36 data.TakeBufferHandle(), data.size());
37 return out->memory() != nullptr;
38 }
39
40 // static
41 mojo_base::mojom::BigBufferDataView::Tag
GetTag(const mojo_base::BigBuffer & buffer)42 UnionTraits<mojo_base::mojom::BigBufferDataView, mojo_base::BigBuffer>::GetTag(
43 const mojo_base::BigBuffer& buffer) {
44 switch (buffer.storage_type()) {
45 case mojo_base::BigBuffer::StorageType::kBytes:
46 return mojo_base::mojom::BigBufferDataView::Tag::BYTES;
47 case mojo_base::BigBuffer::StorageType::kSharedMemory:
48 return mojo_base::mojom::BigBufferDataView::Tag::SHARED_MEMORY;
49 }
50
51 NOTREACHED();
52 return mojo_base::mojom::BigBufferDataView::Tag::BYTES;
53 }
54
55 // static
56 const std::vector<uint8_t>&
bytes(const mojo_base::BigBuffer & buffer)57 UnionTraits<mojo_base::mojom::BigBufferDataView, mojo_base::BigBuffer>::bytes(
58 const mojo_base::BigBuffer& buffer) {
59 return buffer.bytes();
60 }
61
62 // static
63 mojo_base::internal::BigBufferSharedMemoryRegion&
64 UnionTraits<mojo_base::mojom::BigBufferDataView,
shared_memory(mojo_base::BigBuffer & buffer)65 mojo_base::BigBuffer>::shared_memory(mojo_base::BigBuffer& buffer) {
66 return buffer.shared_memory();
67 }
68
69 // static
70 bool UnionTraits<mojo_base::mojom::BigBufferDataView, mojo_base::BigBuffer>::
Read(mojo_base::mojom::BigBufferDataView data,mojo_base::BigBuffer * out)71 Read(mojo_base::mojom::BigBufferDataView data, mojo_base::BigBuffer* out) {
72 switch (data.tag()) {
73 case mojo_base::mojom::BigBufferDataView::Tag::BYTES: {
74 mojo::ArrayDataView<uint8_t> bytes_view;
75 data.GetBytesDataView(&bytes_view);
76 *out = mojo_base::BigBuffer(bytes_view);
77 return true;
78 }
79
80 case mojo_base::mojom::BigBufferDataView::Tag::SHARED_MEMORY: {
81 mojo_base::internal::BigBufferSharedMemoryRegion shared_memory;
82 if (!data.ReadSharedMemory(&shared_memory))
83 return false;
84 *out = mojo_base::BigBuffer(std::move(shared_memory));
85 return true;
86 }
87 }
88
89 return false;
90 }
91
92 // static
93 mojo_base::mojom::BigBufferDataView::Tag UnionTraits<
94 mojo_base::mojom::BigBufferDataView,
GetTag(const mojo_base::BigBufferView & view)95 mojo_base::BigBufferView>::GetTag(const mojo_base::BigBufferView& view) {
96 switch (view.storage_type()) {
97 case mojo_base::BigBuffer::StorageType::kBytes:
98 return mojo_base::mojom::BigBufferDataView::Tag::BYTES;
99 case mojo_base::BigBuffer::StorageType::kSharedMemory:
100 return mojo_base::mojom::BigBufferDataView::Tag::SHARED_MEMORY;
101 }
102
103 NOTREACHED();
104 return mojo_base::mojom::BigBufferDataView::Tag::BYTES;
105 }
106
107 // static
108 base::span<const uint8_t> UnionTraits<
109 mojo_base::mojom::BigBufferDataView,
bytes(const mojo_base::BigBufferView & view)110 mojo_base::BigBufferView>::bytes(const mojo_base::BigBufferView& view) {
111 return view.bytes();
112 }
113
114 // static
115 mojo_base::internal::BigBufferSharedMemoryRegion& UnionTraits<
116 mojo_base::mojom::BigBufferDataView,
shared_memory(mojo_base::BigBufferView & view)117 mojo_base::BigBufferView>::shared_memory(mojo_base::BigBufferView& view) {
118 return view.shared_memory();
119 }
120
121 // static
122 bool UnionTraits<
123 mojo_base::mojom::BigBufferDataView,
Read(mojo_base::mojom::BigBufferDataView data,mojo_base::BigBufferView * out)124 mojo_base::BigBufferView>::Read(mojo_base::mojom::BigBufferDataView data,
125 mojo_base::BigBufferView* out) {
126 switch (data.tag()) {
127 case mojo_base::mojom::BigBufferDataView::Tag::BYTES: {
128 mojo::ArrayDataView<uint8_t> bytes_view;
129 data.GetBytesDataView(&bytes_view);
130 out->SetBytes(bytes_view);
131 return true;
132 }
133
134 case mojo_base::mojom::BigBufferDataView::Tag::SHARED_MEMORY: {
135 mojo_base::internal::BigBufferSharedMemoryRegion shared_memory;
136 if (!data.ReadSharedMemory(&shared_memory))
137 return false;
138 out->SetSharedMemory(std::move(shared_memory));
139 return true;
140 }
141 }
142
143 return false;
144 }
145
146 } // namespace mojo
147