OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_ |
6 #define CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_ | 6 #define CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_ |
7 | 7 |
| 8 #include <map> |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/notifications/notification.h" |
11 #include "chrome/browser/notifications/notification_ui_manager.h" | 13 #include "chrome/browser/notifications/notification_ui_manager.h" |
12 #include "chrome/browser/notifications/notification_ui_manager_impl.h" | 14 #include "chrome/browser/notifications/notification_ui_manager_impl.h" |
| 15 #include "ui/message_center/message_center.h" |
13 | 16 |
14 class Notification; | 17 class Notification; |
15 class Profile; | 18 class Profile; |
16 | 19 |
17 // This class extends NotificationUIManagerImpl and delegates actual display | 20 // This class extends NotificationUIManagerImpl and delegates actual display |
18 // of notifications to MessageCenter, doing necessary conversions. | 21 // of notifications to MessageCenter, doing necessary conversions. |
19 class MessageCenterNotificationManager : public NotificationUIManagerImpl { | 22 class MessageCenterNotificationManager |
| 23 : public NotificationUIManagerImpl, |
| 24 public message_center::MessageCenter::Delegate { |
20 public: | 25 public: |
21 MessageCenterNotificationManager(); | 26 explicit MessageCenterNotificationManager( |
| 27 message_center::MessageCenter* message_center); |
22 virtual ~MessageCenterNotificationManager(); | 28 virtual ~MessageCenterNotificationManager(); |
23 | 29 |
24 // NotificationUIManager: | 30 // NotificationUIManager |
25 virtual bool CancelById(const std::string& notification_id) OVERRIDE; | 31 virtual bool CancelById(const std::string& notification_id) OVERRIDE; |
26 virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE; | 32 virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE; |
27 virtual bool CancelAllByProfile(Profile* profile) OVERRIDE; | 33 virtual bool CancelAllByProfile(Profile* profile) OVERRIDE; |
28 virtual void CancelAll() OVERRIDE; | 34 virtual void CancelAll() OVERRIDE; |
29 | 35 |
30 // NotificationUIManagerImpl: | 36 // NotificationUIManagerImpl |
31 virtual bool ShowNotification(const Notification& notification, | 37 virtual bool ShowNotification(const Notification& notification, |
32 Profile* profile) OVERRIDE; | 38 Profile* profile) OVERRIDE; |
33 virtual bool UpdateNotification(const Notification& notification) OVERRIDE; | 39 virtual bool UpdateNotification(const Notification& notification, |
| 40 Profile* profile) OVERRIDE; |
| 41 |
| 42 // MessageCenter::Delegate |
| 43 virtual void NotificationRemoved(const std::string& notification_id) OVERRIDE; |
| 44 virtual void DisableExtension(const std::string& notification_id) OVERRIDE; |
| 45 virtual void DisableNotificationsFromSource( |
| 46 const std::string& notification_id) OVERRIDE; |
| 47 virtual void ShowSettings(const std::string& notification_id) OVERRIDE; |
| 48 virtual void OnClicked(const std::string& notification_id) OVERRIDE; |
| 49 virtual void OnButtonClicked(const std::string& notification_id, |
| 50 int button_index) OVERRIDE; |
34 | 51 |
35 private: | 52 private: |
| 53 message_center::MessageCenter* message_center_; // Weak, global. |
| 54 |
| 55 // This class keeps a set of original Notification objects and corresponding |
| 56 // Profiles, so when MessageCenter calls back with a notification_id, this |
| 57 // class has necessary mapping to other source info - for example, it calls |
| 58 // NotificationDelegate supplied by client when someone clicks on a Notification |
| 59 // in MessageCenter. Likewise, if a Profile or Extension is being removed, the |
| 60 // map makes it possible to revoke the notifications from MessageCenter. |
| 61 // To keep that set, we use the private ProfileNotification class that stores |
| 62 // a superset of all information about a notification. |
| 63 |
| 64 // TODO(dimich): Consider merging all 4 types (Notification, QueuedNotification, |
| 65 // ProfileNotification and NotificationList::Notification) into a single class. |
| 66 class ProfileNotification { |
| 67 public: |
| 68 ProfileNotification(Profile* profile, const Notification& notification); |
| 69 |
| 70 Profile* profile() const { return profile_; } |
| 71 const Notification& notification() const { return notification_; } |
| 72 |
| 73 // Returns extension_id if the notification originates from an extension, |
| 74 // empty string otherwise. |
| 75 std::string GetExtensionId(); |
| 76 |
| 77 private: |
| 78 // Weak, guaranteed not to be used after profile removal by parent class. |
| 79 Profile* profile_; |
| 80 Notification notification_; |
| 81 }; |
| 82 |
| 83 // Use a map by notification_id since this mapping is the most often used. |
| 84 typedef std::map<std::string, ProfileNotification*> NotificationMap; |
| 85 NotificationMap profile_notifications_; |
| 86 |
| 87 // Helpers that add/remove the notification from local map and MessageCenter. |
| 88 // They take ownership of profile_notification object. |
| 89 void AddProfileNotification(ProfileNotification* profile_notification); |
| 90 void RemoveProfileNotification(ProfileNotification* profile_notification); |
| 91 |
36 DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager); | 92 DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager); |
37 }; | 93 }; |
38 | 94 |
39 #endif // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_ | 95 #endif // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_ |
40 | 96 |
41 | 97 |
OLD | NEW |