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.
5 #ifndef CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/prefs/pref_member.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "chrome/browser/notifications/google_now_notification_stats_collector.h"
18 #include "chrome/browser/notifications/message_center_stats_collector.h"
19 #include "chrome/browser/notifications/notification.h"
20 #include "chrome/browser/notifications/notification_system_observer.h"
21 #include "chrome/browser/notifications/notification_ui_manager.h"
22 #include "ui/message_center/message_center.h"
23 #include "ui/message_center/message_center_observer.h"
24 #include "ui/message_center/message_center_tray_delegate.h"
25 #include "ui/message_center/message_center_types.h"
27 class MessageCenterSettingsController
;
29 class PrefRegistrySimple
;
32 class ProfileNotification
;
34 namespace message_center
{
35 class NotificationBlocker
;
36 FORWARD_DECLARE_TEST(WebNotificationTrayTest
, ManuallyCloseMessageCenter
);
39 // This class extends NotificationUIManagerImpl and delegates actual display
40 // of notifications to MessageCenter, doing necessary conversions.
41 class MessageCenterNotificationManager
42 : public NotificationUIManager
,
43 public message_center::MessageCenterObserver
{
45 MessageCenterNotificationManager(
46 message_center::MessageCenter
* message_center
,
47 PrefService
* local_state
,
48 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider
);
49 ~MessageCenterNotificationManager() override
;
51 // Registers preferences.
52 static void RegisterPrefs(PrefRegistrySimple
* registry
);
54 // NotificationUIManager
55 void Add(const Notification
& notification
, Profile
* profile
) override
;
56 bool Update(const Notification
& notification
, Profile
* profile
) override
;
57 const Notification
* FindById(const std::string
& delegate_id
,
58 ProfileID profile_id
) const override
;
59 bool CancelById(const std::string
& delegate_id
,
60 ProfileID profile_id
) override
;
61 std::set
<std::string
> GetAllIdsByProfileAndSourceOrigin(
63 const GURL
& source
) override
;
64 std::set
<std::string
> GetAllIdsByProfile(ProfileID profile_id
) override
;
65 bool CancelAllBySourceOrigin(const GURL
& source_origin
) override
;
66 bool CancelAllByProfile(ProfileID profile_id
) override
;
67 void CancelAll() override
;
69 // MessageCenterObserver
70 void OnNotificationRemoved(const std::string
& notification_id
,
71 bool by_user
) override
;
72 void OnCenterVisibilityChanged(message_center::Visibility
) override
;
73 void OnNotificationUpdated(const std::string
& notification_id
) override
;
75 void EnsureMessageCenterClosed();
78 // Called when the pref changes for the first run balloon. The first run
79 // balloon is only displayed on Windows, since the visibility of the tray
81 void DisplayFirstRunBalloon();
83 void SetFirstRunTimeoutForTest(base::TimeDelta timeout
);
84 bool FirstRunTimerIsActive() const;
87 // Takes ownership of |delegate|.
88 void SetMessageCenterTrayDelegateForTest(
89 message_center::MessageCenterTrayDelegate
* delegate
);
91 // Returns the notification id which this manager will use to add to message
92 // center, for this combination of delegate id and profile.
93 std::string
GetMessageCenterNotificationIdForTest(
94 const std::string
& delegate_id
, Profile
* profile
);
97 // Adds |profile_notification| to an alternative provider extension or app.
98 void AddNotificationToAlternateProvider(
99 const Notification
& notification
,
101 const std::string
& extension_id
) const;
103 FRIEND_TEST_ALL_PREFIXES(message_center::WebNotificationTrayTest
,
104 ManuallyCloseMessageCenter
);
106 scoped_ptr
<message_center::MessageCenterTrayDelegate
> tray_
;
107 message_center::MessageCenter
* message_center_
; // Weak, global.
109 // Use a map by notification_id since this mapping is the most often used.
110 typedef std::map
<std::string
, ProfileNotification
*> NotificationMap
;
111 NotificationMap profile_notifications_
;
113 // Helpers that add/remove the notification from local map.
114 // The local map takes ownership of profile_notification object.
115 void AddProfileNotification(ProfileNotification
* profile_notification
);
116 void RemoveProfileNotification(ProfileNotification
* profile_notification
);
118 // Returns the ProfileNotification for the |id|, or NULL if no such
119 // notification is found.
120 ProfileNotification
* FindProfileNotification(const std::string
& id
) const;
122 // Get the extension ID of the extension that the user chose to take over
123 // Chorme Notification Center.
124 std::string
GetExtensionTakingOverNotifications(Profile
* profile
);
127 // This function is run on update to ensure that the notification balloon is
128 // shown only when there are no popups present.
129 void CheckFirstRunTimer();
131 // |first_run_pref_| is used to keep track of whether we've ever shown the
132 // first run balloon before, even across restarts.
133 BooleanPrefMember first_run_pref_
;
135 // The timer after which we will show the first run balloon. This timer is
136 // restarted every time the message center is closed and every time the last
137 // popup disappears from the screen.
138 base::OneShotTimer
<MessageCenterNotificationManager
> first_run_balloon_timer_
;
140 // The first-run balloon will be shown |first_run_idle_timeout_| after all
141 // popups go away and the user has notifications in the message center.
142 base::TimeDelta first_run_idle_timeout_
;
145 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider_
;
147 // To own the blockers.
148 ScopedVector
<message_center::NotificationBlocker
> blockers_
;
150 NotificationSystemObserver system_observer_
;
152 // Keeps track of all notification statistics for UMA purposes.
153 MessageCenterStatsCollector stats_collector_
;
155 // Keeps track of notifications specific to Google Now for UMA purposes.
156 GoogleNowNotificationStatsCollector google_now_stats_collector_
;
159 // Provides weak pointers for the purpose of the first run timer.
160 base::WeakPtrFactory
<MessageCenterNotificationManager
> weak_factory_
;
163 DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager
);
166 #endif // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_