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

Side by Side Diff: chrome/browser/chromeos/session_length_limiter.cc

Issue 11499012: Add policy for limiting the session length (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Not sure why the patch is not applying - bot source out of sync with ToT? Created 8 years 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/session_length_limiter.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/prefs/public/pref_service_base.h"
14 #include "chrome/browser/chromeos/login/user_manager.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/common/pref_names.h"
18
19 namespace chromeos {
20
21 namespace {
22
23 // The minimum session time limit that can be set.
24 const int kSessionLengthLimitMinMs = 30 * 1000; // 30 seconds.
25
26 // The maximum session time limit that can be set.
27 const int kSessionLengthLimitMaxMs = 24 * 60 * 60 * 1000; // 24 hours.
28
29 // The interval at which to fire periodic callbacks and check whether the
30 // session time limit has been reached.
31 const int kSessionLengthLimitTimerIntervalMs = 1000;
32
33 // A default delegate implementation that returns the current time and does log
34 // out the current user when requested. This can be replaced with a mock in
35 // tests.
36 class SessionLengthLimiterDelegateImpl : public SessionLengthLimiter::Delegate {
37 public:
38 SessionLengthLimiterDelegateImpl();
39 virtual ~SessionLengthLimiterDelegateImpl();
40
41 virtual const base::Time GetCurrentTime() const;
42 virtual void Logout();
43
44 private:
45 DISALLOW_COPY_AND_ASSIGN(SessionLengthLimiterDelegateImpl);
46 };
47
48 SessionLengthLimiterDelegateImpl::SessionLengthLimiterDelegateImpl() {
49 }
50
51 SessionLengthLimiterDelegateImpl::~SessionLengthLimiterDelegateImpl() {
52 }
53
54 const base::Time SessionLengthLimiterDelegateImpl::GetCurrentTime() const {
55 return base::Time::Now();
56 }
57
58 void SessionLengthLimiterDelegateImpl::Logout() {
59 browser::AttemptUserExit();
60 }
61
62 } // namespace
63
64 SessionLengthLimiter::Observer::~Observer() {
65 }
66
67 SessionLengthLimiter::Delegate::~Delegate() {
68 }
69
70 SessionLengthLimiter::SessionLengthLimiter(Delegate* delegate,
71 PrefService* prefs)
72 : delegate_(delegate ? delegate : new SessionLengthLimiterDelegateImpl) {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 // Update the session start time in prefs to the current time when going
75 // through a user login. If the browser is restarting after a crash instead,
76 // only update the value if it appears corrupted (value unset, value lying in
77 // the future, zero value).
78 int64 session_start_time = prefs->GetInt64(prefs::kSessionStartTime);
79 int64 now = delegate_->GetCurrentTime().ToInternalValue();
80 if (!UserManager::Get()->HasBrowserRestarted() ||
81 session_start_time <= 0 || session_start_time > now) {
82 prefs->SetInt64(prefs::kSessionStartTime, now);
83 session_start_time = now;
84 }
85 session_start_time_ = base::Time::FromInternalValue(session_start_time);
86
87 // Listen for changes to the session length limit.
88 pref_change_registrar_.Init(prefs);
89 pref_change_registrar_.Add(
90 prefs::kSessionLengthLimit,
91 base::Bind(&SessionLengthLimiter::OnSessionLengthLimitChanged,
92 base::Unretained(this)));
93 OnSessionLengthLimitChanged();
94 }
95
96 SessionLengthLimiter::~SessionLengthLimiter() {
97 }
98
99 void SessionLengthLimiter::Shutdown() {
100 pref_change_registrar_.RemoveAll();
Mattias Nissler (ping if slow) 2012/12/12 10:12:57 not needed AFAICS
bartfab (slow) 2012/12/13 16:22:48 Correct. I only did this because other PKS do it,
101 }
102
103 void SessionLengthLimiter::AddObserver(Observer* observer) {
104 DCHECK(thread_checker_.CalledOnValidThread());
105 observers_.AddObserver(observer);
106 observer->SessionTimeRemainingChanged(GetRemainingTime().get());
107 }
108
109 void SessionLengthLimiter::RemoveObserver(Observer* observer) {
110 DCHECK(thread_checker_.CalledOnValidThread());
111 observers_.RemoveObserver(observer);
112 }
113
114 void SessionLengthLimiter::OnSessionLengthLimitChanged() {
115 DCHECK(thread_checker_.CalledOnValidThread());
116 int limit;
117 const PrefServiceBase::Preference* session_length_limit_pref =
118 pref_change_registrar_.prefs()->
119 FindPreference(prefs::kSessionLengthLimit);
120 // If no session length limit is set, stop the timer, calling the observers
121 // one last time to notify them that there is no more limit.
122 if (session_length_limit_pref->IsDefaultValue() ||
123 !session_length_limit_pref->GetValue()->GetAsInteger(&limit)) {
124 session_length_limit_ = base::TimeDelta();
125 StopTimer();
126 return;
127 }
128 // If a session length limit is set, clamp it to the valid range and start
129 // the timer after calling the observers immediately to notify them that there
130 // now is a limit.
131 session_length_limit_ = base::TimeDelta::FromMilliseconds(
132 std::min(std::max(limit, kSessionLengthLimitMinMs),
133 kSessionLengthLimitMaxMs));
134 StartTimer();
135 }
136
137 void SessionLengthLimiter::StartTimer() {
138 TimerTick();
139 if (repeating_timer_.IsRunning())
140 return;
141 repeating_timer_.Start(
142 FROM_HERE,
143 base::TimeDelta::FromMilliseconds(kSessionLengthLimitTimerIntervalMs),
144 this,
145 &SessionLengthLimiter::TimerTick);
146 }
147
148 void SessionLengthLimiter::StopTimer() {
149 if (repeating_timer_.IsRunning())
150 repeating_timer_.Stop();
151 TimerTick();
152 }
153
154 void SessionLengthLimiter::TimerTick() {
155 scoped_ptr<base::TimeDelta> remaining = GetRemainingTime();
156 FOR_EACH_OBSERVER(Observer, observers_,
157 SessionTimeRemainingChanged(remaining.get()));
158 // End the session if a limit is set and the remaining time reaches zero.
159 if (remaining && *remaining == base::TimeDelta())
160 delegate_->Logout();
161 }
162
163 scoped_ptr<base::TimeDelta> SessionLengthLimiter::GetRemainingTime() const {
Mattias Nissler (ping if slow) 2012/12/12 10:12:57 Wrapping in a scoped_ptr for returning an optional
bartfab (slow) 2012/12/13 16:22:48 Done.
164 // If no limit is set, return |NULL|.
165 const base::TimeDelta kZeroTimeDelta = base::TimeDelta();
166 if (session_length_limit_ == kZeroTimeDelta)
167 return scoped_ptr<base::TimeDelta>();
168 // If a limit is set, return the remaining time, clamped so that it never
169 // falls below zero.
170 base::TimeDelta remaining = session_length_limit_ -
171 (delegate_->GetCurrentTime() - session_start_time_);
172 if (remaining < kZeroTimeDelta)
173 remaining = kZeroTimeDelta;
174 return scoped_ptr<base::TimeDelta>(new base::TimeDelta(remaining));
175 }
176
177 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698