Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / ui / message_center / message_center_tray.cc
blob3b2cbac7be7ca992f5d739079a777a7b3342d5ba
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.
5 #include "ui/message_center/message_center_tray.h"
7 #include "base/observer_list.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/base/models/simple_menu_model.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 #include "ui/strings/grit/ui_strings.h"
17 namespace message_center {
19 namespace {
21 // Menu constants
22 const int kTogglePermissionCommand = 0;
24 #if defined(OS_CHROMEOS)
25 const int kShowSettingsCommand = 1;
26 #endif
28 // The model of the context menu for a notification card.
29 class NotificationMenuModel : public ui::SimpleMenuModel,
30 public ui::SimpleMenuModel::Delegate {
31 public:
32 NotificationMenuModel(MessageCenterTray* tray,
33 const NotifierId& notifier_id,
34 const base::string16& display_source);
35 ~NotificationMenuModel() override;
37 // Overridden from ui::SimpleMenuModel::Delegate:
38 bool IsCommandIdChecked(int command_id) const override;
39 bool IsCommandIdEnabled(int command_id) const override;
40 bool GetAcceleratorForCommandId(int command_id,
41 ui::Accelerator* accelerator) override;
42 void ExecuteCommand(int command_id, int event_flags) override;
44 private:
45 MessageCenterTray* tray_;
46 NotifierId notifier_id_;
47 DISALLOW_COPY_AND_ASSIGN(NotificationMenuModel);
50 NotificationMenuModel::NotificationMenuModel(
51 MessageCenterTray* tray,
52 const NotifierId& notifier_id,
53 const base::string16& display_source)
54 : ui::SimpleMenuModel(this),
55 tray_(tray),
56 notifier_id_(notifier_id) {
57 if (!display_source.empty()) {
58 AddItem(kTogglePermissionCommand,
59 l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_NOTIFIER_DISABLE,
60 display_source));
63 #ifdef OS_CHROMEOS
64 // Add settings menu item.
65 AddItem(kShowSettingsCommand,
66 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS));
67 #endif
70 NotificationMenuModel::~NotificationMenuModel() {
73 bool NotificationMenuModel::IsCommandIdChecked(int command_id) const {
74 return false;
77 bool NotificationMenuModel::IsCommandIdEnabled(int command_id) const {
78 return tray_->delegate()->IsContextMenuEnabled();
81 bool NotificationMenuModel::GetAcceleratorForCommandId(
82 int command_id,
83 ui::Accelerator* accelerator) {
84 return false;
87 void NotificationMenuModel::ExecuteCommand(int command_id, int event_flags) {
88 switch (command_id) {
89 case kTogglePermissionCommand:
90 tray_->message_center()->DisableNotificationsByNotifier(notifier_id_);
91 break;
92 #ifdef OS_CHROMEOS
93 case kShowSettingsCommand:
94 tray_->ShowNotifierSettingsBubble();
95 break;
96 #endif
97 default:
98 NOTREACHED();
102 } // namespace
104 MessageCenterTray::MessageCenterTray(
105 MessageCenterTrayDelegate* delegate,
106 message_center::MessageCenter* message_center)
107 : message_center_(message_center),
108 message_center_visible_(false),
109 popups_visible_(false),
110 delegate_(delegate) {
111 message_center_->AddObserver(this);
114 MessageCenterTray::~MessageCenterTray() {
115 message_center_->RemoveObserver(this);
118 bool MessageCenterTray::ShowMessageCenterBubble() {
119 if (message_center_visible_)
120 return true;
122 HidePopupBubbleInternal();
124 message_center_visible_ = delegate_->ShowMessageCenter();
125 if (message_center_visible_) {
126 message_center_->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
127 NotifyMessageCenterTrayChanged();
129 return message_center_visible_;
132 bool MessageCenterTray::HideMessageCenterBubble() {
133 if (!message_center_visible_)
134 return false;
135 delegate_->HideMessageCenter();
136 MarkMessageCenterHidden();
137 return true;
140 void MessageCenterTray::MarkMessageCenterHidden() {
141 if (!message_center_visible_)
142 return;
143 message_center_visible_ = false;
144 message_center_->SetVisibility(message_center::VISIBILITY_TRANSIENT);
146 // Some notifications (like system ones) should appear as popups again
147 // after the message center is closed.
148 if (message_center_->HasPopupNotifications()) {
149 ShowPopupBubble();
150 return;
153 NotifyMessageCenterTrayChanged();
156 void MessageCenterTray::ShowPopupBubble() {
157 if (message_center_visible_)
158 return;
160 if (popups_visible_) {
161 NotifyMessageCenterTrayChanged();
162 return;
165 if (!message_center_->HasPopupNotifications())
166 return;
168 popups_visible_ = delegate_->ShowPopups();
170 NotifyMessageCenterTrayChanged();
173 bool MessageCenterTray::HidePopupBubble() {
174 if (!popups_visible_)
175 return false;
176 HidePopupBubbleInternal();
177 NotifyMessageCenterTrayChanged();
179 return true;
182 void MessageCenterTray::HidePopupBubbleInternal() {
183 if (!popups_visible_)
184 return;
186 delegate_->HidePopups();
187 popups_visible_ = false;
190 void MessageCenterTray::ShowNotifierSettingsBubble() {
191 if (popups_visible_)
192 HidePopupBubbleInternal();
194 message_center_visible_ = delegate_->ShowNotifierSettings();
195 message_center_->SetVisibility(message_center::VISIBILITY_SETTINGS);
197 NotifyMessageCenterTrayChanged();
200 scoped_ptr<ui::MenuModel> MessageCenterTray::CreateNotificationMenuModel(
201 const NotifierId& notifier_id,
202 const base::string16& display_source) {
203 #if !defined(OS_CHROMEOS)
204 // Only web pages are configurable on non-chromeos platforms.
205 if (notifier_id.type != NotifierId::WEB_PAGE) {
206 return nullptr;
208 #endif
210 return make_scoped_ptr(
211 new NotificationMenuModel(this, notifier_id, display_source));
214 void MessageCenterTray::OnNotificationAdded(
215 const std::string& notification_id) {
216 OnMessageCenterChanged();
219 void MessageCenterTray::OnNotificationRemoved(
220 const std::string& notification_id,
221 bool by_user) {
222 OnMessageCenterChanged();
225 void MessageCenterTray::OnNotificationUpdated(
226 const std::string& notification_id) {
227 OnMessageCenterChanged();
230 void MessageCenterTray::OnNotificationClicked(
231 const std::string& notification_id) {
232 if (popups_visible_)
233 OnMessageCenterChanged();
236 void MessageCenterTray::OnNotificationButtonClicked(
237 const std::string& notification_id,
238 int button_index) {
239 if (popups_visible_)
240 OnMessageCenterChanged();
243 void MessageCenterTray::OnNotificationDisplayed(
244 const std::string& notification_id,
245 const DisplaySource source) {
246 NotifyMessageCenterTrayChanged();
249 void MessageCenterTray::OnQuietModeChanged(bool in_quiet_mode) {
250 NotifyMessageCenterTrayChanged();
253 void MessageCenterTray::OnBlockingStateChanged(NotificationBlocker* blocker) {
254 OnMessageCenterChanged();
257 void MessageCenterTray::OnMessageCenterChanged() {
258 if (message_center_visible_ && message_center_->NotificationCount() == 0)
259 HideMessageCenterBubble();
261 if (popups_visible_ && !message_center_->HasPopupNotifications())
262 HidePopupBubbleInternal();
263 else if (!popups_visible_ && message_center_->HasPopupNotifications())
264 ShowPopupBubble();
266 NotifyMessageCenterTrayChanged();
269 void MessageCenterTray::NotifyMessageCenterTrayChanged() {
270 delegate_->OnMessageCenterTrayChanged();
273 } // namespace message_center