OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/power/session_length_limiter.h" | 5 #include "chrome/browser/chromeos/power/session_length_limiter.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/prefs/pref_registry_simple.h" | 13 #include "base/prefs/pref_registry_simple.h" |
14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
15 #include "base/prefs/public/pref_service_base.h" | 15 #include "base/prefs/public/pref_service_base.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/lifetime/application_lifetime.h" | 17 #include "chrome/browser/lifetime/application_lifetime.h" |
18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
19 | 19 |
20 namespace chromeos { | 20 namespace chromeos { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // The minimum session time limit that can be set. | 24 // The minimum session time limit that can be set. |
25 const int kSessionLengthLimitMinMs = 30 * 1000; // 30 seconds. | 25 const int kSessionLengthLimitMinMs = 30 * 1000; // 30 seconds. |
26 | 26 |
27 // The maximum session time limit that can be set. | 27 // The maximum session time limit that can be set. |
28 const int kSessionLengthLimitMaxMs = 24 * 60 * 60 * 1000; // 24 hours. | 28 const int kSessionLengthLimitMaxMs = 24 * 60 * 60 * 1000; // 24 hours. |
29 | 29 |
30 // The interval at which to fire periodic callbacks and check whether the | |
31 // session time limit has been reached. | |
32 const int kSessionLengthLimitTimerIntervalMs = 1000; | |
33 | |
34 // A default delegate implementation that returns the current time and does end | 30 // A default delegate implementation that returns the current time and does end |
35 // the current user's session when requested. This can be replaced with a mock | 31 // the current user's session when requested. This can be replaced with a mock |
36 // in tests. | 32 // in tests. |
37 class SessionLengthLimiterDelegateImpl : public SessionLengthLimiter::Delegate { | 33 class SessionLengthLimiterDelegateImpl : public SessionLengthLimiter::Delegate { |
38 public: | 34 public: |
39 SessionLengthLimiterDelegateImpl(); | 35 SessionLengthLimiterDelegateImpl(); |
40 virtual ~SessionLengthLimiterDelegateImpl(); | 36 virtual ~SessionLengthLimiterDelegateImpl(); |
41 | 37 |
42 virtual const base::Time GetCurrentTime() const OVERRIDE; | 38 virtual const base::Time GetCurrentTime() const OVERRIDE; |
43 virtual void StopSession() OVERRIDE; | 39 virtual void StopSession() OVERRIDE; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 session_start_time = now; | 87 session_start_time = now; |
92 } | 88 } |
93 session_start_time_ = base::Time::FromInternalValue(session_start_time); | 89 session_start_time_ = base::Time::FromInternalValue(session_start_time); |
94 | 90 |
95 // Listen for changes to the session length limit. | 91 // Listen for changes to the session length limit. |
96 pref_change_registrar_.Init(local_state); | 92 pref_change_registrar_.Init(local_state); |
97 pref_change_registrar_.Add( | 93 pref_change_registrar_.Add( |
98 prefs::kSessionLengthLimit, | 94 prefs::kSessionLengthLimit, |
99 base::Bind(&SessionLengthLimiter::OnSessionLengthLimitChanged, | 95 base::Bind(&SessionLengthLimiter::OnSessionLengthLimitChanged, |
100 base::Unretained(this))); | 96 base::Unretained(this))); |
97 | |
98 // Handle the current session length limit, if any. | |
101 OnSessionLengthLimitChanged(); | 99 OnSessionLengthLimitChanged(); |
102 } | 100 } |
103 | 101 |
104 SessionLengthLimiter::~SessionLengthLimiter() { | 102 SessionLengthLimiter::~SessionLengthLimiter() { |
105 } | 103 } |
106 | 104 |
107 void SessionLengthLimiter::OnSessionLengthLimitChanged() { | 105 void SessionLengthLimiter::OnSessionLengthLimitChanged() { |
108 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
107 | |
108 // Stop any currently running timer. | |
109 if (timer_) | |
110 timer_->Stop(); | |
111 | |
109 int limit; | 112 int limit; |
110 const PrefServiceBase::Preference* session_length_limit_pref = | 113 const PrefServiceBase::Preference* session_length_limit_pref = |
111 pref_change_registrar_.prefs()-> | 114 pref_change_registrar_.prefs()-> |
112 FindPreference(prefs::kSessionLengthLimit); | 115 FindPreference(prefs::kSessionLengthLimit); |
113 // If no session length limit is set, stop the timer. | |
114 if (session_length_limit_pref->IsDefaultValue() || | 116 if (session_length_limit_pref->IsDefaultValue() || |
115 !session_length_limit_pref->GetValue()->GetAsInteger(&limit)) { | 117 !session_length_limit_pref->GetValue()->GetAsInteger(&limit)) { |
116 session_length_limit_ = base::TimeDelta(); | 118 // If no session length limit is set, destroy the timer. |
117 StopTimer(); | 119 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
| |
118 return; | 120 return; |
119 } | 121 } |
120 | 122 |
121 // If a session length limit is set, clamp it to the valid range and start | 123 // Clamp the session length limit to the valid range. |
122 // the timer. | 124 const base::TimeDelta session_length_limit = |
123 session_length_limit_ = base::TimeDelta::FromMilliseconds( | 125 base::TimeDelta::FromMilliseconds(std::min(std::max( |
124 std::min(std::max(limit, kSessionLengthLimitMinMs), | 126 limit, kSessionLengthLimitMinMs), kSessionLengthLimitMaxMs)); |
125 kSessionLengthLimitMaxMs)); | |
126 StartTimer(); | |
127 } | |
128 | 127 |
129 void SessionLengthLimiter::StartTimer() { | 128 // Calculate the remaining session time. |
130 if (repeating_timer_ && repeating_timer_->IsRunning()) | 129 const base::TimeDelta remaining = session_length_limit - |
130 (delegate_->GetCurrentTime() - session_start_time_); | |
131 | |
132 // Log out the user immediately if the session length limit has been reached | |
133 // or exceeded. | |
134 if (remaining <= base::TimeDelta()) { | |
135 delegate_->StopSession(); | |
131 return; | 136 return; |
132 if (!repeating_timer_) | 137 } |
133 repeating_timer_.reset(new base::RepeatingTimer<SessionLengthLimiter>); | |
134 repeating_timer_->Start( | |
135 FROM_HERE, | |
136 base::TimeDelta::FromMilliseconds(kSessionLengthLimitTimerIntervalMs), | |
137 this, | |
138 &SessionLengthLimiter::UpdateRemainingTime); | |
139 } | |
140 | 138 |
141 void SessionLengthLimiter::StopTimer() { | 139 // Set a timer to log out the user when the session length limit is reached. |
142 if (!repeating_timer_) | 140 if (!timer_) |
143 return; | 141 timer_.reset(new base::OneShotTimer<SessionLengthLimiter::Delegate>); |
144 repeating_timer_.reset(); | 142 timer_->Start(FROM_HERE, remaining, delegate_.get(), |
145 UpdateRemainingTime(); | 143 &SessionLengthLimiter::Delegate::StopSession); |
146 } | |
147 | |
148 void SessionLengthLimiter::UpdateRemainingTime() { | |
149 const base::TimeDelta kZeroTimeDelta = base::TimeDelta(); | |
150 // If no session length limit is set, return. | |
151 if (session_length_limit_ == kZeroTimeDelta) | |
152 return; | |
153 | |
154 // Calculate the remaining session time, clamping so that it never falls below | |
155 // zero. | |
156 base::TimeDelta remaining = session_length_limit_ - | |
157 (delegate_->GetCurrentTime() - session_start_time_); | |
158 if (remaining < kZeroTimeDelta) | |
159 remaining = kZeroTimeDelta; | |
160 | |
161 // End the session if the remaining time reaches zero. | |
162 if (remaining == base::TimeDelta()) | |
163 delegate_->StopSession(); | |
164 } | 144 } |
165 | 145 |
166 } // namespace chromeos | 146 } // namespace chromeos |
OLD | NEW |