summaryrefslogtreecommitdiff
path: root/chromium/net/reporting/reporting_service.h
blob: a473073a5bb687c6be9254967aa4b97a40050037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_REPORTING_REPORTING_SERVICE_H_
#define NET_REPORTING_REPORTING_SERVICE_H_

#include <memory>
#include <string>

#include "base/callback.h"
#include "base/containers/flat_map.h"
#include "base/unguessable_token.h"
#include "net/base/net_export.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_cache_observer.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

class GURL;

namespace base {
class Value;
}  // namespace base

namespace url {
class Origin;
}  // namespace url

namespace net {

class IsolationInfo;
class NetworkIsolationKey;
class ReportingContext;
struct ReportingPolicy;
class URLRequestContext;

// The external interface to the Reporting system, used by the embedder of //net
// and also other parts of //net.
class NET_EXPORT ReportingService {
 public:
  ReportingService(const ReportingService&) = delete;
  ReportingService& operator=(const ReportingService&) = delete;

  virtual ~ReportingService();

  // Creates a ReportingService. |policy| will be copied. |request_context| must
  // outlive the ReportingService. |store| must outlive the ReportingService.
  // If |store| is null, the ReportingCache will be in-memory only.
  static std::unique_ptr<ReportingService> Create(
      const ReportingPolicy& policy,
      URLRequestContext* request_context,
      ReportingCache::PersistentReportingStore* store);

  // Creates a ReportingService for testing purposes using an
  // already-constructed ReportingContext. The ReportingService will take
  // ownership of |reporting_context| and destroy it when the service is
  // destroyed.
  static std::unique_ptr<ReportingService> CreateForTesting(
      std::unique_ptr<ReportingContext> reporting_context);

  // Queues a report for delivery. |url| is the URL that originated the report.
  // |reporting_source| is the reporting source token for the document or
  // worker which triggered this report, if it can be associated with one, or
  // nullopt otherwise. If present, it may not be empty.
  // Along with |network_isolation_key|, it is used to restrict what reports
  // can be merged, and for sending the report.
  // |user_agent| is the User-Agent header that was used for the request.
  // |group| is the endpoint group to which the report should be delivered.
  // |type| is the type of the report. |body| is the body of the report.
  //
  // The Reporting system will take ownership of |body|; all other parameters
  // will be copied.
  virtual void QueueReport(
      const GURL& url,
      const absl::optional<base::UnguessableToken>& reporting_source,
      const NetworkIsolationKey& network_isolation_key,
      const std::string& user_agent,
      const std::string& group,
      const std::string& type,
      base::Value::Dict body,
      int depth) = 0;

  // Processes a Report-To header. |origin| is the Origin of the URL that the
  // header came from; |header_value| is the normalized value of the Report-To
  // header.
  virtual void ProcessReportToHeader(
      const url::Origin& origin,
      const NetworkIsolationKey& network_isolation_key,
      const std::string& header_value) = 0;

  // Configures reporting endpoints set by the Reporting-Endpoints header, once
  // the associated document or worker (represented by |reporting_source|) has
  // been committed.
  // |reporting_source| is the unique identifier for the resource with which
  // this header was received, and must not be empty.
  // |endpoints| is a mapping of endpoint names to URLs.
  // |origin| is the origin of the reporting source, and
  // |isolation_info| is the appropriate IsolationInfo struct for that source.
  virtual void SetDocumentReportingEndpoints(
      const base::UnguessableToken& reporting_source,
      const url::Origin& origin,
      const IsolationInfo& isolation_info,
      const base::flat_map<std::string, std::string>& endpoints) = 0;

  // Attempts to send any queued reports and removes all associated
  // configuration for `reporting_source`. This is called when a source is
  // destroyed.
  virtual void SendReportsAndRemoveSource(
      const base::UnguessableToken& reporting_source) = 0;

  // Removes browsing data from the Reporting system. See
  // ReportingBrowsingDataRemover for more details.
  virtual void RemoveBrowsingData(
      uint64_t data_type_mask,
      const base::RepeatingCallback<bool(const url::Origin&)>&
          origin_filter) = 0;

  // Like RemoveBrowsingData except removes data for all origins without a
  // filter.
  virtual void RemoveAllBrowsingData(uint64_t data_type_mask) = 0;

  // Shuts down the Reporting service so that no new headers or reports are
  // processed, and pending uploads are cancelled.
  virtual void OnShutdown() = 0;

  virtual const ReportingPolicy& GetPolicy() const = 0;

  virtual base::Value StatusAsValue() const;

  virtual std::vector<const ReportingReport*> GetReports() const = 0;

  virtual base::flat_map<url::Origin, std::vector<ReportingEndpoint>>
  GetV1ReportingEndpointsByOrigin() const = 0;

  virtual void AddReportingCacheObserver(ReportingCacheObserver* observer) = 0;
  virtual void RemoveReportingCacheObserver(
      ReportingCacheObserver* observer) = 0;

  virtual ReportingContext* GetContextForTesting() const = 0;

 protected:
  ReportingService() = default;
};

}  // namespace net

#endif  // NET_REPORTING_REPORTING_SERVICE_H_