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 #include "base/time/clock.h" | |
| 6 #include "base/time/default_clock.h" | |
| 7 #include "chrome/browser/ssl/certificate_reporting_service.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 | |
| 10 namespace { | |
| 11 // First item is ordered before the second item if it's newer. | |
| 12 bool ReportCompareFunc(const CertificateReportingService::Report& item1, | |
| 13 const CertificateReportingService::Report& item2) { | |
| 14 return item1.creation_time > item2.creation_time; | |
| 15 } | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 void CertificateReportingService::BoundedReportList::Add(const Report& item) { | |
| 20 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 21 DCHECK(items_.size() <= max_size_); | |
| 22 if (items_.size() == max_size_) { | |
| 23 const Report& last = items_.back(); | |
| 24 if (item.creation_time <= last.creation_time) { | |
| 25 // Report older than the oldest item in the queue, ignore. | |
|
estark
2016/11/19 00:29:52
The comments in this function are helpful 👍
| |
| 26 return; | |
| 27 } | |
| 28 // Reached the maximum item count, remove the oldest item. | |
| 29 items_.pop_back(); | |
| 30 } | |
| 31 items_.push_back(item); | |
| 32 std::sort(items_.begin(), items_.end(), ReportCompareFunc); | |
| 33 } | |
| 34 | |
| 35 CertificateReportingService::BoundedReportList::BoundedReportList( | |
| 36 size_t max_size) | |
| 37 : max_size_(max_size) { | |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 39 } | |
| 40 | |
| 41 void CertificateReportingService::BoundedReportList::Clear() { | |
| 42 items_.clear(); | |
| 43 } | |
| 44 | |
| 45 CertificateReportingService::BoundedReportList::~BoundedReportList() {} | |
| 46 | |
| 47 const std::vector<CertificateReportingService::Report>& | |
| 48 CertificateReportingService::BoundedReportList::items() const { | |
| 49 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 50 return items_; | |
| 51 } | |
| 52 | |
| 53 CertificateReportingService::Reporter::Reporter( | |
| 54 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter, | |
| 55 std::unique_ptr<BoundedReportList> retry_list, | |
| 56 base::Clock* clock, | |
| 57 base::TimeDelta max_item_age) | |
| 58 : error_reporter_(std::move(error_reporter)), | |
| 59 retry_list_(std::move(retry_list)), | |
| 60 test_clock_(clock), | |
| 61 max_item_age_(max_item_age), | |
| 62 current_report_id_(0), | |
| 63 weak_factory_(this) {} | |
| 64 | |
| 65 void CertificateReportingService::Reporter::Send( | |
| 66 const std::string& serialized_report) { | |
| 67 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 68 base::Time now = | |
| 69 test_clock_ ? test_clock_->Now() : base::Time::NowFromSystemTime(); | |
| 70 SendInternal(Report(current_report_id_++, now, serialized_report)); | |
| 71 } | |
| 72 | |
| 73 void CertificateReportingService::Reporter::SendPending() { | |
| 74 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 75 base::Time now = | |
| 76 test_clock_ ? test_clock_->Now() : base::Time::NowFromSystemTime(); | |
| 77 // Copy pending reports and clear the retry list. | |
| 78 std::vector<Report> items = retry_list_->items(); | |
| 79 retry_list_->Clear(); | |
| 80 for (const Report& report : items) { | |
| 81 if (report.creation_time < now - max_item_age_) { | |
| 82 // Report too old, ignore. | |
| 83 continue; | |
| 84 } | |
| 85 SendInternal(report); | |
| 86 } | |
| 87 retry_list_->Clear(); | |
| 88 } | |
| 89 | |
| 90 size_t | |
| 91 CertificateReportingService::Reporter::inflight_report_count_for_testing() | |
| 92 const { | |
| 93 return inflight_reports_.size(); | |
| 94 } | |
| 95 | |
| 96 CertificateReportingService::BoundedReportList* | |
| 97 CertificateReportingService::Reporter::GetQueueForTesting() const { | |
| 98 return retry_list_.get(); | |
| 99 } | |
| 100 | |
| 101 CertificateReportingService::Reporter::~Reporter() {} | |
| 102 | |
| 103 void CertificateReportingService::Reporter::SendInternal( | |
| 104 const CertificateReportingService::Report& report) { | |
| 105 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 106 inflight_reports_.insert(std::make_pair(report.report_id, report)); | |
| 107 error_reporter_->SendExtendedReportingReport( | |
| 108 report.serialized_report, | |
| 109 base::Bind(&CertificateReportingService::Reporter::SuccessCallback, | |
| 110 weak_factory_.GetWeakPtr(), report.report_id), | |
| 111 base::Bind(&CertificateReportingService::Reporter::ErrorCallback, | |
| 112 weak_factory_.GetWeakPtr(), report.report_id)); | |
| 113 } | |
| 114 | |
| 115 void CertificateReportingService::Reporter::ErrorCallback(int report_id, | |
| 116 const GURL& url, | |
| 117 int error) { | |
| 118 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 119 auto it = inflight_reports_.find(report_id); | |
| 120 DCHECK(it != inflight_reports_.end()); | |
| 121 retry_list_->Add(it->second); | |
| 122 CHECK_GT(inflight_reports_.erase(report_id), 0u); | |
| 123 } | |
| 124 | |
| 125 void CertificateReportingService::Reporter::SuccessCallback(int report_id) { | |
| 126 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 127 CHECK_GT(inflight_reports_.erase(report_id), 0u); | |
| 128 } | |
| OLD | NEW |