Commit 9ae0cbe7 authored by Henrique Grandinetti's avatar Henrique Grandinetti Committed by Commit Bot

Make time window limit cancel override

Bug: 907227
Change-Id: Ibf6b14fb8198b5c5878436e4e80e0e48622e458a
Reviewed-on: https://chromium-review.googlesource.com/c/1345129Reviewed-by: default avatarMichael Giuffrida <michaelpg@chromium.org>
Commit-Queue: Henrique Grandinetti <hgrandinetti@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610478}
parent 3cc62d65
...@@ -505,20 +505,47 @@ UsageTimeLimitProcessor::GetActiveTimeUsageLimit() { ...@@ -505,20 +505,47 @@ UsageTimeLimitProcessor::GetActiveTimeUsageLimit() {
} }
bool UsageTimeLimitProcessor::HasActiveOverride() { bool UsageTimeLimitProcessor::HasActiveOverride() {
if (!time_limit_override_ || active_time_window_limit_ ||
active_time_usage_limit_) {
return false;
}
if (overridden_window_limit_ || overridden_usage_limit_)
return true;
base::Time last_reset_time = ConvertPolicyTime(LockOverrideResetTime(), 0); base::Time last_reset_time = ConvertPolicyTime(LockOverrideResetTime(), 0);
if (current_time_ < last_reset_time) if (current_time_ < last_reset_time)
last_reset_time -= base::TimeDelta::FromDays(1); last_reset_time -= base::TimeDelta::FromDays(1);
bool has_lock_override = bool override_cancelled_by_window_limit = false;
time_limit_override_ && time_limit_override_->action == if (time_window_limit_) {
internal::TimeLimitOverride::Action::kLock; // Check if yestardays or todays window limit ended after override was
// created.
for (int i = -1; i <= 0; i++) {
internal::Weekday weekday = WeekdayShift(current_weekday_, i);
base::Optional<TimeWindowLimitEntry> window_limit =
time_window_limit_->entries[weekday];
if (window_limit) {
base::Time window_limit_start =
ConvertPolicyTime(window_limit->starts_at, i);
base::Time window_limit_end =
window_limit_start + GetConsecutiveTimeWindowLimitDuration(weekday);
if (current_time_ >= window_limit_end &&
window_limit_end > time_limit_override_->created_at) {
override_cancelled_by_window_limit = true;
break;
}
}
}
}
bool has_lock_override = time_limit_override_->action ==
internal::TimeLimitOverride::Action::kLock;
bool has_valid_lock_override = bool has_valid_lock_override =
has_lock_override && time_limit_override_->created_at > last_reset_time; has_lock_override && time_limit_override_->created_at > last_reset_time &&
if (!time_limit_override_ || (has_lock_override && !has_valid_lock_override)) !override_cancelled_by_window_limit;
return false;
return overridden_window_limit_ || overridden_usage_limit_ || return has_valid_lock_override;
has_valid_lock_override;
} }
bool UsageTimeLimitProcessor::IsLocked() { bool UsageTimeLimitProcessor::IsLocked() {
...@@ -613,30 +640,28 @@ base::Time UsageTimeLimitProcessor::GetNextUnlockTime() { ...@@ -613,30 +640,28 @@ base::Time UsageTimeLimitProcessor::GetNextUnlockTime() {
// When a lock override will become inactive. // When a lock override will become inactive.
if (HasActiveOverride() && time_limit_override_->action == if (HasActiveOverride() && time_limit_override_->action ==
internal::TimeLimitOverride::Action::kLock) { internal::TimeLimitOverride::Action::kLock) {
unlock_time = std::max(unlock_time, GetLockOverrideEndTime()); // The lock override ends either on the next reset time or when a bedtime
// ends.
// The usage limit could reset when a window limit is active, we must check base::Time lock_override_ends = GetLockOverrideEndTime();
// that, and if this is the case calculate the end of the window limit.
if (time_window_limit_) { if (time_window_limit_) {
// Check if yestardays, todays or tomorrows window limit will be active // Check yestardays, todays or tomorrows window limit, since these can end
// when the reset happens. // before the reset time.
for (int i = -1; i <= 1; i++) { for (int i = -1; i <= 1; i++) {
internal::Weekday weekday = WeekdayShift(current_weekday_, i);
base::Optional<TimeWindowLimitEntry> window_limit = base::Optional<TimeWindowLimitEntry> window_limit =
time_window_limit_->entries[WeekdayShift(current_weekday_, i)]; time_window_limit_->entries[weekday];
if (window_limit) { if (window_limit) {
TimeWindowLimitBoundaries limits = window_limit->GetLimits( base::Time window_limit_start =
LocalMidnight(current_time_) + base::TimeDelta::FromDays(i)); ConvertPolicyTime(window_limit->starts_at, i);
// Ignores time window limit if it is overridden. base::Time window_limit_end =
if (overridden_window_limit_ && window_limit_start +
ContainsTime(limits.starts, limits.ends, current_time_)) { GetConsecutiveTimeWindowLimitDuration(weekday);
continue; if (window_limit_end > time_limit_override_->created_at)
} lock_override_ends = std::min(lock_override_ends, window_limit_end);
if (ContainsTime(limits.starts, limits.ends,
GetLockOverrideEndTime()))
unlock_time = std::max(unlock_time, limits.ends);
} }
} }
} }
unlock_time = std::max(unlock_time, lock_override_ends);
} }
return unlock_time; return unlock_time;
......
...@@ -1104,6 +1104,143 @@ TEST_F(UsageTimeLimitProcessorTest, GetStateWithWeekendTimeUsageLimit) { ...@@ -1104,6 +1104,143 @@ TEST_F(UsageTimeLimitProcessorTest, GetStateWithWeekendTimeUsageLimit) {
AssertEqState(expected_state_one, state_one); AssertEqState(expected_state_one, state_one);
} }
// Test that a lock override followed by a time window limit will be ignored
// after the time window limit is over.
TEST_F(UsageTimeLimitProcessorTest, GetStateLockOverrideFollowedByBedtime) {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("PST"));
// Setup policy.
std::string last_updated = CreatePolicyTimestamp("1 Jan 2018 8:00 PST");
std::unique_ptr<base::DictionaryValue> policy =
CreateTimeLimitPolicy(CreateTime(6, 0));
AddTimeWindowLimit(policy.get(), kMonday, CreateTime(18, 0),
CreateTime(20, 0), last_updated);
AddOverride(policy.get(), kLock,
CreatePolicyTimestamp("Mon, 1 Jan 2018 15:00 PST"));
// Check that the device is locked because of the override.
base::Time time_one = TimeFromString("Mon, 1 Jan 2018 15:00 PST");
State state_one = GetState(policy, base::TimeDelta::FromMinutes(60), time_one,
time_one, timezone.get(), base::nullopt);
State expected_state_one;
expected_state_one.is_locked = true;
expected_state_one.active_policy = ActivePolicies::kOverride;
expected_state_one.is_time_usage_limit_enabled = false;
expected_state_one.next_state_change_time =
TimeFromString("Mon, 1 Jan 2018 18:00 PST");
expected_state_one.next_state_active_policy = ActivePolicies::kFixedLimit;
expected_state_one.next_unlock_time =
TimeFromString("Mon, 1 Jan 2018 20:00 PST");
expected_state_one.last_state_changed = base::Time();
AssertEqState(expected_state_one, state_one);
// Check that the device is locked because of the bedtime.
base::Time time_two = TimeFromString("Mon, 1 Jan 2018 18:00 PST");
State state_two = GetState(policy, base::TimeDelta::FromMinutes(60), time_two,
time_two, timezone.get(), state_one);
State expected_state_two;
expected_state_two.is_locked = true;
expected_state_two.active_policy = ActivePolicies::kFixedLimit;
expected_state_two.is_time_usage_limit_enabled = false;
expected_state_two.next_state_change_time =
TimeFromString("Mon, 1 Jan 2018 20:00 PST");
expected_state_two.next_state_active_policy = ActivePolicies::kNoActivePolicy;
expected_state_two.next_unlock_time =
TimeFromString("Mon, 1 Jan 2018 20:00 PST");
expected_state_two.last_state_changed = time_two;
AssertEqState(expected_state_two, state_two);
// Check that the device is unlocked after the bedtime ends.
base::Time time_three = TimeFromString("Mon, 1 Jan 2018 20:00 PST");
State state_three =
GetState(policy, base::TimeDelta::FromMinutes(60), time_three, time_three,
timezone.get(), state_two);
State expected_state_three;
expected_state_three.is_locked = false;
expected_state_three.active_policy = ActivePolicies::kNoActivePolicy;
expected_state_three.is_time_usage_limit_enabled = false;
expected_state_three.next_state_change_time =
TimeFromString("Mon, 8 Jan 2018 18:00 PST");
expected_state_three.next_state_active_policy = ActivePolicies::kFixedLimit;
expected_state_three.last_state_changed = time_three;
AssertEqState(expected_state_three, state_three);
}
// Test unlock-lock pair during time window limit.
TEST_F(UsageTimeLimitProcessorTest, GetStateUnlockLockDuringBedtime) {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("PST"));
// Setup policy.
std::string last_updated = CreatePolicyTimestamp("1 Jan 2018 8:00 PST");
std::unique_ptr<base::DictionaryValue> policy =
CreateTimeLimitPolicy(CreateTime(6, 0));
AddTimeWindowLimit(policy.get(), kMonday, CreateTime(10, 0),
CreateTime(20, 0), last_updated);
AddOverride(policy.get(), kUnlock,
CreatePolicyTimestamp("Mon, 1 Jan 2018 12:00 PST"));
// Check that the device is unlocked because of the override.
base::Time time_one = TimeFromString("Mon, 1 Jan 2018 12:00 PST");
State state_one = GetState(policy, base::TimeDelta::FromMinutes(60), time_one,
time_one, timezone.get(), base::nullopt);
State expected_state_one;
expected_state_one.is_locked = false;
expected_state_one.active_policy = ActivePolicies::kOverride;
expected_state_one.is_time_usage_limit_enabled = false;
expected_state_one.next_state_change_time =
TimeFromString("Mon, 8 Jan 2018 10:00 PST");
expected_state_one.next_state_active_policy = ActivePolicies::kFixedLimit;
expected_state_one.last_state_changed = base::Time();
AssertEqState(expected_state_one, state_one);
// Create lock.
AddOverride(policy.get(), kLock,
CreatePolicyTimestamp("Mon, 1 Jan 2018 14:00 PST"));
// Check that the device is locked because of the bedtime.
base::Time time_two = TimeFromString("Mon, 1 Jan 2018 14:00 PST");
State state_two = GetState(policy, base::TimeDelta::FromMinutes(60), time_two,
time_two, timezone.get(), state_one);
State expected_state_two;
expected_state_two.is_locked = true;
expected_state_two.active_policy = ActivePolicies::kFixedLimit;
expected_state_two.is_time_usage_limit_enabled = false;
expected_state_two.next_state_change_time =
TimeFromString("Mon, 1 Jan 2018 20:00 PST");
expected_state_two.next_state_active_policy = ActivePolicies::kNoActivePolicy;
expected_state_two.next_unlock_time =
TimeFromString("Mon, 1 Jan 2018 20:00 PST");
expected_state_two.last_state_changed = time_two;
AssertEqState(expected_state_two, state_two);
// Check that the device is unlocked after the bedtime ends.
base::Time time_three = TimeFromString("Mon, 1 Jan 2018 20:00 PST");
State state_three =
GetState(policy, base::TimeDelta::FromMinutes(60), time_three, time_three,
timezone.get(), state_two);
State expected_state_three;
expected_state_three.is_locked = false;
expected_state_three.active_policy = ActivePolicies::kNoActivePolicy;
expected_state_three.is_time_usage_limit_enabled = false;
expected_state_three.next_state_change_time =
TimeFromString("Mon, 8 Jan 2018 10:00 PST");
expected_state_three.next_state_active_policy = ActivePolicies::kFixedLimit;
expected_state_three.last_state_changed = time_three;
AssertEqState(expected_state_three, state_three);
}
// Test GetExpectedResetTime with an empty policy. // Test GetExpectedResetTime with an empty policy.
TEST_F(UsageTimeLimitProcessorTest, GetExpectedResetTimeWithEmptyPolicy) { TEST_F(UsageTimeLimitProcessorTest, GetExpectedResetTimeWithEmptyPolicy) {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("GMT")); std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("GMT"));
......
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