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