blob: 6ad26996dd9d63cfb4389df43545e02f984ebed3 [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2016 The Chromium Authors
James Cookb0bf8e82017-04-09 17:01:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Xiyuan Xia7107d492019-06-05 16:58:545#include "ash/shutdown_controller_impl.h"
James Cookb0bf8e82017-04-09 17:01:446
Lann Martin4fd794c32017-06-30 17:51:417#include <utility>
8
Xiyuan Xiae7b19542019-05-06 23:05:189#include "ash/session/session_controller_impl.h"
James Cookb0bf8e82017-04-09 17:01:4410#include "ash/shell.h"
Lann Martin4fd794c32017-06-30 17:51:4111#include "ash/shutdown_reason.h"
James Cook8706262d2017-08-22 23:15:2612#include "base/metrics/user_metrics.h"
Daniel Eratf86314fc2018-02-23 21:03:2713#include "base/strings/stringprintf.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0314#include "base/system/sys_info.h"
Steven Bennetts3330b9f2019-03-15 20:24:1315#include "chromeos/dbus/power/power_manager_client.h"
Daniel Erat03de51e22017-09-09 00:51:5116#include "third_party/cros_system_api/dbus/service_constants.h"
James Cookb0bf8e82017-04-09 17:01:4417
18namespace ash {
19
Xiyuan Xia7107d492019-06-05 16:58:5420ShutdownControllerImpl::ShutdownControllerImpl() = default;
James Cookb0bf8e82017-04-09 17:01:4421
Xiyuan Xia7107d492019-06-05 16:58:5422ShutdownControllerImpl::~ShutdownControllerImpl() = default;
James Cookb0bf8e82017-04-09 17:01:4423
Xiyuan Xia7107d492019-06-05 16:58:5424void ShutdownControllerImpl::AddObserver(Observer* observer) {
Wenzhao Zang16e7ea722017-09-16 01:27:3025 observers_.AddObserver(observer);
26}
27
Xiyuan Xia7107d492019-06-05 16:58:5428void ShutdownControllerImpl::RemoveObserver(Observer* observer) {
Wenzhao Zang16e7ea722017-09-16 01:27:3029 observers_.RemoveObserver(observer);
30}
31
Xiyuan Xia7107d492019-06-05 16:58:5432void ShutdownControllerImpl::SetRebootOnShutdown(bool reboot_on_shutdown) {
33 if (reboot_on_shutdown_ == reboot_on_shutdown)
34 return;
35 reboot_on_shutdown_ = reboot_on_shutdown;
36 for (auto& observer : observers_)
37 observer.OnShutdownPolicyChanged(reboot_on_shutdown_);
38}
39
40void ShutdownControllerImpl::ShutDownOrReboot(ShutdownReason reason) {
James Cookb0bf8e82017-04-09 17:01:4441 // For developers on Linux desktop just exit the app.
42 if (!base::SysInfo::IsRunningOnChromeOS()) {
Steven Bennettsd1b937b52017-10-05 17:18:4943 Shell::Get()->session_controller()->RequestSignOut();
James Cookb0bf8e82017-04-09 17:01:4444 return;
45 }
46
James Cook8706262d2017-08-22 23:15:2647 if (reason == ShutdownReason::POWER_BUTTON)
48 base::RecordAction(base::UserMetricsAction("Accel_ShutDown_PowerButton"));
Lann Martin4fd794c32017-06-30 17:51:4149
James Cookb0bf8e82017-04-09 17:01:4450 // On real Chrome OS hardware the power manager handles shutdown.
Daniel Eratf86314fc2018-02-23 21:03:2751 std::string description = base::StringPrintf("UI request from ash: %s",
52 ShutdownReasonToString(reason));
Daniel Erat03de51e22017-09-09 00:51:5153 if (reboot_on_shutdown_) {
Evan Stade523f7fc2019-03-02 19:20:5154 chromeos::PowerManagerClient::Get()->RequestRestart(
Daniel Eratf86314fc2018-02-23 21:03:2755 power_manager::REQUEST_RESTART_FOR_USER, description);
Daniel Erat03de51e22017-09-09 00:51:5156 } else {
Evan Stade523f7fc2019-03-02 19:20:5157 chromeos::PowerManagerClient::Get()->RequestShutdown(
Daniel Eratf86314fc2018-02-23 21:03:2758 power_manager::REQUEST_SHUTDOWN_FOR_USER, description);
Daniel Erat03de51e22017-09-09 00:51:5159 }
James Cookb0bf8e82017-04-09 17:01:4460}
61
James Cookb0bf8e82017-04-09 17:01:4462} // namespace ash