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/string16_mojom_traits.h"
6
7 #include "mojo/public/cpp/base/big_buffer_mojom_traits.h"
8
9 namespace mojo {
10
11 // static
Read(mojo_base::mojom::String16DataView data,base::string16 * out)12 bool StructTraits<mojo_base::mojom::String16DataView, base::string16>::Read(
13 mojo_base::mojom::String16DataView data,
14 base::string16* out) {
15 ArrayDataView<uint16_t> view;
16 data.GetDataDataView(&view);
17 out->assign(reinterpret_cast<const base::char16*>(view.data()), view.size());
18 return true;
19 }
20
21 // static
22 mojo_base::BigBuffer
data(const base::string16 & str)23 StructTraits<mojo_base::mojom::BigString16DataView, base::string16>::data(
24 const base::string16& str) {
25 const auto* bytes = reinterpret_cast<const uint8_t*>(str.data());
26 return mojo_base::BigBuffer(
27 base::make_span(bytes, str.size() * sizeof(base::char16)));
28 }
29
30 // static
Read(mojo_base::mojom::BigString16DataView data,base::string16 * out)31 bool StructTraits<mojo_base::mojom::BigString16DataView, base::string16>::Read(
32 mojo_base::mojom::BigString16DataView data,
33 base::string16* out) {
34 mojo_base::BigBuffer buffer;
35 if (!data.ReadData(&buffer))
36 return false;
37 if (buffer.size() % sizeof(base::char16))
38 return false;
39 *out = base::string16(reinterpret_cast<const base::char16*>(buffer.data()),
40 buffer.size() / sizeof(base::char16));
41 return true;
42 }
43
44 } // namespace mojo
45