Commit 376503e2 authored by Sarah Hu's avatar Sarah Hu Committed by Commit Bot

Interagion with the processor stub and time limit pref

Change-Id: I819742fc61d41ffdeb00b5ccd888a1fe56feb3d2
Bug: 823536
Reviewed-on: https://chromium-review.googlesource.com/1068659Reviewed-by: default avatarRahul Chaturvedi <rkc@chromium.org>
Reviewed-by: default avatarJacob Dufault <jdufault@chromium.org>
Commit-Queue: Xiaoyin Hu <xiaoyinh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561696}
parent dae6df8a
...@@ -5,8 +5,11 @@ ...@@ -5,8 +5,11 @@
#include "chrome/browser/chromeos/child_accounts/screen_time_controller.h" #include "chrome/browser/chromeos/child_accounts/screen_time_controller.h"
#include "ash/public/cpp/vector_icons/vector_icons.h" #include "ash/public/cpp/vector_icons/vector_icons.h"
#include "base/optional.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/notifications/notification_display_service.h" #include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/login_screen_client.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
...@@ -37,6 +40,14 @@ constexpr char kTimeLimitNotificationId[] = "time-limit-notification"; ...@@ -37,6 +40,14 @@ constexpr char kTimeLimitNotificationId[] = "time-limit-notification";
// The notifier id representing the app. // The notifier id representing the app.
constexpr char kTimeLimitNotifierId[] = "family-link"; constexpr char kTimeLimitNotifierId[] = "family-link";
// Dictionary keys for prefs::kScreenTimeLastState. Time relavant states are
// not saved because processor should not count on them as they could become
// invalid easiliy.
constexpr char kScreenStateLocked[] = "locked";
constexpr char kScreenStateCurrentPolicyType[] = "active_policy";
constexpr char kScreenStateTimeUsageLimitEnabled[] = "time_usage_limit_enabled";
constexpr char kScreenStateNextPolicyType[] = "next_active_policy";
} // namespace } // namespace
// static // static
...@@ -45,14 +56,18 @@ void ScreenTimeController::RegisterProfilePrefs(PrefRegistrySimple* registry) { ...@@ -45,14 +56,18 @@ void ScreenTimeController::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterTimePref(prefs::kFirstScreenStartTime, base::Time()); registry->RegisterTimePref(prefs::kFirstScreenStartTime, base::Time());
registry->RegisterIntegerPref(prefs::kScreenTimeMinutesUsed, 0); registry->RegisterIntegerPref(prefs::kScreenTimeMinutesUsed, 0);
registry->RegisterDictionaryPref(prefs::kUsageTimeLimit); registry->RegisterDictionaryPref(prefs::kUsageTimeLimit);
registry->RegisterDictionaryPref(prefs::kScreenTimeLastState);
} }
ScreenTimeController::ScreenTimeController(content::BrowserContext* context) ScreenTimeController::ScreenTimeController(content::BrowserContext* context)
: context_(context), : context_(context),
pref_service_(Profile::FromBrowserContext(context)->GetPrefs()) { pref_service_(Profile::FromBrowserContext(context)->GetPrefs()) {
// TODO(https://crbug.com/823536): observe the policy changes for: screen time
// limit, bed time, parental lock/unlock. And call OnPolicyChanged.
session_manager::SessionManager::Get()->AddObserver(this); session_manager::SessionManager::Get()->AddObserver(this);
pref_change_registrar_.Init(pref_service_);
pref_change_registrar_.Add(
prefs::kUsageTimeLimit,
base::BindRepeating(&ScreenTimeController::OnPolicyChanged,
base::Unretained(this)));
} }
ScreenTimeController::~ScreenTimeController() { ScreenTimeController::~ScreenTimeController() {
...@@ -75,26 +90,33 @@ void ScreenTimeController::CheckTimeLimit() { ...@@ -75,26 +90,33 @@ void ScreenTimeController::CheckTimeLimit() {
// Stop any currently running timer. // Stop any currently running timer.
ResetTimers(); ResetTimers();
TimeLimitState state; base::Time now = base::Time::Now();
// TODO(https://crbug.com/823536): Call time limit processer. const base::DictionaryValue* time_limit =
// state = GetState(time_limit, GetScreenTimeDuration(), pref_service_->GetDictionary(prefs::kUsageTimeLimit);
// first_screen_start_time_, base::Time::Now()); usage_time_limit::State state = usage_time_limit::GetState(
time_limit->CreateDeepCopy(), GetScreenTimeDuration(),
first_screen_start_time_, now, GetLastStateFromPref());
SaveCurrentStateToPref(state);
if (state.is_locked) { if (state.is_locked) {
base::Time reset_time = base::Time(); base::Time reset_time = usage_time_limit::GetExpectedResetTime(
// TODO(https://crbug.com/823536): Get the expected reset time. time_limit->CreateDeepCopy(), now);
// reset_time = getExpectedResetTime(time_limit, base::Time::Now());
LockScreen(true /*force_lock_by_policy*/, reset_time); LockScreen(true /*force_lock_by_policy*/, reset_time);
} else { } else {
if (state.active_policy == ActivePolicy::kNoActivePolicy) usage_time_limit::ActivePolicies active_policy = state.active_policy;
if (active_policy == usage_time_limit::ActivePolicies::kNoActivePolicy)
RefreshScreenLimit(); RefreshScreenLimit();
LockScreen(false /*force_lock_by_policy*/, base::Time() /*come_back_time*/); LockScreen(false /*force_lock_by_policy*/, base::Time() /*come_back_time*/);
if (state.is_time_usage_limit_enabled) { if (!state.next_state_change_time.is_null() &&
(active_policy == usage_time_limit::ActivePolicies::kFixedLimit ||
active_policy == usage_time_limit::ActivePolicies::kUsageLimit)) {
// Schedule notification based on the remaining screen time. // Schedule notification based on the remaining screen time.
const base::TimeDelta remaining_usage = state.remaining_usage; const base::TimeDelta remaining_usage = state.remaining_usage;
// TODO(wzang): Distinguish the notification type based on const TimeLimitNotificationType notification_type =
// |TimeLimitState|. active_policy == usage_time_limit::ActivePolicies::kFixedLimit
const TimeLimitNotificationType notification_type = kScreenTime; ? kBedTime
: kScreenTime;
if (remaining_usage >= kWarningNotificationTimeout) { if (remaining_usage >= kWarningNotificationTimeout) {
warning_notification_timer_.Start( warning_notification_timer_.Start(
...@@ -137,8 +159,13 @@ void ScreenTimeController::LockScreen(bool force_lock_by_policy, ...@@ -137,8 +159,13 @@ void ScreenTimeController::LockScreen(bool force_lock_by_policy,
->RequestLockScreen(); ->RequestLockScreen();
} }
// TODO(https://crbug.com/823536): Mojo call to show/hide screen time message AccountId account_id =
// in lock screen based on the policy. chromeos::ProfileHelper::Get()
->GetUserByProfile(Profile::FromBrowserContext(context_))
->GetAccountId();
LoginScreenClient::Get()->login_screen()->SetAuthEnabledForUser(
account_id, !force_lock_by_policy,
force_lock_by_policy ? come_back_time : base::Optional<base::Time>());
} }
void ScreenTimeController::ShowNotification( void ScreenTimeController::ShowNotification(
...@@ -208,6 +235,74 @@ void ScreenTimeController::SaveScreenTimeProgressPeriodically() { ...@@ -208,6 +235,74 @@ void ScreenTimeController::SaveScreenTimeProgressPeriodically() {
pref_service_->CommitPendingWrite(); pref_service_->CommitPendingWrite();
} }
void ScreenTimeController::SaveCurrentStateToPref(
const usage_time_limit::State& state) {
auto state_dict =
std::make_unique<base::Value>(base::Value::Type::DICTIONARY);
state_dict->SetKey(kScreenStateLocked, base::Value(state.is_locked));
state_dict->SetKey(kScreenStateCurrentPolicyType,
base::Value(static_cast<int>(state.active_policy)));
state_dict->SetKey(kScreenStateTimeUsageLimitEnabled,
base::Value(state.is_time_usage_limit_enabled));
state_dict->SetKey(
kScreenStateNextPolicyType,
base::Value(static_cast<int>(state.next_state_active_policy)));
pref_service_->Set(prefs::kScreenTimeLastState, *state_dict);
pref_service_->CommitPendingWrite();
}
base::Optional<usage_time_limit::State>
ScreenTimeController::GetLastStateFromPref() {
const base::DictionaryValue* last_state =
pref_service_->GetDictionary(prefs::kScreenTimeLastState);
usage_time_limit::State result;
if (last_state->empty())
return base::nullopt;
// Verify is_locked from the pref is a boolean value.
const base::Value* is_locked = last_state->FindKey(kScreenStateLocked);
if (!is_locked || !is_locked->is_bool())
return base::nullopt;
result.is_locked = is_locked->GetBool();
// Verify active policy type is a value of usage_time_limit::ActivePolicies.
const base::Value* active_policy =
last_state->FindKey(kScreenStateCurrentPolicyType);
// TODO(crbug.com/823536): Add kCount in usage_time_limit::ActivePolicies
// instead of checking kUsageLimit here.
if (!active_policy || !active_policy->is_int() ||
active_policy->GetInt() < 0 ||
active_policy->GetInt() >
static_cast<int>(usage_time_limit::ActivePolicies::kUsageLimit)) {
return base::nullopt;
}
result.active_policy =
static_cast<usage_time_limit::ActivePolicies>(active_policy->GetInt());
// Verify time_usage_limit_enabled from the pref is a boolean value.
const base::Value* time_usage_limit_enabled =
last_state->FindKey(kScreenStateTimeUsageLimitEnabled);
if (!time_usage_limit_enabled || !time_usage_limit_enabled->is_bool())
return base::nullopt;
result.is_time_usage_limit_enabled = time_usage_limit_enabled->GetBool();
// Verify next policy type is a value of usage_time_limit::ActivePolicies.
const base::Value* next_active_policy =
last_state->FindKey(kScreenStateNextPolicyType);
if (!next_active_policy || !next_active_policy->is_int() ||
next_active_policy->GetInt() < 0 ||
next_active_policy->GetInt() >
static_cast<int>(usage_time_limit::ActivePolicies::kUsageLimit)) {
return base::nullopt;
}
result.next_state_active_policy =
static_cast<usage_time_limit::ActivePolicies>(
next_active_policy->GetInt());
return result;
}
void ScreenTimeController::OnSessionStateChanged() { void ScreenTimeController::OnSessionStateChanged() {
session_manager::SessionState session_state = session_manager::SessionState session_state =
session_manager::SessionManager::Get()->session_state(); session_manager::SessionManager::Get()->session_state();
......
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "chrome/browser/chromeos/child_accounts/usage_time_limit_processor.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/session_manager/core/session_manager_observer.h" #include "components/session_manager/core/session_manager_observer.h"
class PrefRegistrySimple; class PrefRegistrySimple;
...@@ -26,26 +28,6 @@ namespace chromeos { ...@@ -26,26 +28,6 @@ namespace chromeos {
class ScreenTimeController : public KeyedService, class ScreenTimeController : public KeyedService,
public session_manager::SessionManagerObserver { public session_manager::SessionManagerObserver {
public: public:
// Fake enum from time limit processor, should be removed when the processer
// is ready.
enum class ActivePolicy {
kNoActivePolicy,
kOverride,
kFixedLimit,
kUsageLimit
};
// Fake struct from time limit processor, should be removed when the
// processer is ready.
struct TimeLimitState {
bool is_locked = false;
ActivePolicy active_policy = ActivePolicy::kNoActivePolicy;
bool is_time_usage_limit_enabled = false;
base::TimeDelta remaining_usage = base::TimeDelta();
base::Time next_state_change_time = base::Time();
ActivePolicy next_state_active_policy = ActivePolicy::kNoActivePolicy;
};
// Registers preferences. // Registers preferences.
static void RegisterProfilePrefs(PrefRegistrySimple* registry); static void RegisterProfilePrefs(PrefRegistrySimple* registry);
...@@ -92,6 +74,13 @@ class ScreenTimeController : public KeyedService, ...@@ -92,6 +74,13 @@ class ScreenTimeController : public KeyedService,
// outage. // outage.
void SaveScreenTimeProgressPeriodically(); void SaveScreenTimeProgressPeriodically();
// Save the |state| to |prefs::kScreenTimeLastState|.
void SaveCurrentStateToPref(const usage_time_limit::State& state);
// Get the last calculated |state| from |prefs::kScreenTimeLastState|, if it
// exists.
base::Optional<usage_time_limit::State> GetLastStateFromPref();
// session_manager::SessionManagerObserver: // session_manager::SessionManagerObserver:
void OnSessionStateChanged() override; void OnSessionStateChanged() override;
...@@ -114,6 +103,8 @@ class ScreenTimeController : public KeyedService, ...@@ -114,6 +103,8 @@ class ScreenTimeController : public KeyedService,
// RefreshScreenLimit(); // RefreshScreenLimit();
base::Time first_screen_start_time_; base::Time first_screen_start_time_;
PrefChangeRegistrar pref_change_registrar_;
DISALLOW_COPY_AND_ASSIGN(ScreenTimeController); DISALLOW_COPY_AND_ASSIGN(ScreenTimeController);
}; };
......
...@@ -860,6 +860,9 @@ const char kScreenTimeMinutesUsed[] = "screen_time.time_usage"; ...@@ -860,6 +860,9 @@ const char kScreenTimeMinutesUsed[] = "screen_time.time_usage";
// A dictionary preference holding the usage time limit definitions for a user. // A dictionary preference holding the usage time limit definitions for a user.
const char kUsageTimeLimit[] = "screen_time.limit"; const char kUsageTimeLimit[] = "screen_time.limit";
// Last state of the screen time limit.
const char kScreenTimeLastState[] = "screen_time.last_state";
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
// A boolean pref set to true if a Home button to open the Home pages should be // A boolean pref set to true if a Home button to open the Home pages should be
......
...@@ -280,6 +280,7 @@ extern const char kFirstScreenStartTime[]; ...@@ -280,6 +280,7 @@ extern const char kFirstScreenStartTime[];
extern const char kCurrentScreenStartTime[]; extern const char kCurrentScreenStartTime[];
extern const char kScreenTimeMinutesUsed[]; extern const char kScreenTimeMinutesUsed[];
extern const char kUsageTimeLimit[]; extern const char kUsageTimeLimit[];
extern const char kScreenTimeLastState[];
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
extern const char kShowHomeButton[]; extern const char kShowHomeButton[];
extern const char kSpeechRecognitionFilterProfanities[]; extern const char kSpeechRecognitionFilterProfanities[];
......
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