From 8e0ba06bd3a63a6d5ae3a342bc1321146b6e18b7 Mon Sep 17 00:00:00 2001 From: "bartfab@chromium.org" Date: Mon, 28 Oct 2013 15:38:09 +0000 Subject: [PATCH] Add WaitForInitialUserActivity policy This CL adds a user policy that controls whether power management delays and the session length limit should only start running after initial user activity in a session. The SessionLengthLimiter is modified to obey the new policy. Power management will hooked up to the new policy in a follow-up CL. It would technically be possible to add separate policies that determine whether power management delays on the one hand and the session length limit on the other hand should wait for initial user activity. However, it does not make sense for one of these to start running on session start while the other waits for user activity. Therefore, a single policy is added for both. BUG=310176 TEST=Updated unit and browser tests Review URL: https://codereview.chromium.org/35943002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@231332 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/app/policy/policy_templates.json | 20 +- chrome/browser/chromeos/power/power_prefs.cc | 4 + chrome/browser/chromeos/session_length_limiter.cc | 125 +++- chrome/browser/chromeos/session_length_limiter.h | 27 +- .../chromeos/session_length_limiter_unittest.cc | 645 ++++++++++++++++++--- .../policy/configuration_policy_handler_list.cc | 6 + chrome/browser/policy/policy_browsertest.cc | 97 +++- chrome/common/pref_names.cc | 25 +- chrome/common/pref_names.h | 3 + chrome/test/data/policy/policy_test_cases.json | 10 + 10 files changed, 833 insertions(+), 129 deletions(-) diff --git a/chrome/app/policy/policy_templates.json b/chrome/app/policy/policy_templates.json index d676299951e5..ff97e1562625 100644 --- a/chrome/app/policy/policy_templates.json +++ b/chrome/app/policy/policy_templates.json @@ -118,7 +118,7 @@ # persistent IDs for all fields (but not for groups!) are needed. These are # specified by the 'id' keys of each policy. NEVER CHANGE EXISTING IDs, # because doing so would break the deployed wire format! -# For your editing convenience: highest ID currently used: 246 +# For your editing convenience: highest ID currently used: 247 # # Placeholders: # The following placeholder strings are automatically substituted: @@ -4919,6 +4919,24 @@ The scale factor must be 100% or more.''', }, + { + 'name': 'WaitForInitialUserActivity', + 'type': 'main', + 'schema': { 'type': 'boolean' }, + 'supported_on': ['chrome_os:32-'], + 'features': { + 'dynamic_refresh': True, + 'per_profile': False, + }, + 'example_value': True, + 'id': 247, + 'caption': '''Specify whether power management delays and the session length limit should only start running after initial user activity in a session''', + 'desc': '''Specifies whether power management delays and the session length limit should only start running after the first user activity has been observed in a session. + + If this policy is set to True, power management delays and the session length limit do not start running until after the first user activity has been observed in a session. + + If this policy is set to False or left unset, power management delays and the session length limit start running immediately on session start.''', + }, ], }, { diff --git a/chrome/browser/chromeos/power/power_prefs.cc b/chrome/browser/chromeos/power/power_prefs.cc index c05f07425f93..aa96fd44fe66 100644 --- a/chrome/browser/chromeos/power/power_prefs.cc +++ b/chrome/browser/chromeos/power/power_prefs.cc @@ -220,6 +220,10 @@ void PowerPrefs::RegisterProfilePrefs( prefs::kPowerUserActivityScreenDimDelayFactor, 2.0, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); + registry->RegisterBooleanPref( + prefs::kPowerWaitForInitialUserActivity, + false, + user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); } void PowerPrefs::SetProfile(Profile* profile) { diff --git a/chrome/browser/chromeos/session_length_limiter.cc b/chrome/browser/chromeos/session_length_limiter.cc index 14c901b866f4..4ea224a2c44b 100644 --- a/chrome/browser/chromeos/session_length_limiter.cc +++ b/chrome/browser/chromeos/session_length_limiter.cc @@ -6,6 +6,8 @@ #include +#include "ash/shell.h" +#include "ash/wm/user_activity_detector.h" #include "base/bind.h" #include "base/bind_helpers.h" #include "base/location.h" @@ -15,6 +17,7 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/lifetime/application_lifetime.h" #include "chrome/common/pref_names.h" +#include "ui/events/event.h" namespace chromeos { @@ -62,61 +65,126 @@ SessionLengthLimiter::Delegate::~Delegate() { // static void SessionLengthLimiter::RegisterPrefs(PrefRegistrySimple* registry) { + registry->RegisterBooleanPref(prefs::kSessionUserActivitySeen, false); registry->RegisterInt64Pref(prefs::kSessionStartTime, 0); registry->RegisterIntegerPref(prefs::kSessionLengthLimit, 0); + registry->RegisterBooleanPref(prefs::kSessionWaitForInitialUserActivity, + false); } SessionLengthLimiter::SessionLengthLimiter(Delegate* delegate, bool browser_restarted) - : delegate_(delegate ? delegate : new SessionLengthLimiterDelegateImpl) { + : delegate_(delegate ? delegate : new SessionLengthLimiterDelegateImpl), + user_activity_seen_(false) { DCHECK(thread_checker_.CalledOnValidThread()); - // If this is a user login, set the session start time in local state to the - // current time. If this a browser restart after a crash, set the session - // start time only if its current value appears corrupted (value unset, value - // lying in the future). PrefService* local_state = g_browser_process->local_state(); - int64 session_start_time = local_state->GetInt64(prefs::kSessionStartTime); - const int64 now = delegate_->GetCurrentTime().ToInternalValue(); - if (!browser_restarted || - !local_state->HasPrefPath(prefs::kSessionStartTime) || - session_start_time > now) { - local_state->SetInt64(prefs::kSessionStartTime, now); - // Ensure that the session start time is persisted to local state. - local_state->CommitPendingWrite(); - session_start_time = now; - } - session_start_time_ = base::TimeTicks::FromInternalValue(session_start_time); - - // Listen for changes to the session length limit. pref_change_registrar_.Init(local_state); + pref_change_registrar_.Add(prefs::kSessionLengthLimit, + base::Bind(&SessionLengthLimiter::UpdateLimit, + base::Unretained(this))); pref_change_registrar_.Add( - prefs::kSessionLengthLimit, - base::Bind(&SessionLengthLimiter::OnSessionLengthLimitChanged, + prefs::kSessionWaitForInitialUserActivity, + base::Bind(&SessionLengthLimiter::UpdateSessionStartTime, base::Unretained(this))); - // Handle the current session length limit, if any. - OnSessionLengthLimitChanged(); + // If this is a browser restart after a crash, try to restore the session + // start time and the boolean indicating user activity from local state. If + // this is not a browser restart after a crash or the attempt to restore + // fails, set the session start time to the current time and clear the + // boolean indicating user activity. + if (!browser_restarted || !RestoreStateAfterCrash()) { + local_state->ClearPref(prefs::kSessionUserActivitySeen); + UpdateSessionStartTime(); + } + + if (!user_activity_seen_ && ash::Shell::HasInstance()) + ash::Shell::GetInstance()->user_activity_detector()->AddObserver(this); } SessionLengthLimiter::~SessionLengthLimiter() { + if (!user_activity_seen_ && ash::Shell::HasInstance()) + ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this); +} + +void SessionLengthLimiter::OnUserActivity(const ui::Event* event) { + if (user_activity_seen_) + return; + if (ash::Shell::HasInstance()) + ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this); + user_activity_seen_ = true; + + PrefService* local_state = g_browser_process->local_state(); + local_state->SetBoolean(prefs::kSessionUserActivitySeen, true); + if (session_start_time_.is_null()) { + // If instructed to wait for initial user activity and this is the first + // activity in the session, set the session start time to the current time + // and persist it in local state. + session_start_time_ = delegate_->GetCurrentTime(); + local_state->SetInt64(prefs::kSessionStartTime, + session_start_time_.ToInternalValue()); + } + local_state->CommitPendingWrite(); + + UpdateLimit(); +} + +bool SessionLengthLimiter::RestoreStateAfterCrash() { + PrefService* local_state = g_browser_process->local_state(); + const base::TimeTicks session_start_time = + base::TimeTicks::FromInternalValue( + local_state->GetInt64(prefs::kSessionStartTime)); + if (session_start_time.is_null() || + session_start_time >= delegate_->GetCurrentTime()) { + return false; + } + + session_start_time_ = session_start_time; + user_activity_seen_ = + local_state->GetBoolean(prefs::kSessionUserActivitySeen); + + UpdateLimit(); + return true; +} + +void SessionLengthLimiter::UpdateSessionStartTime() { + DCHECK(thread_checker_.CalledOnValidThread()); + + if (user_activity_seen_) + return; + + PrefService* local_state = g_browser_process->local_state(); + if (local_state->GetBoolean(prefs::kSessionWaitForInitialUserActivity)) { + session_start_time_ = base::TimeTicks(); + local_state->ClearPref(prefs::kSessionStartTime); + } else { + session_start_time_ = delegate_->GetCurrentTime(); + local_state->SetInt64(prefs::kSessionStartTime, + session_start_time_.ToInternalValue()); + } + local_state->CommitPendingWrite(); + + UpdateLimit(); } -void SessionLengthLimiter::OnSessionLengthLimitChanged() { +void SessionLengthLimiter::UpdateLimit() { DCHECK(thread_checker_.CalledOnValidThread()); // Stop any currently running timer. - if (timer_) - timer_->Stop(); + timer_.reset(); + + // If instructed to wait for initial user activity and no user activity has + // occurred yet, do not start a timer. + if (session_start_time_.is_null()) + return; + // If no session length limit is set, do not start a timer. int limit; const PrefService::Preference* session_length_limit_pref = pref_change_registrar_.prefs()-> FindPreference(prefs::kSessionLengthLimit); if (session_length_limit_pref->IsDefaultValue() || !session_length_limit_pref->GetValue()->GetAsInteger(&limit)) { - // If no session length limit is set, destroy the timer. - timer_.reset(); return; } @@ -137,8 +205,7 @@ void SessionLengthLimiter::OnSessionLengthLimitChanged() { } // Set a timer to log out the user when the session length limit is reached. - if (!timer_) - timer_.reset(new base::OneShotTimer); + timer_.reset(new base::OneShotTimer); timer_->Start(FROM_HERE, remaining, delegate_.get(), &SessionLengthLimiter::Delegate::StopSession); } diff --git a/chrome/browser/chromeos/session_length_limiter.h b/chrome/browser/chromeos/session_length_limiter.h index 8c6d0892336e..fb8216f1cf1a 100644 --- a/chrome/browser/chromeos/session_length_limiter.h +++ b/chrome/browser/chromeos/session_length_limiter.h @@ -5,7 +5,9 @@ #ifndef CHROME_BROWSER_CHROMEOS_SESSION_LENGTH_LIMITER_H_ #define CHROME_BROWSER_CHROMEOS_SESSION_LENGTH_LIMITER_H_ +#include "ash/wm/user_activity_observer.h" #include "base/basictypes.h" +#include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "base/prefs/pref_change_registrar.h" #include "base/threading/thread_checker.h" @@ -19,7 +21,7 @@ namespace chromeos { // Enforces a session length limit by terminating the session when the limit is // reached. -class SessionLengthLimiter { +class SessionLengthLimiter : public ash::UserActivityObserver { public: class Delegate { public: @@ -33,10 +35,28 @@ class SessionLengthLimiter { static void RegisterPrefs(PrefRegistrySimple* registry); SessionLengthLimiter(Delegate* delegate, bool browser_restarted); - ~SessionLengthLimiter(); + virtual ~SessionLengthLimiter(); + + // ash::UserActivityObserver: + virtual void OnUserActivity(const ui::Event* event) OVERRIDE; private: - void OnSessionLengthLimitChanged(); + // Attempt to restore the session start time and the flag indicating user + // activity from local state. Return |true| if the restore is successful. + bool RestoreStateAfterCrash(); + + // Update the session start time if possible: + // * If instructed to wait for initial user activity, the session start time + // advances every time this method is called as long as no user activity has + // occurred yet. The time is not persisted in local state. + // * If instructed not to wait for initial user activity, the session start + // time is set and persisted in local state the first time this method is + // called. + // The pref indicating whether to wait for initial user activity may change at + // any time, switching between the two behaviors. + void UpdateSessionStartTime(); + + void UpdateLimit(); base::ThreadChecker thread_checker_; @@ -45,6 +65,7 @@ class SessionLengthLimiter { scoped_ptr > timer_; base::TimeTicks session_start_time_; + bool user_activity_seen_; DISALLOW_COPY_AND_ASSIGN(SessionLengthLimiter); }; diff --git a/chrome/browser/chromeos/session_length_limiter_unittest.cc b/chrome/browser/chromeos/session_length_limiter_unittest.cc index 39495a456d7b..625cb94d28ed 100644 --- a/chrome/browser/chromeos/session_length_limiter_unittest.cc +++ b/chrome/browser/chromeos/session_length_limiter_unittest.cc @@ -56,7 +56,7 @@ class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner { const base::TimeTicks& GetCurrentTime() const; - void FastForwardBy(int64 milliseconds); + void FastForwardBy(const base::TimeDelta& time_delta); void FastForwardUntilNoTasksRemain(); private: @@ -86,26 +86,49 @@ class SessionLengthLimiterTest : public testing::Test { virtual void SetUp() OVERRIDE; virtual void TearDown() OVERRIDE; - void SetSessionStartTimePref(int64 session_start_time); - void VerifySessionStartTimePref(); - void SetSessionLengthLimitPref(int64 session_length_limit); + void SetSessionUserActivitySeenPref(bool user_activity_seen); + void ClearSessionUserActivitySeenPref(); + bool IsSessionUserActivitySeenPrefSet(); + bool GetSessionUserActivitySeenPref(); + + void SetSessionStartTimePref(const base::TimeTicks& session_start_time); + void ClearSessionStartTimePref(); + bool IsSessionStartTimePrefSet(); + base::TimeTicks GetSessionStartTimePref(); + + void SetSessionLengthLimitPref(const base::TimeDelta& session_length_limit); + void ClearSessionLengthLimitPref(); + + void SetWaitForInitialUserActivityPref(bool wait_for_initial_user_activity); + + void SimulateUserActivity(); + + void UpdateSessionStartTimeIfWaitingForUserActivity(); void ExpectStopSession(); - void CheckStopSessionTime(); + void SaveSessionStopTime(); + // Clears the session state by resetting |user_activity_| and + // |session_start_time_| and creates a new SessionLengthLimiter. void CreateSessionLengthLimiter(bool browser_restarted); - TestingPrefServiceSimple local_state_; + void DestroySessionLengthLimiter(); + scoped_refptr runner_; base::TimeTicks session_start_time_; - base::TimeTicks session_end_time_; + base::TimeTicks session_stop_time_; + + private: + TestingPrefServiceSimple local_state_; + bool user_activity_seen_; MockSessionLengthLimiterDelegate* delegate_; // Owned by // session_length_limiter_. scoped_ptr session_length_limiter_; }; -MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner() { +MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner() + : now_(base::TimeTicks::FromInternalValue(1000)) { } bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const { @@ -132,9 +155,9 @@ const base::TimeTicks& MockTimeSingleThreadTaskRunner::GetCurrentTime() const { return now_; } -void MockTimeSingleThreadTaskRunner::FastForwardBy(int64 delta) { - const base::TimeTicks latest = - now_ + base::TimeDelta::FromMilliseconds(delta); +void MockTimeSingleThreadTaskRunner::FastForwardBy( + const base::TimeDelta& time_delta) { + const base::TimeTicks latest = now_ + time_delta; while (!tasks_.empty() && tasks_.top().first <= latest) { now_ = tasks_.top().first; base::Closure task = tasks_.top().second; @@ -169,139 +192,582 @@ void SessionLengthLimiterTest::SetUp() { TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); SessionLengthLimiter::RegisterPrefs(local_state_.registry()); runner_ = new MockTimeSingleThreadTaskRunner; - session_start_time_ = runner_->GetCurrentTime(); - - delegate_ = new NiceMock; - ON_CALL(*delegate_, GetCurrentTime()) - .WillByDefault(Invoke(runner_.get(), - &MockTimeSingleThreadTaskRunner::GetCurrentTime)); - EXPECT_CALL(*delegate_, StopSession()).Times(0); } void SessionLengthLimiterTest::TearDown() { + session_length_limiter_.reset(); TestingBrowserProcess::GetGlobal()->SetLocalState(NULL); } +void SessionLengthLimiterTest::SetSessionUserActivitySeenPref( + bool user_activity_seen) { + local_state_.SetUserPref(prefs::kSessionUserActivitySeen, + new base::FundamentalValue(user_activity_seen)); +} + +void SessionLengthLimiterTest::ClearSessionUserActivitySeenPref() { + local_state_.ClearPref(prefs::kSessionUserActivitySeen); +} + +bool SessionLengthLimiterTest::IsSessionUserActivitySeenPrefSet() { + return local_state_.HasPrefPath(prefs::kSessionUserActivitySeen); +} + +bool SessionLengthLimiterTest::GetSessionUserActivitySeenPref() { + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + return local_state_.GetBoolean(prefs::kSessionUserActivitySeen); +} + void SessionLengthLimiterTest::SetSessionStartTimePref( - int64 session_start_time) { - local_state_.SetUserPref(prefs::kSessionStartTime, - base::Value::CreateStringValue( - base::Int64ToString(session_start_time))); + const base::TimeTicks& session_start_time) { + local_state_.SetUserPref( + prefs::kSessionStartTime, + new base::StringValue( + base::Int64ToString(session_start_time.ToInternalValue()))); +} + +void SessionLengthLimiterTest::ClearSessionStartTimePref() { + local_state_.ClearPref(prefs::kSessionStartTime); +} + +bool SessionLengthLimiterTest::IsSessionStartTimePrefSet() { + return local_state_.HasPrefPath(prefs::kSessionStartTime); } -void SessionLengthLimiterTest::VerifySessionStartTimePref() { - base::TimeTicks session_start_time(base::TimeTicks::FromInternalValue( - local_state_.GetInt64(prefs::kSessionStartTime))); - EXPECT_EQ(session_start_time_, session_start_time); +base::TimeTicks SessionLengthLimiterTest::GetSessionStartTimePref() { + EXPECT_TRUE(IsSessionStartTimePrefSet()); + return base::TimeTicks::FromInternalValue( + local_state_.GetInt64(prefs::kSessionStartTime)); } void SessionLengthLimiterTest::SetSessionLengthLimitPref( - int64 session_length_limit) { - session_end_time_ = session_start_time_ + - base::TimeDelta::FromMilliseconds(session_length_limit); - // If the new session end time has passed already, the session should end now. - if (session_end_time_ < runner_->GetCurrentTime()) - session_end_time_ = runner_->GetCurrentTime(); + const base::TimeDelta& session_length_limit) { local_state_.SetUserPref(prefs::kSessionLengthLimit, - base::Value::CreateIntegerValue( - session_length_limit)); + new base::FundamentalValue( + static_cast(session_length_limit.InMilliseconds()))); + UpdateSessionStartTimeIfWaitingForUserActivity(); +} + +void SessionLengthLimiterTest::ClearSessionLengthLimitPref() { + local_state_.RemoveUserPref(prefs::kSessionLengthLimit); + UpdateSessionStartTimeIfWaitingForUserActivity(); +} + +void SessionLengthLimiterTest::SetWaitForInitialUserActivityPref( + bool wait_for_initial_user_activity) { + UpdateSessionStartTimeIfWaitingForUserActivity(); + local_state_.SetUserPref( + prefs::kSessionWaitForInitialUserActivity, + new base::FundamentalValue(wait_for_initial_user_activity)); +} + +void SessionLengthLimiterTest::SimulateUserActivity() { + if (session_length_limiter_) + session_length_limiter_->OnUserActivity(NULL); + UpdateSessionStartTimeIfWaitingForUserActivity(); + user_activity_seen_ = true; +} + +void SessionLengthLimiterTest:: + UpdateSessionStartTimeIfWaitingForUserActivity() { + if (!user_activity_seen_ && + local_state_.GetBoolean(prefs::kSessionWaitForInitialUserActivity)) { + session_start_time_ = runner_->GetCurrentTime(); + } } void SessionLengthLimiterTest::ExpectStopSession() { Mock::VerifyAndClearExpectations(delegate_); EXPECT_CALL(*delegate_, StopSession()) - .Times(1) - .WillOnce(Invoke(this, &SessionLengthLimiterTest::CheckStopSessionTime)); + .Times(1) + .WillOnce(Invoke(this, &SessionLengthLimiterTest::SaveSessionStopTime)); } -void SessionLengthLimiterTest::CheckStopSessionTime() { - EXPECT_EQ(session_end_time_, runner_->GetCurrentTime()); +void SessionLengthLimiterTest::SaveSessionStopTime() { + session_stop_time_ = runner_->GetCurrentTime(); } void SessionLengthLimiterTest::CreateSessionLengthLimiter( bool browser_restarted) { + user_activity_seen_ = false; + session_start_time_ = runner_->GetCurrentTime(); + + EXPECT_FALSE(delegate_); + delegate_ = new NiceMock; + ON_CALL(*delegate_, GetCurrentTime()) + .WillByDefault(Invoke(runner_.get(), + &MockTimeSingleThreadTaskRunner::GetCurrentTime)); + EXPECT_CALL(*delegate_, StopSession()).Times(0); session_length_limiter_.reset( new SessionLengthLimiter(delegate_, browser_restarted)); } -// Verifies that the session start time in local state is updated during login -// if no session start time has been stored before. -TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeUnset) { + +void SessionLengthLimiterTest::DestroySessionLengthLimiter() { + session_length_limiter_.reset(); + delegate_ = NULL; +} + +// Verifies that when not instructed to wait for initial user activity, the +// session start time is set and the pref indicating user activity is cleared +// in local state during login. +TEST_F(SessionLengthLimiterTest, StartDoNotWaitForInitialUserActivity) { + // Pref indicating user activity not set. Session start time not set. + ClearSessionUserActivitySeenPref(); + ClearSessionStartTimePref(); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time not set. + SetSessionUserActivitySeenPref(true); + ClearSessionStartTimePref(); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity not set. Session start time in the future. + ClearSessionUserActivitySeenPref(); + SetSessionStartTimePref(session_start_time_ + base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time in the future. + SetSessionUserActivitySeenPref(true); + SetSessionStartTimePref(session_start_time_ + base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity not set. Session start time valid. + ClearSessionUserActivitySeenPref(); + SetSessionStartTimePref(session_start_time_ - base::TimeDelta::FromHours(2)); CreateSessionLengthLimiter(false); - VerifySessionStartTimePref(); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time valid. + SetSessionUserActivitySeenPref(true); + SetSessionStartTimePref(session_start_time_ - base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); } -// Verifies that the session start time in local state is updated during login -// if a session start time lying in the future has been stored before. -TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeFuture) { +// Verifies that when instructed to wait for initial user activity, the session +// start time and the pref indicating user activity are cleared in local state +// during login. +TEST_F(SessionLengthLimiterTest, StartWaitForInitialUserActivity) { + SetWaitForInitialUserActivityPref(true); + + // Pref indicating user activity not set. Session start time not set. + ClearSessionUserActivitySeenPref(); + ClearSessionStartTimePref(); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time not set. + SetSessionUserActivitySeenPref(true); + ClearSessionStartTimePref(); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity not set. Session start time in the future. + ClearSessionUserActivitySeenPref(); SetSessionStartTimePref( - (session_start_time_ + base::TimeDelta::FromHours(2)).ToInternalValue()); + runner_->GetCurrentTime() + base::TimeDelta::FromHours(2)); CreateSessionLengthLimiter(false); - VerifySessionStartTimePref(); -} + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); -// Verifies that the session start time in local state is updated during login -// if a valid session start time has been stored before. -TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeValid) { + // Pref indicating user activity set. Session start time in the future. + SetSessionUserActivitySeenPref(true); SetSessionStartTimePref( - (session_start_time_ - base::TimeDelta::FromHours(2)).ToInternalValue()); + runner_->GetCurrentTime() + base::TimeDelta::FromHours(2)); CreateSessionLengthLimiter(false); - VerifySessionStartTimePref(); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity not set. Session start time valid. + ClearSessionUserActivitySeenPref(); + SetSessionStartTimePref( + runner_->GetCurrentTime() - base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time valid. + SetSessionUserActivitySeenPref(true); + SetSessionStartTimePref( + runner_->GetCurrentTime() - base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); } -// Verifies that the session start time in local state is updated during restart -// after a crash if no session start time has been stored before. -TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeUnset) { +// Verifies that when not instructed to wait for initial user activity, local +// state is correctly updated during restart after a crash: +// * If no valid session start time is found in local state, the session start +// time is set and the pref indicating user activity is cleared. +// * If a valid session start time is found in local state, the session start +// time and the pref indicating user activity are *not* modified. +TEST_F(SessionLengthLimiterTest, RestartDoNotWaitForInitialUserActivity) { + // Pref indicating user activity not set. Session start time not set. + ClearSessionUserActivitySeenPref(); + ClearSessionStartTimePref(); CreateSessionLengthLimiter(true); - VerifySessionStartTimePref(); -} + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time not set. + SetSessionUserActivitySeenPref(true); + ClearSessionStartTimePref(); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity not set. Session start time in the future. + ClearSessionUserActivitySeenPref(); + SetSessionStartTimePref( + runner_->GetCurrentTime() + base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); -// Verifies that the session start time in local state is updated during restart -// after a crash if a session start time lying in the future has been stored -// before. -TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeFuture) { + // Pref indicating user activity set. Session start time in the future. + SetSessionUserActivitySeenPref(true); SetSessionStartTimePref( - (session_start_time_ + base::TimeDelta::FromHours(2)).ToInternalValue()); + runner_->GetCurrentTime() + base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + const base::TimeTicks stored_session_start_time = + runner_->GetCurrentTime() - base::TimeDelta::FromHours(2); + + // Pref indicating user activity not set. Session start time valid. + ClearSessionUserActivitySeenPref(); + SetSessionStartTimePref(stored_session_start_time); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time valid. + SetSessionUserActivitySeenPref(true); + SetSessionStartTimePref(stored_session_start_time); CreateSessionLengthLimiter(true); - VerifySessionStartTimePref(); + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + EXPECT_TRUE(GetSessionUserActivitySeenPref()); + EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); } -// Verifies that the session start time in local state is *not* updated during -// restart after a crash if a valid session start time has been stored before. -TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeValid) { - session_start_time_ -= base::TimeDelta::FromHours(2); - SetSessionStartTimePref(session_start_time_.ToInternalValue()); +// Verifies that when instructed to wait for initial user activity, local state +// is correctly updated during restart after a crash: +// * If no valid session start time is found in local state, the session start +// time and the pref indicating user activity are cleared. +// * If a valid session start time is found in local state, the session start +// time and the pref indicating user activity are *not* modified. +TEST_F(SessionLengthLimiterTest, RestartWaitForInitialUserActivity) { + SetWaitForInitialUserActivityPref(true); + + // Pref indicating user activity not set. Session start time not set. + ClearSessionUserActivitySeenPref(); + ClearSessionStartTimePref(); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time not set. + SetSessionUserActivitySeenPref(true); + ClearSessionStartTimePref(); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity not set. Session start time in the future. + ClearSessionUserActivitySeenPref(); + SetSessionStartTimePref( + runner_->GetCurrentTime() + base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time in the future. + SetSessionUserActivitySeenPref(true); + SetSessionStartTimePref( + runner_->GetCurrentTime() + base::TimeDelta::FromHours(2)); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + DestroySessionLengthLimiter(); + + const base::TimeTicks stored_session_start_time = + runner_->GetCurrentTime() - base::TimeDelta::FromHours(2); + + // Pref indicating user activity not set. Session start time valid. + ClearSessionUserActivitySeenPref(); + SetSessionStartTimePref(stored_session_start_time); + CreateSessionLengthLimiter(true); + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); + + // Pref indicating user activity set. Session start time valid. + SetSessionUserActivitySeenPref(true); + SetSessionStartTimePref(stored_session_start_time); CreateSessionLengthLimiter(true); - VerifySessionStartTimePref(); + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + EXPECT_TRUE(GetSessionUserActivitySeenPref()); + EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref()); + DestroySessionLengthLimiter(); +} + +// Verifies that local state is correctly updated when waiting for initial user +// activity is toggled and no user activity has occurred yet. +TEST_F(SessionLengthLimiterTest, ToggleWaitForInitialUserActivity) { + CreateSessionLengthLimiter(false); + + // Verify that the pref indicating user activity was not set and the session + // start time was set. + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Enable waiting for initial user activity. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); + SetWaitForInitialUserActivityPref(true); + + // Verify that the session start time was cleared and the pref indicating user + // activity was not set. + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + + // Disable waiting for initial user activity. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); + SetWaitForInitialUserActivityPref(false); + + // Verify that the pref indicating user activity was not set and the session + // start time was. + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); +} + +// Verifies that local state is correctly updated when instructed not to wait +// for initial user activity and user activity occurs. Also verifies that once +// initial user activity has occurred, neither the session start time nor the +// pref indicating user activity change in local state anymore. +TEST_F(SessionLengthLimiterTest, UserActivityWhileNotWaiting) { + CreateSessionLengthLimiter(false); + + // Verify that the pref indicating user activity was not set and the session + // start time was set. + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Simulate user activity. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); + SimulateUserActivity(); + + // Verify that the pref indicating user activity and the session start time + // were set. + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + EXPECT_TRUE(GetSessionUserActivitySeenPref()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Simulate user activity. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); + SimulateUserActivity(); + + // Verify that the pref indicating user activity and the session start time + // were not changed. + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + EXPECT_TRUE(GetSessionUserActivitySeenPref()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Enable waiting for initial user activity. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); + SetWaitForInitialUserActivityPref(true); + + // Verify that the pref indicating user activity and the session start time + // were not changed. + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + EXPECT_TRUE(GetSessionUserActivitySeenPref()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); +} + +// Verifies that local state is correctly updated when instructed to wait for +// initial user activity and user activity occurs. Also verifies that once +// initial user activity has occurred, neither the session start time nor the +// pref indicating user activity change in local state anymore. +TEST_F(SessionLengthLimiterTest, UserActivityWhileWaiting) { + SetWaitForInitialUserActivityPref(true); + + CreateSessionLengthLimiter(false); + + // Verify that the pref indicating user activity and the session start time + // were not set. + EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + + // Simulate user activity. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); + SimulateUserActivity(); + + // Verify that the pref indicating user activity and the session start time + // were set. + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + EXPECT_TRUE(GetSessionUserActivitySeenPref()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Simulate user activity. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); + SimulateUserActivity(); + + // Verify that the pref indicating user activity and the session start time + // were not changed. + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + EXPECT_TRUE(GetSessionUserActivitySeenPref()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Disable waiting for initial user activity. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); + SetWaitForInitialUserActivityPref(false); + + // Verify that the pref indicating user activity and the session start time + // were not changed. + EXPECT_TRUE(IsSessionUserActivitySeenPrefSet()); + EXPECT_TRUE(GetSessionUserActivitySeenPref()); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); } // Creates a SessionLengthLimiter without setting a limit. Verifies that the // limiter does not start a timer. -TEST_F(SessionLengthLimiterTest, RunWithoutSessionLengthLimit) { +TEST_F(SessionLengthLimiterTest, RunWithoutLimit) { + base::ThreadTaskRunnerHandle runner_handler(runner_); + + CreateSessionLengthLimiter(false); + + // Verify that no timer fires to terminate the session. + runner_->FastForwardUntilNoTasksRemain(); +} + +// Creates a SessionLengthLimiter after setting a limit and instructs it not to +// wait for user activity. Verifies that the limiter starts a timer even if no +// user activity occurs and that when the session length reaches the limit, the +// session is terminated. +TEST_F(SessionLengthLimiterTest, RunWithoutUserActivityWhileNotWaiting) { + base::ThreadTaskRunnerHandle runner_handler(runner_); + + // Set a 60 second session time limit. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60)); + + CreateSessionLengthLimiter(false); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Verify that the timer fires and the session is terminated when the session + // length limit is reached. + ExpectStopSession(); + runner_->FastForwardUntilNoTasksRemain(); + EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60), + session_stop_time_); +} + +// Creates a SessionLengthLimiter after setting a limit and instructs it to wait +// for initial user activity. Verifies that if no user activity occurs, the +// limiter does not start a timer. +TEST_F(SessionLengthLimiterTest, RunWithoutUserActivityWhileWaiting) { base::ThreadTaskRunnerHandle runner_handler(runner_); + SetWaitForInitialUserActivityPref(true); + + // Set a 60 second session time limit. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60)); - // Create a SessionLengthLimiter. CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionStartTimePrefSet()); // Verify that no timer fires to terminate the session. runner_->FastForwardUntilNoTasksRemain(); } -// Creates a SessionLengthLimiter after setting a limit. Verifies that the +// Creates a SessionLengthLimiter after setting a limit and instructs it not to +// wait for user activity. Verifies that the limiter starts a timer and that +// when the session length reaches the limit, the session is terminated. Also +// verifies that user activity does not affect the timer. +TEST_F(SessionLengthLimiterTest, RunWithUserActivityWhileNotWaiting) { + base::ThreadTaskRunnerHandle runner_handler(runner_); + + // Set a 60 second session time limit. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60)); + + CreateSessionLengthLimiter(false); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Simulate user activity after 20 seconds. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(20)); + SimulateUserActivity(); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Verify that the timer fires and the session is terminated when the session + // length limit is reached. + ExpectStopSession(); + runner_->FastForwardUntilNoTasksRemain(); + EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60), + session_stop_time_); +} + +// Creates a SessionLengthLimiter after setting a limit and instructs it to wait +// for initial user activity. Verifies that once user activity occurs, the // limiter starts a timer and that when the session length reaches the limit, -// the session is terminated. -TEST_F(SessionLengthLimiterTest, RunWithSessionLengthLimit) { +// the session is terminated. Also verifies that further user activity does not +// affect the timer. +TEST_F(SessionLengthLimiterTest, RunWithUserActivityWhileWaiting) { base::ThreadTaskRunnerHandle runner_handler(runner_); + SetWaitForInitialUserActivityPref(true); // Set a 60 second session time limit. - SetSessionLengthLimitPref(60 * 1000); // 60 seconds. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60)); - // Create a SessionLengthLimiter. CreateSessionLengthLimiter(false); + EXPECT_FALSE(IsSessionStartTimePrefSet()); + + // Simulate user activity after 20 seconds. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(20)); + SimulateUserActivity(); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); + + // Simulate user activity after 20 seconds. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(20)); + SimulateUserActivity(); + EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); // Verify that the timer fires and the session is terminated when the session // length limit is reached. ExpectStopSession(); runner_->FastForwardUntilNoTasksRemain(); + EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60), + session_stop_time_); } // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 @@ -312,22 +778,23 @@ TEST_F(SessionLengthLimiterTest, RunAndIncreaseSessionLengthLimit) { base::ThreadTaskRunnerHandle runner_handler(runner_); // Set a 60 second session time limit. - SetSessionLengthLimitPref(60 * 1000); // 60 seconds. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60)); - // Create a SessionLengthLimiter. CreateSessionLengthLimiter(false); // Fast forward the time by 50 seconds, verifying that no timer fires to // terminate the session. - runner_->FastForwardBy(50 * 1000); // 50 seconds. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(50)); // Increase the session length limit to 90 seconds. - SetSessionLengthLimitPref(90 * 1000); // 90 seconds. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(90)); // Verify that the the timer fires and the session is terminated when the // session length limit is reached. ExpectStopSession(); runner_->FastForwardUntilNoTasksRemain(); + EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(90), + session_stop_time_); } // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 @@ -339,19 +806,20 @@ TEST_F(SessionLengthLimiterTest, RunAndDecreaseSessionLengthLimit) { base::ThreadTaskRunnerHandle runner_handler(runner_); // Set a 60 second session time limit. - SetSessionLengthLimitPref(60 * 1000); // 60 seconds. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60)); - // Create a SessionLengthLimiter. CreateSessionLengthLimiter(false); // Fast forward the time by 50 seconds, verifying that no timer fires to // terminate the session. - runner_->FastForwardBy(50 * 1000); // 50 seconds. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(50)); // Verify that reducing the session length limit below the 50 seconds that // have already elapsed causes the session to be terminated immediately. ExpectStopSession(); - SetSessionLengthLimitPref(40 * 1000); // 40 seconds. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(40)); + EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(50), + session_stop_time_); } // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 @@ -362,17 +830,16 @@ TEST_F(SessionLengthLimiterTest, RunAndRemoveSessionLengthLimit) { base::ThreadTaskRunnerHandle runner_handler(runner_); // Set a 60 second session time limit. - SetSessionLengthLimitPref(60 * 1000); // 60 seconds. + SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60)); - // Create a SessionLengthLimiter. CreateSessionLengthLimiter(false); // Fast forward the time by 50 seconds, verifying that no timer fires to // terminate the session. - runner_->FastForwardBy(50 * 1000); // 50 seconds. + runner_->FastForwardBy(base::TimeDelta::FromSeconds(50)); // Remove the session length limit. - local_state_.RemoveUserPref(prefs::kSessionLengthLimit); + ClearSessionLengthLimitPref(); // Verify that no timer fires to terminate the session. runner_->FastForwardUntilNoTasksRemain(); diff --git a/chrome/browser/policy/configuration_policy_handler_list.cc b/chrome/browser/policy/configuration_policy_handler_list.cc index e108e8e9a31f..30b1489c1740 100644 --- a/chrome/browser/policy/configuration_policy_handler_list.cc +++ b/chrome/browser/policy/configuration_policy_handler_list.cc @@ -386,6 +386,9 @@ const PolicyToPreferenceMapEntry kSimplePolicyMap[] = { { key::kSessionLengthLimit, prefs::kSessionLengthLimit, Value::TYPE_INTEGER }, + { key::kWaitForInitialUserActivity, + prefs::kSessionWaitForInitialUserActivity, + Value::TYPE_BOOLEAN }, { key::kPowerManagementUsesAudioActivity, prefs::kPowerUseAudioActivity, Value::TYPE_BOOLEAN }, @@ -395,6 +398,9 @@ const PolicyToPreferenceMapEntry kSimplePolicyMap[] = { { key::kAllowScreenWakeLocks, prefs::kPowerAllowScreenWakeLocks, Value::TYPE_BOOLEAN }, + { key::kWaitForInitialUserActivity, + prefs::kPowerWaitForInitialUserActivity, + Value::TYPE_BOOLEAN }, { key::kTermsOfServiceURL, prefs::kTermsOfServiceURL, Value::TYPE_STRING }, diff --git a/chrome/browser/policy/policy_browsertest.cc b/chrome/browser/policy/policy_browsertest.cc index 95fe37c33a22..0a6b9b9f077f 100644 --- a/chrome/browser/policy/policy_browsertest.cc +++ b/chrome/browser/policy/policy_browsertest.cc @@ -161,6 +161,11 @@ namespace policy { namespace { +#if defined(OS_CHROMEOS) +const int kOneHourInMs = 60 * 60 * 1000; +const int kThreeHoursInMs = 180 * 60 * 1000; +#endif + const char kURL[] = "http://example.com"; const char kCookieValue[] = "converted=true"; // Assigned to Philip J. Fry to fix eventually. @@ -2092,7 +2097,8 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, DisableAudioOutput) { } IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_SessionLengthLimit) { - // Set the session start time to 2 hours ago. + // Indicate that the session started 2 hours ago and no user activity has + // occurred yet. g_browser_process->local_state()->SetInt64( prefs::kSessionStartTime, (base::TimeTicks::Now() - base::TimeDelta::FromHours(2)) @@ -2113,7 +2119,92 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, SessionLengthLimit) { PolicyMap policies; policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateIntegerValue(180 * 60 * 1000), // 3 hours. + base::Value::CreateIntegerValue(kThreeHoursInMs), + NULL); + UpdateProviderPolicy(policies); + base::RunLoop().RunUntilIdle(); + Mock::VerifyAndClearExpectations(&observer); + + // Decrease the session length limit to 1 hour. Verify that the session is + // terminated immediately. + EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _)); + policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, + base::Value::CreateIntegerValue(kOneHourInMs), + NULL); + UpdateProviderPolicy(policies); + base::RunLoop().RunUntilIdle(); + Mock::VerifyAndClearExpectations(&observer); +} + +IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_WaitForInitialUserActivityUsatisfied) { + // Indicate that the session started 2 hours ago and no user activity has + // occurred yet. + g_browser_process->local_state()->SetInt64( + prefs::kSessionStartTime, + (base::TimeTicks::Now() - base::TimeDelta::FromHours(2)) + .ToInternalValue()); +} + +IN_PROC_BROWSER_TEST_F(PolicyTest, WaitForInitialUserActivityUsatisfied) { + content::MockNotificationObserver observer; + content::NotificationRegistrar registrar; + registrar.Add(&observer, + chrome::NOTIFICATION_APP_TERMINATING, + content::NotificationService::AllSources()); + + // Require initial user activity. + PolicyMap policies; + policies.Set(key::kWaitForInitialUserActivity, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, + new base::FundamentalValue(true), + NULL); + UpdateProviderPolicy(policies); + base::RunLoop().RunUntilIdle(); + + // Set the session length limit to 1 hour. Verify that the session is not + // terminated. + EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _)) + .Times(0); + policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, + base::Value::CreateIntegerValue(kOneHourInMs), + NULL); + UpdateProviderPolicy(policies); + base::RunLoop().RunUntilIdle(); + Mock::VerifyAndClearExpectations(&observer); +} + +IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_WaitForInitialUserActivitySatisfied) { + // Indicate that initial user activity in this session occurred 2 hours ago. + g_browser_process->local_state()->SetInt64( + prefs::kSessionStartTime, + (base::TimeTicks::Now() - base::TimeDelta::FromHours(2)) + .ToInternalValue()); + g_browser_process->local_state()->SetBoolean( + prefs::kSessionUserActivitySeen, + true); +} + +IN_PROC_BROWSER_TEST_F(PolicyTest, WaitForInitialUserActivitySatisfied) { + content::MockNotificationObserver observer; + content::NotificationRegistrar registrar; + registrar.Add(&observer, + chrome::NOTIFICATION_APP_TERMINATING, + content::NotificationService::AllSources()); + + // Require initial user activity and set the session length limit to 3 hours. + // Verify that the session is not terminated. + EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _)) + .Times(0); + PolicyMap policies; + policies.Set(key::kWaitForInitialUserActivity, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, + new base::FundamentalValue(true), + NULL); + policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, + base::Value::CreateIntegerValue(kThreeHoursInMs), NULL); UpdateProviderPolicy(policies); base::RunLoop().RunUntilIdle(); @@ -2124,7 +2215,7 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, SessionLengthLimit) { EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _)); policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateIntegerValue(60 * 60 * 1000), // 1 hour. + base::Value::CreateIntegerValue(kOneHourInMs), NULL); UpdateProviderPolicy(policies); base::RunLoop().RunUntilIdle(); diff --git a/chrome/common/pref_names.cc b/chrome/common/pref_names.cc index 95aafa0b59f3..b2b0b8080213 100644 --- a/chrome/common/pref_names.cc +++ b/chrome/common/pref_names.cc @@ -729,10 +729,17 @@ const char kDisplayProperties[] = "settings.display.properties"; // layout/offset information. const char kSecondaryDisplays[] = "settings.display.secondary_displays"; -// A preference to keep track of the session start time. The value is set -// after login. When the browser restarts after a crash, the pref value is not -// changed unless it appears corrupted (value unset, value lying in the future, -// zero value). +// A boolean pref indicating whether user activity has been observed in the +// current session already. The pref is used to restore information about user +// activity after browser crashes. +const char kSessionUserActivitySeen[] = "session.user_activity_seen"; + +// A preference to keep track of the session start time. If the session length +// limit is configured to start running after initial user activity has been +// observed, the pref is set after the first user activity in a session. +// Otherwise, it is set immediately after session start. The pref is used to +// restore the session start time after browser crashes. The time is expressed +// as the serialization obtained from base::TimeTicks::ToInternalValue(). const char kSessionStartTime[] = "session.start_time"; // Holds the maximum session time in milliseconds. If this pref is set, the @@ -741,6 +748,11 @@ const char kSessionStartTime[] = "session.start_time"; // system tray. const char kSessionLengthLimit[] = "session.length_limit"; +// Whether the session length limit should start running only after the first +// user activity has been observed in a session. +const char kSessionWaitForInitialUserActivity[] = + "session.wait_for_initial_user_activity"; + // Inactivity time in milliseconds while the system is on AC power before // the screen should be dimmed, turned off, or locked, before an // IdleActionImminent D-Bus signal should be sent, or before @@ -793,6 +805,11 @@ const char kPowerPresentationScreenDimDelayFactor[] = const char kPowerUserActivityScreenDimDelayFactor[] = "power.user_activity_screen_dim_delay_factor"; +// Whether the power management delays should start running only after the first +// user activity has been observed in a session. +const char kPowerWaitForInitialUserActivity[] = + "power.wait_for_initial_user_activity"; + // The URL from which the Terms of Service can be downloaded. The value is only // honored for public accounts. const char kTermsOfServiceURL[] = "terms_of_service.url"; diff --git a/chrome/common/pref_names.h b/chrome/common/pref_names.h index 7348231b6c0b..5794b08feef8 100644 --- a/chrome/common/pref_names.h +++ b/chrome/common/pref_names.h @@ -242,8 +242,10 @@ extern const char kUseSharedProxies[]; extern const char kDisplayPowerState[]; extern const char kDisplayProperties[]; extern const char kSecondaryDisplays[]; +extern const char kSessionUserActivitySeen[]; extern const char kSessionStartTime[]; extern const char kSessionLengthLimit[]; +extern const char kSessionWaitForInitialUserActivity[]; extern const char kPowerAcScreenDimDelayMs[]; extern const char kPowerAcScreenOffDelayMs[]; extern const char kPowerAcScreenLockDelayMs[]; @@ -262,6 +264,7 @@ extern const char kPowerUseVideoActivity[]; extern const char kPowerAllowScreenWakeLocks[]; extern const char kPowerPresentationScreenDimDelayFactor[]; extern const char kPowerUserActivityScreenDimDelayFactor[]; +extern const char kPowerWaitForInitialUserActivity[]; extern const char kTermsOfServiceURL[]; extern const char kUsedPolicyCertificatesOnce[]; extern const char kAttestationEnabled[]; diff --git a/chrome/test/data/policy/policy_test_cases.json b/chrome/test/data/policy/policy_test_cases.json index fe474328ba3f..78458ded9e41 100644 --- a/chrome/test/data/policy/policy_test_cases.json +++ b/chrome/test/data/policy/policy_test_cases.json @@ -1822,6 +1822,16 @@ ] }, + "WaitForInitialUserActivity": { + "os": ["chromeos"], + "test_policy": { "WaitForInitialUserActivity": true }, + "pref_mappings": [ + { "pref": "session.wait_for_initial_user_activity", + "local_state": true }, + { "pref": "power.wait_for_initial_user_activity" } + ] + }, + "TermsOfServiceURL": { "os": ["chromeos"], "test_policy": { "TermsOfServiceURL": "http://www.example.com/terms_of_service.txt" }, -- 2.11.4.GIT