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

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

Issue 2708503002: Reporting: Implement cache. (Closed)
Patch Set: rebase Created 3 years, 9 months 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 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
5 #ifndef NET_REPORTING_REPORTING_CACHE_H_
6 #define NET_REPORTING_REPORTING_CACHE_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/macros.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "net/base/net_export.h"
16 #include "url/gurl.h"
17 #include "url/origin.h"
18
19 namespace net {
20
21 class NET_EXPORT ReportingCache {
jkarlin 2017/03/15 18:54:32 Class needs a comment. E.g., call out what in the
Julia Tuttle 2017/03/16 14:50:37 Done.
22 public:
23 // An undelivered report.
24 class Report {
jkarlin 2017/03/15 18:54:32 Seems like this should be a struct, given that all
Julia Tuttle 2017/03/16 14:50:38 Done.
25 public:
26 Report();
jkarlin 2017/03/15 18:54:32 How about a constructor that takes all of the para
Julia Tuttle 2017/03/16 14:50:38 Done.
27 ~Report();
28
29 // The URL of the document that triggered the report.
30 GURL url;
31
32 // The endpoint group that should be used to deliver the report.
33 std::string group;
34
35 // The type of the report.
36 std::string type;
37
38 // The body of the report.
39 std::unique_ptr<const base::Value> body;
40
41 // When the report was queued.
42 base::TimeTicks queued;
jkarlin 2017/03/15 18:54:32 queued_time?
Julia Tuttle 2017/03/16 14:50:38 Already says "time" in the type name :)
43
44 // The number of delivery attempts made so far, not including an active
45 // attempt.
46 int attempts;
jkarlin 2017/03/15 18:54:32 failed_attempts?
Julia Tuttle 2017/03/16 14:50:37 The *last* one might not be failed. Also, the spec
47
48 // Whether there is currently an active delivery attempt including this
49 // report.
50 bool pending;
51
52 // Whether this report should be removed once it is no longer part of an
53 // active delivery attempt. (Should never be set unless |pending| also is.)
jkarlin 2017/03/15 18:54:32 Perhaps replace the parens with, "If true, |pendin
Julia Tuttle 2017/03/16 14:50:38 Done.
54 bool doomed;
55
jkarlin 2017/03/15 18:54:32 Don't forget to add a CL to keep track of the memo
Julia Tuttle 2017/03/16 14:50:37 Wilco.
56 private:
57 DISALLOW_COPY_AND_ASSIGN(Report);
58 };
59
60 // The configuration by an origin to use an endpoint for report delivery.
61 struct Client {
62 Client();
63 Client(const Client& other);
64 ~Client();
65
66 // |origin| and |endpoint| are duplicated to make the interface to the cache
67 // simpler. (If they end up using too much memory, switching them to refs to
68 // the keys in |clients_| and the elements thereof should help.)
69
70 // The origin from which reports will be delivered.
71 url::Origin origin;
72
73 // The endpoint to which reports may be delivered. (Origins may configure
74 // many.)
75 GURL endpoint;
76
77 // Whether subdomains of the host of |origin| should also be handled by this
78 // client.
79 bool subdomains;
80
81 // The endpoint group to which this client belongs.
82 std::string group;
83
84 // When this client's max-age has expired.
85 base::TimeTicks expires;
jkarlin 2017/03/15 18:54:32 Perhaps rename to ttl to match the spec?
Julia Tuttle 2017/03/16 14:50:37 The spec stores "creation" and "ttl", but never us
86
87 // Copy and assign allowed.
88 };
89
90 ReportingCache();
91 ~ReportingCache();
92
93 // Adds a report to the cache.
94 //
95 // All parameters correspond to the desired values for the relevant fields in
96 // |Report|.
97 void AddReport(const GURL& url,
jkarlin 2017/03/15 18:54:32 Why not AddReport(std::unique_ptr<Report> report)?
Julia Tuttle 2017/03/16 14:50:38 I'd rather save callers the hassle of assembling i
98 const std::string& group,
99 const std::string& type,
100 std::unique_ptr<const base::Value> body,
101 base::TimeTicks queued,
102 int attempts);
103
104 // Gets all reports in the cache. The returned pointers are valid as long as
105 // either no calls to |RemoveReports| have happened or the reports' |pending|
106 // flag has been set to true using |SetReportsPending|. Does not return
107 // doomed reports (pending reports for which removal has been requested).
108 void GetReports(std::vector<const Report*>* reports_out) const;
jkarlin 2017/03/15 18:54:32 Specify that reports_out gets cleared.
Julia Tuttle 2017/03/16 14:50:37 Done.
109
110 // Sets |pending| on a set of reports.
111 void SetReportsPending(const std::vector<const Report*>& reports);
112
113 // Clear |pending| on a set of reports.
114 void ClearReportsPending(const std::vector<const Report*>& reports);
115
116 // Increments |attempts| on a set of reports.
117 void IncrementReportsAttempts(const std::vector<const Report*>& reports);
118
119 // Removes a set of reports. Any reports that are pending will not be removed
120 // immediately, but rather marked doomed and removed once they are no longer
121 // pending.
122 void RemoveReports(const std::vector<const Report*>& reports);
123
124 // Creates or updates a client for a particular origin and a particular
125 // endpoint.
126 //
127 // All parameters correspond to the desired values for the fields in
128 // |Client|.
129 //
130 // |endpoint| must use a cryptographic scheme; if it doesn't, SetClient will
131 // trigger a NOTREACHED() and return without setting the client.
jkarlin 2017/03/15 18:54:32 I'd leave it at |endpoint| must use a cryptographi
Julia Tuttle 2017/03/16 14:50:38 Done.
132 void SetClient(const url::Origin& origin,
133 const GURL& endpoint,
134 bool subdomains,
135 const std::string& group,
136 base::TimeTicks expires);
137
138 // Gets all of the clients in the cache, regardless of origin or group.
139 void GetClients(std::vector<const Client*>* clients_out) const;
jkarlin 2017/03/15 18:54:32 Specify that clients_out gets cleared.
Julia Tuttle 2017/03/16 14:50:37 Done.
140
141 // Gets all of the clients configured for a particular origin in a particular
142 // group. The returned pointers are only guaranteed to be valid if no calls
143 // have been made to |SetClient| or |RemoveEndpoint| in between.
144 void GetClientsForOriginAndGroup(
145 const url::Origin& origin,
146 const std::string& group,
147 std::vector<const Client*>* clients_out) const;
148
149 // Removes a set of clients.
150 //
151 // May invalidate Client pointers returned by |GetClients| or
152 // |GetClientsForOriginAndGroup|.
153 void RemoveClients(const std::vector<const Client*>& clients);
154
155 // Removes a client for a particular origin and a particular endpoint.
156 void RemoveClientForOriginAndEndpoint(const url::Origin& origin,
157 const GURL& endpoint);
158
159 // Removes all clients whose endpoint is |endpoint|.
160 //
161 // May invalidate Client pointers returned by |GetClients| or
162 // |GetClientsForOriginAndGroup|.
163 void RemoveClientsForEndpoint(const GURL& endpoint);
164
165 // Gets the count of reports in the cache, *including* doomed reports.
166 //
167 // Needed to ensure that doomed reports are eventually deleted, since no
168 // method provides a view of *every* report in the cache, just non-doomed
169 // ones.
170 size_t GetFullReportCountForTesting() const { return reports_.size(); }
171
172 private:
173 // Map from origin to map from endpoint to client.
174 std::map<url::Origin, std::map<GURL, Client>> clients_;
175 // Map from raw pointer to unique_ptr.
jkarlin 2017/03/15 18:54:32 whitespace between comment and previous line
Julia Tuttle 2017/03/16 14:50:38 Done.
176 std::map<const Report*, std::unique_ptr<Report>> reports_;
177
178 DISALLOW_COPY_AND_ASSIGN(ReportingCache);
179 };
180
181 } // namespace net
182
183 #endif // NET_REPORTING_REPORTING_CACHE_H_
OLDNEW
« no previous file with comments | « net/BUILD.gn ('k') | net/reporting/reporting_cache.cc » ('j') | net/reporting/reporting_cache.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698