summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/notification_ui_manager_impl.h
blob: f8544ea38a06e6a99b5ef47f5e6452e08ac36135 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_IMPL_H_
#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_IMPL_H_

#include <deque>
#include <string>
#include <vector>

#include "base/id_map.h"
#include "base/memory/linked_ptr.h"
#include "base/prefs/pref_member.h"
#include "base/timer/timer.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

class Notification;
class Profile;
class QueuedNotification;

// The notification manager manages use of the desktop for notifications.
// It maintains a queue of pending notifications when space becomes constrained.
// Subclasses manage actual display and UI.
class NotificationUIManagerImpl
    : public NotificationUIManager,
      public content::NotificationObserver {
 public:
  NotificationUIManagerImpl();
  virtual ~NotificationUIManagerImpl();

  // NotificationUIManager:
  virtual void Add(const Notification& notification,
                   Profile* profile) OVERRIDE;
  virtual bool DoesIdExist(const std::string& notification_id) OVERRIDE;
  virtual bool CancelById(const std::string& notification_id) OVERRIDE;
  virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
      Profile* profile,
      const GURL& source) OVERRIDE;
  virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
  virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
  virtual void CancelAll() OVERRIDE;

  void GetQueuedNotificationsForTesting(
    std::vector<const Notification*>* notifications);

 protected:
  // Attempts to pass a notification from a waiting queue to the subclass for
  // presentation. The subclass can return 'false' if it cannot show the
  // notification right away. In that case it should invoke
  // CheckAndShowNotificaitons() later.
  virtual bool ShowNotification(const Notification& notification,
                                Profile* profile) = 0;

 // Replace an existing notification of the same id with this one if applicable;
 // subclass returns 'true' if the replacement happened.
 virtual bool UpdateNotification(const Notification& notification,
                                 Profile* profile) = 0;

 // Attempts to display notifications from the show_queue. Invoked by subclasses
 // if they previously returned 'false' from ShowNotifications, which may happen
 // when there is no room to show another notification. When room appears, the
 // subclass should call this method to cause an attempt to show more
 // notifications from the waiting queue.
 void CheckAndShowNotifications();

 private:
  // content::NotificationObserver override.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  // Attempts to display notifications from the show_queue.
  void ShowNotifications();

  // Replace an existing notification with this one if applicable;
  // returns true if the replacement happened.
  bool TryReplacement(const Notification& notification, Profile* profile);

  // Checks the user state to decide if we want to show the notification.
  void CheckUserState();

  // A queue of notifications which are waiting to be shown.
  typedef std::deque<linked_ptr<QueuedNotification> > NotificationDeque;
  NotificationDeque show_queue_;

  // Registrar for the other kind of notifications (event signaling).
  content::NotificationRegistrar registrar_;

  // Used by screen-saver and full-screen handling support.
  bool is_user_active_;
  base::RepeatingTimer<NotificationUIManagerImpl> user_state_check_timer_;

  DISALLOW_COPY_AND_ASSIGN(NotificationUIManagerImpl);
};

#endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_IMPL_H_