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

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: Upload flaked. 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/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 end
34 // the current user's session when requested. This can be replaced with a mock
35 // in 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 StopSession();
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::StopSession() {
59 browser::AttemptUserExit();
60 }
61
62 } // namespace
63
64 SessionLengthLimiter::Delegate::~Delegate() {
65 }
66
67 // static
68 void SessionLengthLimiter::RegisterPrefs(PrefService* local_state) {
69 local_state->RegisterInt64Pref(prefs::kSessionStartTime,
70 0,
71 PrefService::UNSYNCABLE_PREF);
72 local_state->RegisterIntegerPref(prefs::kSessionLengthLimit,
73 0,
74 PrefService::UNSYNCABLE_PREF);
75 }
76
77 SessionLengthLimiter::SessionLengthLimiter(Delegate* delegate,
78 bool browser_restarted)
79 : delegate_(delegate ? delegate : new SessionLengthLimiterDelegateImpl) {
80 DCHECK(thread_checker_.CalledOnValidThread());
81
82 // If this is a user login, set the session start time in local state to the
83 // current time. If this a browser restart after a crash, set the session
84 // start time only if its current value appears corrupted (value unset, value
85 // lying in the future, zero value).
86 PrefService* local_state = g_browser_process->local_state();
87 int64 session_start_time = local_state->GetInt64(prefs::kSessionStartTime);
88 int64 now = delegate_->GetCurrentTime().ToInternalValue();
89 if (!browser_restarted ||
90 session_start_time <= 0 || session_start_time > now) {
91 local_state->SetInt64(prefs::kSessionStartTime, now);
92 // Ensure that the session start time is persisted to local state.
93 local_state->CommitPendingWrite();
94 session_start_time = now;
95 }
96 session_start_time_ = base::Time::FromInternalValue(session_start_time);
97
98 // Listen for changes to the session length limit.
99 pref_change_registrar_.Init(local_state);
100 pref_change_registrar_.Add(
101 prefs::kSessionLengthLimit,
102 base::Bind(&SessionLengthLimiter::OnSessionLengthLimitChanged,
103 base::Unretained(this)));
104 OnSessionLengthLimitChanged();
105 }
106
107 void SessionLengthLimiter::OnSessionLengthLimitChanged() {
108 DCHECK(thread_checker_.CalledOnValidThread());
109 int limit;
110 const PrefServiceBase::Preference* session_length_limit_pref =
111 pref_change_registrar_.prefs()->
112 FindPreference(prefs::kSessionLengthLimit);
113 // If no session length limit is set, stop the timer.
114 if (session_length_limit_pref->IsDefaultValue() ||
115 !session_length_limit_pref->GetValue()->GetAsInteger(&limit)) {
116 session_length_limit_ = base::TimeDelta();
117 StopTimer();
118 return;
119 }
120
121 // If a session length limit is set, clamp it to the valid range and start
122 // the timer.
123 session_length_limit_ = base::TimeDelta::FromMilliseconds(
124 std::min(std::max(limit, kSessionLengthLimitMinMs),
125 kSessionLengthLimitMaxMs));
126 StartTimer();
127 }
128
129 void SessionLengthLimiter::StartTimer() {
130 if (repeating_timer_ && repeating_timer_->IsRunning())
131 return;
132 if (!repeating_timer_)
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
141 void SessionLengthLimiter::StopTimer() {
142 if (!repeating_timer_)
143 return;
144 repeating_timer_.reset();
145 UpdateRemainingTime();
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 }
165
166 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698