Home | History | Annotate | Download | only in login
      1 // Copyright 2014 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/ui/webui/chromeos/login/demo_mode_detector.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "chrome/browser/browser_process.h"
     11 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
     12 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
     13 #include "chrome/common/pref_names.h"
     14 #include "chromeos/chromeos_switches.h"
     15 
     16 namespace {
     17   const int kDerelectDetectionTimeoutSeconds = 8 * 60 * 60;  // 8 hours.
     18   const int kDerelectIdleTimeoutSeconds = 5 * 60;            // 5 minutes.
     19   const int kOobeTimerUpdateIntervalSeconds = 5 * 60;        // 5 minutes.
     20 }  // namespace
     21 
     22 namespace chromeos {
     23 
     24 DemoModeDetector::DemoModeDetector()
     25     : demo_launched_(false),
     26       weak_ptr_factory_(this) {
     27   SetupTimeouts();
     28 }
     29 
     30 DemoModeDetector::~DemoModeDetector() {
     31 }
     32 
     33 void DemoModeDetector::InitDetection() {
     34   if (IsDerelict())
     35     StartIdleDetection();
     36   else
     37     StartOobeTimer();
     38 }
     39 
     40 void DemoModeDetector::StopDetection() {
     41   idle_detector_.reset();
     42 }
     43 
     44 void DemoModeDetector::StartIdleDetection() {
     45   if (!idle_detector_.get()) {
     46     idle_detector_.reset(
     47         new IdleDetector(base::Closure(),
     48                          base::Bind(&DemoModeDetector::OnIdle,
     49                                     weak_ptr_factory_.GetWeakPtr())));
     50   }
     51   idle_detector_->Start(derelict_idle_timeout_);
     52 }
     53 
     54 void DemoModeDetector::StartOobeTimer() {
     55   if (oobe_timer_.IsRunning())
     56     return;
     57   oobe_timer_.Start(FROM_HERE,
     58                     oobe_timer_update_interval_,
     59                     this,
     60                     &DemoModeDetector::OnOobeTimerUpdate);
     61 }
     62 
     63 void DemoModeDetector::OnIdle() {
     64   if (demo_launched_)
     65     return;
     66   demo_launched_ = true;
     67   LoginDisplayHost* host = LoginDisplayHostImpl::default_host();
     68   host->StartDemoAppLaunch();
     69 }
     70 
     71 void DemoModeDetector::OnOobeTimerUpdate() {
     72   time_on_oobe_ += oobe_timer_update_interval_;
     73 
     74   PrefService* prefs = g_browser_process->local_state();
     75   prefs->SetInt64(prefs::kTimeOnOobe, time_on_oobe_.InSeconds());
     76 
     77   if (IsDerelict()) {
     78     oobe_timer_.Stop();
     79     StartIdleDetection();
     80   }
     81 }
     82 
     83 void DemoModeDetector::SetupTimeouts() {
     84   CommandLine* cmdline = CommandLine::ForCurrentProcess();
     85   DCHECK(cmdline);
     86 
     87   PrefService* prefs = g_browser_process->local_state();
     88   time_on_oobe_ =
     89       base::TimeDelta::FromSeconds(prefs->GetInt64(prefs::kTimeOnOobe));
     90 
     91   int derelict_detection_timeout;
     92   if (!cmdline->HasSwitch(switches::kDerelictDetectionTimeout) ||
     93       !base::StringToInt(
     94           cmdline->GetSwitchValueASCII(switches::kDerelictDetectionTimeout),
     95           &derelict_detection_timeout)) {
     96     derelict_detection_timeout = kDerelectDetectionTimeoutSeconds;
     97   }
     98   derelict_detection_timeout_ =
     99       base::TimeDelta::FromSeconds(derelict_detection_timeout);
    100 
    101   int derelict_idle_timeout;
    102   if (!cmdline->HasSwitch(switches::kDerelictIdleTimeout) ||
    103       !base::StringToInt(
    104           cmdline->GetSwitchValueASCII(switches::kDerelictIdleTimeout),
    105           &derelict_idle_timeout)) {
    106     derelict_idle_timeout = kDerelectIdleTimeoutSeconds;
    107   }
    108   derelict_idle_timeout_ = base::TimeDelta::FromSeconds(derelict_idle_timeout);
    109 
    110 
    111   int oobe_timer_update_interval;
    112   if (!cmdline->HasSwitch(switches::kOobeTimerInterval) ||
    113       !base::StringToInt(
    114           cmdline->GetSwitchValueASCII(switches::kOobeTimerInterval),
    115           &oobe_timer_update_interval)) {
    116     oobe_timer_update_interval = kOobeTimerUpdateIntervalSeconds;
    117   }
    118   oobe_timer_update_interval_ =
    119       base::TimeDelta::FromSeconds(oobe_timer_update_interval);
    120 
    121   // In case we'd be derelict before our timer is set to trigger, reduce
    122   // the interval so we check again when we're scheduled to go derelict.
    123   oobe_timer_update_interval_ =
    124       std::max(std::min(oobe_timer_update_interval_,
    125                         derelict_detection_timeout_ - time_on_oobe_),
    126                base::TimeDelta::FromSeconds(0));
    127 }
    128 
    129 bool DemoModeDetector::IsDerelict() {
    130   return time_on_oobe_ >= derelict_detection_timeout_;
    131 }
    132 
    133 }  // namespace chromeos
    134