message_center.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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. 4 5#ifndef UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ 6#define UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ 7 8#include "base/memory/scoped_ptr.h" 9#include "ui/message_center/message_center_export.h" 10#include "ui/message_center/notification_list.h" 11#include "ui/notifications/notification_types.h" 12 13namespace base { 14class DictionaryValue; 15} 16 17// Class for managing the NotificationList. The client (e.g. Chrome) calls 18// [Add|Remove|Update]Notification to create and update notifications in the 19// list. It can also implement Delegate to receive callbacks when a 20// notification is removed (closed), or clicked on. 21// If a Host is provided, it will be informed when the notification list 22// changes, and is expected to handle creating, showing, and hiding of any 23// bubbles. 24 25namespace message_center { 26 27class MESSAGE_CENTER_EXPORT MessageCenter : public NotificationList::Delegate { 28 public: 29 // Class that hosts the message center. 30 class MESSAGE_CENTER_EXPORT Host { 31 public: 32 // Called when the notification list has changed. |new_notification| will 33 // be true if a notification was added or updated. 34 virtual void MessageCenterChanged(bool new_notification) = 0; 35 36 protected: 37 virtual ~Host() {} 38 }; 39 40 class MESSAGE_CENTER_EXPORT Delegate { 41 public: 42 // Called when the notification associated with |notification_id| is 43 // removed (i.e. closed by the user). 44 virtual void NotificationRemoved(const std::string& notifcation_id) = 0; 45 46 // Request to disable the extension associated with |notification_id|. 47 virtual void DisableExtension(const std::string& notifcation_id) = 0; 48 49 // Request to disable notifications from the source of |notification_id|. 50 virtual void DisableNotificationsFromSource( 51 const std::string& notifcation_id) = 0; 52 53 // Request to show the notification settings (|notification_id| is used 54 // to identify the requesting browser context). 55 virtual void ShowSettings(const std::string& notifcation_id) = 0; 56 57 // Called when the notification body is clicked on. 58 virtual void OnClicked(const std::string& notifcation_id) = 0; 59 60 protected: 61 virtual ~Delegate() {} 62 }; 63 64 // |host| is expected to manage any notification bubbles. It may be NULL. 65 explicit MessageCenter(Host* host); 66 67 virtual ~MessageCenter(); 68 69 // Called once to set the delegate. 70 void SetDelegate(Delegate* delegate); 71 72 // Informs the notification list whether the message center is visible. 73 // This affects whether or not a message has been "read". 74 void SetMessageCenterVisible(bool visible); 75 76 // Accessors to notification_list_ 77 size_t NotificationCount() const; 78 size_t UnreadNotificationCount() const; 79 bool HasPopupNotifications() const; 80 81 // Adds a new notification. |id| is a unique identifier, used to update or 82 // remove notifications. |title| and |meesage| describe the notification text. 83 // Use SetNotificationImage to set the icon image. If |extension_id| is 84 // provided then 'Disable extension' will appear in a dropdown menu and the 85 // id will be used to disable notifications from the extension. Otherwise if 86 // |display_source| is provided, a menu item showing the source and allowing 87 // notifications from that source to be disabled will be shown. All actual 88 // disabling is handled by the Delegate. 89 void AddNotification(ui::notifications::NotificationType type, 90 const std::string& id, 91 const string16& title, 92 const string16& message, 93 const string16& display_source, 94 const std::string& extension_id, 95 const base::DictionaryValue* optional_fields); 96 97 // Updates an existing notification with id = old_id and set its id to new_id. 98 void UpdateNotification(const std::string& old_id, 99 const std::string& new_id, 100 const string16& title, 101 const string16& message); 102 103 // Removes an existing notification. 104 void RemoveNotification(const std::string& id); 105 106 // Sets the notification image. 107 void SetNotificationImage(const std::string& id, 108 const gfx::ImageSkia& image); 109 110 NotificationList* notification_list() { return notification_list_.get(); } 111 112 // Overridden from NotificationList::Delegate. 113 virtual void SendRemoveNotification(const std::string& id) OVERRIDE; 114 virtual void SendRemoveAllNotifications() OVERRIDE; 115 virtual void DisableNotificationByExtension(const std::string& id) OVERRIDE; 116 virtual void DisableNotificationByUrl(const std::string& id) OVERRIDE; 117 virtual void ShowNotificationSettings(const std::string& id) OVERRIDE; 118 virtual void OnNotificationClicked(const std::string& id) OVERRIDE; 119 virtual NotificationList* GetNotificationList() OVERRIDE; 120 121 private: 122 scoped_ptr<NotificationList> notification_list_; 123 Host* host_; 124 Delegate* delegate_; 125 126 DISALLOW_COPY_AND_ASSIGN(MessageCenter); 127}; 128 129} // namespace message_center 130 131#endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ 132