Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Xiyuan Xia | 7107d49 | 2019-06-05 16:58:54 | [diff] [blame] | 5 | #include "ash/shutdown_controller_impl.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 6 | |
Lann Martin | 4fd794c3 | 2017-06-30 17:51:41 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Xiyuan Xia | e7b1954 | 2019-05-06 23:05:18 | [diff] [blame] | 9 | #include "ash/session/session_controller_impl.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 10 | #include "ash/shell.h" |
Lann Martin | 4fd794c3 | 2017-06-30 17:51:41 | [diff] [blame] | 11 | #include "ash/shutdown_reason.h" |
James Cook | 8706262d | 2017-08-22 23:15:26 | [diff] [blame] | 12 | #include "base/metrics/user_metrics.h" |
Daniel Erat | f86314fc | 2018-02-23 21:03:27 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
Sebastien Marchand | 75a7cdf | 2018-11-13 23:47:03 | [diff] [blame] | 14 | #include "base/system/sys_info.h" |
Steven Bennetts | 3330b9f | 2019-03-15 20:24:13 | [diff] [blame] | 15 | #include "chromeos/dbus/power/power_manager_client.h" |
Daniel Erat | 03de51e2 | 2017-09-09 00:51:51 | [diff] [blame] | 16 | #include "third_party/cros_system_api/dbus/service_constants.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 17 | |
| 18 | namespace ash { |
| 19 | |
Xiyuan Xia | 7107d49 | 2019-06-05 16:58:54 | [diff] [blame] | 20 | ShutdownControllerImpl::ShutdownControllerImpl() = default; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 21 | |
Xiyuan Xia | 7107d49 | 2019-06-05 16:58:54 | [diff] [blame] | 22 | ShutdownControllerImpl::~ShutdownControllerImpl() = default; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 23 | |
Xiyuan Xia | 7107d49 | 2019-06-05 16:58:54 | [diff] [blame] | 24 | void ShutdownControllerImpl::AddObserver(Observer* observer) { |
Wenzhao Zang | 16e7ea72 | 2017-09-16 01:27:30 | [diff] [blame] | 25 | observers_.AddObserver(observer); |
| 26 | } |
| 27 | |
Xiyuan Xia | 7107d49 | 2019-06-05 16:58:54 | [diff] [blame] | 28 | void ShutdownControllerImpl::RemoveObserver(Observer* observer) { |
Wenzhao Zang | 16e7ea72 | 2017-09-16 01:27:30 | [diff] [blame] | 29 | observers_.RemoveObserver(observer); |
| 30 | } |
| 31 | |
Xiyuan Xia | 7107d49 | 2019-06-05 16:58:54 | [diff] [blame] | 32 | void 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 | |
| 40 | void ShutdownControllerImpl::ShutDownOrReboot(ShutdownReason reason) { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 41 | // For developers on Linux desktop just exit the app. |
| 42 | if (!base::SysInfo::IsRunningOnChromeOS()) { |
Steven Bennetts | d1b937b5 | 2017-10-05 17:18:49 | [diff] [blame] | 43 | Shell::Get()->session_controller()->RequestSignOut(); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | |
James Cook | 8706262d | 2017-08-22 23:15:26 | [diff] [blame] | 47 | if (reason == ShutdownReason::POWER_BUTTON) |
| 48 | base::RecordAction(base::UserMetricsAction("Accel_ShutDown_PowerButton")); |
Lann Martin | 4fd794c3 | 2017-06-30 17:51:41 | [diff] [blame] | 49 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 50 | // On real Chrome OS hardware the power manager handles shutdown. |
Daniel Erat | f86314fc | 2018-02-23 21:03:27 | [diff] [blame] | 51 | std::string description = base::StringPrintf("UI request from ash: %s", |
| 52 | ShutdownReasonToString(reason)); |
Daniel Erat | 03de51e2 | 2017-09-09 00:51:51 | [diff] [blame] | 53 | if (reboot_on_shutdown_) { |
Evan Stade | 523f7fc | 2019-03-02 19:20:51 | [diff] [blame] | 54 | chromeos::PowerManagerClient::Get()->RequestRestart( |
Daniel Erat | f86314fc | 2018-02-23 21:03:27 | [diff] [blame] | 55 | power_manager::REQUEST_RESTART_FOR_USER, description); |
Daniel Erat | 03de51e2 | 2017-09-09 00:51:51 | [diff] [blame] | 56 | } else { |
Evan Stade | 523f7fc | 2019-03-02 19:20:51 | [diff] [blame] | 57 | chromeos::PowerManagerClient::Get()->RequestShutdown( |
Daniel Erat | f86314fc | 2018-02-23 21:03:27 | [diff] [blame] | 58 | power_manager::REQUEST_SHUTDOWN_FOR_USER, description); |
Daniel Erat | 03de51e2 | 2017-09-09 00:51:51 | [diff] [blame] | 59 | } |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 60 | } |
| 61 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 62 | } // namespace ash |