OLD | NEW |
---|---|
(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 <memory> | |
10 #include <string> | |
11 #include <unordered_map> | |
12 #include <unordered_set> | |
13 #include <vector> | |
14 | |
15 #include "base/macros.h" | |
16 #include "base/stl_util.h" | |
17 #include "base/time/time.h" | |
18 #include "base/values.h" | |
19 #include "net/base/net_export.h" | |
20 #include "net/reporting/reporting_client.h" | |
21 #include "url/gurl.h" | |
22 #include "url/origin.h" | |
23 | |
24 namespace net { | |
25 | |
26 struct ReportingReport; | |
27 | |
28 // The cache holds undelivered reports and clients (per-origin endpoint | |
29 // configurations) in memory. (It is not responsible for persisting them.) | |
30 // | |
31 // This corresponds roughly to the "Reporting cache" in the spec, except that | |
32 // endpoints and clients are stored in a more structurally-convenient way, and | |
33 // endpoint failures/retry-after are tracked elsewhere. | |
shivanisha
2017/03/23 19:11:59
"are tracked elsewhere" => "are tracked in <name o
Julia Tuttle
2017/03/23 19:24:04
Done.
| |
34 class NET_EXPORT ReportingCache { | |
35 public: | |
36 ReportingCache(); | |
37 | |
38 ~ReportingCache(); | |
39 | |
40 // Adds a report to the cache. | |
41 // | |
42 // All parameters correspond to the desired values for the relevant fields in | |
43 // |Report|. | |
shivanisha
2017/03/23 19:11:59
|| are used for argument names so may be just say
Julia Tuttle
2017/03/23 19:24:04
Done.
| |
44 void AddReport(const GURL& url, | |
45 const std::string& group, | |
46 const std::string& type, | |
47 std::unique_ptr<const base::Value> body, | |
48 base::TimeTicks queued, | |
49 int attempts); | |
50 | |
51 // Gets all reports in the cache. The returned pointers are valid as long as | |
52 // either no calls to |RemoveReports| have happened or the reports' |pending| | |
53 // flag has been set to true using |SetReportsPending|. Does not return | |
54 // doomed reports (pending reports for which removal has been requested). | |
55 // | |
56 // (Clears any existing data in |*reports_out|.) | |
57 void GetReports(std::vector<const ReportingReport*>* reports_out) const; | |
58 | |
59 // Marks a set of reports as pending. |reports| must not already be marked as | |
60 // pending. | |
61 void SetReportsPending(const std::vector<const ReportingReport*>& reports); | |
62 | |
63 // Unmarks a set of reports as pending. |reports| must be previously marked as | |
64 // pending. | |
65 void ClearReportsPending(const std::vector<const ReportingReport*>& reports); | |
66 | |
67 // Increments |attempts| on a set of reports. | |
68 void IncrementReportsAttempts( | |
69 const std::vector<const ReportingReport*>& reports); | |
70 | |
71 // Removes a set of reports. Any reports that are pending will not be removed | |
72 // immediately, but rather marked doomed and removed once they are no longer | |
73 // pending. | |
74 void RemoveReports(const std::vector<const ReportingReport*>& reports); | |
75 | |
76 // Removes all reports. Like |RemoveReports()|, pending reports are doomed | |
77 // until no longer pending. | |
78 void RemoveAllReports(); | |
79 | |
80 // Creates or updates a client for a particular origin and a particular | |
81 // endpoint. | |
82 // | |
83 // All parameters correspond to the desired values for the fields in | |
84 // |Client|. | |
85 // | |
86 // |endpoint| must use a cryptographic scheme. | |
87 void SetClient(const url::Origin& origin, | |
88 const GURL& endpoint, | |
89 ReportingClient::Subdomains subdomains, | |
90 const std::string& group, | |
91 base::TimeTicks expires); | |
92 | |
93 // Gets all of the clients in the cache, regardless of origin or group. | |
94 // | |
95 // (Clears any existing data in |*clients_out|.) | |
96 void GetClients(std::vector<const ReportingClient*>* clients_out) const; | |
97 | |
98 // Gets all of the clients configured for a particular origin in a particular | |
99 // group. The returned pointers are only guaranteed to be valid if no calls | |
100 // have been made to |SetClient| or |RemoveEndpoint| in between. | |
101 // | |
102 // (Clears any existing data in |*clients_out|.) | |
103 void GetClientsForOriginAndGroup( | |
104 const url::Origin& origin, | |
105 const std::string& group, | |
106 std::vector<const ReportingClient*>* clients_out) const; | |
107 | |
108 // Removes a set of clients. | |
109 // | |
110 // May invalidate ReportingClient pointers returned by |GetClients| or | |
111 // |GetClientsForOriginAndGroup|. | |
112 void RemoveClients(const std::vector<const ReportingClient*>& clients); | |
113 | |
114 // Removes a client for a particular origin and a particular endpoint. | |
115 void RemoveClientForOriginAndEndpoint(const url::Origin& origin, | |
116 const GURL& endpoint); | |
117 | |
118 // Removes all clients whose endpoint is |endpoint|. | |
119 // | |
120 // May invalidate ReportingClient pointers returned by |GetClients| or | |
121 // |GetClientsForOriginAndGroup|. | |
122 void RemoveClientsForEndpoint(const GURL& endpoint); | |
123 | |
124 // Removes all clients. | |
125 void RemoveAllClients(); | |
126 | |
127 // Gets the count of reports in the cache, *including* doomed reports. | |
128 // | |
129 // Needed to ensure that doomed reports are eventually deleted, since no | |
130 // method provides a view of *every* report in the cache, just non-doomed | |
131 // ones. | |
132 size_t GetFullReportCountForTesting() const { return reports_.size(); } | |
133 | |
134 bool IsReportPendingForTesting(const ReportingReport* report) const { | |
135 return base::ContainsKey(pending_reports_, report); | |
136 } | |
137 | |
138 bool IsReportDoomedForTesting(const ReportingReport* report) const { | |
139 return base::ContainsKey(doomed_reports_, report); | |
140 } | |
141 | |
142 private: | |
143 // Owns all clients, keyed by origin, then endpoint URL. | |
144 // (These would be unordered_map, but neither url::Origin nor GURL has a hash | |
145 // function implemented.) | |
146 std::map<url::Origin, std::map<GURL, std::unique_ptr<ReportingClient>>> | |
147 clients_; | |
148 | |
149 // Owns all reports, keyed by const raw pointer for easier lookup. | |
150 std::unordered_map<const ReportingReport*, std::unique_ptr<ReportingReport>> | |
151 reports_; | |
152 | |
153 // Reports that have been marked "pending" (in use elsewhere and should not be | |
154 // deleted until no longer pending). | |
155 std::unordered_set<const ReportingReport*> pending_reports_; | |
156 | |
157 // Reports that have been marked "doomed" (would have been deleted, but were | |
158 // pending when the deletion was requested). | |
159 std::unordered_set<const ReportingReport*> doomed_reports_; | |
160 | |
161 DISALLOW_COPY_AND_ASSIGN(ReportingCache); | |
162 }; | |
163 | |
164 } // namespace net | |
165 | |
166 #endif // NET_REPORTING_REPORTING_CACHE_H_ | |
OLD | NEW |