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/time/time.h"
15 #include "base/timer/timer.h"
16 #include "chrome/browser/notifications/google_now_notification_stats_collector.h"
17 #include "chrome/browser/notifications/message_center_stats_collector.h"
18 #include "chrome/browser/notifications/notification.h"
19 #include "chrome/browser/notifications/notification_system_observer.h"
20 #include "chrome/browser/notifications/notification_ui_manager.h"
21 #include "ui/message_center/message_center.h"
22 #include "ui/message_center/message_center_observer.h"
23 #include "ui/message_center/message_center_tray_delegate.h"
24 #include "ui/message_center/message_center_types.h"
26 class MessageCenterSettingsController
;
29 class ProfileNotification
;
31 namespace message_center
{
32 class NotificationBlocker
;
33 FORWARD_DECLARE_TEST(WebNotificationTrayTest
, ManuallyCloseMessageCenter
);
36 // This class extends NotificationUIManagerImpl and delegates actual display
37 // of notifications to MessageCenter, doing necessary conversions.
38 class MessageCenterNotificationManager
39 : public NotificationUIManager
,
40 public message_center::MessageCenterObserver
{
42 MessageCenterNotificationManager(
43 message_center::MessageCenter
* message_center
,
44 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider
);
45 ~MessageCenterNotificationManager() override
;
47 // NotificationUIManager
48 void Add(const Notification
& notification
, Profile
* profile
) override
;
49 bool Update(const Notification
& notification
, Profile
* profile
) override
;
50 const Notification
* FindById(const std::string
& delegate_id
,
51 ProfileID profile_id
) const override
;
52 bool CancelById(const std::string
& delegate_id
,
53 ProfileID profile_id
) override
;
54 std::set
<std::string
> GetAllIdsByProfileAndSourceOrigin(
56 const GURL
& source
) override
;
57 std::set
<std::string
> GetAllIdsByProfile(ProfileID profile_id
) override
;
58 bool CancelAllBySourceOrigin(const GURL
& source_origin
) override
;
59 bool CancelAllByProfile(ProfileID profile_id
) override
;
60 void CancelAll() override
;
62 // MessageCenterObserver
63 void OnNotificationRemoved(const std::string
& notification_id
,
64 bool by_user
) override
;
65 void OnCenterVisibilityChanged(message_center::Visibility
) override
;
66 void OnNotificationUpdated(const std::string
& notification_id
) override
;
68 void EnsureMessageCenterClosed();
70 // Takes ownership of |delegate|.
71 void SetMessageCenterTrayDelegateForTest(
72 message_center::MessageCenterTrayDelegate
* delegate
);
74 // Returns the notification id which this manager will use to add to message
75 // center, for this combination of delegate id and profile.
76 std::string
GetMessageCenterNotificationIdForTest(
77 const std::string
& delegate_id
, Profile
* profile
);
80 // Adds |profile_notification| to an alternative provider extension or app.
81 void AddNotificationToAlternateProvider(
82 const Notification
& notification
,
84 const std::string
& extension_id
) const;
86 FRIEND_TEST_ALL_PREFIXES(message_center::WebNotificationTrayTest
,
87 ManuallyCloseMessageCenter
);
89 scoped_ptr
<message_center::MessageCenterTrayDelegate
> tray_
;
90 message_center::MessageCenter
* message_center_
; // Weak, global.
92 // Use a map by notification_id since this mapping is the most often used.
93 typedef std::map
<std::string
, ProfileNotification
*> NotificationMap
;
94 NotificationMap profile_notifications_
;
96 // Helpers that add/remove the notification from local map.
97 // The local map takes ownership of profile_notification object.
98 void AddProfileNotification(ProfileNotification
* profile_notification
);
99 void RemoveProfileNotification(ProfileNotification
* profile_notification
);
101 // Returns the ProfileNotification for the |id|, or NULL if no such
102 // notification is found.
103 ProfileNotification
* FindProfileNotification(const std::string
& id
) const;
105 // Get the extension ID of the extension that the user chose to take over
106 // Chorme Notification Center.
107 std::string
GetExtensionTakingOverNotifications(Profile
* profile
);
109 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider_
;
111 // To own the blockers.
112 ScopedVector
<message_center::NotificationBlocker
> blockers_
;
114 NotificationSystemObserver system_observer_
;
116 // Keeps track of all notification statistics for UMA purposes.
117 MessageCenterStatsCollector stats_collector_
;
119 // Keeps track of notifications specific to Google Now for UMA purposes.
120 GoogleNowNotificationStatsCollector google_now_stats_collector_
;
122 DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager
);
125 #endif // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_