1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <deque>
10201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include <string>
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/id_map.h"
13ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h"
1472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "base/timer.h"
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/notifications/balloon.h"
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/notifications/balloon_collection.h"
173f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#include "chrome/browser/prefs/pref_member.h"
18ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_observer.h"
19ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_registrar.h"
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass Notification;
223f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsenclass PrefService;
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass Profile;
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass QueuedNotification;
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SiteInstance;
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The notification manager manages use of the desktop for notifications.
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// It maintains a queue of pending notifications when space becomes constrained.
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass NotificationUIManager
30201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    : public BalloonCollection::BalloonSpaceChangeListener,
31201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch      public NotificationObserver {
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
333f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  explicit NotificationUIManager(PrefService* local_state);
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual ~NotificationUIManager();
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Creates an initialized UI manager with a new balloon collection
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // and the listener relationship setup.
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Except for unit tests, this is the way to construct the object.
393f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  static NotificationUIManager* Create(PrefService* local_state);
403f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
413f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Registers preferences.
423f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  static void RegisterPrefs(PrefService* prefs);
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Initializes the UI manager with a balloon collection; this object
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // takes ownership of the balloon collection.
463f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  void Initialize(BalloonCollection* balloon_collection);
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Adds a notification to be displayed. Virtual for unit test override.
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void Add(const Notification& notification,
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                   Profile* profile);
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
52201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // Removes any notifications matching the supplied ID, either currently
53201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // displayed or in the queue.  Returns true if anything was removed.
54201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  virtual bool CancelById(const std::string& notification_id);
55201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
56201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // Removes any notifications matching the supplied source origin
57201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // (which could be an extension ID), either currently displayed or in the
58201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // queue.  Returns true if anything was removed.
59201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  virtual bool CancelAllBySourceOrigin(const GURL& source_origin);
60201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
61201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // Cancels all pending notifications and closes anything currently showing.
62201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // Used when the app is terminating.
63201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  void CancelAll();
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns balloon collection.
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  BalloonCollection* balloon_collection() {
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return balloon_collection_.get();
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
703f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Gets the preference indicating where notifications should be placed.
713f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  BalloonCollection::PositionPreference GetPositionPreference();
723f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
733f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Sets the preference that indicates where notifications should
743f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // be placed on the screen.
753f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  void SetPositionPreference(BalloonCollection::PositionPreference preference);
763f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
7772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen private:
7872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // NotificationObserver override.
79201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  virtual void Observe(NotificationType type,
80201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch                       const NotificationSource& source,
81201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch                       const NotificationDetails& details);
82201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Attempts to display notifications from the show_queue if the user
84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // is active.
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void CheckAndShowNotifications();
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Attempts to display notifications from the show_queue.
88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void ShowNotifications();
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // BalloonCollectionObserver implementation.
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void OnBalloonSpaceChanged();
92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Replace an existing notification with this one if applicable;
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // returns true if the replacement happened.
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool TryReplacement(const Notification& notification);
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
9772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Checks the user state to decide if we want to show the notification.
9872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void CheckUserState();
9972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // An owned pointer to the collection of active balloons.
101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  scoped_ptr<BalloonCollection> balloon_collection_;
102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // A queue of notifications which are waiting to be shown.
104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::deque<QueuedNotification*> NotificationDeque;
105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  NotificationDeque show_queue_;
106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
107201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // Registrar for the other kind of notifications (event signaling).
108201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  NotificationRegistrar registrar_;
109201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
1103f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Prefs listener for the position preference.
1113f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  IntegerPrefMember position_pref_;
1123f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
11372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Used by screen-saver and full-screen handling support.
11472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool is_user_active_;
11572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  base::RepeatingTimer<NotificationUIManager> user_state_check_timer_;
11672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_COPY_AND_ASSIGN(NotificationUIManager);
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_UI_MANAGER_H_
121