Index: net/reporting/reporting_cache.h |
diff --git a/net/reporting/reporting_cache.h b/net/reporting/reporting_cache.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3bc60ad1e0949f5ca16bb6fca7c2274d6561b12d |
--- /dev/null |
+++ b/net/reporting/reporting_cache.h |
@@ -0,0 +1,166 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef NET_REPORTING_REPORTING_CACHE_H_ |
+#define NET_REPORTING_REPORTING_CACHE_H_ |
+ |
+#include <map> |
+#include <memory> |
+#include <string> |
+#include <unordered_map> |
+#include <unordered_set> |
+#include <vector> |
+ |
+#include "base/macros.h" |
+#include "base/stl_util.h" |
+#include "base/time/time.h" |
+#include "base/values.h" |
+#include "net/base/net_export.h" |
+#include "net/reporting/reporting_client.h" |
+#include "url/gurl.h" |
+#include "url/origin.h" |
+ |
+namespace net { |
+ |
+struct ReportingReport; |
+ |
+// The cache holds undelivered reports and clients (per-origin endpoint |
+// configurations) in memory. (It is not responsible for persisting them.) |
+// |
+// This corresponds roughly to the "Reporting cache" in the spec, except that |
+// endpoints and clients are stored in a more structurally-convenient way, and |
+// endpoint failures/retry-after are tracked in ReportingEndpointManager. |
Randy Smith (Not in Mondays)
2017/03/27 16:13:25
Willing to have a README.md file with a quick sket
Randy Smith (Not in Mondays)
2017/03/27 16:13:25
Request: Go into a bit more detail in this comment
Julia Tuttle
2017/03/27 16:39:56
Done.
Julia Tuttle
2017/03/27 16:39:56
Yeah, sure, sounds great.
|
+class NET_EXPORT ReportingCache { |
+ public: |
+ ReportingCache(); |
+ |
+ ~ReportingCache(); |
+ |
+ // Adds a report to the cache. |
+ // |
+ // All parameters correspond to the desired values for the relevant fields in |
+ // ReportingReport. |
+ void AddReport(const GURL& url, |
+ const std::string& group, |
+ const std::string& type, |
+ std::unique_ptr<const base::Value> body, |
+ base::TimeTicks queued, |
+ int attempts); |
+ |
+ // Gets all reports in the cache. The returned pointers are valid as long as |
+ // either no calls to |RemoveReports| have happened or the reports' |pending| |
+ // flag has been set to true using |SetReportsPending|. Does not return |
Randy Smith (Not in Mondays)
2017/03/27 16:13:25
This continues to bother me; handing out raw point
Julia Tuttle
2017/03/27 16:39:56
It wouldn't work great for upcoming CLs -- I need
Randy Smith (Not in Mondays)
2017/03/28 15:30:09
I wasn't suggesting changing the lifetime behavior
|
+ // doomed reports (pending reports for which removal has been requested). |
+ // |
+ // (Clears any existing data in |*reports_out|.) |
+ void GetReports(std::vector<const ReportingReport*>* reports_out) const; |
+ |
+ // Marks a set of reports as pending. |reports| must not already be marked as |
+ // pending. |
+ void SetReportsPending(const std::vector<const ReportingReport*>& reports); |
+ |
+ // Unmarks a set of reports as pending. |reports| must be previously marked as |
+ // pending. |
+ void ClearReportsPending(const std::vector<const ReportingReport*>& reports); |
+ |
+ // Increments |attempts| on a set of reports. |
+ void IncrementReportsAttempts( |
+ const std::vector<const ReportingReport*>& reports); |
+ |
+ // Removes a set of reports. Any reports that are pending will not be removed |
+ // immediately, but rather marked doomed and removed once they are no longer |
+ // pending. |
+ void RemoveReports(const std::vector<const ReportingReport*>& reports); |
+ |
+ // Removes all reports. Like |RemoveReports()|, pending reports are doomed |
+ // until no longer pending. |
+ void RemoveAllReports(); |
+ |
+ // Creates or updates a client for a particular origin and a particular |
+ // endpoint. |
+ // |
+ // All parameters correspond to the desired values for the fields in |
+ // |Client|. |
+ // |
+ // |endpoint| must use a cryptographic scheme. |
+ void SetClient(const url::Origin& origin, |
+ const GURL& endpoint, |
+ ReportingClient::Subdomains subdomains, |
+ const std::string& group, |
+ base::TimeTicks expires); |
+ |
+ // Gets all of the clients in the cache, regardless of origin or group. |
+ // |
+ // (Clears any existing data in |*clients_out|.) |
+ void GetClients(std::vector<const ReportingClient*>* clients_out) const; |
+ |
+ // Gets all of the clients configured for a particular origin in a particular |
+ // group. The returned pointers are only guaranteed to be valid if no calls |
+ // have been made to |SetClient| or |RemoveEndpoint| in between. |
Randy Smith (Not in Mondays)
2017/03/27 16:13:25
As implied above, this type of guarantee bothers m
Julia Tuttle
2017/03/27 16:39:56
Oh, sure, only used synchronously would be great.
|
+ // |
+ // (Clears any existing data in |*clients_out|.) |
+ void GetClientsForOriginAndGroup( |
+ const url::Origin& origin, |
+ const std::string& group, |
+ std::vector<const ReportingClient*>* clients_out) const; |
+ |
+ // Removes a set of clients. |
+ // |
+ // May invalidate ReportingClient pointers returned by |GetClients| or |
+ // |GetClientsForOriginAndGroup|. |
+ void RemoveClients(const std::vector<const ReportingClient*>& clients); |
+ |
+ // Removes a client for a particular origin and a particular endpoint. |
+ void RemoveClientForOriginAndEndpoint(const url::Origin& origin, |
+ const GURL& endpoint); |
+ |
+ // Removes all clients whose endpoint is |endpoint|. |
+ // |
+ // May invalidate ReportingClient pointers returned by |GetClients| or |
+ // |GetClientsForOriginAndGroup|. |
+ void RemoveClientsForEndpoint(const GURL& endpoint); |
+ |
+ // Removes all clients. |
+ void RemoveAllClients(); |
+ |
+ // Gets the count of reports in the cache, *including* doomed reports. |
+ // |
+ // Needed to ensure that doomed reports are eventually deleted, since no |
+ // method provides a view of *every* report in the cache, just non-doomed |
+ // ones. |
+ size_t GetFullReportCountForTesting() const { return reports_.size(); } |
+ |
+ bool IsReportPendingForTesting(const ReportingReport* report) const { |
+ return base::ContainsKey(pending_reports_, report); |
+ } |
+ |
+ bool IsReportDoomedForTesting(const ReportingReport* report) const { |
+ return base::ContainsKey(doomed_reports_, report); |
+ } |
+ |
+ private: |
+ // Owns all clients, keyed by origin, then endpoint URL. |
+ // (These would be unordered_map, but neither url::Origin nor GURL has a hash |
+ // function implemented.) |
+ std::map<url::Origin, std::map<GURL, std::unique_ptr<ReportingClient>>> |
+ clients_; |
+ |
+ // Owns all reports, keyed by const raw pointer for easier lookup. |
+ std::unordered_map<const ReportingReport*, std::unique_ptr<ReportingReport>> |
+ reports_; |
+ |
+ // Reports that have been marked "pending" (in use elsewhere and should not be |
+ // deleted until no longer pending). |
+ std::unordered_set<const ReportingReport*> pending_reports_; |
+ |
+ // Reports that have been marked "doomed" (would have been deleted, but were |
+ // pending when the deletion was requested). |
+ std::unordered_set<const ReportingReport*> doomed_reports_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ReportingCache); |
+}; |
+ |
+} // namespace net |
+ |
+#endif // NET_REPORTING_REPORTING_CACHE_H_ |