Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 available at all times, and | |
| 42 // it should know when SafeBrowsing shuts down. ChromeContentBrowserClient | |
| 43 // subscribes this service to SafeBrowsing service shut downs during | |
| 44 // initialization, and this service shuts down when SafeBrowsing shuts down. | |
| 26 class CertificateReportingService : public KeyedService { | 45 class CertificateReportingService : public KeyedService { |
| 27 public: | 46 public: |
| 28 // Represent a report to be sent. | 47 // Represents a report to be sent. |
| 29 struct Report { | 48 struct Report { |
| 30 int report_id; | 49 int report_id; |
| 31 base::Time creation_time; | 50 base::Time creation_time; |
| 32 std::string serialized_report; | 51 std::string serialized_report; |
| 33 Report(int report_id, | 52 Report(int report_id, |
| 34 base::Time creation_time, | 53 base::Time creation_time, |
| 35 const std::string& serialized_report) | 54 const std::string& serialized_report) |
| 36 : report_id(report_id), | 55 : report_id(report_id), |
| 37 creation_time(creation_time), | 56 creation_time(creation_time), |
| 38 serialized_report(serialized_report) {} | 57 serialized_report(serialized_report) {} |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 57 | 76 |
| 58 private: | 77 private: |
| 59 // Maximum number of reports in the list. If the number of reports in the | 78 // 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 | 79 // 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 | 80 // 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. | 81 // added when it's newer than the oldest item in the list. |
| 63 const size_t max_size_; | 82 const size_t max_size_; |
| 64 | 83 |
| 65 std::vector<Report> items_; | 84 std::vector<Report> items_; |
| 66 base::ThreadChecker thread_checker_; | 85 base::ThreadChecker thread_checker_; |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(BoundedReportList); | |
| 88 }; | |
| 89 | |
| 90 // A class to observe events by the service. Used for testing. | |
| 91 class EventObserver { | |
| 92 public: | |
| 93 EventObserver() {} | |
| 94 virtual ~EventObserver() {} | |
| 95 | |
| 96 // Called when sending of a report is attempted. If attempt was cancelled, | |
| 97 // |completed| is false. Otherwise, it's true. | |
| 98 virtual void OnSendAttempt(bool completed) {} | |
| 99 // Called when sending a report is completed. If attempt was successful, | |
| 100 // |success| is true. Otherwise, it's false. | |
| 101 virtual void OnSendComplete(int report_id, bool success) {} | |
| 102 // Called when reporter is created. This can happen when changing | |
| 103 // SafeBrowsing or extended reporting preferences. | |
| 104 virtual void OnCreated() {} | |
| 105 // The service is being reset because SafeBrowsing preferences have changed. | |
| 106 virtual void OnReset() {} | |
| 67 }; | 107 }; |
| 68 | 108 |
| 69 // Class that handles report uploads and implements the upload retry logic. | 109 // Class that handles report uploads and implements the upload retry logic. |
| 70 class Reporter { | 110 class Reporter { |
|
estark
2016/12/01 01:46:53
Why does this need to be its own class?
meacer
2016/12/06 02:22:34
I think there is better separation this way. In pa
| |
| 71 public: | 111 public: |
| 72 Reporter( | 112 Reporter( |
| 73 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, | 113 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, |
| 74 std::unique_ptr<BoundedReportList> retry_list, | 114 std::unique_ptr<BoundedReportList> retry_list, |
| 75 base::Clock* clock, | 115 base::Clock* clock, |
| 76 base::TimeDelta report_ttl); | 116 base::TimeDelta report_ttl, |
| 117 EventObserver* observer, | |
| 118 bool retries_enabled); | |
| 77 ~Reporter(); | 119 ~Reporter(); |
| 78 | 120 |
| 79 // Sends a report. If the send fails, the report will be added to the retry | 121 // Sends a report. If the send fails, the report will be added to the retry |
| 80 // list. | 122 // list. |
| 81 void Send(const std::string& serialized_report); | 123 void Send(const std::string& serialized_report); |
| 82 | 124 |
| 83 // Sends all pending reports. Skips reports older than the |report_ttl| | 125 // Sends all pending reports. Skips reports older than the |report_ttl| |
| 84 // provided in the constructor. Failed reports will be added to the retry | 126 // provided in the constructor. Failed reports will be added to the retry |
| 85 // list. | 127 // list. |
| 86 void SendPending(); | 128 void SendPending(); |
| 87 | 129 |
| 88 // Getter and setters for testing: | 130 // Getter and setters for testing: |
| 89 size_t inflight_report_count_for_testing() const; | 131 size_t inflight_report_count_for_testing() const; |
| 90 BoundedReportList* GetQueueForTesting() const; | 132 BoundedReportList* GetQueueForTesting() const; |
| 133 void SetEventObserverForTesting(EventObserver* observer); | |
| 91 | 134 |
| 92 private: | 135 private: |
| 93 void SendInternal(const Report& report); | 136 void SendInternal(const Report& report); |
| 94 void ErrorCallback(int report_id, const GURL& url, int error); | 137 void ErrorCallback(int report_id, const GURL& url, int error); |
| 95 void SuccessCallback(int report_id); | 138 void SuccessCallback(int report_id); |
| 96 | 139 |
| 97 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; | 140 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; |
| 98 std::unique_ptr<BoundedReportList> retry_list_; | 141 std::unique_ptr<BoundedReportList> retry_list_; |
| 99 base::Clock* test_clock_; | 142 base::Clock* test_clock_; |
| 143 // Maximum age of a queued report. Reports older than this are discarded in | |
| 144 // the next |SendPending| call. | |
|
estark
2016/12/01 01:46:53
very tiny nit: |blah| is only supposed to be used
meacer
2016/12/07 21:37:33
Done.
| |
| 100 const base::TimeDelta report_ttl_; | 145 const base::TimeDelta report_ttl_; |
| 146 // Event observer to observe events generated by the reporter. Can be null. | |
| 147 EventObserver* event_observer_; | |
| 148 // If true, retries are enabled. | |
|
estark
2016/12/01 01:46:53
nit: I think you could remove this comment, doesn'
meacer
2016/12/07 21:37:33
Done.
| |
| 149 const bool retries_enabled_; | |
| 150 // Current report id, starting from zero and monotonically incrementing. | |
| 101 int current_report_id_; | 151 int current_report_id_; |
| 102 | 152 |
| 103 std::map<int, Report> inflight_reports_; | 153 std::map<int, Report> inflight_reports_; |
| 104 | 154 |
| 105 base::WeakPtrFactory<Reporter> weak_factory_; | 155 base::WeakPtrFactory<Reporter> weak_factory_; |
| 106 | 156 |
| 107 DISALLOW_IMPLICIT_CONSTRUCTORS(Reporter); | 157 DISALLOW_COPY_AND_ASSIGN(Reporter); |
| 108 }; | 158 }; |
| 159 | |
| 160 CertificateReportingService( | |
| 161 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | |
| 162 std::unique_ptr<EventObserver> event_observer, | |
| 163 size_t max_queued_report_count, | |
| 164 base::TimeDelta max_report_age, | |
| 165 base::Clock* test_clock); | |
| 166 | |
| 167 ~CertificateReportingService() override; | |
| 168 | |
| 169 // KeyedService implementation: | |
| 170 void Shutdown() override; | |
| 171 | |
| 172 // Sends a serialized report. If the report upload fails, the upload is | |
| 173 // retried in a future time. | |
|
estark
2016/12/01 01:46:53
nit: in => at
meacer
2016/12/07 21:37:32
Done.
| |
| 174 void Send(const std::string& serialized_report); | |
| 175 | |
| 176 // Sends pending reports that are in the retry queue. | |
| 177 void SendPending(); | |
| 178 | |
| 179 // Enables or disables reporting. When disabled, pending report queue is | |
| 180 // cleared and incoming reports are ignored. Reporting is enabled by default | |
| 181 // once the service is initialized. | |
| 182 void SetEnabled(bool enabled); | |
| 183 | |
| 184 // Called when a send attempt is made. Public so that clients can notify this | |
| 185 // service that they abandoned a send attempt. | |
| 186 void DidAttemptSend(bool sent); | |
| 187 | |
| 188 // Getters and setters for testing. | |
| 189 Reporter* get_reporter_for_testing() const; | |
| 190 GURL GetReportingURLForTesting() const; | |
| 191 void SetEventObserverForTesting(std::unique_ptr<EventObserver> observer); | |
| 192 void SetMaxQueuedReportCountForTesting(size_t max_report_count); | |
| 193 void SetClockForTesting(base::Clock* clock); | |
| 194 void SetMaxReportAgeForTesting(base::TimeDelta max_report_age); | |
| 195 | |
| 196 // URL to upload invalid certificate chain reports. An HTTP URL is | |
|
estark
2016/12/01 01:46:53
nit: I don't think this detail about the HTTP URL
meacer
2016/12/07 21:37:33
Done.
| |
| 197 // used because a client seeing an invalid cert might not be able to | |
| 198 // make an HTTPS connection to report it. | |
| 199 static const char kExtendedReportingUploadUrlInsecure[]; | |
| 200 | |
| 201 private: | |
| 202 void Reset(const base::Callback<void()>& callback); | |
| 203 | |
| 204 void InitializeOnIOThread( | |
| 205 bool enabled, | |
| 206 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | |
| 207 size_t max_queued_report_count, | |
| 208 base::TimeDelta max_report_age, | |
| 209 base::Clock* test_clock, | |
| 210 EventObserver* event_observer); | |
| 211 | |
| 212 // Resets the reporter on the IO thread. Changes in SafeBrowsing or extended | |
| 213 // reporting enabled states cause the reporter to be reset. | |
| 214 // If |enabled| is false or |url_request_context_getter| is null, report is | |
| 215 // set to null, effectively cancelling all in flight uploads and clearing the | |
| 216 // pending reports queue. | |
| 217 void ResetOnIOThread(bool enabled, | |
| 218 net::URLRequestContext* url_request_context, | |
| 219 size_t max_queued_report_count, | |
| 220 base::TimeDelta max_report_age, | |
| 221 base::Clock* test_clock, | |
| 222 EventObserver* event_observer); | |
| 223 | |
| 224 // If true, reporting is enabled. When SafeBrowsing preferences change, this | |
| 225 // might be set to false. | |
| 226 bool enabled_; | |
| 227 | |
| 228 // scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | |
|
estark
2016/12/01 01:46:53
should be deleted?
meacer
2016/12/07 21:37:33
Done.
| |
| 229 net::URLRequestContext* url_request_context_; | |
| 230 | |
| 231 std::unique_ptr<Reporter> reporter_; | |
| 232 | |
| 233 // Subscription for url request context shutdowns. When this subscription is | |
| 234 // notified, it means that the SafeBrowsing service is shutting down, and this | |
| 235 // service must also shut down. | |
| 236 std::unique_ptr<base::CallbackList<void(void)>::Subscription> | |
| 237 safe_browsing_service_shutdown_subscription_; | |
|
estark
2016/12/01 01:46:53
this is not hooked up yet, correct? (no change nee
meacer
2016/12/07 21:37:33
Yes, removed from this CL.
| |
| 238 | |
| 239 // Observes events from this service. Default implementation doesn't do | |
| 240 // anything. Tests use this to keep track of sent/failed reports etc. | |
| 241 std::unique_ptr<EventObserver> event_observer_; | |
| 242 | |
| 243 // Maximum number of reports to be queued for retry. | |
| 244 size_t max_queued_report_count_; | |
| 245 | |
| 246 // Maximum age of the reports to be queued for retry, from the time the | |
| 247 // certificate error was first encountered by the user. Any report older than | |
| 248 // this age is ignored and is not re-uploaded. | |
| 249 base::TimeDelta max_report_age_; | |
| 250 | |
| 251 // Test clock. If null, system clock is used. | |
| 252 base::Clock* test_clock_; | |
| 253 | |
| 254 // Whether a send has ever been made. Used to verify that test setters are | |
| 255 // only called after initialization. | |
| 256 bool made_send_attempt_; | |
| 257 | |
| 258 DISALLOW_COPY_AND_ASSIGN(CertificateReportingService); | |
| 109 }; | 259 }; |
| 110 | 260 |
| 111 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ | 261 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ |
| OLD | NEW |