blob: 6d9f9053462b58584ac9c4623b2abce503c7f737 [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"
James Cookb0bf8e82017-04-09 17:01:4414#include "base/sys_info.h"
15#include "chromeos/dbus/dbus_thread_manager.h"
16#include "chromeos/dbus/power_manager_client.h"
Daniel Erat03de51e22017-09-09 00:51:5117#include "third_party/cros_system_api/dbus/service_constants.h"
James Cookb0bf8e82017-04-09 17:01:4418
19namespace ash {
20
Chris Watkinsc24daf62017-11-28 03:43:0921ShutdownController::ShutdownController() = default;
James Cookb0bf8e82017-04-09 17:01:4422
Chris Watkinsc24daf62017-11-28 03:43:0923ShutdownController::~ShutdownController() = default;
James Cookb0bf8e82017-04-09 17:01:4424
Wenzhao Zang16e7ea722017-09-16 01:27:3025void ShutdownController::AddObserver(Observer* observer) {
26 observers_.AddObserver(observer);
27}
28
29void ShutdownController::RemoveObserver(Observer* observer) {
30 observers_.RemoveObserver(observer);
31}
32
Lann Martin4fd794c32017-06-30 17:51:4133void ShutdownController::ShutDownOrReboot(ShutdownReason reason) {
James Cookb0bf8e82017-04-09 17:01:4434 // For developers on Linux desktop just exit the app.
35 if (!base::SysInfo::IsRunningOnChromeOS()) {
Steven Bennettsd1b937b52017-10-05 17:18:4936 Shell::Get()->session_controller()->RequestSignOut();
James Cookb0bf8e82017-04-09 17:01:4437 return;
38 }
39
James Cook8706262d2017-08-22 23:15:2640 if (reason == ShutdownReason::POWER_BUTTON)
41 base::RecordAction(base::UserMetricsAction("Accel_ShutDown_PowerButton"));
Lann Martin4fd794c32017-06-30 17:51:4142
James Cookb0bf8e82017-04-09 17:01:4443 // On real Chrome OS hardware the power manager handles shutdown.
44 using chromeos::DBusThreadManager;
Daniel Erat03de51e22017-09-09 00:51:5145 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 Cookb0bf8e82017-04-09 17:01:4459}
60
Wenzhao Zang16e7ea722017-09-16 01:27:3061void ShutdownController::BindRequest(mojom::ShutdownControllerRequest request) {
62 bindings_.AddBinding(this, std::move(request));
63}
64
65void ShutdownController::SetRebootOnShutdownForTesting(
66 bool reboot_on_shutdown) {
67 SetRebootOnShutdown(reboot_on_shutdown);
68}
69
James Cookb0bf8e82017-04-09 17:01:4470void ShutdownController::SetRebootOnShutdown(bool reboot_on_shutdown) {
Wenzhao Zang16e7ea722017-09-16 01:27:3071 if (reboot_on_shutdown_ == reboot_on_shutdown)
72 return;
James Cookb0bf8e82017-04-09 17:01:4473 reboot_on_shutdown_ = reboot_on_shutdown;
Wenzhao Zang16e7ea722017-09-16 01:27:3074 for (auto& observer : observers_)
75 observer.OnShutdownPolicyChanged(reboot_on_shutdown_);
James Cookb0bf8e82017-04-09 17:01:4476}
77
Evan Stadeb95cbc32017-08-09 20:42:4178void ShutdownController::RequestShutdownFromLoginScreen() {
79 Shell::Get()->lock_state_controller()->RequestShutdown(
80 ShutdownReason::LOGIN_SHUT_DOWN_BUTTON);
81}
82
James Cookb0bf8e82017-04-09 17:01:4483} // namespace ash