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