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