blob: 53d39adba1b28ffb86cc13033410fd47150c7445 [file] [log] [blame]
Nan Lin95a2b1402022-11-10 14:03:461// Copyright 2022 The Chromium Authors
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 "components/attribution_reporting/event_trigger_data.h"
6
Nan Lin35cc46af62022-11-15 22:24:497#include <stdint.h>
8
Nan Lin95a2b1402022-11-10 14:03:469#include <utility>
10
Nan Lin35cc46af62022-11-15 22:24:4911#include "base/types/expected.h"
12#include "base/values.h"
13#include "components/attribution_reporting/filters.h"
14#include "components/attribution_reporting/parsing_utils.h"
15#include "components/attribution_reporting/trigger_registration_error.mojom.h"
16
Nan Lin95a2b1402022-11-10 14:03:4617namespace attribution_reporting {
18
Nan Lin35cc46af62022-11-15 22:24:4919namespace {
20
21using ::attribution_reporting::mojom::TriggerRegistrationError;
22
23} // namespace
24
25// static
26base::expected<EventTriggerData, TriggerRegistrationError>
27EventTriggerData::FromJSON(base::Value& value) {
28 base::Value::Dict* dict = value.GetIfDict();
29 if (!dict) {
30 return base::unexpected(
31 TriggerRegistrationError::kEventTriggerDataWrongType);
32 }
33
34 auto filters = Filters::FromJSON(dict->Find("filters"));
35 if (!filters.has_value())
36 return base::unexpected(filters.error());
37
38 auto not_filters = Filters::FromJSON(dict->Find("not_filters"));
39 if (!not_filters.has_value())
40 return base::unexpected(not_filters.error());
41
42 uint64_t data = ParseUint64(*dict, "trigger_data").value_or(0);
43 int64_t priority = ParsePriority(*dict);
44 absl::optional<uint64_t> dedup_key = ParseUint64(*dict, "deduplication_key");
45
46 return EventTriggerData(data, priority, dedup_key, std::move(*filters),
47 std::move(*not_filters));
48}
49
50EventTriggerData::EventTriggerData() = default;
51
Nan Lin95a2b1402022-11-10 14:03:4652EventTriggerData::EventTriggerData(uint64_t data,
53 int64_t priority,
54 absl::optional<uint64_t> dedup_key,
55 Filters filters,
56 Filters not_filters)
57 : data(data),
58 priority(priority),
59 dedup_key(dedup_key),
60 filters(std::move(filters)),
61 not_filters(std::move(not_filters)) {}
62
63} // namespace attribution_reporting