File: gvariant_ref.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (408 lines) | stat: -rw-r--r-- 11,974 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/linux/gvariant_ref.h"

#include <glib.h>
#include <glibconfig.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>

#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/strings/strcat.h"
#include "base/types/expected.h"
#include "remoting/host/base/loggable.h"
#include "remoting/host/linux/gvariant_type.h"

namespace remoting::gvariant {

// GVariantRef implementation

GVariantBase::operator GVariantRef<>() const {
  return GVariantRef<>::Ref(raw());
}

GVariant* GVariantBase::raw() const {
  return variant_.get();
}

GVariant* GVariantBase::release() && {
  return variant_.release();
}

Type<> GVariantBase::GetType() const {
  return Type(g_variant_get_type(variant_.get()));
}

GVariantBase::GVariantBase() = default;

GVariantBase::GVariantBase(GVariantPtr variant)
    : variant_(std::move(variant)) {}

GVariantBase::GVariantBase(const GVariantBase& other) = default;

GVariantBase::GVariantBase(GVariantBase&& other) = default;

GVariantBase& GVariantBase::operator=(const GVariantBase& other) = default;

GVariantBase& GVariantBase::operator=(GVariantBase&& other) = default;

GVariantBase::~GVariantBase() = default;

bool operator==(const GVariantBase& lhs, const GVariantBase& rhs) {
  return g_variant_equal(lhs.raw(), rhs.raw());
}

// GVariantRef implementation

// static
template <Type C>
GVariantRef<C> GVariantRef<C>::Take(GVariant* variant)
  requires(C == Type("*"))
{
  return GVariantRef<C>(GVariantPtr::Take(variant));
}
template GVariantRef<Type("*")> GVariantRef<Type("*")>::Take(GVariant* variant);

// static
template <Type C>
GVariantRef<C> GVariantRef<C>::Ref(GVariant* variant)
  requires(C == Type("*"))
{
  return GVariantRef<C>(GVariantPtr::Ref(variant));
}
template GVariantRef<Type("*")> GVariantRef<Type("*")>::Ref(GVariant* variant);

// static
template <Type C>
GVariantRef<C> GVariantRef<C>::RefSink(GVariant* variant)
  requires(C == Type("*"))
{
  return GVariantRef<C>(GVariantPtr::RefSink(variant));
}
template GVariantRef<Type("*")> GVariantRef<Type("*")>::RefSink(
    GVariant* variant);

// Wrapper implementations

ObjectPathCStr::ObjectPathCStr(const ObjectPath& path LIFETIME_BOUND)
    : path_(path.c_str()) {}

// static
base::expected<ObjectPathCStr, Loggable> ObjectPathCStr::TryFrom(
    const char* path LIFETIME_BOUND) {
  if (g_variant_is_object_path(path)) {
    return base::ok(ObjectPathCStr(path, Checked()));
  } else {
    return base::unexpected(
        Loggable(FROM_HERE,
                 base::StrCat({"String is not a valid object path: ", path})));
  }
}

ObjectPathCStr::ObjectPathCStr(const char* path LIFETIME_BOUND, Checked)
    : path_(path) {}

ObjectPath::ObjectPath() : path_("/") {}

ObjectPath::ObjectPath(ObjectPathCStr path) : path_(path.c_str()) {}

// static
base::expected<ObjectPath, Loggable> ObjectPath::TryFrom(std::string path) {
  return ObjectPathCStr::TryFrom(path.c_str()).transform([&](ObjectPathCStr) {
    return ObjectPath(std::move(path));
  });
}

const std::string& ObjectPath::value() const {
  return path_;
}

const char* ObjectPath::c_str() const {
  return path_.c_str();
}

ObjectPath::ObjectPath(std::string path) : path_(path) {}

TypeSignatureCStr::TypeSignatureCStr(
    const TypeSignature& signature LIFETIME_BOUND)
    : signature_(signature.c_str()) {}

// static
base::expected<TypeSignatureCStr, Loggable> TypeSignatureCStr::TryFrom(
    const char* signature LIFETIME_BOUND) {
  if (g_variant_is_signature(signature)) {
    return base::ok(TypeSignatureCStr(signature, Checked()));
  } else {
    return base::unexpected(Loggable(
        FROM_HERE,
        base::StrCat({"String is not a valid type signature: ", signature})));
  }
}

TypeSignatureCStr::TypeSignatureCStr(const char* signature LIFETIME_BOUND,
                                     Checked)
    : signature_(signature) {}

TypeSignature::TypeSignature() = default;

TypeSignature::TypeSignature(TypeSignatureCStr signature)
    : signature_(signature.c_str()) {}

// static
base::expected<TypeSignature, Loggable> TypeSignature::TryFrom(
    std::string signature) {
  return TypeSignatureCStr::TryFrom(signature.c_str())
      .transform([&](TypeSignatureCStr) {
        return TypeSignature(std::move(signature));
      });
}

const std::string& TypeSignature::value() const {
  return signature_;
}

const char* TypeSignature::c_str() const {
  return signature_.c_str();
}

TypeSignature::TypeSignature(std::string path) : signature_(path) {}

// Mapping implementations

// static
auto Mapping<bool>::From(bool value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_boolean(value));
}

