Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ | |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <memory> | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/threading/thread_checker.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "components/certificate_reporting/error_reporter.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class Clock; | |
| 21 } | |
| 22 | |
| 23 // This service initiates uploads of invalid certificate reports and retries any | |
| 24 // failed uploads. | |
| 25 class CertificateReportingService { | |
| 26 public: | |
| 27 // Represent a report to be sent. | |
| 28 struct Report { | |
| 29 int report_id; | |
| 30 base::Time creation_time; | |
| 31 std::string serialized_report; | |
| 32 Report(int report_id, | |
| 33 base::Time creation_time, | |
| 34 const std::string& serialized_report) | |
| 35 : report_id(report_id), | |
| 36 creation_time(creation_time), | |
| 37 serialized_report(serialized_report) {} | |
| 38 }; | |
| 39 | |
| 40 // This class contains a number of reports, sorted by the first time the | |
| 41 // report was to be sent. Oldest reports are at the end of the list. The | |
| 42 // number of reports are bounded by |max_size|. The implementation sorts all | |
| 43 // items in the list whenever a new item is added. This should be fine for | |
| 44 // small values of |max_size| (e.g. fewer than 100 items). In case this is not | |
| 45 // sufficient in the future, an array based implementation should be | |
| 46 // considered where the array is maintained as a heap. | |
| 47 class BoundedReportList { | |
| 48 public: | |
| 49 explicit BoundedReportList(size_t max_size); | |
| 50 ~BoundedReportList(); | |
| 51 | |
| 52 void Add(const Report& report); | |
| 53 void Clear(); | |
| 54 | |
| 55 const std::vector<Report>& items() const; | |
| 56 | |
| 57 private: | |
| 58 // Maximum number of reports in the list. If the number of reports in the | |
| 59 // list is smaller than this number, a new item is immediately added to the | |
| 60 // list. Otherwise, the item is compared to the items in the list and only | |
| 61 // added when it's newer than the oldest item in the list. | |
| 62 const size_t max_size_; | |
| 63 | |
| 64 std::vector<Report> items_; | |
| 65 base::ThreadChecker thread_checker_; | |
| 66 }; | |
| 67 | |
| 68 // Class that handles report uploads and implements the upload retry logic. | |
| 69 class Reporter { | |
| 70 public: | |
| 71 Reporter( | |
| 72 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_, | |
| 73 std::unique_ptr<BoundedReportList> retry_list, | |
| 74 base::Clock* clock, | |
| 75 base::TimeDelta max_item_age); | |
| 76 ~Reporter(); | |
| 77 | |
| 78 // Sends a report. If the send fails, the report will be added to the retry | |
| 79 // list. | |
| 80 void Send(const std::string& serialized_report); | |
| 81 | |
| 82 // Sends all pending reports. Skips reports older than the |max_item_age| | |
| 83 // provided in the constructor. Failed reports will be added to the retry | |
| 84 // list. | |
| 85 void SendPending(); | |
| 86 | |
| 87 // Getter and setters for testing: | |
| 88 size_t inflight_report_count_for_testing() const; | |
| 89 BoundedReportList* GetQueueForTesting() const; | |
| 90 | |
| 91 private: | |
| 92 void SendInternal(const Report& report); | |
| 93 void ErrorCallback(int report_id, const GURL& url, int error); | |
| 94 void SuccessCallback(int report_id); | |
| 95 | |
| 96 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_; | |
| 97 std::unique_ptr<BoundedReportList> retry_list_; | |
| 98 base::Clock* test_clock_; | |
| 99 const base::TimeDelta max_item_age_; | |
|
Jialiu Lin
2016/11/23 23:06:14
maybe call it report_ttl_?
meacer
2016/11/23 23:59:03
Done.
| |
| 100 int current_report_id_; | |
| 101 | |
| 102 std::map<int, Report> inflight_reports_; | |
| 103 | |
| 104 base::WeakPtrFactory<Reporter> weak_factory_; | |
| 105 | |
| 106 DISALLOW_IMPLICIT_CONSTRUCTORS(Reporter); | |
| 107 }; | |
| 108 }; | |
| 109 | |
| 110 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_H_ | |
| OLD | NEW |