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. | |
|
estark
2016/12/07 00:42:55
nit: "this service" gets a little confusing with a
meacer
2016/12/07 21:37:33
Done, and removed the last sentence for now.
| |
| 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); | |
| 67 }; | 88 }; |
| 68 | 89 |
| 69 // Class that handles report uploads and implements the upload retry logic. | 90 // Class that handles report uploads and implements the upload retry logic. |
| 70 class Reporter { | 91 class Reporter { |
| 71 public: | 92 public: |
| 72 Reporter( | 93 Reporter( |
| 73 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, | 94 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, |
| 74 std::unique_ptr<BoundedReportList> retry_list, | 95 std::unique_ptr<BoundedReportList> retry_list, |
| 75 base::Clock* clock, | 96 base::Clock* clock, |
| 76 base::TimeDelta report_ttl); | 97 base::TimeDelta report_ttl, |
| 98 bool retries_enabled); | |
| 77 ~Reporter(); | 99 ~Reporter(); |
| 78 | 100 |
| 79 // Sends a report. If the send fails, the report will be added to the retry | 101 // Sends a report. If the send fails, the report will be added to the retry |
| 80 // list. | 102 // list. |
| 81 void Send(const std::string& serialized_report); | 103 void Send(const std::string& serialized_report); |
| 82 | 104 |
| 83 // Sends all pending reports. Skips reports older than the |report_ttl| | 105 // Sends all pending reports. Skips reports older than the |report_ttl| |
| 84 // provided in the constructor. Failed reports will be added to the retry | 106 // provided in the constructor. Failed reports will be added to the retry |
| 85 // list. | 107 // list. |
| 86 void SendPending(); | 108 void SendPending(); |
| 87 | 109 |
| 88 // Getter and setters for testing: | 110 // Getter and setters for testing: |
| 89 size_t inflight_report_count_for_testing() const; | 111 size_t inflight_report_count_for_testing() const; |
| 90 BoundedReportList* GetQueueForTesting() const; | 112 BoundedReportList* GetQueueForTesting() const; |
| 91 | 113 |
| 92 private: | 114 private: |
| 93 void SendInternal(const Report& report); | 115 void SendInternal(const Report& report); |
| 94 void ErrorCallback(int report_id, const GURL& url, int error); | 116 void ErrorCallback(int report_id, const GURL& url, int error); |
| 95 void SuccessCallback(int report_id); | 117 void SuccessCallback(int report_id); |
| 96 | 118 |
| 97 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; | 119 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; |
| 98 std::unique_ptr<BoundedReportList> retry_list_; | 120 std::unique_ptr<BoundedReportList> retry_list_; |
| 99 base::Clock* test_clock_; | 121 base::Clock* clock_; |
| 122 // Maximum age of a queued report. Reports older than this are discarded in | |
| 123 // the next |SendPending| call. | |
| 100 const base::TimeDelta report_ttl_; | 124 const base::TimeDelta report_ttl_; |
| 125 // If true, retries are enabled. | |
| 126 const bool retries_enabled_; | |
| 127 // Current report id, starting from zero and monotonically incrementing. | |
| 101 int current_report_id_; | 128 int current_report_id_; |
| 102 | 129 |
| 103 std::map<int, Report> inflight_reports_; | 130 std::map<int, Report> inflight_reports_; |
| 104 | 131 |
| 105 base::WeakPtrFactory<Reporter> weak_factory_; | 132 base::WeakPtrFactory<Reporter> weak_factory_; |
| 106 | 133 |
| 107 DISALLOW_IMPLICIT_CONSTRUCTORS(Reporter); | 134 DISALLOW_COPY_AND_ASSIGN(Reporter); |
| 108 }; | 135 }; |
| 136 | |
| 137 CertificateReportingService( | |
| 138 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | |
| 139 uint8_t server_public_key[/* 32 */], | |
| 140 uint32_t server_public_key_version, | |
| 141 size_t max_queued_report_count, | |
| 142 base::TimeDelta max_report_age, | |
| 143 std::unique_ptr<base::Clock> clock); | |
| 144 | |
| 145 ~CertificateReportingService() override; | |
| 146 | |
| 147 // KeyedService implementation: | |
| 148 void Shutdown() override; | |
| 149 | |
| 150 // Sends a serialized report. If the report upload fails, the upload is | |
| 151 // retried in a future time. | |
| 152 void Send(const std::string& serialized_report); | |
| 153 | |
| 154 // Sends pending reports that are in the retry queue. | |
| 155 void SendPending(); | |
| 156 | |
| 157 // Enables or disables reporting. When disabled, pending report queue is | |
| 158 // cleared and incoming reports are ignored. Reporting is enabled by default | |
| 159 // once the service is initialized. | |
| 160 void SetEnabled(bool enabled); | |
| 161 | |
| 162 // Called when a send attempt is made. Public so that clients can notify this | |
| 163 // service that they abandoned a send attempt. | |
| 164 void DidAttemptSend(bool sent); | |
| 165 | |
| 166 // Getters and setters for testing. | |
| 167 Reporter* get_reporter_for_testing() const; | |
| 168 GURL GetReportingURLForTesting() const; | |
| 169 void SetMaxQueuedReportCountForTesting(size_t max_report_count); | |
| 170 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | |
| 171 void SetMaxReportAgeForTesting(base::TimeDelta max_report_age); | |
| 172 void SetServerPublicKeyForTesting(uint8_t server_public_key[/* 32 */], | |
| 173 uint32_t server_public_key_version); | |
| 174 | |
| 175 // URL to upload invalid certificate chain reports. An HTTP URL is | |
| 176 // used because a client seeing an invalid cert might not be able to | |
| 177 // make an HTTPS connection to report it. | |
| 178 static const char kExtendedReportingUploadUrlInsecure[]; | |
| 179 | |
| 180 private: | |
| 181 void Reset(); | |
| 182 | |
| 183 void InitializeOnIOThread( | |
| 184 bool enabled, | |
| 185 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | |
| 186 size_t max_queued_report_count, | |
| 187 base::TimeDelta max_report_age, | |
| 188 base::Clock* clock, | |
| 189 uint8_t* server_public_key, | |
| 190 uint32_t server_public_key_version); | |
| 191 | |
| 192 // Resets the reporter on the IO thread. Changes in SafeBrowsing or extended | |
| 193 // reporting enabled states cause the reporter to be reset. | |
| 194 // If |enabled| is false or |url_request_context_getter| is null, report is | |
| 195 // set to null, effectively cancelling all in flight uploads and clearing the | |
| 196 // pending reports queue. | |
| 197 void ResetOnIOThread(bool enabled, | |
| 198 net::URLRequestContext* url_request_context, | |
| 199 size_t max_queued_report_count, | |
| 200 base::TimeDelta max_report_age, | |
| 201 base::Clock* clock, | |
| 202 uint8_t* server_public_key, | |
| 203 uint32_t server_public_key_version); | |
| 204 | |
| 205 // If true, reporting is enabled. When SafeBrowsing preferences change, this | |
| 206 // might be set to false. | |
| 207 bool enabled_; | |
| 208 | |
| 209 // scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | |
|
estark
2016/12/07 00:42:55
don't forget to delete
meacer
2016/12/07 21:37:33
Done.
| |
| 210 net::URLRequestContext* url_request_context_; | |
| 211 | |
| 212 std::unique_ptr<Reporter> reporter_; | |
| 213 | |
| 214 // Subscription for url request context shutdowns. When this subscription is | |
| 215 // notified, it means that the SafeBrowsing service is shutting down, and this | |
| 216 // service must also shut down. | |
| 217 std::unique_ptr<base::CallbackList<void(void)>::Subscription> | |
| 218 safe_browsing_service_shutdown_subscription_; | |
| 219 | |
| 220 // Maximum number of reports to be queued for retry. | |
| 221 size_t max_queued_report_count_; | |
| 222 | |
| 223 // Maximum age of the reports to be queued for retry, from the time the | |
| 224 // certificate error was first encountered by the user. Any report older than | |
| 225 // this age is ignored and is not re-uploaded. | |
| 226 base::TimeDelta max_report_age_; | |
| 227 | |
| 228 std::unique_ptr<base::Clock> clock_; | |
| 229 | |
| 230 // Whether a send has ever been made. Used to verify that test setters are | |
| 231 // only called after initialization. | |
| 232 bool made_send_attempt_; | |
| 233 | |
| 234 // Encryption parameters. | |
| 235 uint8_t* server_public_key_; | |
| 236 uint32_t server_public_key_version_; | |
| 237 | |
| 238 DISALLOW_COPY_AND_ASSIGN(CertificateReportingService); | |
| 109 }; | 239 }; |
| 110 | 240 |
| 111 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ | 241 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ |
| OLD | NEW |