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