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

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: Fix unit tests 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;
estark 2016/12/09 00:49:16 not needed, you're #include'ing it above
meacer 2016/12/09 01:07:04 Done.
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 CertificateReportingService,
40 // but is also shut down before any KeyedService is shut down. This means that
41 // CertificateReportingService cannot depend on SafeBrowsing's url request being
42 // available at all times, and it should know when SafeBrowsing shuts down.
26 class CertificateReportingService : public KeyedService { 43 class CertificateReportingService : public KeyedService {
27 public: 44 public:
28 // Represent a report to be sent. 45 // Represents a report to be sent.
29 struct Report { 46 struct Report {
30 int report_id; 47 int report_id;
31 base::Time creation_time; 48 base::Time creation_time;
32 std::string serialized_report; 49 std::string serialized_report;
33 Report(int report_id, 50 Report(int report_id,
34 base::Time creation_time, 51 base::Time creation_time,
35 const std::string& serialized_report) 52 const std::string& serialized_report)
36 : report_id(report_id), 53 : report_id(report_id),
37 creation_time(creation_time), 54 creation_time(creation_time),
38 serialized_report(serialized_report) {} 55 serialized_report(serialized_report) {}
(...skipping 18 matching lines...) Expand all
57 74
58 private: 75 private:
59 // Maximum number of reports in the list. If the number of reports in the 76 // 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 77 // 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 78 // 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. 79 // added when it's newer than the oldest item in the list.
63 const size_t max_size_; 80 const size_t max_size_;
64 81
65 std::vector<Report> items_; 82 std::vector<Report> items_;
66 base::ThreadChecker thread_checker_; 83 base::ThreadChecker thread_checker_;
84
85 DISALLOW_COPY_AND_ASSIGN(BoundedReportList);
67 }; 86 };
68 87
69 // Class that handles report uploads and implements the upload retry logic. 88 // Class that handles report uploads and implements the upload retry logic.
70 class Reporter { 89 class Reporter {
71 public: 90 public:
72 Reporter( 91 Reporter(
73 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, 92 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_,
74 std::unique_ptr<BoundedReportList> retry_list, 93 std::unique_ptr<BoundedReportList> retry_list,
75 base::Clock* clock, 94 base::Clock* clock,
76 base::TimeDelta report_ttl); 95 base::TimeDelta report_ttl,
96 bool retries_enabled);
77 ~Reporter(); 97 ~Reporter();
78 98
79 // Sends a report. If the send fails, the report will be added to the retry 99 // Sends a report. If the send fails, the report will be added to the retry
80 // list. 100 // list.
81 void Send(const std::string& serialized_report); 101 void Send(const std::string& serialized_report);
82 102
83 // Sends all pending reports. Skips reports older than the |report_ttl| 103 // Sends all pending reports. Skips reports older than the |report_ttl|
84 // provided in the constructor. Failed reports will be added to the retry 104 // provided in the constructor. Failed reports will be added to the retry
85 // list. 105 // list.
86 void SendPending(); 106 void SendPending();
87 107
88 // Getter and setters for testing: 108 // Getter and setters for testing:
89 size_t inflight_report_count_for_testing() const; 109 size_t inflight_report_count_for_testing() const;
90 BoundedReportList* GetQueueForTesting() const; 110 BoundedReportList* GetQueueForTesting() const;
91 111
92 private: 112 private:
93 void SendInternal(const Report& report); 113 void SendInternal(const Report& report);
94 void ErrorCallback(int report_id, const GURL& url, int error); 114 void ErrorCallback(int report_id, const GURL& url, int error);
95 void SuccessCallback(int report_id); 115 void SuccessCallback(int report_id);
96 116
97 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; 117 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_;
98 std::unique_ptr<BoundedReportList> retry_list_; 118 std::unique_ptr<BoundedReportList> retry_list_;
99 base::Clock* test_clock_; 119 base::Clock* clock_;
120 // Maximum age of a queued report. Reports older than this are discarded in
121 // the next SendPending() call.
100 const base::TimeDelta report_ttl_; 122 const base::TimeDelta report_ttl_;
123 const bool retries_enabled_;
124 // Current report id, starting from zero and monotonically incrementing.
101 int current_report_id_; 125 int current_report_id_;
102 126
103 std::map<int, Report> inflight_reports_; 127 std::map<int, Report> inflight_reports_;
104 128
105 base::WeakPtrFactory<Reporter> weak_factory_; 129 base::WeakPtrFactory<Reporter> weak_factory_;
106 130
107 DISALLOW_IMPLICIT_CONSTRUCTORS(Reporter); 131 DISALLOW_COPY_AND_ASSIGN(Reporter);
108 }; 132 };
133
134 CertificateReportingService(
135 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
136 uint8_t server_public_key[/* 32 */],
137 uint32_t server_public_key_version,
138 size_t max_queued_report_count,
139 base::TimeDelta max_report_age,
140 std::unique_ptr<base::Clock> clock);
141
142 ~CertificateReportingService() override;
143
144 // KeyedService implementation:
145 void Shutdown() override;
146
147 // Sends a serialized report. If the report upload fails, the upload is
148 // retried at a future time.
149 void Send(const std::string& serialized_report);
150
151 // Sends pending reports that are in the retry queue.
152 void SendPending();
153
154 // Enables or disables reporting. When disabled, pending report queue is
155 // cleared and incoming reports are ignored. Reporting is enabled by default
156 // once the service is initialized.
157 void SetEnabled(bool enabled);
158
159 // Called when a send attempt is made. Public so that clients can notify this
160 // service that they abandoned a send attempt.
161 void DidAttemptSend(bool sent);
estark 2016/12/09 00:49:16 not used/implemented?
meacer 2016/12/09 01:07:04 Done.
162
163 // Getters and setters for testing.
164 Reporter* get_reporter_for_testing() const;
estark 2016/12/09 00:49:16 GetReporterForTesting
meacer 2016/12/09 01:07:04 Done.
165 GURL GetReportingURLForTesting() const;
166 void SetMaxQueuedReportCountForTesting(size_t max_report_count);
167 void SetClockForTesting(std::unique_ptr<base::Clock> clock);
168 void SetMaxReportAgeForTesting(base::TimeDelta max_report_age);
169 void SetServerPublicKeyForTesting(uint8_t server_public_key[/* 32 */],
170 uint32_t server_public_key_version);
171
172 static const char kExtendedReportingUploadUrl[];
estark 2016/12/09 00:49:16 I think this is only used in tests; if so, could y
meacer 2016/12/09 01:07:04 Done.
173
174 private:
175 void Reset();
176
177 void InitializeOnIOThread(
178 bool enabled,
179 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
180 size_t max_queued_report_count,
181 base::TimeDelta max_report_age,
182 base::Clock* clock,
183 uint8_t* server_public_key,
184 uint32_t server_public_key_version);
185
186 // Resets the reporter on the IO thread. Changes in SafeBrowsing or extended
187 // reporting enabled states cause the reporter to be reset.
188 // If |enabled| is false or |url_request_context_getter| is null, report is
189 // set to null, effectively cancelling all in flight uploads and clearing the
190 // pending reports queue.
191 void ResetOnIOThread(bool enabled,
192 net::URLRequestContext* url_request_context,
193 size_t max_queued_report_count,
194 base::TimeDelta max_report_age,
195 base::Clock* clock,
196 uint8_t* server_public_key,
197 uint32_t server_public_key_version);
198
199 // If true, reporting is enabled. When SafeBrowsing preferences change, this
200 // might be set to false.
201 bool enabled_;
202
203 net::URLRequestContext* url_request_context_;
204 std::unique_ptr<Reporter> reporter_;
205
206 // Maximum number of reports to be queued for retry.
207 size_t max_queued_report_count_;
208
209 // Maximum age of the reports to be queued for retry, from the time the
210 // certificate error was first encountered by the user. Any report older than
211 // this age is ignored and is not re-uploaded.
212 base::TimeDelta max_report_age_;
213
214 std::unique_ptr<base::Clock> clock_;
215
216 // Whether a send has ever been made. Used to verify that test setters are
217 // only called after initialization.
218 bool made_send_attempt_;
219
220 // Encryption parameters.
221 uint8_t* server_public_key_;
222 uint32_t server_public_key_version_;
223
224 DISALLOW_COPY_AND_ASSIGN(CertificateReportingService);
109 }; 225 };
110 226
111 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ 227 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698