• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_registry_simple.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/sys_info.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
14 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
15 #include "chrome/common/pref_names.h"
16 #include "chromeos/chromeos_switches.h"
17 
18 namespace {
19   const int kDerelectDetectionTimeoutSeconds = 8 * 60 * 60;  // 8 hours.
20   const int kDerelectIdleTimeoutSeconds = 5 * 60;            // 5 minutes.
21   const int kOobeTimerUpdateIntervalSeconds = 5 * 60;        // 5 minutes.
22 }  // namespace
23 
24 namespace chromeos {
25 
DemoModeDetector()26 DemoModeDetector::DemoModeDetector()
27     : demo_launched_(false),
28       weak_ptr_factory_(this) {
29   SetupTimeouts();
30 }
31 
~DemoModeDetector()32 DemoModeDetector::~DemoModeDetector() {
33 }
34 
35 // Public methods.
36 
InitDetection()37 void DemoModeDetector::InitDetection() {
38   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableDemoMode))
39     return;
40 
41   if (base::SysInfo::IsRunningOnChromeOS()) {
42     std::string track;
43     // We're running on an actual device; if we cannot find our release track
44     // value or if the track contains "testimage", don't start demo mode.
45     if (!base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_TRACK", &track) ||
46         track.find("testimage") != std::string::npos)
47       return;
48   }
49 
50   if (IsDerelict())
51     StartIdleDetection();
52   else
53     StartOobeTimer();
54 }
55 
StopDetection()56 void DemoModeDetector::StopDetection() {
57   idle_detector_.reset();
58 }
59 
60 // static
RegisterPrefs(PrefRegistrySimple * registry)61 void DemoModeDetector::RegisterPrefs(PrefRegistrySimple* registry) {
62   registry->RegisterInt64Pref(prefs::kTimeOnOobe, 0);
63 }
64 
65 // Private methods.
66 
StartIdleDetection()67 void DemoModeDetector::StartIdleDetection() {
68   if (!idle_detector_.get()) {
69     idle_detector_.reset(
70         new IdleDetector(base::Closure(),
71                          base::Bind(&DemoModeDetector::OnIdle,
72                                     weak_ptr_factory_.GetWeakPtr())));
73   }
74   idle_detector_->Start(derelict_idle_timeout_);
75 }
76 
StartOobeTimer()77 void DemoModeDetector::StartOobeTimer() {
78   if (oobe_timer_.IsRunning())
79     return;
80   oobe_timer_.Start(FROM_HERE,
81                     oobe_timer_update_interval_,
82                     this,
83                     &DemoModeDetector::OnOobeTimerUpdate);
84 }
85 
OnIdle()86 void DemoModeDetector::OnIdle() {
87   if (demo_launched_)
88     return;
89   demo_launched_ = true;
90   LoginDisplayHost* host = LoginDisplayHostImpl::default_host();
91   host->StartDemoAppLaunch();
92 }
93 
OnOobeTimerUpdate()94 void DemoModeDetector::OnOobeTimerUpdate() {
95   time_on_oobe_ += oobe_timer_update_interval_;
96 
97   PrefService* prefs = g_browser_process->local_state();
98   prefs->SetInt64(prefs::kTimeOnOobe, time_on_oobe_.InSeconds());
99 
100   if (IsDerelict()) {
101     oobe_timer_.Stop();
102     StartIdleDetection();
103   }
104 }
105 
SetupTimeouts()106 void DemoModeDetector::SetupTimeouts() {
107   CommandLine* cmdline = CommandLine::ForCurrentProcess();
108   DCHECK(cmdline);
109 
110   PrefService* prefs = g_browser_process->local_state();
111   time_on_oobe_ =
112       base::TimeDelta::FromSeconds(prefs->GetInt64(prefs::kTimeOnOobe));
113 
114   int derelict_detection_timeout;
115   if (!cmdline->HasSwitch(switches::kDerelictDetectionTimeout) ||
116       !base::StringToInt(
117           cmdline->GetSwitchValueASCII(switches::kDerelictDetectionTimeout),
118           &derelict_detection_timeout)) {
119     derelict_detection_timeout = kDerelectDetectionTimeoutSeconds;
120   }
121   derelict_detection_timeout_ =
122       base::TimeDelta::FromSeconds(derelict_detection_timeout);
123 
124   int derelict_idle_timeout;
125   if (!cmdline->HasSwitch(switches::kDerelictIdleTimeout) ||
126       !base::StringToInt(
127           cmdline->GetSwitchValueASCII(switches::kDerelictIdleTimeout),
128           &derelict_idle_timeout)) {
129     derelict_idle_timeout = kDerelectIdleTimeoutSeconds;
130   }
131   derelict_idle_timeout_ = base::TimeDelta::FromSeconds(derelict_idle_timeout);
132 
133 
134   int oobe_timer_update_interval;
135   if (!cmdline->HasSwitch(switches::kOobeTimerInterval) ||
136       !base::StringToInt(
137           cmdline->GetSwitchValueASCII(switches::kOobeTimerInterval),
138           &oobe_timer_update_interval)) {
139     oobe_timer_update_interval = kOobeTimerUpdateIntervalSeconds;
140   }
141   oobe_timer_update_interval_ =
142       base::TimeDelta::FromSeconds(oobe_timer_update_interval);
143 
144   // In case we'd be derelict before our timer is set to trigger, reduce
145   // the interval so we check again when we're scheduled to go derelict.
146   oobe_timer_update_interval_ =
147       std::max(std::min(oobe_timer_update_interval_,
148                         derelict_detection_timeout_ - time_on_oobe_),
149                base::TimeDelta::FromSeconds(0));
150 }
151 
IsDerelict()152 bool DemoModeDetector::IsDerelict() {
153   return time_on_oobe_ >= derelict_detection_timeout_;
154 }
155 
156 }  // namespace chromeos
157