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 COMPONENTS_REPORTING_REPORTING_CACHE_H_ | |
6 #define COMPONENTS_REPORTING_REPORTING_CACHE_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/time/time.h" | |
16 #include "url/gurl.h" | |
17 #include "url/origin.h" | |
18 | |
19 namespace reporting { | |
20 | |
21 class ReportingCache { | |
22 public: | |
23 class Report { | |
24 public: | |
25 Report(); | |
26 ~Report() = default; | |
27 | |
28 std::unique_ptr<base::Value> body; | |
29 GURL url; | |
30 std::string group; | |
31 std::string type; | |
32 base::TimeTicks queued; | |
33 int attempts; | |
34 | |
35 private: | |
36 friend class ReportingCache; | |
37 | |
38 WeakPtrFactory<Report> weak_factory_; | |
39 | |
40 DISALLOW_COPY_AND_ASSIGN(Report); | |
41 }; | |
42 | |
43 class ReportHandle { | |
44 public: | |
45 ~ReportHandle() = default; | |
46 | |
47 const Report* get() { return weak_pointer_.get(); } | |
Randy Smith (Not in Mondays)
2017/02/21 16:33:02
This has basically the identical interface to a we
Julia Tuttle
2017/02/23 20:01:38
The one thing this has over a weak pointer is that
Randy Smith (Not in Mondays)
2017/02/24 21:26:59
Hmmm. Good point. Sure, it's worth it, just docu
| |
48 | |
49 private: | |
50 friend class ReportingCache; | |
51 | |
52 ReportHandle(WeakPtr<Report>); | |
53 | |
54 WeakPtr<Report> weak_pointer_; | |
55 }; | |
Randy Smith (Not in Mondays)
2017/02/21 16:33:02
nit: Worth a comment that it's intentionally copya
Julia Tuttle
2017/02/23 20:01:38
Oops! Forgot to add this to the latest patchset bu
| |
56 | |
57 struct Client { | |
58 GURL endpoint; | |
59 bool subdomains; | |
60 std::string group; | |
61 base::TimeTicks expires; | |
62 }; | |
63 | |
64 ReportingCache() = default; | |
65 ~ReportingCache() = default; | |
66 | |
67 // Adds a report to the cache. | |
68 void AddReport(std::unique_ptr<const base::Value> body, | |
69 const GURL& url, | |
70 const std::string& group, | |
71 const std::string& type, | |
72 base::TimeTicks queued, | |
73 int attempts); | |
74 | |
75 // Gets all reports in the cache, as ReportHandles. The handles | |
76 // remain valid even if the report is removed, but will return nullptr if | |
77 // asked for the report. | |
78 std::vector<ReportHandle> GetReports(); | |
79 | |
80 // Increments |attempts| on a set of reports. Does nothing for ReportHandles | |
81 // whose reports were removed. | |
82 void MarkReportsAttempted(const std::set<ReportHandle>& reports); | |
83 | |
84 // Removes a set of reports. Does nothing for ReportHandles whose reports were | |
85 // already removed. | |
86 void RemoveReports(const std::set<ReportHandle>& reports); | |
Randy Smith (Not in Mondays)
2017/02/21 16:33:02
Given that this interface is always to std::set<Re
Julia Tuttle
2017/02/23 20:01:38
I've actually switched to std::vector<ReportHandle
Randy Smith (Not in Mondays)
2017/02/24 21:26:59
+1, FWIW. (I don't care a lot.)
| |
87 | |
88 // Gets all of the clients in the cache, regardless of origin or group. | |
89 std::set<const Client*> GetClients(); | |
Randy Smith (Not in Mondays)
2017/02/21 16:33:02
Hmmm. Consider |void GetClients(std::set<const Cl
Julia Tuttle
2017/02/23 20:01:38
Done, here and elsewhere.
| |
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 std::set<const Client*> GetClientsForOriginAndGroup(const url::Origin& origin, | |
95 const std::string& group); | |
96 | |
97 // Creates, updates, or removes a client for a particular origin and a | |
98 // particular endpoint. If |expires.is_null()|, removes the client; otherwise, | |
99 // creates the client if needed and sets |subdomains|, |group|, and |expires|. | |
100 // | |
101 // May invalidate Client pointers returned by |GetClients| or | |
102 // |GetClientsForOriginAndGroup|. | |
103 void SetClient(const url::Origin& origin, | |
104 const GURL& endpoint, | |
105 bool subdomains, | |
106 const std::string& group, | |
107 base::TimeTicks expires); | |
108 | |
109 // Removes a set of clients. | |
110 // | |
111 // May invalidate Client pointers returned by |GetClients| or | |
112 // |GetClientsForOriginAndGroup|. | |
113 void RemoveClients(const std::set<const Client*>& clients); | |
114 | |
115 // Removes all clients whose origin is |origin|.. | |
116 // | |
117 // May invalidate Client pointers returned by |GetClients| or | |
118 // |GetClientsForOriginAndGroup|. | |
119 void RemoveClientsForOrigin(const url::Origin& origin); | |
120 | |
121 // Removes all clients whose endpoint is |endpoint|. | |
122 // | |
123 // May invalidate Client pointers returned by |GetClients| or | |
124 // |GetClientsForOriginAndGroup|. | |
125 void RemoveClientsForEndpoint(const GURL& endpoint); | |
126 | |
127 private: | |
128 std::map<url::Origin, std::vector<std::unique_ptr<Client>>> origin_clients_; | |
129 std::vector<std::unique_ptr<Report>> reports_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(ReportingCache); | |
132 }; | |
133 | |
134 } // namespace reporting | |
135 | |
136 #endif // COMPONENTS_REPORTING_REPORTING_CACHE_H_ | |
OLD | NEW |