Home | History | Annotate | Download | only in kiosk_mode
      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/kiosk_mode/kiosk_mode_idle_logout.h"
      6 
      7 #include "ash/shell.h"
      8 #include "ash/wm/user_activity_detector.h"
      9 #include "base/bind.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"
     20 
     21 namespace chromeos {
     22 
     23 namespace {
     24 
     25 const int64 kLoginIdleTimeout = 100; // seconds
     26 
     27 static base::LazyInstance<KioskModeIdleLogout>
     28     g_kiosk_mode_idle_logout = LAZY_INSTANCE_INITIALIZER;
     29 
     30 }  // namespace
     31 
     32 // static
     33 void KioskModeIdleLogout::Initialize() {
     34   g_kiosk_mode_idle_logout.Get();
     35 }
     36 
     37 KioskModeIdleLogout::KioskModeIdleLogout() {
     38   if (KioskModeSettings::Get()->is_initialized()) {
     39     Setup();
     40   } else {
     41     KioskModeSettings::Get()->Initialize(base::Bind(&KioskModeIdleLogout::Setup,
     42                                                     base::Unretained(this)));
     43   }
     44 }
     45 
     46 KioskModeIdleLogout::~KioskModeIdleLogout() {
     47   if (ash::Shell::HasInstance() &&
     48       ash::Shell::GetInstance()->user_activity_detector()->HasObserver(this))
     49     ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this);
     50 }
     51 
     52 void KioskModeIdleLogout::Setup() {
     53   if (UserManager::Get()->IsLoggedInAsDemoUser()) {
     54     // This means that we're recovering from a crash.  The user is already
     55     // logged in, so go ahead and start the timer.
     56     Start();
     57   } else {
     58     registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_CHANGED,
     59                    content::NotificationService::AllSources());
     60   }
     61 }
     62 
     63 void KioskModeIdleLogout::Observe(
     64     int type,
     65     const content::NotificationSource& source,
     66     const content::NotificationDetails& details) {
     67   if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) {
     68     Start();
     69     registrar_.RemoveAll();
     70   }
     71 }
     72 
     73 void KioskModeIdleLogout::OnUserActivity(const ui::Event* event) {
     74   IdleLogoutDialogView::CloseDialog();
     75   ResetTimer();
     76 }
     77 
     78 void KioskModeIdleLogout::Start() {
     79   if (!ash::Shell::GetInstance()->user_activity_detector()->HasObserver(this))
     80     ash::Shell::GetInstance()->user_activity_detector()->AddObserver(this);
     81   ResetTimer();
     82 }
     83 
     84 void KioskModeIdleLogout::ResetTimer() {
     85   if (timer_.IsRunning()) {
     86     timer_.Reset();
     87   } else {
     88     // OneShotTimer destroys the posted task after running it, so Reset()
     89     // isn't safe to call on a timer that's already fired.
     90     timer_.Start(FROM_HERE, KioskModeSettings::Get()->GetIdleLogoutTimeout(),
     91                  base::Bind(&KioskModeIdleLogout::OnTimeout,
     92                             base::Unretained(this)));
     93   }
     94 }
     95 
     96 void KioskModeIdleLogout::OnTimeout() {
     97   IdleLogoutDialogView::ShowDialog();
     98 }
     99 
    100 }  // namespace chromeos
    101