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.
5 #include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_idle_logout.h"
8 #include "ash/wm/user_activity_detector.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h"
15 #include "chrome/browser/chromeos/login/user_manager.h"
16 #include "chrome/browser/chromeos/ui/idle_logout_dialog_view.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/notification_service.h"
25 static base::LazyInstance
<KioskModeIdleLogout
>
26 g_kiosk_mode_idle_logout
= LAZY_INSTANCE_INITIALIZER
;
31 void KioskModeIdleLogout::Initialize() {
32 g_kiosk_mode_idle_logout
.Get();
35 KioskModeIdleLogout::KioskModeIdleLogout() {
36 if (KioskModeSettings::Get()->is_initialized()) {
39 KioskModeSettings::Get()->Initialize(base::Bind(&KioskModeIdleLogout::Setup
,
40 base::Unretained(this)));
44 KioskModeIdleLogout::~KioskModeIdleLogout() {
45 if (ash::Shell::HasInstance() &&
46 ash::Shell::GetInstance()->user_activity_detector()->HasObserver(this))
47 ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this);
50 void KioskModeIdleLogout::Setup() {
51 if (UserManager::Get()->IsLoggedInAsDemoUser()) {
52 // This means that we're recovering from a crash. The user is already
53 // logged in, so go ahead and start the timer.
56 registrar_
.Add(this, chrome::NOTIFICATION_LOGIN_USER_CHANGED
,
57 content::NotificationService::AllSources());
61 void KioskModeIdleLogout::Observe(
63 const content::NotificationSource
& source
,
64 const content::NotificationDetails
& details
) {
65 if (type
== chrome::NOTIFICATION_LOGIN_USER_CHANGED
) {
67 registrar_
.RemoveAll();
71 void KioskModeIdleLogout::OnUserActivity(const ui::Event
* event
) {
72 IdleLogoutDialogView::CloseDialog();
76 void KioskModeIdleLogout::Start() {
77 if (!ash::Shell::GetInstance()->user_activity_detector()->HasObserver(this))
78 ash::Shell::GetInstance()->user_activity_detector()->AddObserver(this);
82 void KioskModeIdleLogout::ResetTimer() {
83 if (timer_
.IsRunning()) {
86 // OneShotTimer destroys the posted task after running it, so Reset()
87 // isn't safe to call on a timer that's already fired.
88 timer_
.Start(FROM_HERE
, KioskModeSettings::Get()->GetIdleLogoutTimeout(),
89 base::Bind(&KioskModeIdleLogout::OnTimeout
,
90 base::Unretained(this)));
94 void KioskModeIdleLogout::OnTimeout() {
95 IdleLogoutDialogView::ShowDialog();
98 } // namespace chromeos