reporting_service.h 5.4 KB

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