xref: /aosp_15_r20/external/cronet/net/reporting/reporting_service.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_SERVICE_H_
6 #define NET_REPORTING_REPORTING_SERVICE_H_
7 
8 #include <memory>
9 #include <optional>
10 #include <string>
11 
12 #include "base/containers/flat_map.h"
13 #include "base/functional/callback.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/unguessable_token.h"
16 #include "net/base/net_export.h"
17 #include "net/reporting/reporting_cache.h"
18 #include "net/reporting/reporting_cache_observer.h"
19 
20 class GURL;
21 
22 namespace base {
23 class Value;
24 }  // namespace base
25 
26 namespace url {
27 class Origin;
28 }  // namespace url
29 
30 namespace net {
31 
32 class IsolationInfo;
33 class NetworkAnonymizationKey;
34 class ReportingContext;
35 struct ReportingPolicy;
36 class URLRequestContext;
37 
38 // The external interface to the Reporting system, used by the embedder of //net
39 // and also other parts of //net.
40 class NET_EXPORT ReportingService {
41  public:
42   ReportingService(const ReportingService&) = delete;
43   ReportingService& operator=(const ReportingService&) = delete;
44 
45   virtual ~ReportingService();
46 
47   // Creates a ReportingService. |policy| will be copied. |request_context| must
48   // outlive the ReportingService. |store| must outlive the ReportingService.
49   // If |store| is null, the ReportingCache will be in-memory only.
50   static std::unique_ptr<ReportingService> Create(
51       const ReportingPolicy& policy,
52       URLRequestContext* request_context,
53       ReportingCache::PersistentReportingStore* store);
54 
55   // Creates a ReportingService for testing purposes using an
56   // already-constructed ReportingContext. The ReportingService will take
57   // ownership of |reporting_context| and destroy it when the service is
58   // destroyed.
59   static std::unique_ptr<ReportingService> CreateForTesting(
60       std::unique_ptr<ReportingContext> reporting_context);
61 
62   // Queues a report for delivery. |url| is the URL that originated the report.
63   // |reporting_source| is the reporting source token for the document or
64   // worker which triggered this report, if it can be associated with one, or
65   // nullopt otherwise. If present, it may not be empty.
66   // Along with |network_anonymization_key|, it is used to restrict what reports
67   // can be merged, and for sending the report.
68   // |user_agent| is the User-Agent header that was used for the request.
69   // |group| is the endpoint group to which the report should be delivered.
70   // |type| is the type of the report. |body| is the body of the report.
71   //
72   // The Reporting system will take ownership of |body|; all other parameters
73   // will be copied.
74   virtual void QueueReport(
75       const GURL& url,
76       const std::optional<base::UnguessableToken>& reporting_source,
77       const NetworkAnonymizationKey& network_anonymization_key,
78       const std::string& user_agent,
79       const std::string& group,
80       const std::string& type,
81       base::Value::Dict body,
82       int depth) = 0;
83 
84   // Processes a Report-To header. |origin| is the Origin of the URL that the
85   // header came from; |header_value| is the normalized value of the Report-To
86   // header.
87   virtual void ProcessReportToHeader(
88       const url::Origin& origin,
89       const NetworkAnonymizationKey& network_anonymization_key,
90       const std::string& header_value) = 0;
91 
92   // Configures reporting endpoints set by the Reporting-Endpoints header, once
93   // the associated document or worker (represented by |reporting_source|) has
94   // been committed.
95   // |reporting_source| is the unique identifier for the resource with which
96   // this header was received, and must not be empty.
97   // |endpoints| is a mapping of endpoint names to URLs.
98   // |origin| is the origin of the reporting source, and
99   // |isolation_info| is the appropriate IsolationInfo struct for that source.
100   virtual void SetDocumentReportingEndpoints(
101       const base::UnguessableToken& reporting_source,
102       const url::Origin& origin,
103       const IsolationInfo& isolation_info,
104       const base::flat_map<std::string, std::string>& endpoints) = 0;
105 
106   // Attempts to send any queued reports and removes all associated
107   // configuration for `reporting_source`. This is called when a source is
108   // destroyed.
109   virtual void SendReportsAndRemoveSource(
110       const base::UnguessableToken& reporting_source) = 0;
111 
112   // Removes browsing data from the Reporting system. See
113   // ReportingBrowsingDataRemover for more details.
114   virtual void RemoveBrowsingData(
115       uint64_t data_type_mask,
116       const base::RepeatingCallback<bool(const url::Origin&)>&
117           origin_filter) = 0;
118 
119   // Like RemoveBrowsingData except removes data for all origins without a
120   // filter.
121   virtual void RemoveAllBrowsingData(uint64_t data_type_mask) = 0;
122 
123   // Shuts down the Reporting service so that no new headers or reports are
124   // processed, and pending uploads are cancelled.
125   virtual void OnShutdown() = 0;
126 
127   virtual const ReportingPolicy& GetPolicy() const = 0;
128 
129   virtual base::Value StatusAsValue() const;
130 
131   virtual std::vector<raw_ptr<const ReportingReport, VectorExperimental>>
132   GetReports() const = 0;
133 
134   virtual base::flat_map<url::Origin, std::vector<ReportingEndpoint>>
135   GetV1ReportingEndpointsByOrigin() const = 0;
136 
137   virtual void AddReportingCacheObserver(ReportingCacheObserver* observer) = 0;
138   virtual void RemoveReportingCacheObserver(
139       ReportingCacheObserver* observer) = 0;
140 
141   virtual ReportingContext* GetContextForTesting() const = 0;
142 
143  protected:
144   ReportingService() = default;
145 };
146 
147 }  // namespace net
148 
149 #endif  // NET_REPORTING_REPORTING_SERVICE_H_
150