OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #ifndef NET_REPORTING_REPORTING_SERVICE_H_ |
| 6 #define NET_REPORTING_REPORTING_SERVICE_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <unordered_set> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/time/tick_clock.h" |
| 14 #include "base/time/time.h" |
| 15 #include "base/values.h" |
| 16 #include "net/base/backoff_entry.h" |
| 17 #include "net/base/net_export.h" |
| 18 #include "net/base/network_change_notifier.h" |
| 19 #include "net/reporting/reporting_uploader.h" |
| 20 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "url/gurl.h" |
| 22 |
| 23 namespace net { |
| 24 |
| 25 class NET_EXPORT ReportingService |
| 26 : public NetworkChangeNotifier::NetworkChangeObserver { |
| 27 public: |
| 28 struct NET_EXPORT Policy { |
| 29 // Constructs a default policy that is appropriate for use in a browser |
| 30 // context. |
| 31 Policy(); |
| 32 |
| 33 // Returns a default policy that is appropriate for use in non-browser |
| 34 // contexts. Notably, some parameters are tweaked to prioritize reliability |
| 35 // of report delivery in ways that would inappropriately allow correlation |
| 36 // of user behavior across networks in a browser context. |
| 37 static Policy GetNonBrowserDefault(); |
| 38 |
| 39 // Time to keep an unused endpoint around, or zero for no limit. |
| 40 base::TimeDelta endpoint_lifetime; |
| 41 // Exponential backoff policy for uploading to endpoints. |
| 42 BackoffEntry::Policy endpoint_backoff; |
| 43 // Maximum number of failures before discarding an endpoint (once the |
| 44 // BackoffEntry is okay with it). |
| 45 int max_endpoint_failures; |
| 46 // Maximum number of endpoints to keep around. |
| 47 size_t max_endpoint_count; |
| 48 |
| 49 // Time to keep a queued report around. |
| 50 base::TimeDelta report_lifetime; |
| 51 // Maximum number of failed delivery attempts before discarding a report. |
| 52 size_t max_report_failures; |
| 53 // Maximum number of queued reports to keep around. |
| 54 size_t max_report_count; |
| 55 |
| 56 // Whether to persist the report queue across network changes or not. |
| 57 bool persist_reports_across_network_changes; |
| 58 }; |
| 59 |
| 60 ReportingService(const Policy& policy); |
| 61 ~ReportingService() override; |
| 62 |
| 63 void set_uploader(std::unique_ptr<ReportingUploader> uploader); |
| 64 void QueueReport(std::unique_ptr<base::Value> body, |
| 65 const GURL& url, |
| 66 const GURL& origin, |
| 67 const std::string& group, |
| 68 const std::string& type); |
| 69 void ProcessHeader(const GURL& origin, const std::string& header_value); |
| 70 void SendReports(); |
| 71 |
| 72 // NetworkChangeNotifier::NetworkChangeObserver implementation: |
| 73 void OnNetworkChanged(NetworkChangeNotifier::ConnectionType type) override; |
| 74 |
| 75 void set_clock_for_testing(std::unique_ptr<base::TickClock> clock); |
| 76 size_t GetReportCountForTesting(); |
| 77 bool HasEndpointForTesting(const GURL& endpoint_url); |
| 78 bool HasClientForTesting(const GURL& endpoint_url, const GURL& origin_url); |
| 79 int GetEndpointFailuresForTesting(const GURL& endpoint_url); |
| 80 void CollectGarbageForTesting(); |
| 81 |
| 82 private: |
| 83 // Per-origin configuration and state for an endpoint. |
| 84 struct Client; |
| 85 |
| 86 // An endpoint to which one or more origins (represented by clients) want to |
| 87 // upload reports. |
| 88 struct Endpoint; |
| 89 |
| 90 // A report queued by another component to be delivered by Reporting. |
| 91 class Report; |
| 92 |
| 93 // The parsed data from a header representing a single endpoint configuration. |
| 94 struct EndpointTuple; |
| 95 |
| 96 struct Delivery; |
| 97 |
| 98 using EndpointMap = std::map<GURL, std::unique_ptr<Endpoint>>; |
| 99 // TODO: Switch to a linked list for faster removal. |
| 100 using ReportVector = std::vector<std::unique_ptr<Report>>; |
| 101 |
| 102 void ProcessEndpointTuple(const GURL& origin, const EndpointTuple& tuple); |
| 103 |
| 104 void OnDeliveryAttemptComplete(const std::unique_ptr<Delivery>& delivery, |
| 105 ReportingUploader::Outcome outcome); |
| 106 |
| 107 void CollectGarbage(); |
| 108 |
| 109 Endpoint* FindEndpointForReport(const Report& report); |
| 110 bool DoesEndpointMatchReport(const Endpoint& endpoint, const Report& report); |
| 111 std::string SerializeReports(const std::vector<Report*>& reports); |
| 112 |
| 113 Endpoint* GetEndpointByURL(const GURL& url); |
| 114 void DequeueReport(Report* report); |
| 115 |
| 116 Policy policy_; |
| 117 std::unique_ptr<base::TickClock> clock_; |
| 118 std::unique_ptr<ReportingUploader> uploader_; |
| 119 |
| 120 ReportVector reports_; |
| 121 EndpointMap endpoints_; |
| 122 }; |
| 123 |
| 124 } // namespace net |
| 125 |
| 126 #endif // COMPONENTS_REPORTING_REPORTING_SERVICE_H_ |
OLD | NEW |