Home | History | Annotate | Download | only in notifications
      1 // Copyright (c) 2012 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_IMPL_H_
      6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_IMPL_H_
      7 
      8 #include <deque>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/id_map.h"
     13 #include "base/memory/linked_ptr.h"
     14 #include "base/prefs/pref_member.h"
     15 #include "base/timer/timer.h"
     16 #include "chrome/browser/notifications/notification_ui_manager.h"
     17 #include "content/public/browser/notification_observer.h"
     18 #include "content/public/browser/notification_registrar.h"
     19 
     20 class Notification;
     21 class Profile;
     22 class QueuedNotification;
     23 
     24 // The notification manager manages use of the desktop for notifications.
     25 // It maintains a queue of pending notifications when space becomes constrained.
     26 // Subclasses manage actual display and UI.
     27 class NotificationUIManagerImpl
     28     : public NotificationUIManager,
     29       public content::NotificationObserver {
     30  public:
     31   NotificationUIManagerImpl();
     32   virtual ~NotificationUIManagerImpl();
     33 
     34   // NotificationUIManager:
     35   virtual void Add(const Notification& notification,
     36                    Profile* profile) OVERRIDE;
     37   virtual bool Update(const Notification& notification,
     38                       Profile* profile) OVERRIDE;
     39   virtual const Notification* FindById(
     40       const std::string& notification_id) const OVERRIDE;
     41   virtual bool CancelById(const std::string& notification_id) OVERRIDE;
     42   virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
     43       Profile* profile,
     44       const GURL& source) OVERRIDE;
     45   virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
     46   virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
     47   virtual void CancelAll() OVERRIDE;
     48 
     49   void GetQueuedNotificationsForTesting(
     50     std::vector<const Notification*>* notifications);
     51 
     52  protected:
     53   // Attempts to pass a notification from a waiting queue to the subclass for
     54   // presentation. The subclass can return 'false' if it cannot show the
     55   // notification right away. In that case it should invoke
     56   // CheckAndShowNotificaitons() later.
     57   virtual bool ShowNotification(const Notification& notification,
     58                                 Profile* profile) = 0;
     59 
     60   // Replace an existing notification of the same id with this one if
     61   // applicable. Subclass returns 'true' if the replacement happened.
     62   virtual bool UpdateNotification(const Notification& notification,
     63                                   Profile* profile) = 0;
     64 
     65   // Attempts to display notifications from the show_queue. Invoked by
     66   // subclasses if they previously returned 'false' from ShowNotifications,
     67   // which may happen when there is no room to show another notification. When
     68   // room appears, the subclass should call this method to cause an attempt to
     69   // show more notifications from the waiting queue.
     70   void CheckAndShowNotifications();
     71 
     72   // content::NotificationObserver override.
     73   virtual void Observe(int type,
     74                        const content::NotificationSource& source,
     75                        const content::NotificationDetails& details) OVERRIDE;
     76 
     77  private:
     78   // Attempts to display notifications from the show_queue.
     79   void ShowNotifications();
     80 
     81   // Checks the user state to decide if we want to show the notification.
     82   void CheckUserState();
     83 
     84   // A queue of notifications which are waiting to be shown.
     85   typedef std::deque<linked_ptr<QueuedNotification> > NotificationDeque;
     86   NotificationDeque show_queue_;
     87 
     88   // Registrar for the other kind of notifications (event signaling).
     89   content::NotificationRegistrar registrar_;
     90 
     91   // Used by screen-saver and full-screen handling support.
     92   bool is_user_active_;
     93   base::RepeatingTimer<NotificationUIManagerImpl> user_state_check_timer_;
     94 
     95   DISALLOW_COPY_AND_ASSIGN(NotificationUIManagerImpl);
     96 };
     97 
     98 #endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_IMPL_H_
     99