• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_
7 #pragma once
8 
9 #include <deque>
10 #include <string>
11 
12 #include "base/id_map.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/timer.h"
15 #include "chrome/browser/notifications/balloon.h"
16 #include "chrome/browser/notifications/balloon_collection.h"
17 #include "chrome/browser/prefs/pref_member.h"
18 #include "content/common/notification_observer.h"
19 #include "content/common/notification_registrar.h"
20 
21 class Notification;
22 class PrefService;
23 class Profile;
24 class QueuedNotification;
25 class SiteInstance;
26 
27 // The notification manager manages use of the desktop for notifications.
28 // It maintains a queue of pending notifications when space becomes constrained.
29 class NotificationUIManager
30     : public BalloonCollection::BalloonSpaceChangeListener,
31       public NotificationObserver {
32  public:
33   explicit NotificationUIManager(PrefService* local_state);
34   virtual ~NotificationUIManager();
35 
36   // Creates an initialized UI manager with a new balloon collection
37   // and the listener relationship setup.
38   // Except for unit tests, this is the way to construct the object.
39   static NotificationUIManager* Create(PrefService* local_state);
40 
41   // Registers preferences.
42   static void RegisterPrefs(PrefService* prefs);
43 
44   // Initializes the UI manager with a balloon collection; this object
45   // takes ownership of the balloon collection.
46   void Initialize(BalloonCollection* balloon_collection);
47 
48   // Adds a notification to be displayed. Virtual for unit test override.
49   virtual void Add(const Notification& notification,
50                    Profile* profile);
51 
52   // Removes any notifications matching the supplied ID, either currently
53   // displayed or in the queue.  Returns true if anything was removed.
54   virtual bool CancelById(const std::string& notification_id);
55 
56   // Removes any notifications matching the supplied source origin
57   // (which could be an extension ID), either currently displayed or in the
58   // queue.  Returns true if anything was removed.
59   virtual bool CancelAllBySourceOrigin(const GURL& source_origin);
60 
61   // Cancels all pending notifications and closes anything currently showing.
62   // Used when the app is terminating.
63   void CancelAll();
64 
65   // Returns balloon collection.
balloon_collection()66   BalloonCollection* balloon_collection() {
67     return balloon_collection_.get();
68   }
69 
70   // Gets the preference indicating where notifications should be placed.
71   BalloonCollection::PositionPreference GetPositionPreference();
72 
73   // Sets the preference that indicates where notifications should
74   // be placed on the screen.
75   void SetPositionPreference(BalloonCollection::PositionPreference preference);
76 
77  private:
78   // NotificationObserver override.
79   virtual void Observe(NotificationType type,
80                        const NotificationSource& source,
81                        const NotificationDetails& details);
82 
83   // Attempts to display notifications from the show_queue if the user
84   // is active.
85   void CheckAndShowNotifications();
86 
87   // Attempts to display notifications from the show_queue.
88   void ShowNotifications();
89 
90   // BalloonCollectionObserver implementation.
91   virtual void OnBalloonSpaceChanged();
92 
93   // Replace an existing notification with this one if applicable;
94   // returns true if the replacement happened.
95   bool TryReplacement(const Notification& notification);
96 
97   // Checks the user state to decide if we want to show the notification.
98   void CheckUserState();
99 
100   // An owned pointer to the collection of active balloons.
101   scoped_ptr<BalloonCollection> balloon_collection_;
102 
103   // A queue of notifications which are waiting to be shown.
104   typedef std::deque<QueuedNotification*> NotificationDeque;
105   NotificationDeque show_queue_;
106 
107   // Registrar for the other kind of notifications (event signaling).
108   NotificationRegistrar registrar_;
109 
110   // Prefs listener for the position preference.
111   IntegerPrefMember position_pref_;
112 
113   // Used by screen-saver and full-screen handling support.
114   bool is_user_active_;
115   base::RepeatingTimer<NotificationUIManager> user_state_check_timer_;
116 
117   DISALLOW_COPY_AND_ASSIGN(NotificationUIManager);
118 };
119 
120 #endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_
121