// static
bool Mapping<bool>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_boolean(variant.raw());
}

// static
auto Mapping<std::uint8_t>::From(std::uint8_t value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_byte(value));
}

// static
std::uint8_t Mapping<std::uint8_t>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_byte(variant.raw());
}

// static
auto Mapping<std::int16_t>::From(std::int16_t value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_int16(value));
}

// static
std::int16_t Mapping<std::int16_t>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_int16(variant.raw());
}

// static
auto Mapping<std::uint16_t>::From(std::uint16_t value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_uint16(value));
}

// static
std::uint16_t Mapping<std::uint16_t>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_uint16(variant.raw());
}

// static
auto Mapping<std::int32_t>::From(std::int32_t value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_int32(value));
}

// static
std::int32_t Mapping<std::int32_t>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_int32(variant.raw());
}

// static
auto Mapping<std::uint32_t>::From(std::uint32_t value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_uint32(value));
}

// static
std::uint32_t Mapping<std::uint32_t>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_uint32(variant.raw());
}

// static
auto Mapping<std::int64_t>::From(std::int64_t value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_int64(value));
}

// static
std::int64_t Mapping<std::int64_t>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_int64(variant.raw());
}

// static
auto Mapping<std::uint64_t>::From(std::uint64_t value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_uint64(value));
}

// static
std::uint64_t Mapping<std::uint64_t>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_uint64(variant.raw());
}

// static
auto Mapping<double>::From(double value) -> GVariantRef<kType> {
  return GVariantRef<kType>::TakeUnchecked(g_variant_new_double(value));
}

// static
double Mapping<double>::Into(const GVariantRef<kType>& variant) {
  return g_variant_get_double(variant.raw());
}

// Creates a GVariantRef of a string-like type ("s", "o", or "g") from a
// string_view that has already been verified to be of the correct form for the
// type.
template <Type C>
  requires(C == Type("s") || C == Type("o") || C == Type("g"))
