James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 1 | // 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 Martin | 4fd794c3 | 2017-06-30 17:51:41 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Steven Bennetts | d1b937b5 | 2017-10-05 17:18:49 | [diff] [blame] | 9 | #include "ash/session/session_controller.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" |
Evan Stade | b95cbc3 | 2017-08-09 20:42:41 | [diff] [blame] | 12 | #include "ash/wm/lock_state_controller.h" |
James Cook | 8706262d | 2017-08-22 23:15:26 | [diff] [blame] | 13 | #include "base/metrics/user_metrics.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 14 | #include "base/sys_info.h" |
| 15 | #include "chromeos/dbus/dbus_thread_manager.h" |
| 16 | #include "chromeos/dbus/power_manager_client.h" |
Daniel Erat | 03de51e2 | 2017-09-09 00:51:51 | [diff] [blame] | 17 | #include "third_party/cros_system_api/dbus/service_constants.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 18 | |
| 19 | namespace ash { |
| 20 | |
Chris Watkins | c24daf6 | 2017-11-28 03:43:09 | [diff] [blame^] | 21 | ShutdownController::ShutdownController() = default; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 22 | |
Chris Watkins | c24daf6 | 2017-11-28 03:43:09 | [diff] [blame^] | 23 | ShutdownController::~ShutdownController() = default; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 24 | |
Wenzhao Zang | 16e7ea72 | 2017-09-16 01:27:30 | [diff] [blame] | 25 | void ShutdownController::AddObserver(Observer* observer) { |
| 26 | observers_.AddObserver(observer); |
| 27 | } |
| 28 | |
| 29 | void ShutdownController::RemoveObserver(Observer* observer) { |
| 30 | observers_.RemoveObserver(observer); |
| 31 | } |
| 32 | |
Lann Martin | 4fd794c3 | 2017-06-30 17:51:41 | [diff] [blame] | 33 | void ShutdownController::ShutDownOrReboot(ShutdownReason reason) { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 34 | // For developers on Linux desktop just exit the app. |
| 35 | if (!base::SysInfo::IsRunningOnChromeOS()) { |
Steven Bennetts | d1b937b5 | 2017-10-05 17:18:49 | [diff] [blame] | 36 | Shell::Get()->session_controller()->RequestSignOut(); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 37 | return; |
| 38 | } |
| 39 | |
James Cook | 8706262d | 2017-08-22 23:15:26 | [diff] [blame] | 40 | if (reason == ShutdownReason::POWER_BUTTON) |
| 41 | base::RecordAction(base::UserMetricsAction("Accel_ShutDown_PowerButton")); |
Lann Martin | 4fd794c3 | 2017-06-30 17:51:41 | [diff] [blame] | 42 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 43 | // On real Chrome OS hardware the power manager handles shutdown. |
| 44 | using chromeos::DBusThreadManager; |
Daniel Erat | 03de51e2 | 2017-09-09 00:51:51 | [diff] [blame] | 45 | constexpr char kDescription[] = "UI request from ash"; |
| 46 | if (reboot_on_shutdown_) { |
| 47 | DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart( |
| 48 | reason == ShutdownReason::UNKNOWN |
| 49 | ? power_manager::REQUEST_RESTART_OTHER |
| 50 | : power_manager::REQUEST_RESTART_FOR_USER, |
| 51 | kDescription); |
| 52 | } else { |
| 53 | DBusThreadManager::Get()->GetPowerManagerClient()->RequestShutdown( |
| 54 | reason == ShutdownReason::UNKNOWN |
| 55 | ? power_manager::REQUEST_SHUTDOWN_OTHER |
| 56 | : power_manager::REQUEST_SHUTDOWN_FOR_USER, |
| 57 | kDescription); |
| 58 | } |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 59 | } |
| 60 | |
Wenzhao Zang | 16e7ea72 | 2017-09-16 01:27:30 | [diff] [blame] | 61 | void ShutdownController::BindRequest(mojom::ShutdownControllerRequest request) { |
| 62 | bindings_.AddBinding(this, std::move(request)); |
| 63 | } |
| 64 | |
| 65 | void ShutdownController::SetRebootOnShutdownForTesting( |
| 66 | bool reboot_on_shutdown) { |
| 67 | SetRebootOnShutdown(reboot_on_shutdown); |
| 68 | } |
| 69 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 70 | void ShutdownController::SetRebootOnShutdown(bool reboot_on_shutdown) { |
Wenzhao Zang | 16e7ea72 | 2017-09-16 01:27:30 | [diff] [blame] | 71 | if (reboot_on_shutdown_ == reboot_on_shutdown) |
| 72 | return; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 73 | reboot_on_shutdown_ = reboot_on_shutdown; |
Wenzhao Zang | 16e7ea72 | 2017-09-16 01:27:30 | [diff] [blame] | 74 | for (auto& observer : observers_) |
| 75 | observer.OnShutdownPolicyChanged(reboot_on_shutdown_); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 76 | } |
| 77 | |
Evan Stade | b95cbc3 | 2017-08-09 20:42:41 | [diff] [blame] | 78 | void ShutdownController::RequestShutdownFromLoginScreen() { |
| 79 | Shell::Get()->lock_state_controller()->RequestShutdown( |
| 80 | ShutdownReason::LOGIN_SHUT_DOWN_BUTTON); |
| 81 | } |
| 82 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 83 | } // namespace ash |