xref: /aosp_15_r20/external/cronet/net/reporting/reporting_header_parser.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 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 #ifndef NET_REPORTING_REPORTING_HEADER_PARSER_H_
6 #define NET_REPORTING_REPORTING_HEADER_PARSER_H_
7 
8 #include <memory>
9 #include <optional>
10 
11 #include "base/containers/flat_map.h"
12 #include "base/values.h"
13 #include "net/base/net_export.h"
14 #include "net/http/structured_headers.h"
15 #include "url/gurl.h"
16 #include "url/origin.h"
17 
18 namespace net {
19 
20 class IsolationInfo;
21 class NetworkAnonymizationKey;
22 class ReportingContext;
23 
24 // Tries to parse a Reporting-Endpoints header. Returns base::nullopt if parsing
25 // failed and the header should be ignored; otherwise returns a (possibly
26 // empty) mapping of endpoint names to URLs.
27 NET_EXPORT
28 std::optional<base::flat_map<std::string, std::string>> ParseReportingEndpoints(
29     const std::string& header);
30 
31 class NET_EXPORT ReportingHeaderParser {
32  public:
33   // These values are persisted to logs. Entries should not be renumbered and
34   // numeric values should never be reused.
35   // They should also be kept in sync with the NetReportingHeaderType enum
36   // in tools/metrics/histograms/enums.xml
37   enum class ReportingHeaderType {
38     kReportTo = 0,
39     kReportToInvalid = 1,
40     kReportingEndpoints = 2,
41     kReportingEndpointsInvalid = 3,
42     kMaxValue = kReportingEndpointsInvalid,
43   };
44 
45   ReportingHeaderParser() = delete;
46   ReportingHeaderParser(const ReportingHeaderParser&) = delete;
47   ReportingHeaderParser& operator=(const ReportingHeaderParser&) = delete;
48 
49   static void ParseReportToHeader(
50       ReportingContext* context,
51       const NetworkAnonymizationKey& network_anonymization_key,
52       const url::Origin& origin,
53       const base::Value::List& list);
54 
55   // `isolation_info` here will be stored in the cache, associated with the
56   // `reporting_source`. `network_anonymization_key` is the NAK which will be
57   // passed in with reports to be queued. This must match the NAK from
58   // `isolation_source`, unless it is empty (which will be the case if the
59   // network state partitioning is disabled).
60   static void ProcessParsedReportingEndpointsHeader(
61       ReportingContext* context,
62       const base::UnguessableToken& reporting_source,
63       const IsolationInfo& isolation_info,
64       const NetworkAnonymizationKey& network_anonymization_key,
65       const url::Origin& origin,
66       base::flat_map<std::string, std::string> parsed_header);
67 
68   static void RecordReportingHeaderType(ReportingHeaderType header_type);
69 };
70 
71 }  // namespace net
72 
73 #endif  // NET_REPORTING_REPORTING_HEADER_PARSER_H_
74