screen_switch_check_controller.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 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/system/screen_security/screen_switch_check_controller.h"
  5. #include "ash/shell.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/system/tray/system_tray_notifier.h"
  8. #include "base/bind.h"
  9. #include "ui/base/l10n/l10n_util.h"
  10. #include "ui/strings/grit/ui_strings.h"
  11. #include "ui/views/controls/message_box_view.h"
  12. #include "ui/views/layout/fill_layout.h"
  13. #include "ui/views/window/dialog_delegate.h"
  14. namespace ash {
  15. namespace {
  16. // Dialog that confirms the user wants to stop screen share/cast. Calls a
  17. // callback with the result.
  18. class CancelCastingDialog : public views::DialogDelegateView {
  19. public:
  20. CancelCastingDialog(base::OnceCallback<void(bool)> callback)
  21. : callback_(std::move(callback)) {
  22. AddChildView(new views::MessageBoxView(
  23. l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_MESSAGE)));
  24. SetLayoutManager(std::make_unique<views::FillLayout>());
  25. SetTitle(l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_TITLE));
  26. SetShowCloseButton(false);
  27. SetButtonLabel(
  28. ui::DIALOG_BUTTON_OK,
  29. l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_CONTINUE));
  30. SetAcceptCallback(base::BindOnce(&CancelCastingDialog::OnDialogAccepted,
  31. base::Unretained(this)));
  32. SetCancelCallback(base::BindOnce(&CancelCastingDialog::OnDialogCancelled,
  33. base::Unretained(this)));
  34. }
  35. CancelCastingDialog(const CancelCastingDialog&) = delete;
  36. CancelCastingDialog& operator=(const CancelCastingDialog&) = delete;
  37. ~CancelCastingDialog() override = default;
  38. void OnDialogCancelled() { std::move(callback_).Run(false); }
  39. void OnDialogAccepted() {
  40. // Stop screen sharing and capturing. When notified, all capture sessions or
  41. // all share sessions will be stopped.
  42. // Currently, the logic is in ScreenSecurityNotificationController.
  43. Shell::Get()->system_tray_notifier()->NotifyScreenCaptureStop();
  44. Shell::Get()->system_tray_notifier()->NotifyScreenShareStop();
  45. std::move(callback_).Run(true);
  46. }
  47. private:
  48. base::OnceCallback<void(bool)> callback_;
  49. };
  50. } // namespace
  51. ScreenSwitchCheckController::ScreenSwitchCheckController() {
  52. Shell::Get()->system_tray_notifier()->AddScreenCaptureObserver(this);
  53. Shell::Get()->system_tray_notifier()->AddScreenShareObserver(this);
  54. }
  55. ScreenSwitchCheckController::~ScreenSwitchCheckController() {
  56. Shell::Get()->system_tray_notifier()->RemoveScreenShareObserver(this);
  57. Shell::Get()->system_tray_notifier()->RemoveScreenCaptureObserver(this);
  58. }
  59. void ScreenSwitchCheckController::CanSwitchAwayFromActiveUser(
  60. base::OnceCallback<void(bool)> callback) {
  61. // If neither screen sharing nor capturing is going on we can immediately
  62. // switch users.
  63. if (!has_capture_ && !has_share_) {
  64. std::move(callback).Run(true);
  65. return;
  66. }
  67. views::DialogDelegate::CreateDialogWidget(
  68. new CancelCastingDialog(std::move(callback)),
  69. Shell::GetPrimaryRootWindow(), nullptr)
  70. ->Show();
  71. }
  72. void ScreenSwitchCheckController::OnScreenCaptureStart(
  73. const base::RepeatingClosure& stop_callback,
  74. const base::RepeatingClosure& source_callback,
  75. const std::u16string& screen_capture_status) {
  76. has_capture_ = true;
  77. }
  78. void ScreenSwitchCheckController::OnScreenCaptureStop() {
  79. // Multiple screen capture sessions can exist, but they are stopped at once
  80. // for simplicity.
  81. has_capture_ = false;
  82. }
  83. void ScreenSwitchCheckController::OnScreenShareStart(
  84. const base::RepeatingClosure& stop_callback,
  85. const std::u16string& helper_name) {
  86. has_share_ = true;
  87. }
  88. void ScreenSwitchCheckController::OnScreenShareStop() {
  89. // Multiple screen share sessions can exist, but they are stopped at once for
  90. // simplicity.
  91. has_share_ = false;
  92. }
  93. } // namespace ash