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