fullscreen_notification_blocker.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2017 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/message_center/fullscreen_notification_blocker.h"
  5. #include "ash/root_window_controller.h"
  6. #include "ash/shell.h"
  7. #include "ash/wm/window_state.h"
  8. #include "base/metrics/histogram_macros.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/message_center/message_center.h"
  11. #include "ui/message_center/public/cpp/notifier_id.h"
  12. namespace ash {
  13. namespace {
  14. bool ShouldShow(const message_center::Notification& notification,
  15. bool is_fullscreen,
  16. bool include_uma) {
  17. // Show the notification if any of the following are true:
  18. // - we're not in fullscreen
  19. // - the notification explicitly asked to be shown over fullscreen
  20. // - the notification's priority is SYSTEM_PRIORITY
  21. bool enabled = !is_fullscreen ||
  22. (notification.fullscreen_visibility() !=
  23. message_center::FullscreenVisibility::NONE) ||
  24. notification.priority() == message_center::SYSTEM_PRIORITY;
  25. if (include_uma && enabled && !is_fullscreen) {
  26. UMA_HISTOGRAM_ENUMERATION("Notifications.Display_Windowed",
  27. notification.notifier_id().type);
  28. }
  29. return enabled;
  30. }
  31. } // namespace
  32. // static
  33. bool FullscreenNotificationBlocker::BlockForMixedFullscreen(
  34. const message_center::Notification& notification,
  35. bool is_fullscreen) {
  36. return !ShouldShow(notification, is_fullscreen,
  37. /*include_uma=*/false);
  38. }
  39. FullscreenNotificationBlocker::FullscreenNotificationBlocker(
  40. message_center::MessageCenter* message_center)
  41. : NotificationBlocker(message_center) {
  42. Shell::Get()->AddShellObserver(this);
  43. }
  44. FullscreenNotificationBlocker::~FullscreenNotificationBlocker() {
  45. Shell::Get()->RemoveShellObserver(this);
  46. }
  47. bool FullscreenNotificationBlocker::ShouldShowNotificationAsPopup(
  48. const message_center::Notification& notification) const {
  49. return ShouldShow(notification, all_fullscreen_, /*include_uma=*/true);
  50. }
  51. void FullscreenNotificationBlocker::OnFullscreenStateChanged(
  52. bool is_fullscreen,
  53. aura::Window* container) {
  54. // Block notifications if all displays have a fullscreen window. Otherwise
  55. // include the notification and only fullscreen windows will filter it.
  56. all_fullscreen_ = true;
  57. for (auto* controller : RootWindowController::root_window_controllers()) {
  58. // During shutdown |controller| can be nullptr.
  59. controller = RootWindowController::ForWindow(controller->GetRootWindow());
  60. if (controller && !controller->IsInFullscreenMode()) {
  61. all_fullscreen_ = false;
  62. break;
  63. }
  64. }
  65. // Any change to fullscreen state on any of the displays requires
  66. // MessagePopupCollection instances to recheck.
  67. NotifyBlockingStateChanged();
  68. }
  69. } // namespace ash