blob: 85815ffd625082c1707f33a110441f3f8aa5797d [file] [log] [blame]
Hidehiko Abeb268b432018-04-24 01:37:19 +09001// Copyright 2016 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#ifndef MOJO_COMMON_COMMON_CUSTOM_TYPES_STRUCT_TRAITS_H_
6#define MOJO_COMMON_COMMON_CUSTOM_TYPES_STRUCT_TRAITS_H_
7
8#include "base/files/file.h"
9#include "base/i18n/rtl.h"
10#include "base/strings/utf_string_conversions.h"
11#include "base/unguessable_token.h"
12#include "base/version.h"
13#include "mojo/common/file.mojom-shared.h"
14#include "mojo/common/mojo_common_export.h"
15#include "mojo/common/string16.mojom-shared.h"
16#include "mojo/common/text_direction.mojom-shared.h"
17#include "mojo/common/time.mojom-shared.h"
18#include "mojo/common/unguessable_token.mojom-shared.h"
19#include "mojo/common/version.mojom-shared.h"
20
21namespace mojo {
22
23template <>
24struct StructTraits<common::mojom::String16DataView, base::string16> {
25 static ConstCArray<uint16_t> data(const base::string16& str) {
26 return ConstCArray<uint16_t>(str.size(),
27 reinterpret_cast<const uint16_t*>(str.data()));
28 }
29
30 static bool Read(common::mojom::String16DataView data, base::string16* out);
31};
32
33template <>
34struct StructTraits<common::mojom::VersionDataView, base::Version> {
35 static bool IsNull(const base::Version& version) {
36 return !version.IsValid();
37 }
38 static void SetToNull(base::Version* out) {
39 *out = base::Version(std::string());
40 }
41 static const std::vector<uint32_t>& components(const base::Version& version);
42 static bool Read(common::mojom::VersionDataView data, base::Version* out);
43};
44
45// If base::UnguessableToken is no longer 128 bits, the logic below and the
46// mojom::UnguessableToken type should be updated.
47static_assert(sizeof(base::UnguessableToken) == 2 * sizeof(uint64_t),
48 "base::UnguessableToken should be of size 2 * sizeof(uint64_t).");
49
50template <>
51struct StructTraits<common::mojom::UnguessableTokenDataView,
52 base::UnguessableToken> {
53 static uint64_t high(const base::UnguessableToken& token) {
54 return token.GetHighForSerialization();
55 }
56
57 static uint64_t low(const base::UnguessableToken& token) {
58 return token.GetLowForSerialization();
59 }
60
61 static bool Read(common::mojom::UnguessableTokenDataView data,
62 base::UnguessableToken* out);
63};
64
65template <>
66struct StructTraits<common::mojom::FileDataView, base::File> {
67 static bool IsNull(const base::File& file) { return !file.IsValid(); }
68
69 static void SetToNull(base::File* file) { *file = base::File(); }
70
71 static mojo::ScopedHandle fd(base::File& file);
72 static bool Read(common::mojom::FileDataView data, base::File* file);
73};
74
75template <>
76struct EnumTraits<common::mojom::TextDirection, base::i18n::TextDirection> {
77 static common::mojom::TextDirection ToMojom(
78 base::i18n::TextDirection text_direction);
79 static bool FromMojom(common::mojom::TextDirection input,
80 base::i18n::TextDirection* out);
81};
82
83} // namespace mojo
84
85#endif // MOJO_COMMON_COMMON_CUSTOM_TYPES_STRUCT_TRAITS_H_