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" |
| 19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 20 | 21 |
| 22 class Profile; | |
| 23 | |
| 21 namespace base { | 24 namespace base { |
| 22 class Clock; | 25 class Clock; |
| 23 } | 26 } |
| 24 | 27 |
| 25 namespace net { | 28 namespace net { |
| 26 class URLRequestContextGetter; | 29 class URLRequestContextGetter; |
| 27 } | 30 } |
| 28 | 31 |
| 32 namespace safe_browsing { | |
| 33 class SafeBrowsingService; | |
| 34 } | |
| 35 | |
| 29 // This service initiates uploads of invalid certificate reports and retries any | 36 // This service initiates uploads of invalid certificate reports and retries any |
| 30 // failed uploads. Each report is retried until it's older than a certain time | 37 // 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, | 38 // to live (TTL). Reports older than this TTL are dropped and no more retried, |
| 32 // so that the retry list doesn't grow indefinitely. | 39 // so that the retry list doesn't grow indefinitely. |
| 33 // | 40 // |
| 34 // Lifetime and dependencies: | 41 // Lifetime and dependencies: |
| 35 // | 42 // |
| 36 // CertificateReportingService uses the url request context from SafeBrowsing | 43 // CertificateReportingService uses the url request context from SafeBrowsing |
| 37 // service. SafeBrowsing service is created before CertificateReportingService, | 44 // service. SafeBrowsing service is created before CertificateReportingService, |
| 38 // but is also shut down before any KeyedService is shut down. This means that | 45 // but is also shut down before any KeyedService is shut down. This means that |
| 39 // CertificateReportingService cannot depend on SafeBrowsing's url request being | 46 // CertificateReportingService cannot depend on SafeBrowsing's url request being |
| 40 // available at all times, and it should know when SafeBrowsing shuts down. | 47 // available at all times, and it should know when SafeBrowsing shuts down. It |
| 48 // does this by subscribing to SafeBrowsing service shut downs when it's | |
|
Jialiu Lin
2016/12/16 01:55:24
nit s/"SafeBrowsingService"/"SafeBrowsing service"
meacer
2016/12/16 20:26:35
Done.
| |
| 49 // created. When SafeBrowsing service shuts down, CertificateReportingService | |
| 50 // also shuts down. | |
| 51 // | |
| 52 // This class also observes SafeBrowsing preference changes to enable/disable | |
| 53 // reporting. It does this by creating a PreferenceObserver that notifies | |
| 54 // this service of changes in SafeBrowsing and extended reporting preferences. | |
| 41 class CertificateReportingService : public KeyedService { | 55 class CertificateReportingService : public KeyedService { |
| 42 public: | 56 public: |
| 43 // Represents a report to be sent. | 57 // Represents a report to be sent. |
| 44 struct Report { | 58 struct Report { |
| 45 int report_id; | 59 int report_id; |
| 46 base::Time creation_time; | 60 base::Time creation_time; |
| 47 std::string serialized_report; | 61 std::string serialized_report; |
| 48 Report(int report_id, | 62 Report(int report_id, |
| 49 base::Time creation_time, | 63 base::Time creation_time, |
| 50 const std::string& serialized_report) | 64 const std::string& serialized_report) |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 // Current report id, starting from zero and monotonically incrementing. | 136 // Current report id, starting from zero and monotonically incrementing. |
| 123 int current_report_id_; | 137 int current_report_id_; |
| 124 | 138 |
| 125 std::map<int, Report> inflight_reports_; | 139 std::map<int, Report> inflight_reports_; |
| 126 | 140 |
| 127 base::WeakPtrFactory<Reporter> weak_factory_; | 141 base::WeakPtrFactory<Reporter> weak_factory_; |
| 128 | 142 |
| 129 DISALLOW_COPY_AND_ASSIGN(Reporter); | 143 DISALLOW_COPY_AND_ASSIGN(Reporter); |
| 130 }; | 144 }; |
| 131 | 145 |
| 146 // Observes SafeBrowsing preference changes. | |
| 147 class PreferenceObserver { | |
| 148 public: | |
| 149 // Called when SafeBrowsing enabled state changes. | |
| 150 virtual void OnPreferenceChanged() = 0; | |
| 151 | |
| 152 virtual ~PreferenceObserver() {} | |
|
estark
2016/12/16 01:57:58
nit: looks weird to me to have the destructor at t
meacer
2016/12/16 20:26:35
Done.
| |
| 153 }; | |
| 154 | |
| 132 CertificateReportingService( | 155 CertificateReportingService( |
| 156 safe_browsing::SafeBrowsingService* safe_browsing_service, | |
| 133 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 157 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 158 Profile* profile, | |
| 134 uint8_t server_public_key[/* 32 */], | 159 uint8_t server_public_key[/* 32 */], |
| 135 uint32_t server_public_key_version, | 160 uint32_t server_public_key_version, |
| 136 size_t max_queued_report_count, | 161 size_t max_queued_report_count, |
| 137 base::TimeDelta max_report_age, | 162 base::TimeDelta max_report_age, |
| 138 std::unique_ptr<base::Clock> clock); | 163 base::Clock* clock); |
| 139 | 164 |
| 140 ~CertificateReportingService() override; | 165 ~CertificateReportingService() override; |
| 141 | 166 |
| 142 // KeyedService implementation: | 167 // KeyedService implementation: |
| 143 void Shutdown() override; | 168 void Shutdown() override; |
| 144 | 169 |
| 145 // Sends a serialized report. If the report upload fails, the upload is | 170 // Sends a serialized report. If the report upload fails, the upload is |
| 146 // retried at a future time. | 171 // retried at a future time. |
| 147 void Send(const std::string& serialized_report); | 172 void Send(const std::string& serialized_report); |
| 148 | 173 |
| 149 // Sends pending reports that are in the retry queue. | 174 // Sends pending reports that are in the retry queue. |
| 150 void SendPending(); | 175 void SendPending(); |
| 151 | 176 |
| 152 // Enables or disables reporting. When disabled, pending report queue is | 177 // Enables or disables reporting. When disabled, pending report queue is |
| 153 // cleared and incoming reports are ignored. Reporting is enabled by default | 178 // cleared and incoming reports are ignored. Reporting is enabled by default |
| 154 // once the service is initialized. | 179 // once the service is initialized. |
| 155 void SetEnabled(bool enabled); | 180 void SetEnabled(bool enabled); |
| 156 | 181 |
| 157 // Getters and setters for testing. | 182 // Getters for testing. |
| 158 Reporter* GetReporterForTesting() const; | 183 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(); | 184 static GURL GetReportingURLForTesting(); |
| 164 | 185 |
| 165 private: | 186 private: |
| 166 void Reset(); | 187 void Reset(); |
| 167 | 188 |
| 168 void InitializeOnIOThread( | 189 void InitializeOnIOThread( |
| 169 bool enabled, | 190 bool enabled, |
| 170 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 191 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 171 size_t max_queued_report_count, | 192 size_t max_queued_report_count, |
| 172 base::TimeDelta max_report_age, | 193 base::TimeDelta max_report_age, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 187 uint8_t* server_public_key, | 208 uint8_t* server_public_key, |
| 188 uint32_t server_public_key_version); | 209 uint32_t server_public_key_version); |
| 189 | 210 |
| 190 // If true, reporting is enabled. When SafeBrowsing preferences change, this | 211 // If true, reporting is enabled. When SafeBrowsing preferences change, this |
| 191 // might be set to false. | 212 // might be set to false. |
| 192 bool enabled_; | 213 bool enabled_; |
| 193 | 214 |
| 194 net::URLRequestContext* url_request_context_; | 215 net::URLRequestContext* url_request_context_; |
| 195 std::unique_ptr<Reporter> reporter_; | 216 std::unique_ptr<Reporter> reporter_; |
| 196 | 217 |
| 218 // Observes SafeBrowsing preference changes (SB is enabled/disabled, extended | |
| 219 // reporting is enabled/disabled). | |
| 220 std::unique_ptr<PreferenceObserver> preference_observer_; | |
| 221 | |
| 222 // Subscription for url request context shutdowns. When this subscription is | |
| 223 // notified, it means SafeBrowsing service is shutting down, and this service | |
| 224 // must also shut down. | |
| 225 std::unique_ptr<base::CallbackList<void(void)>::Subscription> | |
| 226 safe_browsing_service_shutdown_subscription_; | |
| 227 | |
| 197 // Maximum number of reports to be queued for retry. | 228 // Maximum number of reports to be queued for retry. |
| 198 size_t max_queued_report_count_; | 229 size_t max_queued_report_count_; |
| 199 | 230 |
| 200 // Maximum age of the reports to be queued for retry, from the time the | 231 // 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 | 232 // certificate error was first encountered by the user. Any report older than |
| 202 // this age is ignored and is not re-uploaded. | 233 // this age is ignored and is not re-uploaded. |
| 203 base::TimeDelta max_report_age_; | 234 base::TimeDelta max_report_age_; |
| 204 | 235 |
| 205 std::unique_ptr<base::Clock> clock_; | 236 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 | 237 |
| 211 // Encryption parameters. | 238 // Encryption parameters. |
| 212 uint8_t* server_public_key_; | 239 uint8_t* server_public_key_; |
| 213 uint32_t server_public_key_version_; | 240 uint32_t server_public_key_version_; |
| 214 | 241 |
| 215 DISALLOW_COPY_AND_ASSIGN(CertificateReportingService); | 242 DISALLOW_COPY_AND_ASSIGN(CertificateReportingService); |
| 216 }; | 243 }; |
| 217 | 244 |
| 218 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ | 245 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ |
| OLD | NEW |