Commit 5157b3e6 authored by Rainhard Findling's avatar Rainhard Findling Committed by Commit Bot

Safety check UI: build display string showing how long ago safety check ran

Bug: 1015841
Change-Id: I43f83a1264087e8a986c3685e0816e519429340c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2102534Reviewed-by: default avatarEsmael Elmoslimany <aee@chromium.org>
Commit-Queue: Rainhard Findling <rainhard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#750887}
parent a19d7fc6
......@@ -1621,7 +1621,7 @@
other {Safety check ran # minutes ago}}
</message>
<message name="IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_HOURS" desc="A message shown on the top of the safety check feature, explaining to the user that it ran hours ago.">
{MINS, plural,
{HOURS, plural,
=1 {Safety check ran 1 hour ago}
other {Safety check ran # hours ago}}
</message>
......@@ -1635,8 +1635,8 @@
Safety check ran yesterday
</message>
<message name="IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_DAYS" desc="A message shown on the top of the safety check feature, showing to the user how many days ago it ran.">
{NUM_DAYS, plural,
= 1 {Safety check ran 1 day ago}
{DAYS, plural,
=1 {Safety check ran 1 day ago}
other {Safety check ran # days ago}}
</message>
<message name="IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_DATE" desc="A message shown on the top of the safety check feature, showing to the user on which date it ran.">
......
......@@ -134,13 +134,12 @@ void SafetyCheckHandler::HandlePerformSafetyCheck(const base::ListValue* args) {
void SafetyCheckHandler::HandleGetParentRanDisplayString(
const base::ListValue* args) {
const base::Value* callback_id;
double timestampRanDouble;
CHECK(args->Get(0, &callback_id));
CHECK(args->GetDouble(1, &timestampRanDouble));
// TODO(crbug.com/1015841): Construct string stating when safety check ran
// and send it back to JS.
ResolveJavascriptCallback(
*callback_id, base::Value(l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER)));
*callback_id, base::Value(GetStringForParentRan(timestampRanDouble)));
}
void SafetyCheckHandler::CheckUpdates() {
......@@ -407,6 +406,62 @@ base::string16 SafetyCheckHandler::GetStringForExtensions(
}
}
base::string16 SafetyCheckHandler::GetStringForParentRan(double timestamp_ran) {
return SafetyCheckHandler::GetStringForParentRan(timestamp_ran,
base::Time::Now());
}
base::string16 SafetyCheckHandler::GetStringForParentRan(
double timestamp_ran,
base::Time system_time) {
const base::Time timeRan = base::Time::FromJsTime(timestamp_ran);
base::Time::Exploded timeRanExploded;
timeRan.LocalExplode(&timeRanExploded);
base::Time::Exploded systemTimeExploded;
system_time.LocalExplode(&systemTimeExploded);
const base::Time timeYesterday = system_time - base::TimeDelta::FromDays(1);
base::Time::Exploded timeYesterdayExploded;
timeYesterday.LocalExplode(&timeYesterdayExploded);
const auto timeDiff = system_time - timeRan;
if (timeRanExploded.year == systemTimeExploded.year &&
timeRanExploded.month == systemTimeExploded.month &&
timeRanExploded.day_of_month == systemTimeExploded.day_of_month) {
// Safety check ran today.
const int timeDiffInMinutes = timeDiff.InMinutes();
if (timeDiffInMinutes == 0) {
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER);
} else if (timeDiffInMinutes < 60) {
return l10n_util::GetPluralStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_MINS,
timeDiffInMinutes);
} else {
return l10n_util::GetPluralStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_HOURS,
timeDiffInMinutes / 60);
}
} else if (timeRanExploded.year == timeYesterdayExploded.year &&
timeRanExploded.month == timeYesterdayExploded.month &&
timeRanExploded.day_of_month ==
timeYesterdayExploded.day_of_month) {
// Safety check ran yesterday.
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_YESTERDAY);
} else {
// Safety check ran longer ago than yesterday.
// 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,
// <48 h might still be 2 days ago.
const int timeDiffInDays = timeDiff.InDays();
return l10n_util::GetPluralStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_PARENT_PRIMARY_LABEL_AFTER_DAYS,
timeDiffInDays);
}
}
void SafetyCheckHandler::OnStateChanged(
password_manager::BulkLeakCheckService::State state) {
using password_manager::BulkLeakCheckService;
......
......@@ -79,6 +79,12 @@ class SafetyCheckHandler
// should only be called as a result of an explicit user action.
void PerformSafetyCheck();
// Constructs the 'safety check ran' display string by how long ago safety
// check ran.
base::string16 GetStringForParentRan(double timestamp_ran);
base::string16 GetStringForParentRan(double timestamp_ran,
base::Time systemTime);
protected:
SafetyCheckHandler(std::unique_ptr<VersionUpdater> version_updater,
password_manager::BulkLeakCheckService* leak_service,
......
......@@ -759,3 +759,33 @@ TEST_F(SafetyCheckHandlerTest, CheckExtensions_Error) {
VerifyDisplayString(event,
"Browser can't check your extensions. Try again later.");
}
TEST_F(SafetyCheckHandlerTest, CheckParentRanDisplayString) {
// 1 second before midnight, so that -(24h-1s) is still on the same day.
const base::Time systemTime =
base::Time::Now().LocalMidnight() - base::TimeDelta::FromSeconds(1);
// Display strings for given time deltas in seconds.
std::vector<std::tuple<std::string, int>> tuples{
std::make_tuple("Safety check ran a moment ago", 1),
std::make_tuple("Safety check ran a moment ago", 59),
std::make_tuple("Safety check ran 1 minute ago", 60),
std::make_tuple("Safety check ran 2 minutes ago", 60 * 2),
std::make_tuple("Safety check ran 59 minutes ago", 60 * 60 - 1),
std::make_tuple("Safety check ran 1 hour ago", 60 * 60),
std::make_tuple("Safety check ran 2 hours ago", 60 * 60 * 2),
std::make_tuple("Safety check ran 23 hours ago", 60 * 60 * 23),
std::make_tuple("Safety check ran yesterday", 60 * 60 * 24),
std::make_tuple("Safety check ran yesterday", 60 * 60 * 24 * 2 - 1),
std::make_tuple("Safety check ran 2 days ago", 60 * 60 * 24 * 2),
std::make_tuple("Safety check ran 2 days ago", 60 * 60 * 24 * 3 - 1),
std::make_tuple("Safety check ran 3 days ago", 60 * 60 * 24 * 3),
std::make_tuple("Safety check ran 3 days ago", 60 * 60 * 24 * 4 - 1)};
// Test that above time deltas produce the corresponding display strings.
for (auto tuple : tuples) {
const base::Time time =
systemTime - base::TimeDelta::FromSeconds(std::get<1>(tuple));
const base::string16 displayString = safety_check_->GetStringForParentRan(
time.ToJsTimeIgnoringNull(), systemTime);
EXPECT_EQ(base::UTF8ToUTF16(std::get<0>(tuple)), displayString);
}
}
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