Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: chrome/browser/safe_browsing/certificate_reporting_service.h

Issue 2543523002: Implement main CertificateReportingService code and add unit tests. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ 6 #define CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/callback_list.h"
13 #include "base/macros.h" 14 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
17 #include "components/certificate_reporting/error_reporter.h" 18 #include "components/certificate_reporting/error_reporter.h"
18 #include "components/keyed_service/core/keyed_service.h" 19 #include "components/keyed_service/core/keyed_service.h"
20 #include "net/url_request/url_request_context_getter.h"
19 21
20 namespace base { 22 namespace base {
21 class Clock; 23 class Clock;
22 } 24 }
23 25
26 namespace net {
27 class URLRequestContext;
28 class URLRequestContextGetter;
29 }
30
24 // This service initiates uploads of invalid certificate reports and retries any 31 // This service initiates uploads of invalid certificate reports and retries any
25 // failed uploads. 32 // failed uploads. Each report is retried until it's older than a certain time
33 // to live (TTL). Reports older than this TTL are dropped and no more retried,
34 // so that the retry list doesn't grow indefinitely.
35 //
36 // Lifetime and dependencies:
37 //
38 // CertificateReportingService uses the url request context from SafeBrowsing
39 // service. SafeBrowsing service is created before this service, but is also
40 // shut down before any KeyedService is shut down. This means that this class
41 // cannot depend on SafeBrowsing's url request being
Jialiu Lin 2016/11/30 22:18:06 nit: "available at all times" probably can fit in
meacer 2016/11/30 23:39:40 Done. Also removed last paragraph as this CL doesn
42 // available at all times, and it should know when SafeBrowsing shuts down.
43 // ChromeContentBrowserClient subscribes this service to SafeBrowsing service
44 // shut downs during initialization, and this service shuts down when
45 // SafeBrowsing shuts down.
46 //
47 // This class also observes SafeBrowsing preference changes to enable/disable
48 // reporting. It does this by creating an observer (PreferenceObserver) that
49 // notifies this service of changes in SafeBrowsing and extended reporting
50 // preferences.
26 class CertificateReportingService : public KeyedService { 51 class CertificateReportingService : public KeyedService {
27 public: 52 public:
28 // Represent a report to be sent. 53 // Represents a report to be sent.
29 struct Report { 54 struct Report {
30 int report_id; 55 int report_id;
31 base::Time creation_time; 56 base::Time creation_time;
32 std::string serialized_report; 57 std::string serialized_report;
33 Report(int report_id, 58 Report(int report_id,
34 base::Time creation_time, 59 base::Time creation_time,
35 const std::string& serialized_report) 60 const std::string& serialized_report)
36 : report_id(report_id), 61 : report_id(report_id),
37 creation_time(creation_time), 62 creation_time(creation_time),
38 serialized_report(serialized_report) {} 63 serialized_report(serialized_report) {}
(...skipping 18 matching lines...) Expand all
57 82
58 private: 83 private:
59 // Maximum number of reports in the list. If the number of reports in the 84 // Maximum number of reports in the list. If the number of reports in the
60 // list is smaller than this number, a new item is immediately added to the 85 // list is smaller than this number, a new item is immediately added to the
61 // list. Otherwise, the item is compared to the items in the list and only 86 // list. Otherwise, the item is compared to the items in the list and only
62 // added when it's newer than the oldest item in the list. 87 // added when it's newer than the oldest item in the list.
63 const size_t max_size_; 88 const size_t max_size_;
64 89
65 std::vector<Report> items_; 90 std::vector<Report> items_;
66 base::ThreadChecker thread_checker_; 91 base::ThreadChecker thread_checker_;
92
93 DISALLOW_COPY_AND_ASSIGN(BoundedReportList);
94 };
95
96 // A class to observe events by the service. Used for testing.
97 class EventObserver {
98 public:
99 EventObserver() {}
100 virtual ~EventObserver() {}
101
102 // Called when sending of a report is attempted. If attempt was cancelled,
103 // |completed| is false. Otherwise, it's true.
104 virtual void OnSendAttempt(bool completed) {}
105 // Called when sending a report is completed. If attempt was successful,
106 // |success| is true. Otherwise, it's false.
107 virtual void OnSendComplete(int report_id, bool success) {}
108 // Called when reporter is created. This can happen when changing
109 // SafeBrowsing or extended reporting preferences.
110 virtual void OnCreated() {}
111 // The service is being reset because SafeBrowsing preferences have changed.
112 virtual void OnReset() {}
67 }; 113 };
68 114
69 // Class that handles report uploads and implements the upload retry logic. 115 // Class that handles report uploads and implements the upload retry logic.
70 class Reporter { 116 class Reporter {
71 public: 117 public:
72 Reporter( 118 Reporter(
73 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, 119 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_,
74 std::unique_ptr<BoundedReportList> retry_list, 120 std::unique_ptr<BoundedReportList> retry_list,
75 base::Clock* clock, 121 base::Clock* clock,
76 base::TimeDelta report_ttl); 122 base::TimeDelta report_ttl,
123 EventObserver* observer,
124 bool retries_enabled);
77 ~Reporter(); 125 ~Reporter();
78 126
79 // Sends a report. If the send fails, the report will be added to the retry 127 // Sends a report. If the send fails, the report will be added to the retry
80 // list. 128 // list.
81 void Send(const std::string& serialized_report); 129 void Send(const std::string& serialized_report);
82 130
83 // Sends all pending reports. Skips reports older than the |report_ttl| 131 // Sends all pending reports. Skips reports older than the |report_ttl|
84 // provided in the constructor. Failed reports will be added to the retry 132 // provided in the constructor. Failed reports will be added to the retry
85 // list. 133 // list.
86 void SendPending(); 134 void SendPending();
87 135
88 // Getter and setters for testing: 136 // Getter and setters for testing:
89 size_t inflight_report_count_for_testing() const; 137 size_t inflight_report_count_for_testing() const;
90 BoundedReportList* GetQueueForTesting() const; 138 BoundedReportList* GetQueueForTesting() const;
139 void SetEventObserverForTesting(EventObserver* observer);
91 140
92 private: 141 private:
93 void SendInternal(const Report& report); 142 void SendInternal(const Report& report);
94 void ErrorCallback(int report_id, const GURL& url, int error); 143 void ErrorCallback(int report_id, const GURL& url, int error);
95 void SuccessCallback(int report_id); 144 void SuccessCallback(int report_id);
96 145
97 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; 146 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_;
98 std::unique_ptr<BoundedReportList> retry_list_; 147 std::unique_ptr<BoundedReportList> retry_list_;
99 base::Clock* test_clock_; 148 base::Clock* test_clock_;
149 // Maximum age of a queued report. Reports older than this are discarded in
150 // the next |SendPending| call.
100 const base::TimeDelta report_ttl_; 151 const base::TimeDelta report_ttl_;
152 // Event observer to observe events generated by the reporter. Can be null.
153 EventObserver* event_observer_;
154 // If true, retries are enabled.
155 const bool retries_enabled_;
156 // Current report id, starting from zero and monotonically incrementing.
101 int current_report_id_; 157 int current_report_id_;
102 158
103 std::map<int, Report> inflight_reports_; 159 std::map<int, Report> inflight_reports_;
104 160
105 base::WeakPtrFactory<Reporter> weak_factory_; 161 base::WeakPtrFactory<Reporter> weak_factory_;
106 162
107 DISALLOW_IMPLICIT_CONSTRUCTORS(Reporter); 163 DISALLOW_COPY_AND_ASSIGN(Reporter);
108 }; 164 };
165
166 CertificateReportingService(
167 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
168 std::unique_ptr<EventObserver> event_observer,
169 size_t max_queued_report_count,
170 base::TimeDelta max_report_age,
171 base::Clock* test_clock);
172
173 ~CertificateReportingService() override;
174
175 // KeyedService implementation:
176 void Shutdown() override;
177
178 // Sends a serialized report. If the report upload fails, the upload is
179 // retried in a future time.
180 void Send(const std::string& serialized_report);
181
182 // Sends pending reports that are in the retry queue.
183 void SendPending();
184
185 // Enables or disables reporting. When disabled, pending report queue is
186 // cleared and incoming reports are ignored. Reporting is enabled by default
187 // once the service is initialized.
188 void SetEnabled(bool enabled);
189
190 // Called when a send attempt is made. Public so that clients can notify this
191 // service that they abandoned a send attempt.
192 void DidAttemptSend(bool sent);
193
194 // Getters and setters for testing.
195 Reporter* get_reporter_for_testing() const;
196 GURL GetReportingURLForTesting() const;
197 void SetEventObserverForTesting(std::unique_ptr<EventObserver> observer);
198 void SetMaxQueuedReportCountForTesting(size_t max_report_count);
199 void SetClockForTesting(base::Clock* clock);
200 void SetMaxReportAgeForTesting(base::TimeDelta max_report_age);
201
202 // URL to upload invalid certificate chain reports. An HTTP URL is
203 // used because a client seeing an invalid cert might not be able to
204 // make an HTTPS connection to report it.
205 static const char kExtendedReportingUploadUrlInsecure[];
206
207 private:
208 void Reset(const base::Callback<void()>& callback);
209
210 void InitializeOnIOThread(
211 bool enabled,
212 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
213 size_t max_queued_report_count,
214 base::TimeDelta max_report_age,
215 base::Clock* test_clock,
216 EventObserver* event_observer);
217
218 // Resets the reporter on the IO thread. Changes in SafeBrowsing or extended
219 // reporting enabled states cause the reporter to be reset.
220 // If |enabled| is false or |url_request_context_getter| is null, report is
221 // set to null, effectively cancelling all in flight uploads and clearing the
222 // pending reports queue.
223 void ResetOnIOThread(bool enabled,
224 net::URLRequestContext* url_request_context,
225 size_t max_queued_report_count,
226 base::TimeDelta max_report_age,
227 base::Clock* test_clock,
228 EventObserver* event_observer);
229
230 // If true, reporting is enabled. When SafeBrowsing preferences change, this
231 // might be set to false.
232 bool enabled_;
233
234 // scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
235 net::URLRequestContext* url_request_context_;
236
237 std::unique_ptr<Reporter> reporter_;
238
239 // Subscription for url request context shutdowns. When this subscription is
240 // notified, it means that the SafeBrowsing service is shutting down, and this
241 // service must also shut down.
242 std::unique_ptr<base::CallbackList<void(void)>::Subscription>
243 safe_browsing_service_shutdown_subscription_;
244
245 // Observes events from this service. Default implementation doesn't do
246 // anything. Tests use this to keep track of sent/failed reports etc.
247 std::unique_ptr<EventObserver> event_observer_;
248
249 // Maximum number of reports to be queued for retry.
250 size_t max_queued_report_count_;
251
252 // Maximum age of the reports to be queued for retry, from the time the
253 // certificate error was first encountered by the user. Any report older than
254 // this age is ignored and is not re-uploaded.
255 base::TimeDelta max_report_age_;
256
257 // Test clock. If null, system clock is used.
258 base::Clock* test_clock_;
259
260 // Whether a send has ever been made. Used to verify that test setters are
261 // only called after initialization.
262 bool made_send_attempt_;
263
264 DISALLOW_COPY_AND_ASSIGN(CertificateReportingService);
109 }; 265 };
110 266
111 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ 267 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698