message_center.cc 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#include "ui/message_center/message_center.h" 6 7#include "base/logging.h" 8 9namespace message_center { 10 11//------------------------------------------------------------------------------ 12 13MessageCenter::MessageCenter(Host* host) 14 : host_(host), 15 delegate_(NULL) { 16 notification_list_.reset(new NotificationList(this)); 17} 18 19MessageCenter::~MessageCenter() { 20 notification_list_.reset(); 21} 22 23void MessageCenter::SetDelegate(Delegate* delegate) { 24 DCHECK(!delegate_); 25 delegate_ = delegate; 26} 27 28void MessageCenter::SetMessageCenterVisible(bool visible) { 29 notification_list_->SetMessageCenterVisible(visible); 30} 31 32size_t MessageCenter::NotificationCount() const { 33 return notification_list_->notifications().size(); 34} 35 36size_t MessageCenter::UnreadNotificationCount() const { 37 return notification_list_->unread_count(); 38} 39 40bool MessageCenter::HasPopupNotifications() const { 41 return notification_list_->HasPopupNotifications(); 42} 43 44//------------------------------------------------------------------------------ 45// Client code interface. 46 47void MessageCenter::AddNotification( 48 ui::notifications::NotificationType type, 49 const std::string& id, 50 const string16& title, 51 const string16& message, 52 const string16& display_source, 53 const std::string& extension_id, 54 const base::DictionaryValue* optional_fields) { 55 notification_list_->AddNotification(type, id, title, message, display_source, 56 extension_id, optional_fields); 57 if (host_) 58 host_->MessageCenterChanged(true); 59} 60 61void MessageCenter::UpdateNotification(const std::string& old_id, 62 const std::string& new_id, 63 const string16& title, 64 const string16& message) { 65 notification_list_->UpdateNotificationMessage( 66 old_id, new_id, title, message); 67 if (host_) 68 host_->MessageCenterChanged(true); 69} 70 71void MessageCenter::RemoveNotification(const std::string& id) { 72 if (!notification_list_->RemoveNotification(id)) 73 return; 74 if (host_) 75 host_->MessageCenterChanged(false); 76} 77 78void MessageCenter::SetNotificationImage(const std::string& id, 79 const gfx::ImageSkia& image) { 80 if (!notification_list_->SetNotificationImage(id, image)) 81 return; 82 if (host_) 83 host_->MessageCenterChanged(true); 84} 85 86//------------------------------------------------------------------------------ 87// Overridden from NotificationList::Delegate. 88 89void MessageCenter::SendRemoveNotification(const std::string& id) { 90 if (delegate_) 91 delegate_->NotificationRemoved(id); 92} 93 94void MessageCenter::SendRemoveAllNotifications() { 95 if (delegate_) { 96 const NotificationList::Notifications& notifications = 97 notification_list_->notifications(); 98 for (NotificationList::Notifications::const_iterator loopiter = 99 notifications.begin(); 100 loopiter != notifications.end(); ) { 101 NotificationList::Notifications::const_iterator curiter = loopiter++; 102 std::string notification_id = curiter->id; 103 // May call RemoveNotification and erase curiter. 104 delegate_->NotificationRemoved(notification_id); 105 } 106 } 107} 108 109void MessageCenter::DisableNotificationByExtension( 110 const std::string& id) { 111 if (delegate_) 112 delegate_->DisableExtension(id); 113 // When we disable notifications, we remove any existing matching 114 // notifications to avoid adding complicated UI to re-enable the source. 115 notification_list_->SendRemoveNotificationsByExtension(id); 116} 117 118void MessageCenter::DisableNotificationByUrl(const std::string& id) { 119 if (delegate_) 120 delegate_->DisableNotificationsFromSource(id); 121 notification_list_->SendRemoveNotificationsBySource(id); 122} 123 124void MessageCenter::ShowNotificationSettings(const std::string& id) { 125 if (delegate_) 126 delegate_->ShowSettings(id); 127} 128 129void MessageCenter::OnNotificationClicked(const std::string& id) { 130 if (delegate_) 131 delegate_->OnClicked(id); 132} 133 134NotificationList* MessageCenter::GetNotificationList() { 135 return notification_list_.get(); 136} 137 138} // namespace message_center 139