Stop demo mode detection correctly.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / demo_mode_detector.cc
blobbb465d4d644accb208536c8fa8b1e64c76628a89
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"
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
24 namespace chromeos {
26 DemoModeDetector::DemoModeDetector()
27 : demo_launched_(false),
28 weak_ptr_factory_(this) {
29 SetupTimeouts();
32 DemoModeDetector::~DemoModeDetector() {
35 // Public methods.
37 void DemoModeDetector::InitDetection() {
38 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableDemoMode))
39 return;
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;
50 if (IsDerelict())
51 StartIdleDetection();
52 else
53 StartOobeTimer();
56 void DemoModeDetector::StopDetection() {
57 oobe_timer_.Stop();
58 idle_detector_.reset();
61 // static
62 void DemoModeDetector::RegisterPrefs(PrefRegistrySimple* registry) {
63 registry->RegisterInt64Pref(prefs::kTimeOnOobe, 0);
66 // Private methods.
68 void DemoModeDetector::StartIdleDetection() {
69 if (!idle_detector_.get()) {
70 idle_detector_.reset(
71 new IdleDetector(base::Bind(&DemoModeDetector::OnIdle,
72 weak_ptr_factory_.GetWeakPtr())));
74 idle_detector_->Start(derelict_idle_timeout_);
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);
86 void DemoModeDetector::OnIdle() {
87 if (demo_launched_)
88 return;
89 demo_launched_ = true;
90 LoginDisplayHost* host = LoginDisplayHostImpl::default_host();
91 host->StartDemoAppLaunch();
94 void DemoModeDetector::OnOobeTimerUpdate() {
95 time_on_oobe_ += oobe_timer_update_interval_;
97 PrefService* prefs = g_browser_process->local_state();
98 prefs->SetInt64(prefs::kTimeOnOobe, time_on_oobe_.InSeconds());
100 if (IsDerelict()) {
101 oobe_timer_.Stop();
102 StartIdleDetection();
106 void DemoModeDetector::SetupTimeouts() {
107 CommandLine* cmdline = CommandLine::ForCurrentProcess();
108 DCHECK(cmdline);
110 PrefService* prefs = g_browser_process->local_state();
111 time_on_oobe_ =
112 base::TimeDelta::FromSeconds(prefs->GetInt64(prefs::kTimeOnOobe));
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;
121 derelict_detection_timeout_ =
122 base::TimeDelta::FromSeconds(derelict_detection_timeout);
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;
131 derelict_idle_timeout_ = base::TimeDelta::FromSeconds(derelict_idle_timeout);
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;
141 oobe_timer_update_interval_ =
142 base::TimeDelta::FromSeconds(oobe_timer_update_interval);
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));
152 bool DemoModeDetector::IsDerelict() {
153 return time_on_oobe_ >= derelict_detection_timeout_;
156 } // namespace chromeos