Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(919)

Unified Diff: chrome/browser/chromeos/power/session_length_limiter.cc

Issue 12218123: Convert SessionLengthLimiter to OneShotTimer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/power/session_length_limiter.cc
diff --git a/chrome/browser/chromeos/power/session_length_limiter.cc b/chrome/browser/chromeos/power/session_length_limiter.cc
index 59a6267b3e3cdaaaab27a5d879eb9ff0d7246fee..c9920a64cd0bfb81c63203449b2467edfac98c08 100644
--- a/chrome/browser/chromeos/power/session_length_limiter.cc
+++ b/chrome/browser/chromeos/power/session_length_limiter.cc
@@ -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();
Daniel Erat 2013/02/12 20:40:43 what's the reason to destroy the timer here?
bartfab (slow) 2013/02/13 10:32:59 This simply frees up the memory that the timer was
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

Powered by Google App Engine
This is Rietveld 408576698