Commit b642922e authored by bartfab@chromium.org's avatar bartfab@chromium.org

Convert SessionLengthLimiter to OneShotTimer

This CL converts the SessionLengthLimiter from a RepeatingTimer that
fires once per second to a OneShotTimer that fires when the session
time is up and the user should be logged out.

BUG=175797
TEST=Manual, unittests


Review URL: https://chromiumcodereview.appspot.com/12218123

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@182214 0039d316-1c4b-4281-b951-d872f2087c98
parent 20ddac71
......@@ -27,10 +27,6 @@ const int kSessionLengthLimitMinMs = 30 * 1000; // 30 seconds.
// The maximum session time limit that can be set.
const int kSessionLengthLimitMaxMs = 24 * 60 * 60 * 1000; // 24 hours.
// The interval at which to fire periodic callbacks and check whether the
// session time limit has been reached.
const int kSessionLengthLimitTimerIntervalMs = 1000;
// A default delegate implementation that returns the current time and does end
// the current user's session when requested. This can be replaced with a mock
// in tests.
......@@ -98,6 +94,8 @@ SessionLengthLimiter::SessionLengthLimiter(Delegate* delegate,
prefs::kSessionLengthLimit,
base::Bind(&SessionLengthLimiter::OnSessionLengthLimitChanged,
base::Unretained(this)));
// Handle the current session length limit, if any.
OnSessionLengthLimitChanged();
}
......@@ -106,61 +104,43 @@ SessionLengthLimiter::~SessionLengthLimiter() {
void SessionLengthLimiter::OnSessionLengthLimitChanged() {
DCHECK(thread_checker_.CalledOnValidThread());
// Stop any currently running timer.
if (timer_)
timer_->Stop();
int limit;
const PrefServiceBase::Preference* session_length_limit_pref =
pref_change_registrar_.prefs()->
FindPreference(prefs::kSessionLengthLimit);
// If no session length limit is set, stop the timer.
if (session_length_limit_pref->IsDefaultValue() ||
!session_length_limit_pref->GetValue()->GetAsInteger(&limit)) {
session_length_limit_ = base::TimeDelta();
StopTimer();
// If no session length limit is set, destroy the timer.
timer_.reset();
return;
}
// If a session length limit is set, clamp it to the valid range and start
// the timer.
session_length_limit_ = base::TimeDelta::FromMilliseconds(
std::min(std::max(limit, kSessionLengthLimitMinMs),
kSessionLengthLimitMaxMs));
StartTimer();
}
void SessionLengthLimiter::StartTimer() {
if (repeating_timer_ && repeating_timer_->IsRunning())
return;
if (!repeating_timer_)
repeating_timer_.reset(new base::RepeatingTimer<SessionLengthLimiter>);
repeating_timer_->Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kSessionLengthLimitTimerIntervalMs),
this,
&SessionLengthLimiter::UpdateRemainingTime);
}
void SessionLengthLimiter::StopTimer() {
if (!repeating_timer_)
return;
repeating_timer_.reset();
UpdateRemainingTime();
}
void SessionLengthLimiter::UpdateRemainingTime() {
const base::TimeDelta kZeroTimeDelta = base::TimeDelta();
// If no session length limit is set, return.
if (session_length_limit_ == kZeroTimeDelta)
return;
// Clamp the session length limit to the valid range.
const base::TimeDelta session_length_limit =
base::TimeDelta::FromMilliseconds(std::min(std::max(
limit, kSessionLengthLimitMinMs), kSessionLengthLimitMaxMs));
// Calculate the remaining session time, clamping so that it never falls below
// zero.
base::TimeDelta remaining = session_length_limit_ -
// Calculate the remaining session time.
const base::TimeDelta remaining = session_length_limit -
(delegate_->GetCurrentTime() - session_start_time_);
if (remaining < kZeroTimeDelta)
remaining = kZeroTimeDelta;
// End the session if the remaining time reaches zero.
if (remaining == base::TimeDelta())
// Log out the user immediately if the session length limit has been reached
// or exceeded.
if (remaining <= base::TimeDelta()) {
delegate_->StopSession();
return;
}
// Set a timer to log out the user when the session length limit is reached.
if (!timer_)
timer_.reset(new base::OneShotTimer<SessionLengthLimiter::Delegate>);
timer_->Start(FROM_HERE, remaining, delegate_.get(),
&SessionLengthLimiter::Delegate::StopSession);
}
} // namespace chromeos
......@@ -38,25 +38,13 @@ class SessionLengthLimiter {
private:
void OnSessionLengthLimitChanged();
// Starts a timer that periodically checks whether the remaining time has
// reached zero.
void StartTimer();
// Stops the timer.
void StopTimer();
// Updates the remaining time, and terminates the session when the time
// reaches zero.
void UpdateRemainingTime();
base::ThreadChecker thread_checker_;
scoped_ptr<Delegate> delegate_;
PrefChangeRegistrar pref_change_registrar_;
scoped_ptr<base::RepeatingTimer<SessionLengthLimiter> > repeating_timer_;
scoped_ptr<base::OneShotTimer<SessionLengthLimiter::Delegate> > timer_;
base::Time session_start_time_;
base::TimeDelta session_length_limit_;
DISALLOW_COPY_AND_ASSIGN(SessionLengthLimiter);
};
......
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