blob: 205102f85d811f3d07be7b71a4e14b9fd49e410c [file] [log] [blame]
James Cookb0bf8e82017-04-09 17:01:441// Copyright 2016 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 "ash/shutdown_controller.h"
6
Lann Martin4fd794c32017-06-30 17:51:417#include <utility>
8
Steven Bennettsd1b937b52017-10-05 17:18:499#include "ash/session/session_controller.h"
James Cookb0bf8e82017-04-09 17:01:4410#include "ash/shell.h"
Lann Martin4fd794c32017-06-30 17:51:4111#include "ash/shutdown_reason.h"
Evan Stadeb95cbc32017-08-09 20:42:4112#include "ash/wm/lock_state_controller.h"
James Cook8706262d2017-08-22 23:15:2613#include "base/metrics/user_metrics.h"
Daniel Eratf86314fc2018-02-23 21:03:2714#include "base/strings/stringprintf.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0315#include "base/system/sys_info.h"
James Cookb0bf8e82017-04-09 17:01:4416#include "chromeos/dbus/dbus_thread_manager.h"
17#include "chromeos/dbus/power_manager_client.h"
Daniel Erat03de51e22017-09-09 00:51:5118#include "third_party/cros_system_api/dbus/service_constants.h"
James Cookb0bf8e82017-04-09 17:01:4419
20namespace ash {
21
Chris Watkinsc24daf62017-11-28 03:43:0922ShutdownController::ShutdownController() = default;
James Cookb0bf8e82017-04-09 17:01:4423
Chris Watkinsc24daf62017-11-28 03:43:0924ShutdownController::~ShutdownController() = default;
James Cookb0bf8e82017-04-09 17:01:4425
Wenzhao Zang16e7ea722017-09-16 01:27:3026void ShutdownController::AddObserver(Observer* observer) {
27 observers_.AddObserver(observer);
28}
29
30void ShutdownController::RemoveObserver(Observer* observer) {
31 observers_.RemoveObserver(observer);
32}
33
Lann Martin4fd794c32017-06-30 17:51:4134void ShutdownController::ShutDownOrReboot(ShutdownReason reason) {
James Cookb0bf8e82017-04-09 17:01:4435 // For developers on Linux desktop just exit the app.
36 if (!base::SysInfo::IsRunningOnChromeOS()) {
Steven Bennettsd1b937b52017-10-05 17:18:4937 Shell::Get()->session_controller()->RequestSignOut();
James Cookb0bf8e82017-04-09 17:01:4438 return;
39 }
40
James Cook8706262d2017-08-22 23:15:2641 if (reason == ShutdownReason::POWER_BUTTON)
42 base::RecordAction(base::UserMetricsAction("Accel_ShutDown_PowerButton"));
Lann Martin4fd794c32017-06-30 17:51:4143
James Cookb0bf8e82017-04-09 17:01:4444 // On real Chrome OS hardware the power manager handles shutdown.
45 using chromeos::DBusThreadManager;
Daniel Eratf86314fc2018-02-23 21:03:2746 std::string description = base::StringPrintf("UI request from ash: %s",
47 ShutdownReasonToString(reason));
Daniel Erat03de51e22017-09-09 00:51:5148 if (reboot_on_shutdown_) {
49 DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart(
Daniel Eratf86314fc2018-02-23 21:03:2750 power_manager::REQUEST_RESTART_FOR_USER, description);
Daniel Erat03de51e22017-09-09 00:51:5151 } else {
52 DBusThreadManager::Get()->GetPowerManagerClient()->RequestShutdown(
Daniel Eratf86314fc2018-02-23 21:03:2753 power_manager::REQUEST_SHUTDOWN_FOR_USER, description);
Daniel Erat03de51e22017-09-09 00:51:5154 }
James Cookb0bf8e82017-04-09 17:01:4455}
56
Wenzhao Zang16e7ea722017-09-16 01:27:3057void ShutdownController::BindRequest(mojom::ShutdownControllerRequest request) {
58 bindings_.AddBinding(this, std::move(request));
59}
60
61void ShutdownController::SetRebootOnShutdownForTesting(
62 bool reboot_on_shutdown) {
63 SetRebootOnShutdown(reboot_on_shutdown);
64}
65
James Cookb0bf8e82017-04-09 17:01:4466void ShutdownController::SetRebootOnShutdown(bool reboot_on_shutdown) {
Wenzhao Zang16e7ea722017-09-16 01:27:3067 if (reboot_on_shutdown_ == reboot_on_shutdown)
68 return;
James Cookb0bf8e82017-04-09 17:01:4469 reboot_on_shutdown_ = reboot_on_shutdown;
Wenzhao Zang16e7ea722017-09-16 01:27:3070 for (auto& observer : observers_)
71 observer.OnShutdownPolicyChanged(reboot_on_shutdown_);
James Cookb0bf8e82017-04-09 17:01:4472}
73
Evan Stadeb95cbc32017-08-09 20:42:4174void ShutdownController::RequestShutdownFromLoginScreen() {
75 Shell::Get()->lock_state_controller()->RequestShutdown(
76 ShutdownReason::LOGIN_SHUT_DOWN_BUTTON);
77}
78
James Cookb0bf8e82017-04-09 17:01:4479} // namespace ash