summaryrefslogtreecommitdiffstats
path: root/ui/message_center/message_center.cc
blob: 3515f7d6d0987b31150253f6834bbfed784005f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/message_center/message_center.h"

#include "base/logging.h"

namespace message_center {

//------------------------------------------------------------------------------

MessageCenter::MessageCenter(Host* host)
    : host_(host),
      delegate_(NULL) {
  notification_list_.reset(new NotificationList(this));
}

MessageCenter::~MessageCenter() {
  notification_list_.reset();
}

void MessageCenter::SetDelegate(Delegate* delegate) {
  DCHECK(!delegate_);
  delegate_ = delegate;
}

void MessageCenter::SetMessageCenterVisible(bool visible) {
  notification_list_->SetMessageCenterVisible(visible);
}

size_t MessageCenter::NotificationCount() const {
  return notification_list_->NotificationCount();
}

size_t MessageCenter::UnreadNotificationCount() const {
  return notification_list_->unread_count();
}

bool MessageCenter::HasPopupNotifications() const {
  return notification_list_->HasPopupNotifications();
}

//------------------------------------------------------------------------------
// Client code interface.

void MessageCenter::AddNotification(
    ui::notifications::NotificationType type,
    const std::string& id,
    const string16& title,
    const string16& message,
    const string16& display_source,
    const std::string& extension_id,
    const base::DictionaryValue* optional_fields) {
  notification_list_->AddNotification(type, id, title, message, display_source,
                                      extension_id, optional_fields);
  if (host_)
    host_->MessageCenterChanged(true);
}

void MessageCenter::UpdateNotification(const std::string& old_id,
                                       const std::string& new_id,
                                       const string16& title,
                                       const string16& message) {
  notification_list_->UpdateNotificationMessage(
      old_id, new_id, title, message);
  if (host_)
    host_->MessageCenterChanged(true);
}

void MessageCenter::RemoveNotification(const std::string& id) {
  if (!notification_list_->RemoveNotification(id))
    return;
  if (host_)
    host_->MessageCenterChanged(false);
}

void MessageCenter::SetNotificationImage(const std::string& id,
                                         const gfx::ImageSkia& image) {
  if (!notification_list_->SetNotificationImage(id, image))
    return;
  if (host_)
    host_->MessageCenterChanged(true);
}

//------------------------------------------------------------------------------
// Overridden from NotificationList::Delegate.

void MessageCenter::SendRemoveNotification(const std::string& id) {
  if (delegate_)
    delegate_->NotificationRemoved(id);
}

void MessageCenter::SendRemoveAllNotifications() {
  if (delegate_) {
    NotificationList::Notifications notifications;
    notification_list_->GetNotifications(&notifications);
    for (NotificationList::Notifications::const_iterator loopiter =
             notifications.begin();
         loopiter != notifications.end(); ) {
      NotificationList::Notifications::const_iterator curiter = loopiter++;
      std::string notification_id = curiter->id;
      // May call RemoveNotification and erase curiter.
      delegate_->NotificationRemoved(notification_id);
    }
  }
}

void MessageCenter::DisableNotificationByExtension(
    const std::string& id) {
  if (delegate_)
    delegate_->DisableExtension(id);
  // When we disable notifications, we remove any existing matching
  // notifications to avoid adding complicated UI to re-enable the source.
  notification_list_->SendRemoveNotificationsByExtension(id);
}

void MessageCenter::DisableNotificationByUrl(const std::string& id) {
  if (delegate_)
    delegate_->DisableNotificationsFromSource(id);
  notification_list_->SendRemoveNotificationsBySource(id);
}

void MessageCenter::ShowNotificationSettings(const std::string& id) {
  if (delegate_)
    delegate_->ShowSettings(id);
}

void MessageCenter::OnNotificationClicked(const std::string& id) {
  if (delegate_)
    delegate_->OnClicked(id);
}

void MessageCenter::OnQuietModeChanged(bool quiet_mode) {
  host_->MessageCenterChanged(true);
}

NotificationList* MessageCenter::GetNotificationList() {
  return notification_list_.get();
}

}  // namespace message_center