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

Side by Side Diff: chrome/browser/chromeos/power/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: Fix include order. 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/power/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/browser_process.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/pref_names.h"
19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h"
22
23 namespace chromeos {
24
25 namespace {
26
27 // The minimum session time limit that can be set.
28 const int kSessionLengthLimitMinMs = 30 * 1000; // 30 seconds.
29
30 // The maximum session time limit that can be set.
31 const int kSessionLengthLimitMaxMs = 24 * 60 * 60 * 1000; // 24 hours.
Nikita (slow) 2012/12/14 13:07:43 Generic questions: I think you ignore time being s
bartfab (slow) 2012/12/14 17:23:26 No. The logout is based on wall time since login.
32
33 // The interval at which to fire periodic callbacks and check whether the
34 // session time limit has been reached.
35 const int kSessionLengthLimitTimerIntervalMs = 1000;
Nikita (slow) 2012/12/14 13:07:43 Seems to often, no?
bartfab (slow) 2012/12/14 17:23:26 This is so that the UI can be updated once a secon
36
37 // A default delegate implementation that returns the current time and does end
38 // the current user's session when requested. This can be replaced with a mock
39 // in tests.
40 class SessionLengthLimiterDelegateImpl : public SessionLengthLimiter::Delegate {
41 public:
42 SessionLengthLimiterDelegateImpl();
43 virtual ~SessionLengthLimiterDelegateImpl();
44
45 virtual const base::Time GetCurrentTime() const;
46 virtual void StopSession();
47
48 private:
49 DISALLOW_COPY_AND_ASSIGN(SessionLengthLimiterDelegateImpl);
50 };
51
52 SessionLengthLimiterDelegateImpl::SessionLengthLimiterDelegateImpl() {
53 }
54
55 SessionLengthLimiterDelegateImpl::~SessionLengthLimiterDelegateImpl() {
56 }
57
58 const base::Time SessionLengthLimiterDelegateImpl::GetCurrentTime() const {
59 return base::Time::Now();
60 }
61
62 void SessionLengthLimiterDelegateImpl::StopSession() {
63 browser::AttemptUserExit();
64 }
65
66 } // namespace
67
68 SessionLengthLimiter::Delegate::~Delegate() {
69 }
70
71 // static
72 void SessionLengthLimiter::RegisterPrefs(PrefService* local_state) {
73 local_state->RegisterInt64Pref(prefs::kSessionStartTime,
74 0,
75 PrefService::UNSYNCABLE_PREF);
76 local_state->RegisterIntegerPref(prefs::kSessionLengthLimit,
77 0,
78 PrefService::UNSYNCABLE_PREF);
79 }
80
81 SessionLengthLimiter::SessionLengthLimiter(Delegate* delegate,
82 bool browser_restarted)
83 : delegate_(delegate ? delegate : new SessionLengthLimiterDelegateImpl) {
84 DCHECK(thread_checker_.CalledOnValidThread());
85 // If this is a user login, set the session start time in local state to the
Nikita (slow) 2012/12/14 13:07:43 nit: Insert empty line before comment to make it m
bartfab (slow) 2012/12/14 17:23:26 Done.
86 // current time. If this a browser restart after a crash, set the session
87 // start time only if its current value appears corrupted (value unset, value
88 // lying in the future, zero value).
89 PrefService* local_state = g_browser_process->local_state();
90 int64 session_start_time = local_state->GetInt64(prefs::kSessionStartTime);
91 int64 now = delegate_->GetCurrentTime().ToInternalValue();
92 if (!browser_restarted ||
93 session_start_time <= 0 || session_start_time > now) {
94 local_state->SetInt64(prefs::kSessionStartTime, now);
95 // Ensure that the session start time is persisted to local state.
96 local_state->CommitPendingWrite();
97 session_start_time = now;
98 }
99 session_start_time_ = base::Time::FromInternalValue(session_start_time);
100
101 // Listen for changes to the session length limit.
102 pref_change_registrar_.Init(local_state);
103 pref_change_registrar_.Add(
104 prefs::kSessionLengthLimit,
105 base::Bind(&SessionLengthLimiter::OnSessionLengthLimitChanged,
106 base::Unretained(this)));
107 OnSessionLengthLimitChanged();
108 }
109
110 void SessionLengthLimiter::OnSessionLengthLimitChanged() {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 int limit;
113 const PrefServiceBase::Preference* session_length_limit_pref =
114 pref_change_registrar_.prefs()->
115 FindPreference(prefs::kSessionLengthLimit);
116 // If no session length limit is set, stop the timer.
117 if (session_length_limit_pref->IsDefaultValue() ||
118 !session_length_limit_pref->GetValue()->GetAsInteger(&limit)) {
119 session_length_limit_ = base::TimeDelta();
120 StopTimer();
121 return;
122 }
123
124 // If a session length limit is set, clamp it to the valid range and start
125 // the timer.
126 session_length_limit_ = base::TimeDelta::FromMilliseconds(
127 std::min(std::max(limit, kSessionLengthLimitMinMs),
128 kSessionLengthLimitMaxMs));
129 StartTimer();
130 }
131
132 void SessionLengthLimiter::StartTimer() {
133 if (repeating_timer_ && repeating_timer_->IsRunning())
134 return;
135 if (!repeating_timer_)
136 repeating_timer_.reset(new base::RepeatingTimer<SessionLengthLimiter>);
137 repeating_timer_->Start(
138 FROM_HERE,
139 base::TimeDelta::FromMilliseconds(kSessionLengthLimitTimerIntervalMs),
140 this,
141 &SessionLengthLimiter::UpdateRemainingTime);
142 }
143
144 void SessionLengthLimiter::StopTimer() {
145 if (!repeating_timer_)
146 return;
147 repeating_timer_.reset();
148 UpdateRemainingTime();
149 }
150
151 void SessionLengthLimiter::UpdateRemainingTime() {
152 const base::TimeDelta kZeroTimeDelta = base::TimeDelta();
153 // If no session length limit is set, send out a corresponding notification.
154 if (session_length_limit_ == kZeroTimeDelta) {
155 content::NotificationService::current()->Notify(
156 chrome::NOTIFICATION_SESSION_LENGTH_UNLIMITED,
157 content::Source<SessionLengthLimiter>(this),
158 content::NotificationService::NoDetails());
159 return;
160 }
161
162 // Calculate the remaining session time, clamping so that it never falls below
163 // zero.
164 base::TimeDelta remaining = session_length_limit_ -
165 (delegate_->GetCurrentTime() - session_start_time_);
166 if (remaining < kZeroTimeDelta)
167 remaining = kZeroTimeDelta;
168
169 content::NotificationService::current()->Notify(
170 chrome::NOTIFICATION_REMAINING_SESSION_TIME_CHANGED,
171 content::Source<SessionLengthLimiter>(this),
172 content::Details<const base::TimeDelta>(&remaining));
173
174 // End the session if the remaining time reaches zero.
175 if (remaining == base::TimeDelta())
176 delegate_->StopSession();
177 }
178
179 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698