From ead816a6959a31e726d10895fe9e81908512216e Mon Sep 17 00:00:00 2001 From: Daniel Rubery Date: Fri, 18 Mar 2022 18:34:19 +0000 Subject: [PATCH] [M100] Fix timer delay for tailored security integration The current implementation has a small defect where a call to RemoveQueryRequest followed by AddQueryRequest will result in an immediate query, rather than waiting up to 5 minutes. This is especially bad when the user is frequently navigating. (cherry picked from commit c898e6dd53bb7988e4beb929008ca7dd8278e5de) Bug: 1305301 Change-Id: I0bddcb7b411d88f791c783932f90de0113189f11 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3516949 Reviewed-by: Rohit Bhatia Commit-Queue: Daniel Rubery Cr-Original-Commit-Position: refs/heads/main@{#980021} Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3537188 Auto-Submit: Daniel Rubery Commit-Queue: Rubber Stamper Bot-Commit: Rubber Stamper Cr-Commit-Position: refs/branch-heads/4896@{#680} Cr-Branched-From: 1f63ff4bc27570761b35ffbc7f938f6586f7bee8-refs/heads/main@{#972766} --- .../tailored_security_service.cc | 30 ++++++++++++++----- .../tailored_security_service.h | 2 +- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/components/safe_browsing/core/browser/tailored_security_service/tailored_security_service.cc b/components/safe_browsing/core/browser/tailored_security_service/tailored_security_service.cc index e57ad0770519c..8fee6ea9143fe 100644 --- a/components/safe_browsing/core/browser/tailored_security_service/tailored_security_service.cc +++ b/components/safe_browsing/core/browser/tailored_security_service/tailored_security_service.cc @@ -280,13 +280,23 @@ void TailoredSecurityService::AddQueryRequest() { DCHECK(!is_shut_down_); active_query_request_++; if (active_query_request_ == 1) { - // Query now and register a repeating timer to get the tailored security bit - // every `kRepeatingCheckTailoredSecurityBitDelayInMinutes` minutes. - QueryTailoredSecurityBit(); - timer_.Start( - FROM_HERE, - base::Minutes(kRepeatingCheckTailoredSecurityBitDelayInMinutes), this, - &TailoredSecurityService::QueryTailoredSecurityBit); + if (base::Time::Now() - last_updated_ <= + base::Minutes(kRepeatingCheckTailoredSecurityBitDelayInMinutes)) { + // Since we queried recently, start the timer with a shorter delay. + base::TimeDelta delay = + base::Minutes(kRepeatingCheckTailoredSecurityBitDelayInMinutes) - + (base::Time::Now() - last_updated_); + timer_.Start(FROM_HERE, delay, this, + &TailoredSecurityService::QueryTailoredSecurityBit); + } else { + // Query now and register a timer to get the tailored security bit + // every `kRepeatingCheckTailoredSecurityBitDelayInMinutes` minutes. + QueryTailoredSecurityBit(); + timer_.Start( + FROM_HERE, + base::Minutes(kRepeatingCheckTailoredSecurityBitDelayInMinutes), this, + &TailoredSecurityService::QueryTailoredSecurityBit); + } } } @@ -362,6 +372,12 @@ void TailoredSecurityService::OnTailoredSecurityBitRetrieved( } is_tailored_security_enabled_ = is_enabled; last_updated_ = base::Time::Now(); + if (active_query_request_ > 0) { + timer_.Start( + FROM_HERE, + base::Minutes(kRepeatingCheckTailoredSecurityBitDelayInMinutes), this, + &TailoredSecurityService::QueryTailoredSecurityBit); + } } void TailoredSecurityService::QueryTailoredSecurityBitCompletionCallback( diff --git a/components/safe_browsing/core/browser/tailored_security_service/tailored_security_service.h b/components/safe_browsing/core/browser/tailored_security_service/tailored_security_service.h index 7f5b17b7e75c7..1ac110db37adb 100644 --- a/components/safe_browsing/core/browser/tailored_security_service/tailored_security_service.h +++ b/components/safe_browsing/core/browser/tailored_security_service/tailored_security_service.h @@ -174,7 +174,7 @@ class TailoredSecurityService : public KeyedService { size_t active_query_request_ = 0; // Timer to periodically check tailored security bit. - base::RepeatingTimer timer_; + base::OneShotTimer timer_; bool is_tailored_security_enabled_ = false; base::Time last_updated_;