• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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 "chrome/browser/notifications/fullscreen_notification_blocker.h"
6 
7 #include "base/time/time.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/fullscreen.h"
10 #include "content/public/browser/notification_service.h"
11 
12 #if defined(USE_ASH)
13 #include "ash/root_window_controller.h"
14 #include "ash/shell.h"
15 #include "ash/system/system_notifier.h"
16 #include "ash/wm/window_state.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #endif
20 
21 namespace {
22 
DoesFullscreenModeBlockNotifications()23 bool DoesFullscreenModeBlockNotifications() {
24 #if defined(USE_ASH)
25   if (ash::Shell::HasInstance()) {
26     ash::RootWindowController* controller =
27         ash::RootWindowController::ForTargetRootWindow();
28 
29     // During shutdown |controller| can be NULL.
30     if (!controller)
31       return false;
32 
33     // Block notifications if the shelf is hidden because of a fullscreen
34     // window.
35     const aura::Window* fullscreen_window =
36         controller->GetWindowForFullscreenMode();
37     if (!fullscreen_window)
38       return false;
39     return ash::wm::GetWindowState(fullscreen_window)->
40         hide_shelf_when_fullscreen();
41   }
42 #endif
43 
44   return IsFullScreenMode();
45 }
46 
47 }  // namespace
48 
FullscreenNotificationBlocker(message_center::MessageCenter * message_center)49 FullscreenNotificationBlocker::FullscreenNotificationBlocker(
50     message_center::MessageCenter* message_center)
51     : NotificationBlocker(message_center),
52       is_fullscreen_mode_(false) {
53   registrar_.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED,
54                  content::NotificationService::AllSources());
55 }
56 
~FullscreenNotificationBlocker()57 FullscreenNotificationBlocker::~FullscreenNotificationBlocker() {
58 }
59 
CheckState()60 void FullscreenNotificationBlocker::CheckState() {
61   bool was_fullscreen_mode = is_fullscreen_mode_;
62   is_fullscreen_mode_ = DoesFullscreenModeBlockNotifications();
63   if (is_fullscreen_mode_ != was_fullscreen_mode)
64     NotifyBlockingStateChanged();
65 }
66 
ShouldShowNotificationAsPopup(const message_center::NotifierId & notifier_id) const67 bool FullscreenNotificationBlocker::ShouldShowNotificationAsPopup(
68     const message_center::NotifierId& notifier_id) const {
69   bool enabled = !is_fullscreen_mode_;
70 #if defined(USE_ASH)
71   if (ash::Shell::HasInstance())
72     enabled = enabled || ash::system_notifier::ShouldAlwaysShowPopups(
73         notifier_id);
74 #endif
75 
76   return enabled;
77 }
78 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)79 void FullscreenNotificationBlocker::Observe(
80     int type,
81     const content::NotificationSource& source,
82     const content::NotificationDetails& details) {
83   DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type);
84   CheckState();
85 }
86