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 UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ | 5 #ifndef UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ |
6 #define UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ | 6 #define UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ |
7 | 7 |
| 8 #include <string> |
| 9 |
8 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/observer_list.h" |
9 #include "ui/message_center/message_center_export.h" | 12 #include "ui/message_center/message_center_export.h" |
10 #include "ui/message_center/notification_list.h" | 13 #include "ui/message_center/notification_list.h" |
11 #include "ui/notifications/notification_types.h" | 14 #include "ui/notifications/notification_types.h" |
12 | 15 |
| 16 template <typename T> struct DefaultSingletonTraits; |
| 17 |
13 namespace base { | 18 namespace base { |
14 class DictionaryValue; | 19 class DictionaryValue; |
15 } | 20 } |
16 | 21 |
17 // Class for managing the NotificationList. The client (e.g. Chrome) calls | 22 // Class for managing the NotificationList. The client (e.g. Chrome) calls |
18 // [Add|Remove|Update]Notification to create and update notifications in the | 23 // [Add|Remove|Update]Notification to create and update notifications in the |
19 // list. It can also implement Delegate to receive callbacks when a | 24 // list. It can also implement Delegate to receive callbacks when a |
20 // notification is removed (closed), or clicked on. | 25 // notification is removed (closed), or clicked on. |
21 // If a Host is provided, it will be informed when the notification list | 26 // If an Observer is provided, it will be informed when the notification list |
22 // changes, and is expected to handle creating, showing, and hiding of any | 27 // changes, and is expected to handle creating, showing, and hiding of any |
23 // bubbles. | 28 // bubbles. |
24 | 29 |
25 namespace message_center { | 30 namespace message_center { |
26 | 31 |
27 class MESSAGE_CENTER_EXPORT MessageCenter : public NotificationList::Delegate { | 32 class MESSAGE_CENTER_EXPORT MessageCenter : public NotificationList::Delegate { |
28 public: | 33 public: |
29 // Class that hosts the message center. | 34 // Class that hosts the message center. |
30 class MESSAGE_CENTER_EXPORT Host { | 35 class MESSAGE_CENTER_EXPORT Observer { |
31 public: | 36 public: |
32 // Called when the notification list has changed. |new_notification| will | 37 // Called when the notification list has changed. |new_notification| will |
33 // be true if a notification was added or updated. | 38 // be true if a notification was added or updated. |
34 virtual void MessageCenterChanged(bool new_notification) = 0; | 39 virtual void OnMessageCenterChanged(bool new_notification) = 0; |
35 | |
36 protected: | 40 protected: |
37 virtual ~Host() {} | 41 virtual ~Observer() {} |
38 }; | 42 }; |
39 | 43 |
40 class MESSAGE_CENTER_EXPORT Delegate { | 44 class MESSAGE_CENTER_EXPORT Delegate { |
41 public: | 45 public: |
42 // Called when the notification associated with |notification_id| is | 46 // Called when the notification associated with |notification_id| is |
43 // removed (i.e. closed by the user). | 47 // removed (i.e. closed by the user). |
44 virtual void NotificationRemoved(const std::string& notification_id) = 0; | 48 virtual void NotificationRemoved(const std::string& notification_id) = 0; |
45 | 49 |
46 // Request to disable the extension associated with |notification_id|. | 50 // Request to disable the extension associated with |notification_id|. |
47 virtual void DisableExtension(const std::string& notification_id) = 0; | 51 virtual void DisableExtension(const std::string& notification_id) = 0; |
(...skipping 15 matching lines...) Expand all Loading... |
63 // | 67 // |
64 // TODO(miket): consider providing default implementations for the pure | 68 // TODO(miket): consider providing default implementations for the pure |
65 // virtuals above, to avoid changing so many files in disparate parts of | 69 // virtuals above, to avoid changing so many files in disparate parts of |
66 // the codebase each time we enhance this interface. | 70 // the codebase each time we enhance this interface. |
67 virtual void OnButtonClicked(const std::string& id, int button_index); | 71 virtual void OnButtonClicked(const std::string& id, int button_index); |
68 | 72 |
69 protected: | 73 protected: |
70 virtual ~Delegate() {} | 74 virtual ~Delegate() {} |
71 }; | 75 }; |
72 | 76 |
73 // |host| is expected to manage any notification bubbles. It may be NULL. | 77 static MessageCenter* GetInstance(); |
74 explicit MessageCenter(Host* host); | |
75 | 78 |
76 virtual ~MessageCenter(); | 79 virtual ~MessageCenter(); |
77 | 80 |
78 // Called once to set the delegate. | 81 // Called to set the delegate. Generally called only once, except in tests. |
| 82 // Changing the delegate does not affect notifications in its |
| 83 // NotificationList. |
79 void SetDelegate(Delegate* delegate); | 84 void SetDelegate(Delegate* delegate); |
80 | 85 |
| 86 // Management of the observer list. |
| 87 void AddObserver(Observer* observer); |
| 88 void RemoveObserver(Observer* observer); |
| 89 |
81 // Informs the notification list whether the message center is visible. | 90 // Informs the notification list whether the message center is visible. |
82 // This affects whether or not a message has been "read". | 91 // This affects whether or not a message has been "read". |
83 void SetMessageCenterVisible(bool visible); | 92 void SetMessageCenterVisible(bool visible); |
84 | 93 |
85 // Accessors to notification_list_ | 94 // Accessors to notification_list_ |
86 size_t NotificationCount() const; | 95 size_t NotificationCount() const; |
87 size_t UnreadNotificationCount() const; | 96 size_t UnreadNotificationCount() const; |
88 bool HasPopupNotifications() const; | 97 bool HasPopupNotifications() const; |
89 | 98 |
90 // Adds a new notification. |id| is a unique identifier, used to update or | 99 // Adds a new notification. |id| is a unique identifier, used to update or |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 virtual void DisableNotificationByExtension(const std::string& id) OVERRIDE; | 137 virtual void DisableNotificationByExtension(const std::string& id) OVERRIDE; |
129 virtual void DisableNotificationByUrl(const std::string& id) OVERRIDE; | 138 virtual void DisableNotificationByUrl(const std::string& id) OVERRIDE; |
130 virtual void ShowNotificationSettings(const std::string& id) OVERRIDE; | 139 virtual void ShowNotificationSettings(const std::string& id) OVERRIDE; |
131 virtual void OnNotificationClicked(const std::string& id) OVERRIDE; | 140 virtual void OnNotificationClicked(const std::string& id) OVERRIDE; |
132 virtual void OnQuietModeChanged(bool quiet_mode) OVERRIDE; | 141 virtual void OnQuietModeChanged(bool quiet_mode) OVERRIDE; |
133 virtual void OnButtonClicked(const std::string& id, int button_index) | 142 virtual void OnButtonClicked(const std::string& id, int button_index) |
134 OVERRIDE; | 143 OVERRIDE; |
135 virtual NotificationList* GetNotificationList() OVERRIDE; | 144 virtual NotificationList* GetNotificationList() OVERRIDE; |
136 | 145 |
137 private: | 146 private: |
| 147 friend struct DefaultSingletonTraits<MessageCenter>; |
| 148 MessageCenter(); |
| 149 |
| 150 // Calls OnMessageCenterChanged on each observer. |
| 151 void NotifyMessageCenterChanged(bool new_notification); |
| 152 |
138 scoped_ptr<NotificationList> notification_list_; | 153 scoped_ptr<NotificationList> notification_list_; |
139 Host* host_; | 154 ObserverList<Observer> observer_list_; |
140 Delegate* delegate_; | 155 Delegate* delegate_; |
141 | 156 |
142 DISALLOW_COPY_AND_ASSIGN(MessageCenter); | 157 DISALLOW_COPY_AND_ASSIGN(MessageCenter); |
143 }; | 158 }; |
144 | 159 |
145 } // namespace message_center | 160 } // namespace message_center |
146 | 161 |
147 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ | 162 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ |
OLD | NEW |