1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/shutdown_controller_impl.h"
9 #include "ash/session/session_controller_impl.h"
10 #include "ash/shell.h"
11 #include "ash/shutdown_reason.h"
12 #include "base/metrics/user_metrics.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/system/sys_info.h"
15 #include "chromeos/dbus/power/power_manager_client.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
20 ShutdownControllerImpl::ShutdownControllerImpl() = default;
22 ShutdownControllerImpl::~ShutdownControllerImpl() = default;
24 void ShutdownControllerImpl::AddObserver(Observer* observer) {
25 observers_.AddObserver(observer);
28 void ShutdownControllerImpl::RemoveObserver(Observer* observer) {
29 observers_.RemoveObserver(observer);
32 void ShutdownControllerImpl::SetRebootOnShutdown(bool reboot_on_shutdown) {
33 if (reboot_on_shutdown_ == reboot_on_shutdown)
35 reboot_on_shutdown_ = reboot_on_shutdown;
36 for (auto& observer : observers_)
37 observer.OnShutdownPolicyChanged(reboot_on_shutdown_);
40 void ShutdownControllerImpl::ShutDownOrReboot(ShutdownReason reason) {
41 // For developers on Linux desktop just exit the app.
42 if (!base::SysInfo::IsRunningOnChromeOS()) {
43 Shell::Get()->session_controller()->RequestSignOut();
47 if (reason == ShutdownReason::POWER_BUTTON)
48 base::RecordAction(base::UserMetricsAction("Accel_ShutDown_PowerButton"));
50 // On real Chrome OS hardware the power manager handles shutdown.
51 std::string description = base::StringPrintf("UI request from ash: %s",
52 ShutdownReasonToString(reason));
53 if (reboot_on_shutdown_) {
54 chromeos::PowerManagerClient::Get()->RequestRestart(
55 power_manager::REQUEST_RESTART_FOR_USER, description);
57 chromeos::PowerManagerClient::Get()->RequestShutdown(
58 power_manager::REQUEST_SHUTDOWN_FOR_USER, description);