Chromium Code Reviews| Index: chrome/browser/ssl/certificate_reporting_service.h |
| diff --git a/chrome/browser/ssl/certificate_reporting_service.h b/chrome/browser/ssl/certificate_reporting_service.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..35f564cb78be424c07edd60d05ad9abf473d1b39 |
| --- /dev/null |
| +++ b/chrome/browser/ssl/certificate_reporting_service.h |
| @@ -0,0 +1,135 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_SSL_CERTIFICATE_REPORTING_SERVICE_H_ |
| +#define CHROME_BROWSER_SSL_CERTIFICATE_REPORTING_SERVICE_H_ |
| + |
| +#include <map> |
| +#include <memory> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.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" |
| + |
| +namespace base { |
| +class Clock; |
| +} |
| + |
| +// This service initiates uploads invalid certificate reports and retries any |
|
estark
2016/11/17 14:51:59
extra word in this sentence?
meacer
2016/11/17 20:09:08
Missing, in fact :)
|
| +// failed uploads. |
| +class CertificateReportingService : public KeyedService { |
| + public: |
| + // Represent a report to be sent. |
| + struct Report { |
| + int report_id; |
| + base::Time creation_time; |
| + std::string serialized_report; |
| + Report() {} |
|
estark
2016/11/17 14:51:59
do you need this constructor?
meacer
2016/11/17 20:09:08
[] operator of std::map needs it, but I modified t
|
| + Report(int report_id, |
| + base::Time creation_time, |
| + const std::string& serialized_report) |
| + : report_id(report_id), |
| + creation_time(creation_time), |
| + serialized_report(serialized_report) {} |
| + }; |
| + |
| + // This class contains a number of reports, sorted by the first time the |
| + // report was to be sent. Oldest reports are at the end of the list. The |
| + // number of reports are bounded by |max_size|. The implementation sorts all |
| + // items in the list whenever a new item is added. This should be fine for |
| + // small values of |max_size| (e.g. fewer than 100 items). In case this is not |
| + // sufficient in the future, an array based implementation should be |
| + // considered where the array is maintained as a heap. |
| + class BoundedReportList { |
| + public: |
| + explicit BoundedReportList(size_t max_size); |
| + ~BoundedReportList(); |
| + |
| + void Add(const Report& report); |
| + void Clear(); |
| + |
| + const std::vector<Report>& items() const; |
| + |
| + private: |
| + // Maximum number of reports in the list. A newly added item is compared |
| + // to the items in the list and only added when it's newer than the |
| + // oldest item in the list. |
|
estark
2016/11/17 14:51:59
This sentence "A newly added item is compared..."
meacer
2016/11/17 20:09:08
Correct, clarified the comment a bit.
|
| + const size_t max_size_; |
| + |
| + std::vector<Report> items_; |
| + base::ThreadChecker thread_checker_; |
| + }; |
| + |
| + // A class to observe events by the service. Used for testing. |
|
estark
2016/11/17 14:51:59
True confessions, I haven't read the tests yet...
meacer
2016/11/17 20:09:08
Looks like this isn't actually used yet, so I remo
|
| + class EventObserver { |
| + public: |
| + EventObserver() {} |
| + virtual ~EventObserver() {} |
| + |
| + // Called when sending of a report is attempted. If attempt was cancelled, |
| + // |sent| is false. Otherwise, it's true. |
|
estark
2016/11/17 14:51:59
sent => completed
meacer
2016/11/17 20:09:08
Done.
|
| + 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(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() {} |
| + // The service is being reset because SafeBrowsing preferences have changed. |
| + virtual void OnInitialized() {} |
| + }; |
| + |
| + // Class that handles report uploads and implements the upload retry logic. |
| + class Reporter : public base::RefCountedThreadSafe<Reporter> { |
|
estark
2016/11/17 14:51:59
ALERT ALERT REFCOUNTED ALERT
Glanced at the .cc f
meacer
2016/11/17 20:09:08
Yeah, this was no longer necessary and I removed i
|
| + public: |
| + Reporter( |
| + std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, |
| + std::unique_ptr<BoundedReportList> retry_list, |
| + base::Clock* clock, |
| + base::TimeDelta max_item_age, |
| + EventObserver* observer); |
| + |
| + // Sends a report. If the send fails, the report will be added to the retry |
| + // list. |
| + void Send(const std::string& serialized_report); |
| + |
| + // Sends all pending reports. Skips reports older than max_item_age_. Failed |
|
estark
2016/11/17 14:51:59
Tiny nit: referring to a private member on a publi
|
| + // reports will be added to the retry list. |
| + void SendPending(); |
|
estark
2016/11/17 14:51:59
No code change necessary, but for my own understan
meacer
2016/11/17 20:09:08
It will be called periodically by the Metrics serv
|
| + |
| + // Getter and setters for testing: |
| + size_t inflight_report_count_for_testing() const; |
| + BoundedReportList* GetQueueForTesting() const; |
| + void SetEventObserverForTesting(EventObserver* observer); |
| + |
| + private: |
| + ~Reporter(); |
| + friend class base::RefCountedThreadSafe<Reporter>; |
| + |
| + void SendInternal(const Report& report); |
| + void ErrorCallback(int report_id, const GURL& url, int error); |
| + void SuccessCallback(int report_id); |
| + void OnSendComplete(bool success); |
| + |
| + std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; |
| + std::unique_ptr<BoundedReportList> retry_list_; |
| + base::Clock* test_clock_; |
| + const base::TimeDelta max_item_age_; |
| + EventObserver* event_observer_; |
| + int current_report_id_; |
| + |
| + std::map<int, Report> inflight_reports_; |
| + |
| + DISALLOW_IMPLICIT_CONSTRUCTORS(Reporter); |
| + }; |
| +}; |
| + |
| +#endif // CHROME_BROWSER_SSL_CERTIFICATE_REPORTING_SERVICE_H_ |