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