shutdown_controller_impl.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include "ash/shutdown_controller_impl.h"
  5. #include <utility>
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "ash/shutdown_reason.h"
  9. #include "base/metrics/user_metrics.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "base/system/sys_info.h"
  12. #include "chromeos/dbus/power/power_manager_client.h"
  13. #include "third_party/cros_system_api/dbus/service_constants.h"
  14. namespace ash {
  15. ShutdownControllerImpl::ShutdownControllerImpl() = default;
  16. ShutdownControllerImpl::~ShutdownControllerImpl() = default;
  17. void ShutdownControllerImpl::AddObserver(Observer* observer) {
  18. observers_.AddObserver(observer);
  19. }
  20. void ShutdownControllerImpl::RemoveObserver(Observer* observer) {
  21. observers_.RemoveObserver(observer);
  22. }
  23. void ShutdownControllerImpl::SetRebootOnShutdown(bool reboot_on_shutdown) {
  24. if (reboot_on_shutdown_ == reboot_on_shutdown)
  25. return;
  26. reboot_on_shutdown_ = reboot_on_shutdown;
  27. for (auto& observer : observers_)
  28. observer.OnShutdownPolicyChanged(reboot_on_shutdown_);
  29. }
  30. void ShutdownControllerImpl::ShutDownOrReboot(ShutdownReason reason) {
  31. // For developers on Linux desktop just exit the app.
  32. if (!base::SysInfo::IsRunningOnChromeOS()) {
  33. Shell::Get()->session_controller()->RequestSignOut();
  34. return;
  35. }
  36. if (reason == ShutdownReason::POWER_BUTTON)
  37. base::RecordAction(base::UserMetricsAction("Accel_ShutDown_PowerButton"));
  38. // On real Chrome OS hardware the power manager handles shutdown.
  39. std::string description = base::StringPrintf("UI request from ash: %s",
  40. ShutdownReasonToString(reason));
  41. if (reboot_on_shutdown_) {
  42. chromeos::PowerManagerClient::Get()->RequestRestart(
  43. power_manager::REQUEST_RESTART_FOR_USER, description);
  44. } else {
  45. chromeos::PowerManagerClient::Get()->RequestShutdown(
  46. power_manager::REQUEST_SHUTDOWN_FOR_USER, description);
  47. }
  48. }
  49. } // namespace ash