File: parsing_utils.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (133 lines) | stat: -rw-r--r-- 4,489 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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_ATTRIBUTION_REPORTING_PARSING_UTILS_H_
#define COMPONENTS_ATTRIBUTION_REPORTING_PARSING_UTILS_H_

#include <stddef.h>
#include <stdint.h>

#include <concepts>
#include <optional>
#include <string>
#include <string_view>

#include "base/component_export.h"
#include "base/containers/flat_set.h"
#include "base/types/expected.h"
#include "third_party/abseil-cpp/absl/numeric/int128.h"

namespace base {
class DictValue;
class ListValue;
class TimeDelta;
class Value;
}  // namespace base

namespace attribution_reporting {

class SuitableOrigin;

struct ParseError {
  friend bool operator==(ParseError, ParseError) = default;
};

COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<absl::uint128, ParseError> ParseAggregationKeyPiece(
    const base::Value&);

COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
std::string HexEncodeAggregationKey(absl::uint128);

template <typename T>
  requires(std::integral<T>)
constexpr T ValueOrZero(std::optional<T> value) {
  return value.value_or(0);
}

COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<std::optional<uint64_t>, ParseError> ParseUint64(
    const base::DictValue&,
    std::string_view key);

COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<std::optional<int64_t>, ParseError> ParseInt64(
    const base::DictValue&,
    std::string_view key);

base::expected<int64_t, ParseError> ParsePriority(const base::DictValue&);

// Returns `debug_key` value as we do not need to fail the source registration
// if the value is invalid, see
// https://github.com/WICG/attribution-reporting-api/issues/793 for context.
std::optional<uint64_t> ParseDebugKey(const base::DictValue& dict);

// Returns false if `dict` contains `debug_reporting` key but the value is
// invalid, returns true otherwise.
[[nodiscard]] bool ParseDebugReporting(const base::DictValue& dict);

base::expected<std::optional<uint64_t>, ParseError> ParseDeduplicationKey(
    const base::DictValue&);

// The given value must be a non-negative `int`, or a non-negative `double`
// without a fractional part, or a string containing a base-10-formatted
// unsigned 64-bit integer. That value is interpreted as a number of seconds
// clamped to the given range.
base::expected<base::TimeDelta, ParseError> ParseLegacyDuration(
    const base::Value&,
    base::TimeDelta clamp_min,
    base::TimeDelta clamp_max);

// The given value must be an `int` or a `double` without a fractional part.
// That value is interpreted as a number of seconds. The only clamping applied
// is that of `base::TimeDelta` itself, which only affects extremely large
// `double` values that for the purposes of Attribution Reporting are
// effectively infinity and will be clamped or tolerated properly elsewhere.
base::expected<base::TimeDelta, ParseError> ParseDuration(const base::Value&);

base::expected<std::optional<SuitableOrigin>, ParseError>
ParseAggregationCoordinator(const base::DictValue&);

base::expected<int, ParseError> ParseAggregatableValue(const base::Value&);

void SerializeUint64(base::DictValue&, std::string_view key, uint64_t value);

void SerializeInt64(base::DictValue&, std::string_view key, int64_t value);

void SerializePriority(base::DictValue&, int64_t priority);

void SerializeDebugKey(base::DictValue&, std::optional<uint64_t> debug_key);

void SerializeDebugReporting(base::DictValue&, bool debug_reporting);

void SerializeDeduplicationKey(base::DictValue&,
                               std::optional<uint64_t> dedup_key);

void SerializeTimeDeltaInSeconds(base::DictValue& dict,
                                 std::string_view key,
                                 base::TimeDelta value);

COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<int, ParseError> ParseInt(const base::Value&);

COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<uint32_t, ParseError> ParseUint32(const base::Value&);

COMPONENT_EXPORT(ATTRIBUTION_REPORTING)
base::expected<uint32_t, ParseError> ParsePositiveUint32(const base::Value&);

base::Value Uint32ToJson(uint32_t);

enum class StringSetError {
  kWrongType,
  kStringTooLong,
  kSetTooLong,
};

base::expected<base::flat_set<std::string>, StringSetError>
ExtractStringSet(base::ListValue, size_t max_string_size, size_t max_set_size);

}  // namespace attribution_reporting

#endif  // COMPONENTS_ATTRIBUTION_REPORTING_PARSING_UTILS_H_