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 <string> | 11 #include <string> |
11 #include <unordered_map> | 12 #include <unordered_map> |
12 #include <unordered_set> | 13 #include <unordered_set> |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "net/base/net_export.h" | 20 #include "net/base/net_export.h" |
20 #include "net/reporting/reporting_client.h" | 21 #include "net/reporting/reporting_client.h" |
21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
22 #include "url/origin.h" | 23 #include "url/origin.h" |
23 | 24 |
24 namespace net { | 25 namespace net { |
25 | 26 |
| 27 class ReportingContext; |
26 struct ReportingReport; | 28 struct ReportingReport; |
27 | 29 |
28 // The cache holds undelivered reports and clients (per-origin endpoint | 30 // The cache holds undelivered reports and clients (per-origin endpoint |
29 // configurations) in memory. (It is not responsible for persisting them.) | 31 // configurations) in memory. (It is not responsible for persisting them.) |
30 // | 32 // |
31 // This corresponds roughly to the "Reporting cache" in the spec, except that | 33 // 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 | 34 // endpoints and clients are stored in a more structurally-convenient way, and |
33 // endpoint failures/retry-after are tracked in ReportingEndpointManager. | 35 // endpoint failures/retry-after are tracked in ReportingEndpointManager. |
34 // | 36 // |
35 // The cache implementation has the notion of "pending" reports. These are | 37 // The cache implementation has the notion of "pending" reports. These are |
36 // reports that are part of an active delivery attempt, so they won't be | 38 // reports that are part of an active delivery attempt, so they won't be |
37 // actually deallocated. Any attempt to remove a pending report wil mark it | 39 // actually deallocated. Any attempt to remove a pending report wil mark it |
38 // "doomed", which will cause it to be deallocated once it is no longer pending. | 40 // "doomed", which will cause it to be deallocated once it is no longer pending. |
39 class NET_EXPORT ReportingCache { | 41 class NET_EXPORT ReportingCache { |
40 public: | 42 public: |
41 ReportingCache(); | 43 // |context| must outlive the ReportingCache. |
| 44 ReportingCache(ReportingContext* context); |
42 | 45 |
43 ~ReportingCache(); | 46 ~ReportingCache(); |
44 | 47 |
45 // Adds a report to the cache. | 48 // Adds a report to the cache. |
46 // | 49 // |
47 // All parameters correspond to the desired values for the relevant fields in | 50 // All parameters correspond to the desired values for the relevant fields in |
48 // ReportingReport. | 51 // ReportingReport. |
49 void AddReport(const GURL& url, | 52 void AddReport(const GURL& url, |
50 const std::string& group, | 53 const std::string& group, |
51 const std::string& type, | 54 const std::string& type, |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 141 |
139 bool IsReportPendingForTesting(const ReportingReport* report) const { | 142 bool IsReportPendingForTesting(const ReportingReport* report) const { |
140 return base::ContainsKey(pending_reports_, report); | 143 return base::ContainsKey(pending_reports_, report); |
141 } | 144 } |
142 | 145 |
143 bool IsReportDoomedForTesting(const ReportingReport* report) const { | 146 bool IsReportDoomedForTesting(const ReportingReport* report) const { |
144 return base::ContainsKey(doomed_reports_, report); | 147 return base::ContainsKey(doomed_reports_, report); |
145 } | 148 } |
146 | 149 |
147 private: | 150 private: |
| 151 ReportingContext* context_; |
| 152 |
148 // Owns all clients, keyed by origin, then endpoint URL. | 153 // Owns all clients, keyed by origin, then endpoint URL. |
149 // (These would be unordered_map, but neither url::Origin nor GURL has a hash | 154 // (These would be unordered_map, but neither url::Origin nor GURL has a hash |
150 // function implemented.) | 155 // function implemented.) |
151 std::map<url::Origin, std::map<GURL, std::unique_ptr<ReportingClient>>> | 156 std::map<url::Origin, std::map<GURL, std::unique_ptr<ReportingClient>>> |
152 clients_; | 157 clients_; |
153 | 158 |
154 // Owns all reports, keyed by const raw pointer for easier lookup. | 159 // Owns all reports, keyed by const raw pointer for easier lookup. |
155 std::unordered_map<const ReportingReport*, std::unique_ptr<ReportingReport>> | 160 std::unordered_map<const ReportingReport*, std::unique_ptr<ReportingReport>> |
156 reports_; | 161 reports_; |
157 | 162 |
158 // Reports that have been marked "pending" (in use elsewhere and should not be | 163 // Reports that have been marked pending (in use elsewhere and should not be |
159 // deleted until no longer pending). | 164 // deleted until no longer pending). |
160 std::unordered_set<const ReportingReport*> pending_reports_; | 165 std::unordered_set<const ReportingReport*> pending_reports_; |
161 | 166 |
162 // Reports that have been marked "doomed" (would have been deleted, but were | 167 // Reports that have been marked doomed (would have been deleted, but were |
163 // pending when the deletion was requested). | 168 // pending when the deletion was requested). |
164 std::unordered_set<const ReportingReport*> doomed_reports_; | 169 std::unordered_set<const ReportingReport*> doomed_reports_; |
165 | 170 |
166 DISALLOW_COPY_AND_ASSIGN(ReportingCache); | 171 DISALLOW_COPY_AND_ASSIGN(ReportingCache); |
167 }; | 172 }; |
168 | 173 |
169 } // namespace net | 174 } // namespace net |
170 | 175 |
171 #endif // NET_REPORTING_REPORTING_CACHE_H_ | 176 #endif // NET_REPORTING_REPORTING_CACHE_H_ |
OLD | NEW |