Commit 3a0d679d authored by Daniel Erat's avatar Daniel Erat Committed by Commit Bot

chromeos: Make ash::PowerPrefs use primary user's prefs.

When one or more users are signed in, make ash::PowerPrefs
always use the primary (i.e. first) user's prefs to derive
power management policy.

Bug: 872504
Change-Id: Ibe7cf26d3089ee0a69b58bf61d613354e9c6379c
Reviewed-on: https://chromium-review.googlesource.com/1168509Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Dan Erat <derat@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581914}
parent 722b2e29
...@@ -244,7 +244,7 @@ PrefService* SessionController::GetSigninScreenPrefService() const { ...@@ -244,7 +244,7 @@ PrefService* SessionController::GetSigninScreenPrefService() const {
} }
PrefService* SessionController::GetUserPrefServiceForUser( PrefService* SessionController::GetUserPrefServiceForUser(
const AccountId& account_id) { const AccountId& account_id) const {
auto it = per_user_prefs_.find(account_id); auto it = per_user_prefs_.find(account_id);
if (it != per_user_prefs_.end()) if (it != per_user_prefs_.end())
return it->second.get(); return it->second.get();
...@@ -252,6 +252,12 @@ PrefService* SessionController::GetUserPrefServiceForUser( ...@@ -252,6 +252,12 @@ PrefService* SessionController::GetUserPrefServiceForUser(
return nullptr; return nullptr;
} }
PrefService* SessionController::GetPrimaryUserPrefService() const {
const mojom::UserSession* session = GetPrimaryUserSession();
return session ? GetUserPrefServiceForUser(session->user_info->account_id)
: nullptr;
}
PrefService* SessionController::GetLastActiveUserPrefService() const { PrefService* SessionController::GetLastActiveUserPrefService() const {
return last_active_user_prefs_; return last_active_user_prefs_;
} }
......
...@@ -149,7 +149,11 @@ class ASH_EXPORT SessionController : public mojom::SessionController { ...@@ -149,7 +149,11 @@ class ASH_EXPORT SessionController : public mojom::SessionController {
PrefService* GetSigninScreenPrefService() const; PrefService* GetSigninScreenPrefService() const;
// Returns the PrefService for |account_id| or null if one does not exist. // Returns the PrefService for |account_id| or null if one does not exist.
PrefService* GetUserPrefServiceForUser(const AccountId& account_id); PrefService* GetUserPrefServiceForUser(const AccountId& account_id) const;
// Returns the PrefService for the primary user or null if no user is signed
// in or the PrefService connection hasn't been established.
PrefService* GetPrimaryUserPrefService() const;
// Returns the PrefService for the last active user that had one or null if no // Returns the PrefService for the last active user that had one or null if no
// PrefService connection has been successfully established. // PrefService connection has been successfully established.
......
...@@ -49,8 +49,14 @@ chromeos::PowerPolicyController::Action GetPowerPolicyAction( ...@@ -49,8 +49,14 @@ chromeos::PowerPolicyController::Action GetPowerPolicyAction(
return pref_action; return pref_action;
} }
PrefService* GetActivePrefService() { // Returns the PrefService that should be used for determining power-related
return Shell::Get()->session_controller()->GetActivePrefService(); // behavior. When one or more users are logged in, the primary user's prefs are
// used: if more-restrictive power-related prefs are set by policy, it's most
// likely to be on this profile.
PrefService* GetPrefService() {
ash::SessionController* controller = Shell::Get()->session_controller();
PrefService* prefs = controller->GetPrimaryUserPrefService();
return prefs ? prefs : controller->GetActivePrefService();
} }
// Registers power prefs whose default values are the same in user prefs and // Registers power prefs whose default values are the same in user prefs and
...@@ -180,7 +186,7 @@ void PowerPrefs::OnLockStateChanged(bool locked) { ...@@ -180,7 +186,7 @@ void PowerPrefs::OnLockStateChanged(bool locked) {
screen_lock_time_ = locked ? tick_clock_->NowTicks() : base::TimeTicks(); screen_lock_time_ = locked ? tick_clock_->NowTicks() : base::TimeTicks();
// OnLockStateChanged could be called before ash connects user prefs in tests. // OnLockStateChanged could be called before ash connects user prefs in tests.
if (GetActivePrefService()) if (GetPrefService())
UpdatePowerPolicyFromPrefs(); UpdatePowerPolicyFromPrefs();
} }
...@@ -193,7 +199,7 @@ void PowerPrefs::OnActiveUserPrefServiceChanged(PrefService* prefs) { ...@@ -193,7 +199,7 @@ void PowerPrefs::OnActiveUserPrefServiceChanged(PrefService* prefs) {
} }
void PowerPrefs::UpdatePowerPolicyFromPrefs() { void PowerPrefs::UpdatePowerPolicyFromPrefs() {
PrefService* prefs = GetActivePrefService(); PrefService* prefs = GetPrefService();
DCHECK(prefs); DCHECK(prefs);
// It's possible to end up in a situation where a shortened lock-screen idle // It's possible to end up in a situation where a shortened lock-screen idle
......
...@@ -40,8 +40,11 @@ PrefService* GetSigninScreenPrefService() { ...@@ -40,8 +40,11 @@ PrefService* GetSigninScreenPrefService() {
return Shell::Get()->session_controller()->GetSigninScreenPrefService(); return Shell::Get()->session_controller()->GetSigninScreenPrefService();
} }
PrefService* GetLastActiveUserPrefService() { // Returns prefs for the user identified by |user_email|, or null if the user's
return Shell::Get()->session_controller()->GetLastActiveUserPrefService(); // prefs are unavailable (e.g. because they don't exist).
PrefService* GetUserPrefService(const std::string& user_email) {
return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
AccountId::FromUserEmail(user_email));
} }
std::string GetExpectedPowerPolicyForPrefs(PrefService* prefs, std::string GetExpectedPowerPolicyForPrefs(PrefService* prefs,
...@@ -191,21 +194,49 @@ TEST_F(PowerPrefsTest, LoginScreen) { ...@@ -191,21 +194,49 @@ TEST_F(PowerPrefsTest, LoginScreen) {
} }
TEST_F(PowerPrefsTest, UserSession) { TEST_F(PowerPrefsTest, UserSession) {
SimulateUserLogin("user@test.com"); const char kUserEmail[] = "user@example.net";
PrefService* prefs = SimulateUserLogin(kUserEmail);
Shell::Get()->session_controller()->GetActivePrefService(); PrefService* prefs = GetUserPrefService(kUserEmail);
EXPECT_EQ(GetLastActiveUserPrefService(), prefs); ASSERT_TRUE(prefs);
EXPECT_EQ(GetExpectedPowerPolicyForPrefs(prefs, ScreenLockState::UNLOCKED), EXPECT_EQ(GetExpectedPowerPolicyForPrefs(prefs, ScreenLockState::UNLOCKED),
GetCurrentPowerPolicy()); GetCurrentPowerPolicy());
EXPECT_EQ(GetExpectedAllowScreenWakeLocksForPrefs(prefs), EXPECT_EQ(GetExpectedAllowScreenWakeLocksForPrefs(prefs),
GetCurrentAllowScreenWakeLocks()); GetCurrentAllowScreenWakeLocks());
} }
TEST_F(PowerPrefsTest, PrimaryUserPrefs) {
// Add a user with restrictive prefs.
const char kFirstUserEmail[] = "user1@example.net";
SimulateUserLogin(kFirstUserEmail);
PrefService* first_prefs = GetUserPrefService(kFirstUserEmail);
ASSERT_TRUE(first_prefs);
first_prefs->SetBoolean(prefs::kPowerAllowScreenWakeLocks, false);
first_prefs->SetInteger(prefs::kPowerLidClosedAction,
chromeos::PowerPolicyController::ACTION_SHUT_DOWN);
// Add a second user with lenient prefs.
const char kSecondUserEmail[] = "user2@example.net";
SimulateUserLogin(kSecondUserEmail);
PrefService* second_prefs = GetUserPrefService(kSecondUserEmail);
ASSERT_TRUE(second_prefs);
second_prefs->SetBoolean(prefs::kPowerAllowScreenWakeLocks, true);
second_prefs->SetInteger(prefs::kPowerLidClosedAction,
chromeos::PowerPolicyController::ACTION_DO_NOTHING);
// Even though the second user is active, the first (primary) user's prefs
// should still be used.
ASSERT_EQ(second_prefs,
Shell::Get()->session_controller()->GetActivePrefService());
EXPECT_EQ(
GetExpectedPowerPolicyForPrefs(first_prefs, ScreenLockState::UNLOCKED),
GetCurrentPowerPolicy());
}
TEST_F(PowerPrefsTest, AvoidLockDelaysAfterInactivity) { TEST_F(PowerPrefsTest, AvoidLockDelaysAfterInactivity) {
SimulateUserLogin("user@test.com"); const char kUserEmail[] = "user@example.net";
PrefService* prefs = SimulateUserLogin(kUserEmail);
Shell::Get()->session_controller()->GetActivePrefService(); PrefService* prefs = GetUserPrefService(kUserEmail);
EXPECT_EQ(GetLastActiveUserPrefService(), prefs); ASSERT_TRUE(prefs);
EXPECT_EQ(GetExpectedPowerPolicyForPrefs(prefs, ScreenLockState::UNLOCKED), EXPECT_EQ(GetExpectedPowerPolicyForPrefs(prefs, ScreenLockState::UNLOCKED),
GetCurrentPowerPolicy()); GetCurrentPowerPolicy());
...@@ -231,10 +262,10 @@ TEST_F(PowerPrefsTest, AvoidLockDelaysAfterInactivity) { ...@@ -231,10 +262,10 @@ TEST_F(PowerPrefsTest, AvoidLockDelaysAfterInactivity) {
} }
TEST_F(PowerPrefsTest, DisabledLockScreen) { TEST_F(PowerPrefsTest, DisabledLockScreen) {
SimulateUserLogin("user@test.com"); const char kUserEmail[] = "user@example.net";
PrefService* prefs = SimulateUserLogin(kUserEmail);
Shell::Get()->session_controller()->GetActivePrefService(); PrefService* prefs = GetUserPrefService(kUserEmail);
EXPECT_EQ(GetLastActiveUserPrefService(), prefs); ASSERT_TRUE(prefs);
// Verify that the power policy actions are set to default values initially. // Verify that the power policy actions are set to default values initially.
EXPECT_EQ(std::vector<power_manager::PowerManagementPolicy_Action>( EXPECT_EQ(std::vector<power_manager::PowerManagementPolicy_Action>(
......
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