Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ui / message_center / message_center_impl.cc
blob4885aa706eefb590b21aa5118037361614f9f09f
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_impl.h"
7 #include <algorithm>
9 #include "base/memory/scoped_vector.h"
10 #include "base/observer_list.h"
11 #include "ui/message_center/message_center_style.h"
12 #include "ui/message_center/message_center_types.h"
13 #include "ui/message_center/notification.h"
14 #include "ui/message_center/notification_blocker.h"
15 #include "ui/message_center/notification_list.h"
16 #include "ui/message_center/notification_types.h"
18 namespace {
20 base::TimeDelta GetTimeoutForPriority(int priority) {
21 if (priority > message_center::DEFAULT_PRIORITY) {
22 return base::TimeDelta::FromSeconds(
23 message_center::kAutocloseHighPriorityDelaySeconds);
25 return base::TimeDelta::FromSeconds(
26 message_center::kAutocloseDefaultDelaySeconds);
29 } // namespace
31 namespace message_center {
32 namespace internal {
34 // ChangeQueue keeps track of all the changes that we need to make to the
35 // notification list once the visibility is set to VISIBILITY_TRANSIENT.
36 class ChangeQueue {
37 public:
38 enum ChangeType {
39 CHANGE_TYPE_ADD = 0,
40 CHANGE_TYPE_UPDATE,
41 CHANGE_TYPE_DELETE
44 // Change represents an operation made on a notification. Since it contains
45 // the final state of the notification, we only keep the last change for a
46 // particular notification that is in the notification list around. There are
47 // two ids; |id_| is the newest notification id that has been assigned by an
48 // update, and |notification_list_id_| is the id of the notification it should
49 // be updating as it exists in the notification list.
50 class Change {
51 public:
52 Change(ChangeType type,
53 const std::string& id,
54 scoped_ptr<Notification> notification);
55 ~Change();
57 // Used to transfer ownership of the contained notification.
58 scoped_ptr<Notification> PassNotification();
60 Notification* notification() const { return notification_.get(); }
61 const std::string& id() const { return id_; }
62 ChangeType type() const { return type_; }
63 bool by_user() const { return by_user_; }
64 void set_by_user(bool by_user) { by_user_ = by_user; }
65 const std::string& notification_list_id() const {
66 return notification_list_id_;
68 void set_notification_list_id(const std::string& id) {
69 notification_list_id_ = id;
72 private:
73 const ChangeType type_;
74 const std::string id_;
75 std::string notification_list_id_;
76 bool by_user_;
77 scoped_ptr<Notification> notification_;
79 DISALLOW_COPY_AND_ASSIGN(Change);
82 ChangeQueue();
83 ~ChangeQueue();
85 // Called when the message center has appropriate visibility. Modifies
86 // |message_center| but does not retain it. This also causes the queue to
87 // empty itself.
88 void ApplyChanges(MessageCenter* message_center);
90 // Causes a TYPE_ADD change to be added to the queue.
91 void AddNotification(scoped_ptr<Notification> notification);
93 // Causes a TYPE_UPDATE change to be added to the queue.
94 void UpdateNotification(const std::string& old_id,
95 scoped_ptr<Notification> notification);
97 // Causes a TYPE_DELETE change to be added to the queue.
98 void EraseNotification(const std::string& id, bool by_user);
100 // Returns whether the queue matches an id. The id given will be matched
101 // against the ID of all changes post-update, not the id of the notification
102 // as it stands in the notification list.
103 bool Has(const std::string& id) const;
105 // Returns a Change that can be modified by the caller. ChangeQueue retains
106 // ownership of the Change; pointers should not be retained.
107 Notification* GetLatestNotification(const std::string& id) const;
109 private:
110 void Replace(const std::string& id, scoped_ptr<Change> change);
112 ScopedVector<Change> changes_;
115 ////////////////////////////////////////////////////////////////////////////////
116 // ChangeFinder
118 struct ChangeFinder {
119 explicit ChangeFinder(const std::string& id) : id(id) {}
120 bool operator()(ChangeQueue::Change* change) { return change->id() == id; }
122 std::string id;
125 ////////////////////////////////////////////////////////////////////////////////
126 // ChangeQueue::Change
128 ChangeQueue::Change::Change(ChangeType type,
129 const std::string& id,
130 scoped_ptr<Notification> notification)
131 : type_(type),
132 id_(id),
133 notification_list_id_(id),
134 by_user_(false),
135 notification_(notification.Pass()) {
136 DCHECK(!id.empty() &&
137 (type != CHANGE_TYPE_DELETE || notification_.get() == NULL));
140 ChangeQueue::Change::~Change() {}
142 scoped_ptr<Notification> ChangeQueue::Change::PassNotification() {
143 return notification_.Pass();
146 ////////////////////////////////////////////////////////////////////////////////
147 // ChangeQueue
149 ChangeQueue::ChangeQueue() {}
151 ChangeQueue::~ChangeQueue() {}
153 void ChangeQueue::ApplyChanges(MessageCenter* message_center) {
154 // This method is re-entrant.
155 while (!changes_.empty()) {
156 ScopedVector<Change>::iterator iter = changes_.begin();
157 scoped_ptr<Change> change(*iter);
158 // TODO(dewittj): Replace changes_ with a deque.
159 changes_.weak_erase(iter);
160 // |message_center| is taking ownership of each element here.
161 switch (change->type()) {
162 case CHANGE_TYPE_ADD:
163 message_center->AddNotification(change->PassNotification());
164 break;
165 case CHANGE_TYPE_UPDATE:
166 message_center->UpdateNotification(change->notification_list_id(),
167 change->PassNotification());
168 break;
169 case CHANGE_TYPE_DELETE:
170 message_center->RemoveNotification(change->notification_list_id(),
171 change->by_user());
172 break;
173 default:
174 NOTREACHED();
179 void ChangeQueue::AddNotification(scoped_ptr<Notification> notification) {
180 std::string id = notification->id();
182 scoped_ptr<Change> change(
183 new Change(CHANGE_TYPE_ADD, id, notification.Pass()));
184 Replace(id, change.Pass());
187 void ChangeQueue::UpdateNotification(const std::string& old_id,
188 scoped_ptr<Notification> notification) {
189 std::string new_id = notification->id();
190 scoped_ptr<Change> change(
191 new Change(CHANGE_TYPE_UPDATE, new_id, notification.Pass()));
192 Replace(old_id, change.Pass());
195 void ChangeQueue::EraseNotification(const std::string& id, bool by_user) {
196 scoped_ptr<Change> change(
197 new Change(CHANGE_TYPE_DELETE, id, scoped_ptr<Notification>()));
198 change->set_by_user(by_user);
199 Replace(id, change.Pass());
202 bool ChangeQueue::Has(const std::string& id) const {
203 ScopedVector<Change>::const_iterator iter =
204 std::find_if(changes_.begin(), changes_.end(), ChangeFinder(id));
205 return iter != changes_.end();
208 Notification* ChangeQueue::GetLatestNotification(const std::string& id) const {
209 ScopedVector<Change>::const_iterator iter =
210 std::find_if(changes_.begin(), changes_.end(), ChangeFinder(id));
211 if (iter == changes_.end())
212 return NULL;
214 return (*iter)->notification();
217 void ChangeQueue::Replace(const std::string& changed_id,
218 scoped_ptr<Change> new_change) {
219 ScopedVector<Change>::iterator iter =
220 std::find_if(changes_.begin(), changes_.end(), ChangeFinder(changed_id));
221 if (iter != changes_.end()) {
222 Change* old_change = *iter;
223 new_change->set_notification_list_id(old_change->notification_list_id());
224 changes_.erase(iter);
225 } else {
226 new_change->set_notification_list_id(changed_id);
229 changes_.push_back(new_change.release());
232 ////////////////////////////////////////////////////////////////////////////////
233 // PopupTimer
235 PopupTimer::PopupTimer(const std::string& id,
236 base::TimeDelta timeout,
237 base::WeakPtr<PopupTimersController> controller)
238 : id_(id),
239 timeout_(timeout),
240 timer_controller_(controller),
241 timer_(new base::OneShotTimer<PopupTimersController>) {}
243 PopupTimer::~PopupTimer() {
244 if (!timer_)
245 return;
247 if (timer_->IsRunning())
248 timer_->Stop();
251 void PopupTimer::Start() {
252 if (timer_->IsRunning())
253 return;
254 base::TimeDelta timeout_to_close =
255 timeout_ <= passed_ ? base::TimeDelta() : timeout_ - passed_;
256 start_time_ = base::Time::Now();
257 timer_->Start(
258 FROM_HERE,
259 timeout_to_close,
260 base::Bind(
261 &PopupTimersController::TimerFinished, timer_controller_, id_));
264 void PopupTimer::Pause() {
265 if (!timer_.get() || !timer_->IsRunning())
266 return;
268 timer_->Stop();
269 passed_ += base::Time::Now() - start_time_;
272 void PopupTimer::Reset() {
273 if (timer_)
274 timer_->Stop();
275 passed_ = base::TimeDelta();
278 ////////////////////////////////////////////////////////////////////////////////
279 // PopupTimersController
281 PopupTimersController::PopupTimersController(MessageCenter* message_center)
282 : message_center_(message_center), popup_deleter_(&popup_timers_) {
283 message_center_->AddObserver(this);
286 PopupTimersController::~PopupTimersController() {
287 message_center_->RemoveObserver(this);
290 void PopupTimersController::StartTimer(const std::string& id,
291 const base::TimeDelta& timeout) {
292 PopupTimerCollection::iterator iter = popup_timers_.find(id);
293 if (iter != popup_timers_.end()) {
294 DCHECK(iter->second);
295 iter->second->Start();
296 return;
299 PopupTimer* timer = new PopupTimer(id, timeout, AsWeakPtr());
301 timer->Start();
302 popup_timers_[id] = timer;
305 void PopupTimersController::StartAll() {
306 std::map<std::string, PopupTimer*>::iterator iter;
307 for (iter = popup_timers_.begin(); iter != popup_timers_.end(); iter++) {
308 iter->second->Start();
312 void PopupTimersController::ResetTimer(const std::string& id,
313 const base::TimeDelta& timeout) {
314 CancelTimer(id);
315 StartTimer(id, timeout);
318 void PopupTimersController::PauseTimer(const std::string& id) {
319 PopupTimerCollection::iterator iter = popup_timers_.find(id);
320 if (iter == popup_timers_.end())
321 return;
322 iter->second->Pause();
325 void PopupTimersController::PauseAll() {
326 std::map<std::string, PopupTimer*>::iterator iter;
327 for (iter = popup_timers_.begin(); iter != popup_timers_.end(); iter++) {
328 iter->second->Pause();
332 void PopupTimersController::CancelTimer(const std::string& id) {
333 PopupTimerCollection::iterator iter = popup_timers_.find(id);
334 if (iter == popup_timers_.end())
335 return;
337 PopupTimer* timer = iter->second;
338 delete timer;
340 popup_timers_.erase(iter);
343 void PopupTimersController::CancelAll() {
344 STLDeleteValues(&popup_timers_);
345 popup_timers_.clear();
348 void PopupTimersController::TimerFinished(const std::string& id) {
349 PopupTimerCollection::iterator iter = popup_timers_.find(id);
350 if (iter == popup_timers_.end())
351 return;
353 CancelTimer(id);
354 message_center_->MarkSinglePopupAsShown(id, false);
357 void PopupTimersController::OnNotificationDisplayed(const std::string& id) {
358 OnNotificationUpdated(id);
361 void PopupTimersController::OnNotificationUpdated(const std::string& id) {
362 NotificationList::PopupNotifications popup_notifications =
363 message_center_->GetPopupNotifications();
365 if (!popup_notifications.size()) {
366 CancelAll();
367 return;
370 NotificationList::PopupNotifications::const_iterator iter =
371 popup_notifications.begin();
372 for (; iter != popup_notifications.end(); iter++) {
373 if ((*iter)->id() == id)
374 break;
377 if (iter == popup_notifications.end() || (*iter)->never_timeout()) {
378 CancelTimer(id);
379 return;
382 // Start the timer if not yet.
383 if (popup_timers_.find(id) == popup_timers_.end())
384 StartTimer(id, GetTimeoutForPriority((*iter)->priority()));
387 void PopupTimersController::OnNotificationRemoved(const std::string& id,
388 bool by_user) {
389 CancelTimer(id);
392 } // namespace internal
394 ////////////////////////////////////////////////////////////////////////////////
395 // MessageCenterImpl::NotificationCache
397 MessageCenterImpl::NotificationCache::NotificationCache()
398 : unread_count(0) {}
400 MessageCenterImpl::NotificationCache::~NotificationCache() {}
402 void MessageCenterImpl::NotificationCache::Rebuild(
403 const NotificationList::Notifications& notifications) {
404 visible_notifications = notifications;
405 RecountUnread();
408 void MessageCenterImpl::NotificationCache::RecountUnread() {
409 unread_count = 0;
410 for (NotificationList::Notifications::const_iterator iter =
411 visible_notifications.begin();
412 iter != visible_notifications.end(); ++iter) {
413 if (!(*iter)->IsRead())
414 ++unread_count;
418 ////////////////////////////////////////////////////////////////////////////////
419 // MessageCenterImpl
421 MessageCenterImpl::MessageCenterImpl()
422 : MessageCenter(),
423 popup_timers_controller_(new internal::PopupTimersController(this)),
424 settings_provider_(NULL) {
425 notification_list_.reset(new NotificationList());
426 notification_queue_.reset(new internal::ChangeQueue());
429 MessageCenterImpl::~MessageCenterImpl() {}
431 void MessageCenterImpl::AddObserver(MessageCenterObserver* observer) {
432 observer_list_.AddObserver(observer);
435 void MessageCenterImpl::RemoveObserver(MessageCenterObserver* observer) {
436 observer_list_.RemoveObserver(observer);
439 void MessageCenterImpl::AddNotificationBlocker(NotificationBlocker* blocker) {
440 if (std::find(blockers_.begin(), blockers_.end(), blocker) !=
441 blockers_.end()) {
442 return;
444 blocker->AddObserver(this);
445 blockers_.push_back(blocker);
448 void MessageCenterImpl::RemoveNotificationBlocker(
449 NotificationBlocker* blocker) {
450 std::vector<NotificationBlocker*>::iterator iter =
451 std::find(blockers_.begin(), blockers_.end(), blocker);
452 if (iter == blockers_.end())
453 return;
454 blocker->RemoveObserver(this);
455 blockers_.erase(iter);
458 void MessageCenterImpl::OnBlockingStateChanged(NotificationBlocker* blocker) {
459 std::list<std::string> blocked_ids;
460 NotificationList::PopupNotifications popups =
461 notification_list_->GetPopupNotifications(blockers_, &blocked_ids);
463 for (std::list<std::string>::const_iterator iter = blocked_ids.begin();
464 iter != blocked_ids.end(); ++iter) {
465 // Do not call MessageCenterImpl::MarkSinglePopupAsShown() directly here
466 // just for performance reason. MessageCenterImpl::MarkSinglePopupAsShown()
467 // calls NotificationList::MarkSinglePopupAsShown() and then updates the
468 // unread count, but the whole cache will be recreated below.
469 notification_list_->MarkSinglePopupAsShown((*iter), true);
470 FOR_EACH_OBSERVER(MessageCenterObserver,
471 observer_list_,
472 OnNotificationUpdated(*iter));
474 notification_cache_.Rebuild(
475 notification_list_->GetVisibleNotifications(blockers_));
476 FOR_EACH_OBSERVER(MessageCenterObserver,
477 observer_list_,
478 OnBlockingStateChanged(blocker));
481 void MessageCenterImpl::SetVisibility(Visibility visibility) {
482 std::set<std::string> updated_ids;
483 notification_list_->SetMessageCenterVisible(
484 (visibility == VISIBILITY_MESSAGE_CENTER), &updated_ids);
485 notification_cache_.RecountUnread();
487 for (std::set<std::string>::const_iterator iter = updated_ids.begin();
488 iter != updated_ids.end();
489 ++iter) {
490 FOR_EACH_OBSERVER(
491 MessageCenterObserver, observer_list_, OnNotificationUpdated(*iter));
494 if (visibility == VISIBILITY_TRANSIENT)
495 notification_queue_->ApplyChanges(this);
497 FOR_EACH_OBSERVER(MessageCenterObserver,
498 observer_list_,
499 OnCenterVisibilityChanged(visibility));
502 bool MessageCenterImpl::IsMessageCenterVisible() const {
503 return notification_list_->is_message_center_visible();
506 size_t MessageCenterImpl::NotificationCount() const {
507 return notification_cache_.visible_notifications.size();
510 size_t MessageCenterImpl::UnreadNotificationCount() const {
511 return notification_cache_.unread_count;
514 bool MessageCenterImpl::HasPopupNotifications() const {
515 return !IsMessageCenterVisible() &&
516 notification_list_->HasPopupNotifications(blockers_);
519 bool MessageCenterImpl::HasNotification(const std::string& id) {
520 // This will return true if the notification with |id| is hidden by the
521 // ChromeOS multi-profile feature. This would be harmless for now because
522 // this check will be used from the UI, so the |id| for hidden profile won't
523 // arrive here.
524 // TODO(mukai): fix this if necessary.
525 return notification_list_->HasNotification(id);
528 bool MessageCenterImpl::IsQuietMode() const {
529 return notification_list_->quiet_mode();
532 bool MessageCenterImpl::HasClickedListener(const std::string& id) {
533 scoped_refptr<NotificationDelegate> delegate =
534 notification_list_->GetNotificationDelegate(id);
535 return delegate.get() && delegate->HasClickedListener();
538 const NotificationList::Notifications&
539 MessageCenterImpl::GetVisibleNotifications() {
540 return notification_cache_.visible_notifications;
543 NotificationList::PopupNotifications
544 MessageCenterImpl::GetPopupNotifications() {
545 return notification_list_->GetPopupNotifications(blockers_, NULL);
548 //------------------------------------------------------------------------------
549 // Client code interface.
550 void MessageCenterImpl::AddNotification(scoped_ptr<Notification> notification) {
551 DCHECK(notification.get());
552 const std::string id = notification->id();
553 for (size_t i = 0; i < blockers_.size(); ++i)
554 blockers_[i]->CheckState();
556 if (notification_list_->is_message_center_visible()) {
557 notification_queue_->AddNotification(notification.Pass());
558 return;
561 // Sometimes the notification can be added with the same id and the
562 // |notification_list| will replace the notification instead of adding new.
563 // This is essentially an update rather than addition.
564 bool already_exists = notification_list_->HasNotification(id);
565 notification_list_->AddNotification(notification.Pass());
566 notification_cache_.Rebuild(
567 notification_list_->GetVisibleNotifications(blockers_));
569 if (already_exists) {
570 FOR_EACH_OBSERVER(
571 MessageCenterObserver, observer_list_, OnNotificationUpdated(id));
572 } else {
573 FOR_EACH_OBSERVER(
574 MessageCenterObserver, observer_list_, OnNotificationAdded(id));
578 void MessageCenterImpl::UpdateNotification(
579 const std::string& old_id,
580 scoped_ptr<Notification> new_notification) {
581 for (size_t i = 0; i < blockers_.size(); ++i)
582 blockers_[i]->CheckState();
584 if (notification_list_->is_message_center_visible()) {
585 // We will allow notifications that are progress types (and stay progress
586 // types) to be updated even if the message center is open. There are 3
587 // requirements here:
588 // * Notification of type PROGRESS exists with same ID in the center
589 // * There are no queued updates for this notification (they imply a change
590 // that violates the PROGRESS invariant
591 // * The new notification is type PROGRESS.
592 // TODO(dewittj): Ensure this works when the ID is changed by the caller.
593 // This shouldn't be an issue in practice since only W3C notifications
594 // change the ID on update, and they don't have progress type notifications.
595 bool update_keeps_progress_type =
596 new_notification->type() == NOTIFICATION_TYPE_PROGRESS &&
597 !notification_queue_->Has(old_id) &&
598 notification_list_->HasNotificationOfType(old_id,
599 NOTIFICATION_TYPE_PROGRESS);
600 if (!update_keeps_progress_type) {
601 // Updates are allowed only for progress notifications.
602 notification_queue_->UpdateNotification(old_id, new_notification.Pass());
603 return;
607 std::string new_id = new_notification->id();
608 notification_list_->UpdateNotificationMessage(old_id,
609 new_notification.Pass());
610 notification_cache_.Rebuild(
611 notification_list_->GetVisibleNotifications(blockers_));
612 if (old_id == new_id) {
613 FOR_EACH_OBSERVER(
614 MessageCenterObserver, observer_list_, OnNotificationUpdated(new_id));
615 } else {
616 FOR_EACH_OBSERVER(MessageCenterObserver, observer_list_,
617 OnNotificationRemoved(old_id, false));
618 FOR_EACH_OBSERVER(MessageCenterObserver, observer_list_,
619 OnNotificationAdded(new_id));
623 void MessageCenterImpl::RemoveNotification(const std::string& id,
624 bool by_user) {
625 if (!by_user && notification_list_->is_message_center_visible()) {
626 notification_queue_->EraseNotification(id, by_user);
627 return;
630 if (!HasNotification(id))
631 return;
633 // In many cases |id| is a reference to an existing notification instance
634 // but the instance can be destructed in RemoveNotification(). Hence
635 // copies the id explicitly here.
636 std::string copied_id(id);
638 scoped_refptr<NotificationDelegate> delegate =
639 notification_list_->GetNotificationDelegate(copied_id);
640 if (delegate.get())
641 delegate->Close(by_user);
643 notification_list_->RemoveNotification(copied_id);
644 notification_cache_.Rebuild(
645 notification_list_->GetVisibleNotifications(blockers_));
646 FOR_EACH_OBSERVER(MessageCenterObserver,
647 observer_list_,
648 OnNotificationRemoved(copied_id, by_user));
651 void MessageCenterImpl::RemoveAllNotifications(bool by_user) {
652 // Using not |blockers_| but an empty list since it wants to remove literally
653 // all notifications.
654 RemoveNotifications(by_user, NotificationBlockers());
657 void MessageCenterImpl::RemoveAllVisibleNotifications(bool by_user) {
658 RemoveNotifications(by_user, blockers_);
661 void MessageCenterImpl::RemoveNotifications(
662 bool by_user,
663 const NotificationBlockers& blockers) {
664 const NotificationList::Notifications notifications =
665 notification_list_->GetVisibleNotifications(blockers);
666 std::set<std::string> ids;
667 for (NotificationList::Notifications::const_iterator iter =
668 notifications.begin(); iter != notifications.end(); ++iter) {
669 ids.insert((*iter)->id());
670 scoped_refptr<NotificationDelegate> delegate = (*iter)->delegate();
671 if (delegate.get())
672 delegate->Close(by_user);
673 notification_list_->RemoveNotification((*iter)->id());
676 if (!ids.empty()) {
677 notification_cache_.Rebuild(
678 notification_list_->GetVisibleNotifications(blockers_));
680 for (std::set<std::string>::const_iterator iter = ids.begin();
681 iter != ids.end(); ++iter) {
682 FOR_EACH_OBSERVER(MessageCenterObserver,
683 observer_list_,
684 OnNotificationRemoved(*iter, by_user));
688 void MessageCenterImpl::SetNotificationIcon(const std::string& notification_id,
689 const gfx::Image& image) {
690 bool updated = false;
691 Notification* queue_notification = notification_queue_->GetLatestNotification(
692 notification_id);
694 if (queue_notification) {
695 queue_notification->set_icon(image);
696 updated = true;
697 } else {
698 updated = notification_list_->SetNotificationIcon(notification_id, image);
701 if (updated) {
702 FOR_EACH_OBSERVER(MessageCenterObserver, observer_list_,
703 OnNotificationUpdated(notification_id));
707 void MessageCenterImpl::SetNotificationImage(const std::string& notification_id,
708 const gfx::Image& image) {
709 bool updated = false;
710 Notification* queue_notification = notification_queue_->GetLatestNotification(
711 notification_id);
713 if (queue_notification) {
714 queue_notification->set_image(image);
715 updated = true;
716 } else {
717 updated = notification_list_->SetNotificationImage(notification_id, image);
720 if (updated) {
721 FOR_EACH_OBSERVER(MessageCenterObserver, observer_list_,
722 OnNotificationUpdated(notification_id));
726 void MessageCenterImpl::SetNotificationButtonIcon(
727 const std::string& notification_id, int button_index,
728 const gfx::Image& image) {
729 bool updated = false;
730 Notification* queue_notification = notification_queue_->GetLatestNotification(
731 notification_id);
733 if (queue_notification) {
734 queue_notification->SetButtonIcon(button_index, image);
735 updated = true;
736 } else {
737 updated = notification_list_->SetNotificationButtonIcon(
738 notification_id, button_index, image);
741 if (updated) {
742 FOR_EACH_OBSERVER(MessageCenterObserver, observer_list_,
743 OnNotificationUpdated(notification_id));
747 void MessageCenterImpl::DisableNotificationsByNotifier(
748 const NotifierId& notifier_id) {
749 if (settings_provider_) {
750 // TODO(mukai): SetNotifierEnabled can just accept notifier_id?
751 Notifier notifier(notifier_id, base::string16(), true);
752 settings_provider_->SetNotifierEnabled(notifier, false);
755 NotificationList::Notifications notifications =
756 notification_list_->GetNotificationsByNotifierId(notifier_id);
757 for (NotificationList::Notifications::const_iterator iter =
758 notifications.begin(); iter != notifications.end();) {
759 std::string id = (*iter)->id();
760 iter++;
761 RemoveNotification(id, false);
763 if (!notifications.empty()) {
764 notification_cache_.Rebuild(
765 notification_list_->GetVisibleNotifications(blockers_));
769 void MessageCenterImpl::ClickOnNotification(const std::string& id) {
770 if (!HasNotification(id))
771 return;
772 if (HasPopupNotifications())
773 MarkSinglePopupAsShown(id, true);
774 scoped_refptr<NotificationDelegate> delegate =
775 notification_list_->GetNotificationDelegate(id);
776 if (delegate.get())
777 delegate->Click();
778 FOR_EACH_OBSERVER(
779 MessageCenterObserver, observer_list_, OnNotificationClicked(id));
782 void MessageCenterImpl::ClickOnNotificationButton(const std::string& id,
783 int button_index) {
784 if (!HasNotification(id))
785 return;
786 if (HasPopupNotifications())
787 MarkSinglePopupAsShown(id, true);
788 scoped_refptr<NotificationDelegate> delegate =
789 notification_list_->GetNotificationDelegate(id);
790 if (delegate.get())
791 delegate->ButtonClick(button_index);
792 FOR_EACH_OBSERVER(
793 MessageCenterObserver, observer_list_, OnNotificationButtonClicked(
794 id, button_index));
797 void MessageCenterImpl::MarkSinglePopupAsShown(const std::string& id,
798 bool mark_notification_as_read) {
799 if (!HasNotification(id))
800 return;
801 notification_list_->MarkSinglePopupAsShown(id, mark_notification_as_read);
802 notification_cache_.RecountUnread();
803 FOR_EACH_OBSERVER(
804 MessageCenterObserver, observer_list_, OnNotificationUpdated(id));
807 void MessageCenterImpl::DisplayedNotification(const std::string& id) {
808 if (!HasNotification(id))
809 return;
811 if (HasPopupNotifications())
812 notification_list_->MarkSinglePopupAsDisplayed(id);
813 notification_cache_.RecountUnread();
814 scoped_refptr<NotificationDelegate> delegate =
815 notification_list_->GetNotificationDelegate(id);
816 if (delegate.get())
817 delegate->Display();
818 FOR_EACH_OBSERVER(
819 MessageCenterObserver, observer_list_, OnNotificationDisplayed(id));
822 void MessageCenterImpl::SetNotifierSettingsProvider(
823 NotifierSettingsProvider* provider) {
824 settings_provider_ = provider;
827 NotifierSettingsProvider* MessageCenterImpl::GetNotifierSettingsProvider() {
828 return settings_provider_;
831 void MessageCenterImpl::SetQuietMode(bool in_quiet_mode) {
832 if (in_quiet_mode != notification_list_->quiet_mode()) {
833 notification_list_->SetQuietMode(in_quiet_mode);
834 FOR_EACH_OBSERVER(MessageCenterObserver,
835 observer_list_,
836 OnQuietModeChanged(in_quiet_mode));
838 quiet_mode_timer_.reset();
841 void MessageCenterImpl::EnterQuietModeWithExpire(
842 const base::TimeDelta& expires_in) {
843 if (quiet_mode_timer_.get()) {
844 // Note that the capital Reset() is the method to restart the timer, not
845 // scoped_ptr::reset().
846 quiet_mode_timer_->Reset();
847 } else {
848 notification_list_->SetQuietMode(true);
849 FOR_EACH_OBSERVER(
850 MessageCenterObserver, observer_list_, OnQuietModeChanged(true));
852 quiet_mode_timer_.reset(new base::OneShotTimer<MessageCenterImpl>);
853 quiet_mode_timer_->Start(
854 FROM_HERE,
855 expires_in,
856 base::Bind(
857 &MessageCenterImpl::SetQuietMode, base::Unretained(this), false));
861 void MessageCenterImpl::RestartPopupTimers() {
862 if (popup_timers_controller_.get())
863 popup_timers_controller_->StartAll();
866 void MessageCenterImpl::PausePopupTimers() {
867 if (popup_timers_controller_.get())
868 popup_timers_controller_->PauseAll();
871 void MessageCenterImpl::DisableTimersForTest() {
872 popup_timers_controller_.reset();
875 } // namespace message_center