static GVariantRef<C> CreateStringVariantUnchecked(std::string_view value) {
  // g_variant_new_string() can't be used directly because it requires a null-
  // terminated string, which |value| might not be. To avoid making two copies
  // of |value| (one to add a null byte and another to construct the GVariant),
  // we instead create a new backing buffer ourselves for the GVariant to use.

  // The serialized form of a string GVariant is just the string contents
  // followed by a null byte.
  char* data = static_cast<char*>(g_malloc(value.size() + 1));
  std::copy(value.begin(), value.end(), data);
  // SAFETY: We allocate |data| earlier in this function to have a size of
  // value.size() + 1.
  UNSAFE_BUFFERS(data[value.size()] = '\0');

  // GVariant holds the buffer as a reference-counted GBytes object. The GBytes
  // object takes ownership of |data|, and will call g_free on it when the last
  // reference is dropped.
  GBytes* bytes = g_bytes_new_take(data, value.size() + 1);

  // g_variant_new_from_bytes() creates a new GVariant using |bytes| as its
  // backing buffer without making a copy.
  GVariantRef<C> variant = GVariantRef<C>::TakeUnchecked(
      g_variant_new_from_bytes(C.gvariant_type(), bytes, true));

  // g_variant_new_from_bytes() takes its own ref to |bytes|.
  g_bytes_unref(bytes);

  return variant;
}

// static
auto Mapping<std::string>::From(const std::string& value)
    -> GVariantRef<kType> {
  return GVariantRef<kType>::From(std::string_view(value));
}

// static
auto Mapping<std::string>::TryFrom(const std::string& value)
    -> base::expected<GVariantRef<kType>, Loggable> {
  return GVariantRef<kType>::TryFrom(std::string_view(value));
}

// static
std::string Mapping<std::string>::Into(const GVariantRef<kType>& variant) {
  gsize length;
  const char* value = g_variant_get_string(variant.raw(), &length);
  return std::string(value, length);
}

// static
auto Mapping<std::string_view>::From(std::string_view value)
    -> GVariantRef<kType> {
  auto result = TryFrom(value);
  CHECK(result.has_value()) << result.error();
  return result.value();
}

// static
auto Mapping<std::string_view>::TryFrom(std::string_view value)
    -> base::expected<GVariantRef<kType>, Loggable> {
  if (!g_utf8_validate(value.data(), value.length(), nullptr)) {
    return base::unexpected(Loggable(FROM_HERE, "String is not valid UTF-8"));
  }

  return base::ok(CreateStringVariantUnchecked<kType>(value));
}

// static
auto Mapping<const char*>::From(const char* value) -> GVariantRef<kType> {
  return GVariantRef<kType>::From(std::string_view(value));
}

// static
auto Mapping<const char*>::TryFrom(const char* value)
    -> base::expected<GVariantRef<kType>, Loggable> {
  return GVariantRef<kType>::TryFrom(std::string_view(value));
}

// static
Ignored Mapping<Ignored>::Into(const GVariantRef<kType>& variant) {
  return Ignored();
}

// static
decltype(std::ignore) Mapping<decltype(std::ignore)>::Into(
    const GVariantRef<kType>& variant) {
  return std::ignore;
}

// static
auto Mapping<ObjectPathCStr>::From(const ObjectPathCStr& value)
    -> GVariantRef<kType> {
  return CreateStringVariantUnchecked<kType>(value.c_str());
}

// static
auto Mapping<ObjectPath>::From(const ObjectPath& value) -> GVariantRef<kType> {
  return CreateStringVariantUnchecked<kType>(value.value());
}

// static
ObjectPath Mapping<ObjectPath>::Into(const GVariantRef<kType>& variant) {
  gsize length;
  const char* value = g_variant_get_string(variant.raw(), &length);

  // Value is already guaranteed to be a valid object path.
  return ObjectPath(std::string(value, length));
}

// static
auto Mapping<TypeSignatureCStr>::From(const TypeSignatureCStr& value)
    -> GVariantRef<kType> {
  return CreateStringVariantUnchecked<kType>(value.c_str());
}

// static
auto Mapping<TypeSignature>::From(const TypeSignature& value)
    -> GVariantRef<kType> {
  return CreateStringVariantUnchecked<kType>(value.value());
}

// static
TypeSignature Mapping<TypeSignature>::Into(const GVariantRef<kType>& variant) {
  gsize length;
  const char* value = g_variant_get_string(variant.raw(), &length);

  // Value is already guaranteed to be a valid object path.
  return TypeSignature(std::string(value, length));
}

}  // namespace remoting::gvariant