| 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 #include "base/bind_helpers.h" | 5 #include "base/bind_helpers.h" |
| 6 #include "base/time/clock.h" | 6 #include "base/time/clock.h" |
| 7 #include "base/time/default_clock.h" | 7 #include "base/time/default_clock.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" | 9 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
| 10 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 11 #include "components/prefs/pref_service.h" |
| 12 #include "components/safe_browsing_db/safe_browsing_prefs.h" |
| 9 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 10 | 14 |
| 11 namespace { | 15 namespace { |
| 16 // Maximum age in seconds until a report is dropped from the retry list. |
| 17 // By default, reports older than a day are ignored and never retried again. |
| 18 static const uint64_t kMaxReportAgeInSeconds = 86400; |
| 19 |
| 20 // Maximum number of reports to be kept in the report retry list. If an incoming |
| 21 // report has a more recent creation date than the oldest report in the list, |
| 22 // the oldest report is removed from the list and the incoming report is added. |
| 23 // Otherwise, the incoming report is ignored. |
| 24 const size_t kMaxReportCountInQueue = 5; |
| 25 |
| 12 // Compare function that orders Reports in reverse chronological order (i.e. | 26 // Compare function that orders Reports in reverse chronological order (i.e. |
| 13 // oldest item is last). | 27 // oldest item is last). |
| 14 bool ReportCompareFunc(const CertificateReportingService::Report& item1, | 28 bool ReportCompareFunc(const CertificateReportingService::Report& item1, |
| 15 const CertificateReportingService::Report& item2) { | 29 const CertificateReportingService::Report& item2) { |
| 16 return item1.creation_time > item2.creation_time; | 30 return item1.creation_time > item2.creation_time; |
| 17 } | 31 } |
| 18 | 32 |
| 33 // Observes SafeBrowsing preferences and notifies CertificateReportingService |
| 34 // when preferences change. There is one instance of this class per |
| 35 // CertificateReportingService and each instance is owned by the |
| 36 // CertificateReportingService it notifies. |
| 37 class SafeBrowsingPreferenceObserver |
| 38 : public CertificateReportingService::PreferenceObserver { |
| 39 public: |
| 40 SafeBrowsingPreferenceObserver( |
| 41 const PrefService& prefs, |
| 42 safe_browsing::SafeBrowsingService* safe_browsing_service, |
| 43 CertificateReportingService* certificate_reporting_service) |
| 44 : safe_browsing_service_(safe_browsing_service), |
| 45 prefs_(prefs), |
| 46 certificate_reporting_service_(certificate_reporting_service), |
| 47 safe_browsing_state_subscription_( |
| 48 safe_browsing_service->RegisterStateCallback( |
| 49 base::Bind(&SafeBrowsingPreferenceObserver::OnPreferenceChanged, |
| 50 base::Unretained(this)))) {} |
| 51 |
| 52 ~SafeBrowsingPreferenceObserver() override {} |
| 53 |
| 54 // CertificateReportingService::PreferenceObserver implementation: |
| 55 void OnPreferenceChanged() override { |
| 56 const bool enabled = safe_browsing_service_ && |
| 57 safe_browsing_service_->enabled_by_prefs() && |
| 58 safe_browsing::IsExtendedReportingEnabled(prefs_); |
| 59 certificate_reporting_service_->SetEnabled(enabled); |
| 60 } |
| 61 |
| 62 private: |
| 63 const safe_browsing::SafeBrowsingService* safe_browsing_service_; |
| 64 const PrefService& prefs_; |
| 65 CertificateReportingService* certificate_reporting_service_; |
| 66 std::unique_ptr<safe_browsing::SafeBrowsingService::StateSubscription> |
| 67 safe_browsing_state_subscription_; |
| 68 }; |
| 69 |
| 19 } // namespace | 70 } // namespace |
| 20 | 71 |
| 21 // static | 72 // static |
| 22 const char CertificateReportingService::kExtendedReportingUploadUrlInsecure[] = | 73 const char CertificateReportingService::kExtendedReportingUploadUrlInsecure[] = |
| 23 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/"; | 74 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/"; |
| 24 | 75 |
| 25 CertificateReportingService::BoundedReportList::BoundedReportList( | 76 CertificateReportingService::BoundedReportList::BoundedReportList( |
| 26 size_t max_size) | 77 size_t max_size) |
| 27 : max_size_(max_size) { | 78 : max_size_(max_size) { |
| 28 CHECK(max_size <= 20) | 79 CHECK(max_size <= 20) |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 200 |
| 150 void CertificateReportingService::Reporter::SuccessCallback(int report_id) { | 201 void CertificateReportingService::Reporter::SuccessCallback(int report_id) { |
| 151 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 202 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 152 CHECK_GT(inflight_reports_.erase(report_id), 0u); | 203 CHECK_GT(inflight_reports_.erase(report_id), 0u); |
| 153 content::BrowserThread::PostTask( | 204 content::BrowserThread::PostTask( |
| 154 content::BrowserThread::UI, FROM_HERE, | 205 content::BrowserThread::UI, FROM_HERE, |
| 155 base::Bind(&CertificateReportingService::EventObserver::OnSendComplete, | 206 base::Bind(&CertificateReportingService::EventObserver::OnSendComplete, |
| 156 base::Unretained(event_observer_), report_id, true)); | 207 base::Unretained(event_observer_), report_id, true)); |
| 157 } | 208 } |
| 158 | 209 |
| 210 // static |
| 211 CertificateReportingService* CertificateReportingService::Create( |
| 212 safe_browsing::SafeBrowsingService* safe_browsing_service, |
| 213 Profile* profile) { |
| 214 return new CertificateReportingService( |
| 215 safe_browsing_service, safe_browsing_service->url_request_context(), |
| 216 profile, std::unique_ptr<EventObserver>(new EventObserver()), |
| 217 kMaxReportCountInQueue, |
| 218 base::TimeDelta::FromSeconds(kMaxReportAgeInSeconds), nullptr); |
| 219 } |
| 220 |
| 159 CertificateReportingService::CertificateReportingService( | 221 CertificateReportingService::CertificateReportingService( |
| 222 safe_browsing::SafeBrowsingService* safe_browsing_service, |
| 160 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 223 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 224 Profile* profile, |
| 161 std::unique_ptr<EventObserver> event_observer, | 225 std::unique_ptr<EventObserver> event_observer, |
| 162 size_t max_queued_report_count, | 226 size_t max_queued_report_count, |
| 163 base::TimeDelta max_report_age, | 227 base::TimeDelta max_report_age, |
| 164 base::Clock* test_clock) | 228 base::Clock* test_clock) |
| 165 : enabled_(true), | 229 : enabled_(true), |
| 166 url_request_context_(nullptr), | 230 url_request_context_(nullptr), |
| 167 event_observer_(std::move(event_observer)), | 231 event_observer_(std::move(event_observer)), |
| 168 max_queued_report_count_(max_queued_report_count), | 232 max_queued_report_count_(max_queued_report_count), |
| 169 max_report_age_(max_report_age), | 233 max_report_age_(max_report_age), |
| 170 test_clock_(test_clock), | 234 test_clock_(test_clock), |
| 171 made_send_attempt_(false) { | 235 made_send_attempt_(false) { |
| 172 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 236 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 237 // Observe changes in SafeBrowsing preferences. |
| 238 preference_observer_.reset(new SafeBrowsingPreferenceObserver( |
| 239 *profile->GetPrefs(), safe_browsing_service, this)); |
| 240 // Subscribe to SafeBrowsing shutdown notifications. |
| 241 safe_browsing_service_shutdown_subscription_ = |
| 242 safe_browsing_service->RegisterShutdownCallback(base::Bind( |
| 243 &CertificateReportingService::Shutdown, base::Unretained(this))); |
| 244 |
| 173 content::BrowserThread::PostTaskAndReply( | 245 content::BrowserThread::PostTaskAndReply( |
| 174 content::BrowserThread::IO, FROM_HERE, | 246 content::BrowserThread::IO, FROM_HERE, |
| 175 base::Bind(&CertificateReportingService::InitializeOnIOThread, | 247 base::Bind(&CertificateReportingService::InitializeOnIOThread, |
| 176 base::Unretained(this), enabled_, url_request_context_getter, | 248 base::Unretained(this), enabled_, url_request_context_getter, |
| 177 max_queued_report_count_, max_report_age_, test_clock_, | 249 max_queued_report_count_, max_report_age_, test_clock_, |
| 178 nullptr /* error_reporter */), | 250 nullptr /* error_reporter */), |
| 179 base::Bind(&EventObserver::OnReset, | 251 base::Bind(&EventObserver::OnReset, |
| 180 base::Unretained(event_observer_.get()))); | 252 base::Unretained(event_observer_.get()))); |
| 181 } | 253 } |
| 182 | 254 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 } | 382 } |
| 311 reporter_.reset(new Reporter( | 383 reporter_.reset(new Reporter( |
| 312 std::unique_ptr<certificate_reporting::ErrorReporter>( | 384 std::unique_ptr<certificate_reporting::ErrorReporter>( |
| 313 new certificate_reporting::ErrorReporter( | 385 new certificate_reporting::ErrorReporter( |
| 314 url_request_context, GURL(kExtendedReportingUploadUrlInsecure), | 386 url_request_context, GURL(kExtendedReportingUploadUrlInsecure), |
| 315 net::ReportSender::DO_NOT_SEND_COOKIES)), | 387 net::ReportSender::DO_NOT_SEND_COOKIES)), |
| 316 std::unique_ptr<BoundedReportList>( | 388 std::unique_ptr<BoundedReportList>( |
| 317 new BoundedReportList(max_queued_report_count)), | 389 new BoundedReportList(max_queued_report_count)), |
| 318 test_clock, max_report_age, event_observer, true /* retries_enabled */)); | 390 test_clock, max_report_age, event_observer, true /* retries_enabled */)); |
| 319 } | 391 } |
| OLD | NEW |