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(Profile
* profile
) 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 ProfileNotification
* profile_notification
,
100 const std::string
& extension_id
) const;
102 FRIEND_TEST_ALL_PREFIXES(message_center::WebNotificationTrayTest
,
103 ManuallyCloseMessageCenter
);
105 scoped_ptr
<message_center::MessageCenterTrayDelegate
> tray_
;
106 message_center::MessageCenter
* message_center_
; // Weak, global.
108 // Use a map by notification_id since this mapping is the most often used.
109 typedef std::map
<std::string
, ProfileNotification
*> NotificationMap
;
110 NotificationMap profile_notifications_
;
112 // Helpers that add/remove the notification from local map.
113 // The local map takes ownership of profile_notification object.
114 void AddProfileNotification(ProfileNotification
* profile_notification
);
115 void RemoveProfileNotification(ProfileNotification
* profile_notification
);
117 // Returns the ProfileNotification for the |id|, or NULL if no such
118 // notification is found.
119 ProfileNotification
* FindProfileNotification(const std::string
& id
) const;
121 // Get the extension ID of the extension that the user chose to take over
122 // Chorme Notification Center.
123 std::string
GetExtensionTakingOverNotifications(Profile
* profile
);
126 // This function is run on update to ensure that the notification balloon is
127 // shown only when there are no popups present.
128 void CheckFirstRunTimer();
130 // |first_run_pref_| is used to keep track of whether we've ever shown the
131 // first run balloon before, even across restarts.
132 BooleanPrefMember first_run_pref_
;
134 // The timer after which we will show the first run balloon. This timer is
135 // restarted every time the message center is closed and every time the last
136 // popup disappears from the screen.
137 base::OneShotTimer
<MessageCenterNotificationManager
> first_run_balloon_timer_
;
139 // The first-run balloon will be shown |first_run_idle_timeout_| after all
140 // popups go away and the user has notifications in the message center.
141 base::TimeDelta first_run_idle_timeout_
;
144 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider_
;
146 // To own the blockers.
147 ScopedVector
<message_center::NotificationBlocker
> blockers_
;
149 NotificationSystemObserver system_observer_
;
151 // Keeps track of all notification statistics for UMA purposes.
152 MessageCenterStatsCollector stats_collector_
;
154 // Keeps track of notifications specific to Google Now for UMA purposes.
155 GoogleNowNotificationStatsCollector google_now_stats_collector_
;
158 // Provides weak pointers for the purpose of the first run timer.
159 base::WeakPtrFactory
<MessageCenterNotificationManager
> weak_factory_
;
162 DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager
);
165 #endif // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_