OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/message_center/message_center_impl.h" | 5 #include "ui/message_center/message_center_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 | 525 |
526 size_t MessageCenterImpl::UnreadNotificationCount() const { | 526 size_t MessageCenterImpl::UnreadNotificationCount() const { |
527 return notification_cache_.unread_count; | 527 return notification_cache_.unread_count; |
528 } | 528 } |
529 | 529 |
530 bool MessageCenterImpl::HasPopupNotifications() const { | 530 bool MessageCenterImpl::HasPopupNotifications() const { |
531 return !IsMessageCenterVisible() && | 531 return !IsMessageCenterVisible() && |
532 notification_list_->HasPopupNotifications(blockers_); | 532 notification_list_->HasPopupNotifications(blockers_); |
533 } | 533 } |
534 | 534 |
535 bool MessageCenterImpl::HasNotification(const std::string& id) { | |
536 // This will return true if the notification with |id| is hidden by the | |
537 // ChromeOS multi-profile feature. This would be harmless for now because | |
538 // this check will be used from the UI, so the |id| for hidden profile won't | |
539 // arrive here. | |
540 // TODO(mukai): fix this if necessary. | |
541 return notification_list_->HasNotification(id); | |
542 } | |
543 | |
544 bool MessageCenterImpl::IsQuietMode() const { | 535 bool MessageCenterImpl::IsQuietMode() const { |
545 return notification_list_->quiet_mode(); | 536 return notification_list_->quiet_mode(); |
546 } | 537 } |
547 | 538 |
548 bool MessageCenterImpl::HasClickedListener(const std::string& id) { | 539 bool MessageCenterImpl::HasClickedListener(const std::string& id) { |
549 scoped_refptr<NotificationDelegate> delegate = | 540 scoped_refptr<NotificationDelegate> delegate = |
550 notification_list_->GetNotificationDelegate(id); | 541 notification_list_->GetNotificationDelegate(id); |
551 return delegate.get() && delegate->HasClickedListener(); | 542 return delegate.get() && delegate->HasClickedListener(); |
552 } | 543 } |
553 | 544 |
| 545 message_center::Notification* MessageCenterImpl::FindVisibleNotificationById( |
| 546 const std::string& id) { |
| 547 return notification_list_->GetNotificationById(id); |
| 548 } |
| 549 |
554 const NotificationList::Notifications& | 550 const NotificationList::Notifications& |
555 MessageCenterImpl::GetVisibleNotifications() { | 551 MessageCenterImpl::GetVisibleNotifications() { |
556 return notification_cache_.visible_notifications; | 552 return notification_cache_.visible_notifications; |
557 } | 553 } |
558 | 554 |
559 NotificationList::PopupNotifications | 555 NotificationList::PopupNotifications |
560 MessageCenterImpl::GetPopupNotifications() { | 556 MessageCenterImpl::GetPopupNotifications() { |
561 return notification_list_->GetPopupNotifications(blockers_, NULL); | 557 return notification_list_->GetPopupNotifications(blockers_, NULL); |
562 } | 558 } |
563 | 559 |
564 //------------------------------------------------------------------------------ | 560 //------------------------------------------------------------------------------ |
565 // Client code interface. | 561 // Client code interface. |
566 void MessageCenterImpl::AddNotification(scoped_ptr<Notification> notification) { | 562 void MessageCenterImpl::AddNotification(scoped_ptr<Notification> notification) { |
567 DCHECK(notification.get()); | 563 DCHECK(notification.get()); |
568 const std::string id = notification->id(); | 564 const std::string id = notification->id(); |
569 for (size_t i = 0; i < blockers_.size(); ++i) | 565 for (size_t i = 0; i < blockers_.size(); ++i) |
570 blockers_[i]->CheckState(); | 566 blockers_[i]->CheckState(); |
571 | 567 |
572 if (notification_list_->is_message_center_visible()) { | 568 if (notification_list_->is_message_center_visible()) { |
573 notification_queue_->AddNotification(notification.Pass()); | 569 notification_queue_->AddNotification(notification.Pass()); |
574 return; | 570 return; |
575 } | 571 } |
576 | 572 |
577 // Sometimes the notification can be added with the same id and the | 573 // Sometimes the notification can be added with the same id and the |
578 // |notification_list| will replace the notification instead of adding new. | 574 // |notification_list| will replace the notification instead of adding new. |
579 // This is essentially an update rather than addition. | 575 // This is essentially an update rather than addition. |
580 bool already_exists = notification_list_->HasNotification(id); | 576 bool already_exists = (notification_list_->GetNotificationById(id) != NULL); |
581 notification_list_->AddNotification(notification.Pass()); | 577 notification_list_->AddNotification(notification.Pass()); |
582 notification_cache_.Rebuild( | 578 notification_cache_.Rebuild( |
583 notification_list_->GetVisibleNotifications(blockers_)); | 579 notification_list_->GetVisibleNotifications(blockers_)); |
584 | 580 |
585 if (already_exists) { | 581 if (already_exists) { |
586 FOR_EACH_OBSERVER( | 582 FOR_EACH_OBSERVER( |
587 MessageCenterObserver, observer_list_, OnNotificationUpdated(id)); | 583 MessageCenterObserver, observer_list_, OnNotificationUpdated(id)); |
588 } else { | 584 } else { |
589 FOR_EACH_OBSERVER( | 585 FOR_EACH_OBSERVER( |
590 MessageCenterObserver, observer_list_, OnNotificationAdded(id)); | 586 MessageCenterObserver, observer_list_, OnNotificationAdded(id)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 } | 632 } |
637 } | 633 } |
638 | 634 |
639 void MessageCenterImpl::RemoveNotification(const std::string& id, | 635 void MessageCenterImpl::RemoveNotification(const std::string& id, |
640 bool by_user) { | 636 bool by_user) { |
641 if (!by_user && notification_list_->is_message_center_visible()) { | 637 if (!by_user && notification_list_->is_message_center_visible()) { |
642 notification_queue_->EraseNotification(id, by_user); | 638 notification_queue_->EraseNotification(id, by_user); |
643 return; | 639 return; |
644 } | 640 } |
645 | 641 |
646 if (!HasNotification(id)) | 642 if (FindVisibleNotificationById(id) == NULL) |
647 return; | 643 return; |
648 | 644 |
649 // In many cases |id| is a reference to an existing notification instance | 645 // In many cases |id| is a reference to an existing notification instance |
650 // but the instance can be destructed in RemoveNotification(). Hence | 646 // but the instance can be destructed in RemoveNotification(). Hence |
651 // copies the id explicitly here. | 647 // copies the id explicitly here. |
652 std::string copied_id(id); | 648 std::string copied_id(id); |
653 | 649 |
654 scoped_refptr<NotificationDelegate> delegate = | 650 scoped_refptr<NotificationDelegate> delegate = |
655 notification_list_->GetNotificationDelegate(copied_id); | 651 notification_list_->GetNotificationDelegate(copied_id); |
656 if (delegate.get()) | 652 if (delegate.get()) |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 Notifier notifier(notifier_id, base::string16(), true); | 777 Notifier notifier(notifier_id, base::string16(), true); |
782 settings_provider_->SetNotifierEnabled(notifier, false); | 778 settings_provider_->SetNotifierEnabled(notifier, false); |
783 // The settings provider will call back to remove the notifications | 779 // The settings provider will call back to remove the notifications |
784 // belonging to the notifier id. | 780 // belonging to the notifier id. |
785 } else { | 781 } else { |
786 RemoveNotificationsForNotifierId(notifier_id); | 782 RemoveNotificationsForNotifierId(notifier_id); |
787 } | 783 } |
788 } | 784 } |
789 | 785 |
790 void MessageCenterImpl::ClickOnNotification(const std::string& id) { | 786 void MessageCenterImpl::ClickOnNotification(const std::string& id) { |
791 if (!HasNotification(id)) | 787 if (FindVisibleNotificationById(id) == NULL) |
792 return; | 788 return; |
793 if (HasPopupNotifications()) | 789 if (HasPopupNotifications()) |
794 MarkSinglePopupAsShown(id, true); | 790 MarkSinglePopupAsShown(id, true); |
795 scoped_refptr<NotificationDelegate> delegate = | 791 scoped_refptr<NotificationDelegate> delegate = |
796 notification_list_->GetNotificationDelegate(id); | 792 notification_list_->GetNotificationDelegate(id); |
797 if (delegate.get()) | 793 if (delegate.get()) |
798 delegate->Click(); | 794 delegate->Click(); |
799 FOR_EACH_OBSERVER( | 795 FOR_EACH_OBSERVER( |
800 MessageCenterObserver, observer_list_, OnNotificationClicked(id)); | 796 MessageCenterObserver, observer_list_, OnNotificationClicked(id)); |
801 } | 797 } |
802 | 798 |
803 void MessageCenterImpl::ClickOnNotificationButton(const std::string& id, | 799 void MessageCenterImpl::ClickOnNotificationButton(const std::string& id, |
804 int button_index) { | 800 int button_index) { |
805 if (!HasNotification(id)) | 801 if (FindVisibleNotificationById(id) == NULL) |
806 return; | 802 return; |
807 if (HasPopupNotifications()) | 803 if (HasPopupNotifications()) |
808 MarkSinglePopupAsShown(id, true); | 804 MarkSinglePopupAsShown(id, true); |
809 scoped_refptr<NotificationDelegate> delegate = | 805 scoped_refptr<NotificationDelegate> delegate = |
810 notification_list_->GetNotificationDelegate(id); | 806 notification_list_->GetNotificationDelegate(id); |
811 if (delegate.get()) | 807 if (delegate.get()) |
812 delegate->ButtonClick(button_index); | 808 delegate->ButtonClick(button_index); |
813 FOR_EACH_OBSERVER( | 809 FOR_EACH_OBSERVER( |
814 MessageCenterObserver, observer_list_, OnNotificationButtonClicked( | 810 MessageCenterObserver, observer_list_, OnNotificationButtonClicked( |
815 id, button_index)); | 811 id, button_index)); |
816 } | 812 } |
817 | 813 |
818 void MessageCenterImpl::MarkSinglePopupAsShown(const std::string& id, | 814 void MessageCenterImpl::MarkSinglePopupAsShown(const std::string& id, |
819 bool mark_notification_as_read) { | 815 bool mark_notification_as_read) { |
820 if (!HasNotification(id)) | 816 if (FindVisibleNotificationById(id) == NULL) |
821 return; | 817 return; |
822 notification_list_->MarkSinglePopupAsShown(id, mark_notification_as_read); | 818 notification_list_->MarkSinglePopupAsShown(id, mark_notification_as_read); |
823 notification_cache_.RecountUnread(); | 819 notification_cache_.RecountUnread(); |
824 FOR_EACH_OBSERVER( | 820 FOR_EACH_OBSERVER( |
825 MessageCenterObserver, observer_list_, OnNotificationUpdated(id)); | 821 MessageCenterObserver, observer_list_, OnNotificationUpdated(id)); |
826 } | 822 } |
827 | 823 |
828 void MessageCenterImpl::DisplayedNotification( | 824 void MessageCenterImpl::DisplayedNotification( |
829 const std::string& id, | 825 const std::string& id, |
830 const DisplaySource source) { | 826 const DisplaySource source) { |
831 if (!HasNotification(id)) | 827 if (FindVisibleNotificationById(id) == NULL) |
832 return; | 828 return; |
833 | 829 |
834 if (HasPopupNotifications()) | 830 if (HasPopupNotifications()) |
835 notification_list_->MarkSinglePopupAsDisplayed(id); | 831 notification_list_->MarkSinglePopupAsDisplayed(id); |
836 notification_cache_.RecountUnread(); | 832 notification_cache_.RecountUnread(); |
837 scoped_refptr<NotificationDelegate> delegate = | 833 scoped_refptr<NotificationDelegate> delegate = |
838 notification_list_->GetNotificationDelegate(id); | 834 notification_list_->GetNotificationDelegate(id); |
839 if (delegate.get()) | 835 if (delegate.get()) |
840 delegate->Display(); | 836 delegate->Display(); |
841 FOR_EACH_OBSERVER( | 837 FOR_EACH_OBSERVER( |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
897 void MessageCenterImpl::PausePopupTimers() { | 893 void MessageCenterImpl::PausePopupTimers() { |
898 if (popup_timers_controller_.get()) | 894 if (popup_timers_controller_.get()) |
899 popup_timers_controller_->PauseAll(); | 895 popup_timers_controller_->PauseAll(); |
900 } | 896 } |
901 | 897 |
902 void MessageCenterImpl::DisableTimersForTest() { | 898 void MessageCenterImpl::DisableTimersForTest() { |
903 popup_timers_controller_.reset(); | 899 popup_timers_controller_.reset(); |
904 } | 900 } |
905 | 901 |
906 } // namespace message_center | 902 } // namespace message_center |
OLD | NEW |