OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_REPORTING_REPORTING_CACHE_H_ | 5 #ifndef NET_REPORTING_REPORTING_CACHE_H_ |
6 #define NET_REPORTING_REPORTING_CACHE_H_ | 6 #define NET_REPORTING_REPORTING_CACHE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <unordered_map> | 12 #include <unordered_map> |
13 #include <unordered_set> | 13 #include <unordered_set> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "net/base/net_export.h" | 20 #include "net/base/net_export.h" |
21 #include "net/reporting/reporting_client.h" | 21 #include "net/reporting/reporting_client.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 #include "url/origin.h" | 23 #include "url/origin.h" |
24 | 24 |
| 25 namespace base { |
| 26 class TickClock; |
| 27 } // namespace base |
| 28 |
25 namespace net { | 29 namespace net { |
26 | 30 |
27 class ReportingContext; | 31 class ReportingContext; |
28 struct ReportingReport; | 32 struct ReportingReport; |
29 | 33 |
30 // The cache holds undelivered reports and clients (per-origin endpoint | 34 // The cache holds undelivered reports and clients (per-origin endpoint |
31 // configurations) in memory. (It is not responsible for persisting them.) | 35 // configurations) in memory. (It is not responsible for persisting them.) |
32 // | 36 // |
33 // This corresponds roughly to the "Reporting cache" in the spec, except that | 37 // This corresponds roughly to the "Reporting cache" in the spec, except that |
34 // endpoints and clients are stored in a more structurally-convenient way, and | 38 // endpoints and clients are stored in a more structurally-convenient way, and |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 // All parameters correspond to the desired values for the fields in | 95 // All parameters correspond to the desired values for the fields in |
92 // |Client|. | 96 // |Client|. |
93 // | 97 // |
94 // |endpoint| must use a cryptographic scheme. | 98 // |endpoint| must use a cryptographic scheme. |
95 void SetClient(const url::Origin& origin, | 99 void SetClient(const url::Origin& origin, |
96 const GURL& endpoint, | 100 const GURL& endpoint, |
97 ReportingClient::Subdomains subdomains, | 101 ReportingClient::Subdomains subdomains, |
98 const std::string& group, | 102 const std::string& group, |
99 base::TimeTicks expires); | 103 base::TimeTicks expires); |
100 | 104 |
| 105 void MarkClientUsed(const url::Origin& origin, const GURL& endpoint); |
| 106 |
101 // Gets all of the clients in the cache, regardless of origin or group. | 107 // Gets all of the clients in the cache, regardless of origin or group. |
102 // | 108 // |
103 // (Clears any existing data in |*clients_out|.) | 109 // (Clears any existing data in |*clients_out|.) |
104 void GetClients(std::vector<const ReportingClient*>* clients_out) const; | 110 void GetClients(std::vector<const ReportingClient*>* clients_out) const; |
105 | 111 |
106 // Gets all of the clients configured for a particular origin in a particular | 112 // Gets all of the clients configured for a particular origin in a particular |
107 // group. The returned pointers are only guaranteed to be valid if no calls | 113 // group. The returned pointers are only guaranteed to be valid if no calls |
108 // have been made to |SetClient| or |RemoveEndpoint| in between. | 114 // have been made to |SetClient| or |RemoveEndpoint| in between. |
109 // | 115 // |
110 // If no origin match is found, the cache will return clients from the most | 116 // If no origin match is found, the cache will return clients from the most |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 return base::ContainsKey(pending_reports_, report); | 160 return base::ContainsKey(pending_reports_, report); |
155 } | 161 } |
156 | 162 |
157 bool IsReportDoomedForTesting(const ReportingReport* report) const { | 163 bool IsReportDoomedForTesting(const ReportingReport* report) const { |
158 return base::ContainsKey(doomed_reports_, report); | 164 return base::ContainsKey(doomed_reports_, report); |
159 } | 165 } |
160 | 166 |
161 private: | 167 private: |
162 const ReportingReport* FindReportToEvict() const; | 168 const ReportingReport* FindReportToEvict() const; |
163 | 169 |
164 void MaybeAddWildcardClient(const ReportingClient* client); | 170 void AddClient(std::unique_ptr<ReportingClient> client, |
| 171 base::TimeTicks last_used); |
165 | 172 |
166 void MaybeRemoveWildcardClient(const ReportingClient* client); | 173 void RemoveClient(const ReportingClient* client); |
| 174 |
| 175 const ReportingClient* GetClientByOriginAndEndpoint( |
| 176 const url::Origin& origin, |
| 177 const GURL& endpoint) const; |
167 | 178 |
168 void GetWildcardClientsForDomainAndGroup( | 179 void GetWildcardClientsForDomainAndGroup( |
169 const std::string& domain, | 180 const std::string& domain, |
170 const std::string& group, | 181 const std::string& group, |
171 std::vector<const ReportingClient*>* clients_out) const; | 182 std::vector<const ReportingClient*>* clients_out) const; |
172 | 183 |
| 184 const ReportingClient* FindClientToEvict(base::TimeTicks now) const; |
| 185 |
| 186 base::TickClock* tick_clock(); |
| 187 |
173 ReportingContext* context_; | 188 ReportingContext* context_; |
174 | 189 |
175 // Owns all clients, keyed by origin, then endpoint URL. | |
176 // (These would be unordered_map, but neither url::Origin nor GURL has a hash | |
177 // function implemented.) | |
178 std::map<url::Origin, std::map<GURL, std::unique_ptr<ReportingClient>>> | |
179 clients_; | |
180 | |
181 // References but does not own all clients with includeSubdomains set, keyed | |
182 // by domain name. | |
183 std::unordered_map<std::string, std::unordered_set<const ReportingClient*>> | |
184 wildcard_clients_; | |
185 | |
186 // Owns all reports, keyed by const raw pointer for easier lookup. | 190 // Owns all reports, keyed by const raw pointer for easier lookup. |
187 std::unordered_map<const ReportingReport*, std::unique_ptr<ReportingReport>> | 191 std::unordered_map<const ReportingReport*, std::unique_ptr<ReportingReport>> |
188 reports_; | 192 reports_; |
189 | 193 |
190 // Reports that have been marked pending (in use elsewhere and should not be | 194 // Reports that have been marked pending (in use elsewhere and should not be |
191 // deleted until no longer pending). | 195 // deleted until no longer pending). |
192 std::unordered_set<const ReportingReport*> pending_reports_; | 196 std::unordered_set<const ReportingReport*> pending_reports_; |
193 | 197 |
194 // Reports that have been marked doomed (would have been deleted, but were | 198 // Reports that have been marked doomed (would have been deleted, but were |
195 // pending when the deletion was requested). | 199 // pending when the deletion was requested). |
196 std::unordered_set<const ReportingReport*> doomed_reports_; | 200 std::unordered_set<const ReportingReport*> doomed_reports_; |
197 | 201 |
| 202 // Owns all clients, keyed by origin, then endpoint URL. |
| 203 // (These would be unordered_map, but neither url::Origin nor GURL has a hash |
| 204 // function implemented.) |
| 205 std::map<url::Origin, std::map<GURL, std::unique_ptr<ReportingClient>>> |
| 206 clients_; |
| 207 |
| 208 // References but does not own all clients with includeSubdomains set, keyed |
| 209 // by domain name. |
| 210 std::unordered_map<std::string, std::unordered_set<const ReportingClient*>> |
| 211 wildcard_clients_; |
| 212 |
| 213 // The time that each client has last been used. |
| 214 std::unordered_map<const ReportingClient*, base::TimeTicks> client_last_used_; |
| 215 |
198 DISALLOW_COPY_AND_ASSIGN(ReportingCache); | 216 DISALLOW_COPY_AND_ASSIGN(ReportingCache); |
199 }; | 217 }; |
200 | 218 |
201 } // namespace net | 219 } // namespace net |
202 | 220 |
203 #endif // NET_REPORTING_REPORTING_CACHE_H_ | 221 #endif // NET_REPORTING_REPORTING_CACHE_H_ |
OLD | NEW |