Nan Lin | 95a2b140 | 2022-11-10 14:03:46 | [diff] [blame] | 1 | // 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 Lin | 35cc46af6 | 2022-11-15 22:24:49 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
Nan Lin | 95a2b140 | 2022-11-10 14:03:46 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
Nan Lin | 35cc46af6 | 2022-11-15 22:24:49 | [diff] [blame] | 11 | #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 Lin | 95a2b140 | 2022-11-10 14:03:46 | [diff] [blame] | 17 | namespace attribution_reporting { |
| 18 | |
Nan Lin | 35cc46af6 | 2022-11-15 22:24:49 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | using ::attribution_reporting::mojom::TriggerRegistrationError; |
| 22 | |
| 23 | } // namespace |
| 24 | |
| 25 | // static |
| 26 | base::expected<EventTriggerData, TriggerRegistrationError> |
| 27 | EventTriggerData::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 | |
| 50 | EventTriggerData::EventTriggerData() = default; |
| 51 | |
Nan Lin | 95a2b140 | 2022-11-10 14:03:46 | [diff] [blame] | 52 | EventTriggerData::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 |