1 // Copyright (c) 2013 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_tray.h" 6 7 #include "base/observer_list.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "grit/ui_strings.h" 10 #include "ui/base/l10n/l10n_util.h" 11 #include "ui/message_center/message_center.h" 12 #include "ui/message_center/message_center_tray_delegate.h" 13 #include "ui/message_center/message_center_types.h" 14 #include "ui/message_center/notification_blocker.h" 15 16 namespace message_center { 17 18 MessageCenterTray::MessageCenterTray( 19 MessageCenterTrayDelegate* delegate, 20 message_center::MessageCenter* message_center) 21 : message_center_(message_center), 22 message_center_visible_(false), 23 popups_visible_(false), 24 delegate_(delegate) { 25 message_center_->AddObserver(this); 26 } 27 28 MessageCenterTray::~MessageCenterTray() { 29 message_center_->RemoveObserver(this); 30 } 31 32 bool MessageCenterTray::ShowMessageCenterBubble() { 33 if (message_center_visible_) 34 return true; 35 36 HidePopupBubbleInternal(); 37 38 message_center_visible_ = delegate_->ShowMessageCenter(); 39 message_center_->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER); 40 NotifyMessageCenterTrayChanged(); 41 return message_center_visible_; 42 } 43 44 bool MessageCenterTray::HideMessageCenterBubble() { 45 if (!message_center_visible_) 46 return false; 47 delegate_->HideMessageCenter(); 48 MarkMessageCenterHidden(); 49 return true; 50 } 51 52 void MessageCenterTray::MarkMessageCenterHidden() { 53 if (!message_center_visible_) 54 return; 55 message_center_visible_ = false; 56 message_center_->SetVisibility(message_center::VISIBILITY_TRANSIENT); 57 58 // Some notifications (like system ones) should appear as popups again 59 // after the message center is closed. 60 if (message_center_->HasPopupNotifications()) { 61 ShowPopupBubble(); 62 return; 63 } 64 65 NotifyMessageCenterTrayChanged(); 66 } 67 68 void MessageCenterTray::ToggleMessageCenterBubble() { 69 if (message_center_visible_) 70 HideMessageCenterBubble(); 71 else 72 ShowMessageCenterBubble(); 73 } 74 75 void MessageCenterTray::ShowPopupBubble() { 76 if (message_center_visible_) 77 return; 78 79 if (popups_visible_) { 80 NotifyMessageCenterTrayChanged(); 81 return; 82 } 83 84 if (!message_center_->HasPopupNotifications()) 85 return; 86 87 popups_visible_ = delegate_->ShowPopups(); 88 89 NotifyMessageCenterTrayChanged(); 90 } 91 92 bool MessageCenterTray::HidePopupBubble() { 93 if (!popups_visible_) 94 return false; 95 HidePopupBubbleInternal(); 96 NotifyMessageCenterTrayChanged(); 97 98 return true; 99 } 100 101 void MessageCenterTray::HidePopupBubbleInternal() { 102 if (!popups_visible_) 103 return; 104 105 delegate_->HidePopups(); 106 popups_visible_ = false; 107 } 108 109 void MessageCenterTray::ShowNotifierSettingsBubble() { 110 if (popups_visible_) 111 HidePopupBubbleInternal(); 112 113 message_center_visible_ = delegate_->ShowNotifierSettings(); 114 message_center_->SetVisibility(message_center::VISIBILITY_SETTINGS); 115 116 NotifyMessageCenterTrayChanged(); 117 } 118 119 void MessageCenterTray::OnNotificationAdded( 120 const std::string& notification_id) { 121 OnMessageCenterChanged(); 122 } 123 124 void MessageCenterTray::OnNotificationRemoved( 125 const std::string& notification_id, 126 bool by_user) { 127 OnMessageCenterChanged(); 128 } 129 130 void MessageCenterTray::OnNotificationUpdated( 131 const std::string& notification_id) { 132 OnMessageCenterChanged(); 133 } 134 135 void MessageCenterTray::OnNotificationClicked( 136 const std::string& notification_id) { 137 if (popups_visible_) 138 OnMessageCenterChanged(); 139 } 140 141 void MessageCenterTray::OnNotificationButtonClicked( 142 const std::string& notification_id, 143 int button_index) { 144 if (popups_visible_) 145 OnMessageCenterChanged(); 146 } 147 148 void MessageCenterTray::OnNotificationDisplayed( 149 const std::string& notification_id) { 150 NotifyMessageCenterTrayChanged(); 151 } 152 153 void MessageCenterTray::OnQuietModeChanged(bool in_quiet_mode) { 154 NotifyMessageCenterTrayChanged(); 155 } 156 157 void MessageCenterTray::OnBlockingStateChanged(NotificationBlocker* blocker) { 158 OnMessageCenterChanged(); 159 } 160 161 void MessageCenterTray::OnMessageCenterChanged() { 162 if (message_center_visible_ && message_center_->NotificationCount() == 0) 163 HideMessageCenterBubble(); 164 165 if (popups_visible_ && !message_center_->HasPopupNotifications()) 166 HidePopupBubbleInternal(); 167 else if (!popups_visible_ && message_center_->HasPopupNotifications()) 168 ShowPopupBubble(); 169 170 NotifyMessageCenterTrayChanged(); 171 } 172 173 void MessageCenterTray::NotifyMessageCenterTrayChanged() { 174 delegate_->OnMessageCenterTrayChanged(); 175 } 176 177 } // namespace message_center 178