Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: net/reporting/reporting_service.h

Issue 2249213002: [OBSOLETE] Reporting: Initial implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/reporting/reporting_metrics.h"
19 #include "net/reporting/reporting_report.h"
20 #include "net/reporting/reporting_uploader.h"
21 #include "net/url_request/url_request_context_getter.h"
22 #include "url/gurl.h"
23
24 namespace net {
25
26 class NET_EXPORT ReportingService {
Ryan Sleevi 2017/01/03 21:28:13 High level Design remarks: 1) This feels like a lo
Julia Tuttle 2017/01/25 20:27:46 My original design had a separate "ReportingCache"
27 public:
28 struct NET_EXPORT Policy {
29 // Time to keep an unused endpoint around, or zero for no limit.
30 base::TimeDelta endpoint_lifetime;
31 // Exponential backoff policy for uploading to endpoints.
32 BackoffEntry::Policy endpoint_backoff;
33 // Maximum number of failures before discarding an endpoint (once the
34 // BackoffEntry is okay with it), or -1 for no limit.
35 int max_endpoint_failures;
36 // Maximum number of endpoints to keep around, or 0u for no limit.
37 size_t max_endpoint_count;
38
39 // Time to keep a queued report around, or zero for no limit.
40 base::TimeDelta report_lifetime;
41 // Maximum number of failed delivery attempts before discarding a report, or
42 // -1 for no limit.
43 int max_report_failures;
44 // Maximum number of queued reports to keep around, or 0u for no limit.
45 size_t max_report_count;
46
47 // Whether to persist the report queue across network changes or not.
48 bool persist_reports_across_network_changes;
49
50 static Policy GetDefault();
Ryan Sleevi 2017/01/03 21:28:13 DESIGN: Why is an explicit GetDefault required? Wh
Julia Tuttle 2017/01/25 20:27:46 Done.
51 };
52
53 ReportingService(const Policy& policy);
54 ~ReportingService();
55
56 void set_uploader(std::unique_ptr<ReportingUploader> uploader);
Ryan Sleevi 2017/01/03 21:28:13 This seems to have more subtlety than just "set_up
Julia Tuttle 2017/01/25 20:27:46 Not really; it's just a separate setter instead of
57 void QueueReport(std::unique_ptr<base::Value> body,
58 const GURL& url,
59 const GURL& origin,
60 const std::string& group,
61 const std::string& type);
62 void ProcessHeader(const GURL& origin, const std::string& header_value);
63 void SendReports();
64
65 void set_clock_for_testing(std::unique_ptr<base::TickClock> clock);
Ryan Sleevi 2017/01/03 21:28:13 SetClockForTesting
Julia Tuttle 2017/01/25 20:27:46 Why? It is actually just a plain setter under the
66 bool HasEndpointForTesting(const GURL& endpoint_url);
67 bool HasClientForTesting(const GURL& endpoint_url, const GURL& origin_url);
68 int GetEndpointFailuresForTesting(const GURL& endpoint_url);
69 void CollectGarbageForTesting();
70
71 private:
72 // Per-origin configuration and state for an endpoint.
73 struct Client {
Ryan Sleevi 2017/01/03 21:28:13 Why can't you forward declare this?
Julia Tuttle 2017/01/25 20:27:46 Done.
74 public:
75 Client(const GURL& origin,
76 bool subdomains,
77 const std::string& group,
78 base::TimeDelta ttl,
79 base::TimeTicks creation);
80
81 GURL origin;
82 bool subdomains;
83 std::string group;
84 base::TimeDelta ttl;
85 base::TimeTicks creation;
86
87 bool is_expired(base::TimeTicks now) const { return creation + ttl < now; }
88 };
89
90 // An endpoint to which one or more origins (represented by clients) want to
91 // upload reports.
92 struct Endpoint {
Ryan Sleevi 2017/01/03 21:28:13 Why can't you forward declare this?
Julia Tuttle 2017/01/25 20:27:46 Done.
93 public:
94 Endpoint(const GURL& url,
95 const BackoffEntry::Policy& backoff_policy,
96 base::TickClock* clock);
97 ~Endpoint();
98
99 const GURL url;
100
101 BackoffEntry backoff;
102 // For LRU eviction of endpoints.
103 base::TimeTicks last_used;
104 // Whether we currently have an upload in progress to this endpoint.
105 bool pending;
106
107 // Map from client.origin to client.
108 std::map<GURL, Client> clients;
109
110 bool is_expired(base::TimeTicks now) const;
111 };
112
113 // The parsed data from a header representing a single endpoint configuration.
114 struct EndpointTuple {
Ryan Sleevi 2017/01/03 21:28:13 Why can't you forward declare this?
Julia Tuttle 2017/01/25 20:27:46 Done.
115 GURL url;
116 bool subdomains;
117 base::TimeDelta ttl;
118 std::string group;
119
120 static bool FromDictionary(const base::DictionaryValue& dictionary,
121 EndpointTuple* tuple_out,
122 std::string* error_out);
123 static bool FromHeader(const std::string& header,
124 std::vector<EndpointTuple>* tuples_out,
125 std::vector<std::string>* errors_out);
126
127 std::string ToString() const;
128 };
129
130 struct Delivery {
Ryan Sleevi 2017/01/03 21:28:13 Why can't you forward declare this?
Julia Tuttle 2017/01/25 20:27:46 Done.
131 Delivery(const GURL& endpoint_url,
132 const std::vector<ReportingReport*>& reports);
133 ~Delivery();
134
135 const GURL& endpoint_url;
136 const std::vector<ReportingReport*> reports;
137 };
138
139 using EndpointMap = std::map<GURL, std::unique_ptr<Endpoint>>;
140 using ReportVector = std::vector<std::unique_ptr<ReportingReport>>;
141
142 void ProcessEndpointTuple(const GURL& origin, const EndpointTuple& tuple);
143
144 void OnDeliveryAttemptComplete(const std::unique_ptr<Delivery>& delivery,
145 ReportingUploader::Outcome outcome);
146
147 void CollectGarbage();
148
149 Endpoint* FindEndpointForReport(const ReportingReport& report);
150 bool DoesEndpointMatchReport(const Endpoint& endpoint,
151 const ReportingReport& report);
152 std::string SerializeReports(const std::vector<ReportingReport*>& reports);
153
154 Endpoint* GetEndpointByURL(const GURL& url);
155 void DequeueReport(ReportingReport* report);
156
157 void HistogramHeaderEndpointInternal(bool endpoint_exists,
158 bool client_exists,
159 base::TimeDelta ttl) const;
160 void HistogramReportInternal(ReportFate fate,
161 const ReportingReport& report) const;
162
163 Policy policy_;
164 std::unique_ptr<base::TickClock> clock_;
165 std::unique_ptr<ReportingUploader> uploader_;
166
167 ReportVector reports_;
168 EndpointMap endpoints_;
169 };
170
171 } // namespace net
172
173 #endif // COMPONENTS_REPORTING_REPORTING_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698