Chromium Code Reviews| Index: chrome/browser/safe_browsing/certificate_reporting_service.h |
| diff --git a/chrome/browser/safe_browsing/certificate_reporting_service.h b/chrome/browser/safe_browsing/certificate_reporting_service.h |
| index 4c1e4cd21ab5eea499461a58ff401a556f061252..3cc2416e5f3ffa2a76318a70add5c198ac93f981 100644 |
| --- a/chrome/browser/safe_browsing/certificate_reporting_service.h |
| +++ b/chrome/browser/safe_browsing/certificate_reporting_service.h |
| @@ -10,22 +10,47 @@ |
| #include <string> |
| #include <vector> |
| +#include "base/callback_list.h" |
| #include "base/macros.h" |
| #include "base/memory/weak_ptr.h" |
| #include "base/threading/thread_checker.h" |
| #include "base/time/time.h" |
| #include "components/certificate_reporting/error_reporter.h" |
| #include "components/keyed_service/core/keyed_service.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| namespace base { |
| class Clock; |
| } |
| +namespace net { |
| +class URLRequestContext; |
| +class URLRequestContextGetter; |
| +} |
| + |
| // This service initiates uploads of invalid certificate reports and retries any |
| -// failed uploads. |
| +// failed uploads. Each report is retried until it's older than a certain time |
| +// to live (TTL). Reports older than this TTL are dropped and no more retried, |
| +// so that the retry list doesn't grow indefinitely. |
| +// |
| +// Lifetime and dependencies: |
| +// |
| +// CertificateReportingService uses the url request context from SafeBrowsing |
| +// service. SafeBrowsing service is created before this service, but is also |
| +// shut down before any KeyedService is shut down. This means that this class |
| +// cannot depend on SafeBrowsing's url request being |
|
Jialiu Lin
2016/11/30 22:18:06
nit: "available at all times" probably can fit in
meacer
2016/11/30 23:39:40
Done. Also removed last paragraph as this CL doesn
|
| +// available at all times, and it should know when SafeBrowsing shuts down. |
| +// ChromeContentBrowserClient subscribes this service to SafeBrowsing service |
| +// shut downs during initialization, and this service shuts down when |
| +// SafeBrowsing shuts down. |
| +// |
| +// This class also observes SafeBrowsing preference changes to enable/disable |
| +// reporting. It does this by creating an observer (PreferenceObserver) that |
| +// notifies this service of changes in SafeBrowsing and extended reporting |
| +// preferences. |
| class CertificateReportingService : public KeyedService { |
| public: |
| - // Represent a report to be sent. |
| + // Represents a report to be sent. |
| struct Report { |
| int report_id; |
| base::Time creation_time; |
| @@ -64,6 +89,27 @@ class CertificateReportingService : public KeyedService { |
| std::vector<Report> items_; |
| base::ThreadChecker thread_checker_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BoundedReportList); |
| + }; |
| + |
| + // A class to observe events by the service. Used for testing. |
| + class EventObserver { |
| + public: |
| + EventObserver() {} |
| + virtual ~EventObserver() {} |
| + |
| + // Called when sending of a report is attempted. If attempt was cancelled, |
| + // |completed| is false. Otherwise, it's true. |
| + virtual void OnSendAttempt(bool completed) {} |
| + // Called when sending a report is completed. If attempt was successful, |
| + // |success| is true. Otherwise, it's false. |
| + virtual void OnSendComplete(int report_id, bool success) {} |
| + // Called when reporter is created. This can happen when changing |
| + // SafeBrowsing or extended reporting preferences. |
| + virtual void OnCreated() {} |
| + // The service is being reset because SafeBrowsing preferences have changed. |
| + virtual void OnReset() {} |
| }; |
| // Class that handles report uploads and implements the upload retry logic. |
| @@ -73,7 +119,9 @@ class CertificateReportingService : public KeyedService { |
| std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, |
| std::unique_ptr<BoundedReportList> retry_list, |
| base::Clock* clock, |
| - base::TimeDelta report_ttl); |
| + base::TimeDelta report_ttl, |
| + EventObserver* observer, |
| + bool retries_enabled); |
| ~Reporter(); |
| // Sends a report. If the send fails, the report will be added to the retry |
| @@ -88,6 +136,7 @@ class CertificateReportingService : public KeyedService { |
| // Getter and setters for testing: |
| size_t inflight_report_count_for_testing() const; |
| BoundedReportList* GetQueueForTesting() const; |
| + void SetEventObserverForTesting(EventObserver* observer); |
| private: |
| void SendInternal(const Report& report); |
| @@ -97,15 +146,122 @@ class CertificateReportingService : public KeyedService { |
| std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; |
| std::unique_ptr<BoundedReportList> retry_list_; |
| base::Clock* test_clock_; |
| + // Maximum age of a queued report. Reports older than this are discarded in |
| + // the next |SendPending| call. |
| const base::TimeDelta report_ttl_; |
| + // Event observer to observe events generated by the reporter. Can be null. |
| + EventObserver* event_observer_; |
| + // If true, retries are enabled. |
| + const bool retries_enabled_; |
| + // Current report id, starting from zero and monotonically incrementing. |
| int current_report_id_; |
| std::map<int, Report> inflight_reports_; |
| base::WeakPtrFactory<Reporter> weak_factory_; |
| - DISALLOW_IMPLICIT_CONSTRUCTORS(Reporter); |
| + DISALLOW_COPY_AND_ASSIGN(Reporter); |
| }; |
| + |
| + CertificateReportingService( |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| + std::unique_ptr<EventObserver> event_observer, |
| + size_t max_queued_report_count, |
| + base::TimeDelta max_report_age, |
| + base::Clock* test_clock); |
| + |
| + ~CertificateReportingService() override; |
| + |
| + // KeyedService implementation: |
| + void Shutdown() override; |
| + |
| + // Sends a serialized report. If the report upload fails, the upload is |
| + // retried in a future time. |
| + void Send(const std::string& serialized_report); |
| + |
| + // Sends pending reports that are in the retry queue. |
| + void SendPending(); |
| + |
| + // Enables or disables reporting. When disabled, pending report queue is |
| + // cleared and incoming reports are ignored. Reporting is enabled by default |
| + // once the service is initialized. |
| + void SetEnabled(bool enabled); |
| + |
| + // Called when a send attempt is made. Public so that clients can notify this |
| + // service that they abandoned a send attempt. |
| + void DidAttemptSend(bool sent); |
| + |
| + // Getters and setters for testing. |
| + Reporter* get_reporter_for_testing() const; |
| + GURL GetReportingURLForTesting() const; |
| + void SetEventObserverForTesting(std::unique_ptr<EventObserver> observer); |
| + void SetMaxQueuedReportCountForTesting(size_t max_report_count); |
| + void SetClockForTesting(base::Clock* clock); |
| + void SetMaxReportAgeForTesting(base::TimeDelta max_report_age); |
| + |
| + // URL to upload invalid certificate chain reports. An HTTP URL is |
| + // used because a client seeing an invalid cert might not be able to |
| + // make an HTTPS connection to report it. |
| + static const char kExtendedReportingUploadUrlInsecure[]; |
| + |
| + private: |
| + void Reset(const base::Callback<void()>& callback); |
| + |
| + void InitializeOnIOThread( |
| + bool enabled, |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| + size_t max_queued_report_count, |
| + base::TimeDelta max_report_age, |
| + base::Clock* test_clock, |
| + EventObserver* event_observer); |
| + |
| + // Resets the reporter on the IO thread. Changes in SafeBrowsing or extended |
| + // reporting enabled states cause the reporter to be reset. |
| + // If |enabled| is false or |url_request_context_getter| is null, report is |
| + // set to null, effectively cancelling all in flight uploads and clearing the |
| + // pending reports queue. |
| + void ResetOnIOThread(bool enabled, |
| + net::URLRequestContext* url_request_context, |
| + size_t max_queued_report_count, |
| + base::TimeDelta max_report_age, |
| + base::Clock* test_clock, |
| + EventObserver* event_observer); |
| + |
| + // If true, reporting is enabled. When SafeBrowsing preferences change, this |
| + // might be set to false. |
| + bool enabled_; |
| + |
| + // scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| + net::URLRequestContext* url_request_context_; |
| + |
| + std::unique_ptr<Reporter> reporter_; |
| + |
| + // Subscription for url request context shutdowns. When this subscription is |
| + // notified, it means that the SafeBrowsing service is shutting down, and this |
| + // service must also shut down. |
| + std::unique_ptr<base::CallbackList<void(void)>::Subscription> |
| + safe_browsing_service_shutdown_subscription_; |
| + |
| + // Observes events from this service. Default implementation doesn't do |
| + // anything. Tests use this to keep track of sent/failed reports etc. |
| + std::unique_ptr<EventObserver> event_observer_; |
| + |
| + // Maximum number of reports to be queued for retry. |
| + size_t max_queued_report_count_; |
| + |
| + // Maximum age of the reports to be queued for retry, from the time the |
| + // certificate error was first encountered by the user. Any report older than |
| + // this age is ignored and is not re-uploaded. |
| + base::TimeDelta max_report_age_; |
| + |
| + // Test clock. If null, system clock is used. |
| + base::Clock* test_clock_; |
| + |
| + // Whether a send has ever been made. Used to verify that test setters are |
| + // only called after initialization. |
| + bool made_send_attempt_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CertificateReportingService); |
| }; |
| #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ |