Commit 740e4447 authored by Rainhard Findling's avatar Rainhard Findling Committed by Commit Bot

Safety check: move timestamp when safety check ran to backend

* Allows reusing time-based logic for when safety check ran (e.g.
  string construction) to be reused across platforms.

Bug: 1015841
Change-Id: I6db5be39619d510f1a6c95d16767275146477c84
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2144090
Commit-Queue: Rainhard Findling <rainhard@chromium.org>
Reviewed-by: default avatarEsmael Elmoslimany <aee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760007}
parent d59d7979
...@@ -114,10 +114,9 @@ cr.define('settings', function() { ...@@ -114,10 +114,9 @@ cr.define('settings', function() {
/** /**
* Get the display string for the safety check parent, showing how long ago * Get the display string for the safety check parent, showing how long ago
* safety check last ran.` * safety check last ran.`
* @param {number} timestamp The timestamp safety check last ran.
* @return {!Promise<string>} * @return {!Promise<string>}
*/ */
getParentRanDisplayString(timestamp) {} getParentRanDisplayString() {}
} }
/** @implements {settings.SafetyCheckBrowserProxy} */ /** @implements {settings.SafetyCheckBrowserProxy} */
...@@ -128,8 +127,8 @@ cr.define('settings', function() { ...@@ -128,8 +127,8 @@ cr.define('settings', function() {
} }
/** @override */ /** @override */
getParentRanDisplayString(timestamp) { getParentRanDisplayString() {
return cr.sendWithPromise('getSafetyCheckRanDisplayString', timestamp); return cr.sendWithPromise('getSafetyCheckRanDisplayString');
} }
} }
......
...@@ -218,11 +218,9 @@ Polymer({ ...@@ -218,11 +218,9 @@ Polymer({
this.parentDisplayString_ = event.displayString; this.parentDisplayString_ = event.displayString;
if (this.parentStatus_ === settings.SafetyCheckParentStatus.AFTER) { if (this.parentStatus_ === settings.SafetyCheckParentStatus.AFTER) {
// Start periodic safety check parent ran string updates. // Start periodic safety check parent ran string updates.
const timestamp = Date.now();
const update = async () => { const update = async () => {
this.parentDisplayString_ = this.parentDisplayString_ =
await this.safetyCheckBrowserProxy_.getParentRanDisplayString( await this.safetyCheckBrowserProxy_.getParentRanDisplayString();
timestamp);
}; };
clearInterval(this.updateTimerId_); clearInterval(this.updateTimerId_);
this.updateTimerId_ = setInterval(update, 60000); this.updateTimerId_ = setInterval(update, 60000);
......
...@@ -164,12 +164,11 @@ void SafetyCheckHandler::HandlePerformSafetyCheck(const base::ListValue* args) { ...@@ -164,12 +164,11 @@ void SafetyCheckHandler::HandlePerformSafetyCheck(const base::ListValue* args) {
void SafetyCheckHandler::HandleGetParentRanDisplayString( void SafetyCheckHandler::HandleGetParentRanDisplayString(
const base::ListValue* args) { const base::ListValue* args) {
const base::Value* callback_id; const base::Value* callback_id;
double timestampRanDouble;
CHECK(args->Get(0, &callback_id)); CHECK(args->Get(0, &callback_id));
CHECK(args->GetDouble(1, &timestampRanDouble));
ResolveJavascriptCallback( ResolveJavascriptCallback(
*callback_id, base::Value(GetStringForParentRan(timestampRanDouble))); *callback_id,
base::Value(GetStringForParentRan(safety_check_completion_time_)));
} }
void SafetyCheckHandler::CheckUpdates() { void SafetyCheckHandler::CheckUpdates() {
...@@ -492,47 +491,48 @@ base::string16 SafetyCheckHandler::GetStringForExtensions( ...@@ -492,47 +491,48 @@ base::string16 SafetyCheckHandler::GetStringForExtensions(
} }
} }
base::string16 SafetyCheckHandler::GetStringForParentRan(double timestamp_ran) { base::string16 SafetyCheckHandler::GetStringForParentRan(
return SafetyCheckHandler::GetStringForParentRan(timestamp_ran, base::Time safety_check_completion_time) {
return SafetyCheckHandler::GetStringForParentRan(safety_check_completion_time,
base::Time::Now()); base::Time::Now());
} }
base::string16 SafetyCheckHandler::GetStringForParentRan( base::string16 SafetyCheckHandler::GetStringForParentRan(
double timestamp_ran, base::Time safety_check_completion_time,
base::Time system_time) { base::Time system_time) {
const base::Time timeRan = base::Time::FromJsTime(timestamp_ran); base::Time::Exploded completion_time_exploded;
base::Time::Exploded timeRanExploded; safety_check_completion_time.LocalExplode(&completion_time_exploded);
timeRan.LocalExplode(&timeRanExploded);
base::Time::Exploded systemTimeExploded; base::Time::Exploded system_time_exploded;
system_time.LocalExplode(&systemTimeExploded); system_time.LocalExplode(&system_time_exploded);
const base::Time timeYesterday = system_time - base::TimeDelta::FromDays(1); const base::Time time_yesterday = system_time - base::TimeDelta::FromDays(1);
base::Time::Exploded timeYesterdayExploded; base::Time::Exploded time_yesterday_exploded;
timeYesterday.LocalExplode(&timeYesterdayExploded); time_yesterday.LocalExplode(&time_yesterday_exploded);
const auto timeDiff = system_time - timeRan; const auto time_diff = system_time - safety_check_completion_time;
if (timeRanExploded.year == systemTimeExploded.year && if (completion_time_exploded.year == system_time_exploded.year &&
timeRanExploded.month == systemTimeExploded.month && completion_time_exploded.month == system_time_exploded.month &&
timeRanExploded.day_of_month == systemTimeExploded.day_of_month) { completion_time_exploded.day_of_month ==
system_time_exploded.day_of_month) {
// Safety check ran today. // Safety check ran today.
const int timeDiffInMinutes = timeDiff.InMinutes(); const int time_diff_in_mins = time_diff.InMinutes();
if (timeDiffInMinutes == 0) { if (time_diff_in_mins == 0) {
return l10n_util::GetStringUTF16( return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER); IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER);
} else if (timeDiffInMinutes < 60) { } else if (time_diff_in_mins < 60) {
return l10n_util::GetPluralStringFUTF16( return l10n_util::GetPluralStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_MINS, IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_MINS,
timeDiffInMinutes); time_diff_in_mins);
} else { } else {
return l10n_util::GetPluralStringFUTF16( return l10n_util::GetPluralStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_HOURS, IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_HOURS,
timeDiffInMinutes / 60); time_diff_in_mins / 60);
} }
} else if (timeRanExploded.year == timeYesterdayExploded.year && } else if (completion_time_exploded.year == time_yesterday_exploded.year &&
timeRanExploded.month == timeYesterdayExploded.month && completion_time_exploded.month == time_yesterday_exploded.month &&
timeRanExploded.day_of_month == completion_time_exploded.day_of_month ==
timeYesterdayExploded.day_of_month) { time_yesterday_exploded.day_of_month) {
// Safety check ran yesterday. // Safety check ran yesterday.
return l10n_util::GetStringUTF16( return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_YESTERDAY); IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_YESTERDAY);
...@@ -541,10 +541,10 @@ base::string16 SafetyCheckHandler::GetStringForParentRan( ...@@ -541,10 +541,10 @@ base::string16 SafetyCheckHandler::GetStringForParentRan(
// TODO(crbug.com/1015841): While a minor issue, this is not be the ideal // TODO(crbug.com/1015841): While a minor issue, this is not be the ideal
// way to calculate the days passed since safety check ran. For example, // way to calculate the days passed since safety check ran. For example,
// <48 h might still be 2 days ago. // <48 h might still be 2 days ago.
const int timeDiffInDays = timeDiff.InDays(); const int time_diff_in_days = time_diff.InDays();
return l10n_util::GetPluralStringFUTF16( return l10n_util::GetPluralStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_DAYS, IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_DAYS,
timeDiffInDays); time_diff_in_days);
} }
} }
...@@ -651,11 +651,10 @@ void SafetyCheckHandler::CompleteParentIfChildrenCompleted() { ...@@ -651,11 +651,10 @@ void SafetyCheckHandler::CompleteParentIfChildrenCompleted() {
passwords_status_ != PasswordsStatus::kChecking && passwords_status_ != PasswordsStatus::kChecking &&
safe_browsing_status_ != SafeBrowsingStatus::kChecking && safe_browsing_status_ != SafeBrowsingStatus::kChecking &&
extensions_status_ != ExtensionsStatus::kChecking) { extensions_status_ != ExtensionsStatus::kChecking) {
// TODO(crbug.com/1015841): Minor improvement: also store the timestamp when
// safety check completed in the backend instead of in the frontend. This
// allows computing the safety check parent ran string on all platforms
// without the frontend handling the timestamp.
parent_status_ = ParentStatus::kAfter; parent_status_ = ParentStatus::kAfter;
// Remember when safety check completed.
safety_check_completion_time_ = base::Time::Now();
// Update UI.
FireBasicSafetyCheckWebUiListener(kParentEvent, FireBasicSafetyCheckWebUiListener(kParentEvent,
static_cast<int>(parent_status_), static_cast<int>(parent_status_),
GetStringForParent(parent_status_)); GetStringForParent(parent_status_));
......
...@@ -103,9 +103,9 @@ class SafetyCheckHandler ...@@ -103,9 +103,9 @@ class SafetyCheckHandler
// Constructs the 'safety check ran' display string by how long ago safety // Constructs the 'safety check ran' display string by how long ago safety
// check ran. // check ran.
base::string16 GetStringForParentRan(double timestamp_ran); base::string16 GetStringForParentRan(base::Time safety_check_completion_time);
base::string16 GetStringForParentRan(double timestamp_ran, base::string16 GetStringForParentRan(base::Time safety_check_completion_time,
base::Time systemTime); base::Time system_time);
protected: protected:
SafetyCheckHandler(std::unique_ptr<VersionUpdater> version_updater, SafetyCheckHandler(std::unique_ptr<VersionUpdater> version_updater,
...@@ -222,6 +222,9 @@ class SafetyCheckHandler ...@@ -222,6 +222,9 @@ class SafetyCheckHandler
SafeBrowsingStatus safe_browsing_status_ = SafeBrowsingStatus::kChecking; SafeBrowsingStatus safe_browsing_status_ = SafeBrowsingStatus::kChecking;
ExtensionsStatus extensions_status_ = ExtensionsStatus::kChecking; ExtensionsStatus extensions_status_ = ExtensionsStatus::kChecking;
// System time when safety check completed.
base::Time safety_check_completion_time_;
std::unique_ptr<VersionUpdater> version_updater_; std::unique_ptr<VersionUpdater> version_updater_;
password_manager::BulkLeakCheckService* leak_service_ = nullptr; password_manager::BulkLeakCheckService* leak_service_ = nullptr;
extensions::PasswordsPrivateDelegate* passwords_delegate_ = nullptr; extensions::PasswordsPrivateDelegate* passwords_delegate_ = nullptr;
......
...@@ -1034,7 +1034,7 @@ TEST_F(SafetyCheckHandlerTest, CheckParentRanDisplayString) { ...@@ -1034,7 +1034,7 @@ TEST_F(SafetyCheckHandlerTest, CheckParentRanDisplayString) {
// 1 second before midnight Dec 31st 2020, so that -(24h-1s) is still on the // 1 second before midnight Dec 31st 2020, so that -(24h-1s) is still on the
// same day. This test time is hard coded to prevent DST flakiness, see // same day. This test time is hard coded to prevent DST flakiness, see
// crbug.com/1066576. // crbug.com/1066576.
const base::Time systemTime = const base::Time system_time =
base::Time::FromDoubleT(1609459199).LocalMidnight() - base::Time::FromDoubleT(1609459199).LocalMidnight() -
base::TimeDelta::FromSeconds(1); base::TimeDelta::FromSeconds(1);
// Display strings for given time deltas in seconds. // Display strings for given time deltas in seconds.
...@@ -1056,9 +1056,9 @@ TEST_F(SafetyCheckHandlerTest, CheckParentRanDisplayString) { ...@@ -1056,9 +1056,9 @@ TEST_F(SafetyCheckHandlerTest, CheckParentRanDisplayString) {
// Test that above time deltas produce the corresponding display strings. // Test that above time deltas produce the corresponding display strings.
for (auto tuple : tuples) { for (auto tuple : tuples) {
const base::Time time = const base::Time time =
systemTime - base::TimeDelta::FromSeconds(std::get<1>(tuple)); system_time - base::TimeDelta::FromSeconds(std::get<1>(tuple));
const base::string16 displayString = safety_check_->GetStringForParentRan( const base::string16 display_string =
time.ToJsTimeIgnoringNull(), systemTime); safety_check_->GetStringForParentRan(time, system_time);
EXPECT_EQ(base::UTF8ToUTF16(std::get<0>(tuple)), displayString); EXPECT_EQ(base::UTF8ToUTF16(std::get<0>(tuple)), display_string);
} }
} }
...@@ -177,10 +177,7 @@ suite('SafetyCheckUiTests', function() { ...@@ -177,10 +177,7 @@ suite('SafetyCheckUiTests', function() {
assertTrue(page.$$('#safetyCheckCollapse').opened); assertTrue(page.$$('#safetyCheckCollapse').opened);
// Ensure the automatic browser proxy calls are started. // Ensure the automatic browser proxy calls are started.
const timestamp = return safetyCheckBrowserProxy.whenCalled('getParentRanDisplayString');
await safetyCheckBrowserProxy.whenCalled('getParentRanDisplayString');
assertTrue(Number.isInteger(timestamp));
assertTrue(timestamp >= 0);
}); });
test('HappinessTrackingSurveysTest', function() { test('HappinessTrackingSurveysTest', function() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment