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.
5 #include "chrome/browser/ui/webui/chromeos/login/demo_mode_detector.h"
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"
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.
26 DemoModeDetector::DemoModeDetector()
27 : demo_launched_(false),
28 weak_ptr_factory_(this) {
32 DemoModeDetector::~DemoModeDetector() {
37 void DemoModeDetector::InitDetection() {
38 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
39 switches::kDisableDemoMode
))
42 const bool has_derelict_switch
=
43 base::CommandLine::ForCurrentProcess()->HasSwitch(
44 switches::kDerelictDetectionTimeout
) ||
45 base::CommandLine::ForCurrentProcess()->HasSwitch(
46 switches::kDerelictIdleTimeout
);
48 if (base::SysInfo::IsRunningOnChromeOS() && !has_derelict_switch
) {
50 // We're running on an actual device; if we cannot find our release track
51 // value or if the track contains "testimage", don't start demo mode.
52 if (!base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_TRACK", &track
) ||
53 track
.find("testimage") != std::string::npos
)
63 void DemoModeDetector::StopDetection() {
65 idle_detector_
.reset();
69 void DemoModeDetector::RegisterPrefs(PrefRegistrySimple
* registry
) {
70 registry
->RegisterInt64Pref(prefs::kTimeOnOobe
, 0);
75 void DemoModeDetector::StartIdleDetection() {
76 if (!idle_detector_
.get()) {
78 new IdleDetector(base::Bind(&DemoModeDetector::OnIdle
,
79 weak_ptr_factory_
.GetWeakPtr())));
81 idle_detector_
->Start(derelict_idle_timeout_
);
84 void DemoModeDetector::StartOobeTimer() {
85 if (oobe_timer_
.IsRunning())
87 oobe_timer_
.Start(FROM_HERE
,
88 oobe_timer_update_interval_
,
90 &DemoModeDetector::OnOobeTimerUpdate
);
93 void DemoModeDetector::OnIdle() {
96 demo_launched_
= true;
97 LoginDisplayHost
* host
= LoginDisplayHostImpl::default_host();
98 host
->StartDemoAppLaunch();
101 void DemoModeDetector::OnOobeTimerUpdate() {
102 time_on_oobe_
+= oobe_timer_update_interval_
;
104 PrefService
* prefs
= g_browser_process
->local_state();
105 prefs
->SetInt64(prefs::kTimeOnOobe
, time_on_oobe_
.InSeconds());
109 StartIdleDetection();
113 void DemoModeDetector::SetupTimeouts() {
114 base::CommandLine
* cmdline
= base::CommandLine::ForCurrentProcess();
117 PrefService
* prefs
= g_browser_process
->local_state();
119 base::TimeDelta::FromSeconds(prefs
->GetInt64(prefs::kTimeOnOobe
));
121 int derelict_detection_timeout
;
122 if (!cmdline
->HasSwitch(switches::kDerelictDetectionTimeout
) ||
124 cmdline
->GetSwitchValueASCII(switches::kDerelictDetectionTimeout
),
125 &derelict_detection_timeout
)) {
126 derelict_detection_timeout
= kDerelectDetectionTimeoutSeconds
;
128 derelict_detection_timeout_
=
129 base::TimeDelta::FromSeconds(derelict_detection_timeout
);
131 int derelict_idle_timeout
;
132 if (!cmdline
->HasSwitch(switches::kDerelictIdleTimeout
) ||
134 cmdline
->GetSwitchValueASCII(switches::kDerelictIdleTimeout
),
135 &derelict_idle_timeout
)) {
136 derelict_idle_timeout
= kDerelectIdleTimeoutSeconds
;
138 derelict_idle_timeout_
= base::TimeDelta::FromSeconds(derelict_idle_timeout
);
141 int oobe_timer_update_interval
;
142 if (!cmdline
->HasSwitch(switches::kOobeTimerInterval
) ||
144 cmdline
->GetSwitchValueASCII(switches::kOobeTimerInterval
),
145 &oobe_timer_update_interval
)) {
146 oobe_timer_update_interval
= kOobeTimerUpdateIntervalSeconds
;
148 oobe_timer_update_interval_
=
149 base::TimeDelta::FromSeconds(oobe_timer_update_interval
);
151 // In case we'd be derelict before our timer is set to trigger, reduce
152 // the interval so we check again when we're scheduled to go derelict.
153 oobe_timer_update_interval_
=
154 std::max(std::min(oobe_timer_update_interval_
,
155 derelict_detection_timeout_
- time_on_oobe_
),
156 base::TimeDelta::FromSeconds(0));
159 bool DemoModeDetector::IsDerelict() {
160 return time_on_oobe_
>= derelict_detection_timeout_
;
163 } // namespace chromeos