summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.cc
blob: 234eb87ec17518af3e393bc03a36451c11c6e005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h"

#include <algorithm>

#include "base/bind.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/cros_settings.h"
#include "chrome/browser/chromeos/cros_settings_names.h"
#include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_screensaver.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/policy/cloud_policy_constants.h"
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/chrome_switches.h"

namespace chromeos {

namespace {
const int kDeviceModeFetchRetryDelayMs = 500;
}

const int KioskModeSettings::kMaxIdleLogoutTimeout = 600000;  // ms
const int KioskModeSettings::kMinIdleLogoutTimeout = 5000;  // ms

const int KioskModeSettings::kMaxIdleLogoutWarningDuration = 60000;  // ms
const int KioskModeSettings::kMinIdleLogoutWarningDuration = 1000;  // ms

static base::LazyInstance<KioskModeSettings> g_kiosk_mode_settings =
    LAZY_INSTANCE_INITIALIZER;

// static
KioskModeSettings* KioskModeSettings::Get() {
  return g_kiosk_mode_settings.Pointer();
}

bool KioskModeSettings::IsKioskModeEnabled() {
  return is_kiosk_mode_;
}

void KioskModeSettings::Initialize(const base::Closure& notify_initialized) {
  // No need to call more than once.
  if (is_initialized_) {
    notify_initialized.Run();
    return;
  }

  CrosSettings* cros_settings = CrosSettings::Get();
  if (!cros_settings->PrepareTrustedValues(
      base::Bind(&KioskModeSettings::Initialize,
                 base::Unretained(this),
                 notify_initialized))) {
    return;
  }

  // Ignored till we land the code to pull the screensaver path from the app
  // packs with the screensaver id.
  cros_settings->GetString(kScreenSaverExtensionId, &screensaver_id_);

  int screensaver_timeout = 0;
  int idle_logout_timeout = 0;
  int idle_logout_warning_duration = 0;
  cros_settings->GetInteger(kScreenSaverTimeout, &screensaver_timeout);
  cros_settings->GetInteger(kIdleLogoutTimeout, &idle_logout_timeout);
  cros_settings->GetInteger(kIdleLogoutWarningDuration,
                            &idle_logout_warning_duration);

  // Restrict idle timeouts to safe values to prevent them from being turned off
  // or otherwise misused.
  idle_logout_timeout = std::min(idle_logout_timeout,
                                 KioskModeSettings::kMaxIdleLogoutTimeout);
  idle_logout_timeout = std::max(idle_logout_timeout,
                                 KioskModeSettings::kMinIdleLogoutTimeout);

  idle_logout_warning_duration =
      std::min(idle_logout_warning_duration,
               KioskModeSettings::kMaxIdleLogoutWarningDuration);
  idle_logout_warning_duration =
      std::max(idle_logout_warning_duration,
               KioskModeSettings::kMinIdleLogoutWarningDuration);

  screensaver_timeout_ = base::TimeDelta::FromMilliseconds(
      screensaver_timeout);
  idle_logout_timeout_ =
      base::TimeDelta::FromMilliseconds(idle_logout_timeout);
  idle_logout_warning_duration_ =
      base::TimeDelta::FromMilliseconds(idle_logout_warning_duration);

  is_initialized_ = true;
  notify_initialized.Run();
}

bool KioskModeSettings::is_initialized() const {
  return is_initialized_;
}

void KioskModeSettings::GetScreensaverPath(
    policy::AppPackUpdater::ScreenSaverUpdateCallback callback) const {
  if (!is_initialized_) {
    callback.Run(FilePath());
    return;
  }

  // Command line flag overrides policy since it can be used
  // for testing and dev workflows.
  if (CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kKioskModeScreensaverPath)) {
    callback.Run(FilePath(
        CommandLine::ForCurrentProcess()->
            GetSwitchValueASCII(switches::kKioskModeScreensaverPath)));
    return;
  }

  if (g_browser_process) {
    policy::BrowserPolicyConnector* bpc =
        g_browser_process->browser_policy_connector();
    if (bpc && bpc->GetAppPackUpdater()) {
      bpc->GetAppPackUpdater()->SetScreenSaverUpdateCallback(callback);
      return;
    }
  }
}

base::TimeDelta KioskModeSettings::GetScreensaverTimeout() const {
  if (!is_initialized_)
    return base::TimeDelta::FromSeconds(-1);

  return screensaver_timeout_;
}

base::TimeDelta KioskModeSettings::GetIdleLogoutTimeout() const {
  if (!is_initialized_)
    return base::TimeDelta::FromSeconds(-1);

  return idle_logout_timeout_;
}

base::TimeDelta KioskModeSettings::GetIdleLogoutWarningDuration() const {
  if (!is_initialized_)
    return base::TimeDelta::FromSeconds(-1);

  return idle_logout_warning_duration_;
}

KioskModeSettings::KioskModeSettings() : is_initialized_(false) {
  // In case we've force-enabled kiosk mode.
  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableKioskMode)) {
    is_kiosk_mode_ = true;
    return;
  }

  // Precache the value as we know it at construction time to avoid serving
  // different values to different users.
  if (g_browser_process) {
    policy::BrowserPolicyConnector* bpc =
        g_browser_process->browser_policy_connector();
    policy::DeviceMode device_mode = bpc->GetDeviceMode();
    if (device_mode == policy::DEVICE_MODE_KIOSK) {
      is_kiosk_mode_ = true;
      return;
    } else if (device_mode == policy::DEVICE_MODE_PENDING){
      content::BrowserThread::PostDelayedTask(
          content::BrowserThread::UI, FROM_HERE,
          base::Bind(&KioskModeSettings::VerifyModeIsKnown,
                     base::Unretained(this)),
          kDeviceModeFetchRetryDelayMs);
    }
  }
  is_kiosk_mode_ = false;
}

KioskModeSettings::~KioskModeSettings() {
}

void KioskModeSettings::VerifyModeIsKnown() {
  if (g_browser_process) {
    policy::BrowserPolicyConnector* bpc =
        g_browser_process->browser_policy_connector();
    policy::DeviceMode device_mode = bpc->GetDeviceMode();
    // We retry asking for the mode until it becomes known.
    switch (device_mode) {
      case policy::DEVICE_MODE_PENDING:
        content::BrowserThread::PostDelayedTask(
            content::BrowserThread::UI, FROM_HERE,
            base::Bind(&KioskModeSettings::VerifyModeIsKnown,
                       base::Unretained(this)),
            kDeviceModeFetchRetryDelayMs);
        break;
      case policy::DEVICE_MODE_KIOSK:
        BrowserList::ExitCleanly();
        break;
      default:
        break;
    }
  }
}


}  // namespace chromeos