summaryrefslogtreecommitdiffstats
path: root/ui/message_center/message_center.cc
blob: 02ea67e5dc8a687b3c81bc02d1dabe50146b176e (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 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"
#include "base/memory/singleton.h"
#include "base/observer_list.h"

namespace message_center {

//------------------------------------------------------------------------------
MessageCenter::MessageCenter()
    : delegate_(NULL) {
  notification_list_.reset(new NotificationList(this));
}

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

void MessageCenter::AddObserver(Observer* observer) {
  observer_list_.AddObserver(observer);
}

void MessageCenter::RemoveObserver(Observer* observer) {
  observer_list_.RemoveObserver(observer);
}

void MessageCenter::SetDelegate(Delegate* 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);
  NotifyMessageCenterChanged(true);
}

void MessageCenter::UpdateNotification(
    const std::string& old_id,
    const std::string& new_id,
    const string16& title,
    const string16& message,
    const base::DictionaryValue* optional_fields) {
  notification_list_->UpdateNotificationMessage(
      old_id, new_id, title, message, optional_fields);
  NotifyMessageCenterChanged(true);
}

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

void MessageCenter::SetNotificationIcon(const std::string& notification_id,
                                        const gfx::ImageSkia& image) {
  if (notification_list_->SetNotificationIcon(notification_id, image))
    NotifyMessageCenterChanged(true);
}

void MessageCenter::SetNotificationImage(const std::string& notification_id,
                                         const gfx::ImageSkia& image) {
  if (notification_list_->SetNotificationImage(notification_id, image))
    NotifyMessageCenterChanged(true);
}

void MessageCenter::SetNotificationButtonIcon(
    const std::string& notification_id, int button_index,
    const gfx::ImageSkia& image) {
  if (notification_list_->SetNotificationButtonIcon(notification_id,
                                                    button_index, image))
    NotifyMessageCenterChanged(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::ShowNotificationSettingsDialog(gfx::NativeView context) {
  if (delegate_)
    delegate_->ShowSettingsDialog(context);
}

void MessageCenter::OnNotificationClicked(const std::string& id) {
  if (delegate_)
    delegate_->OnClicked(id);
  if (HasPopupNotifications()) {
    notification_list_->MarkSinglePopupAsShown(id, true);
    NotifyMessageCenterChanged(false);
  }
}

void MessageCenter::OnQuietModeChanged(bool quiet_mode) {
  NotifyMessageCenterChanged(true);
}

void MessageCenter::OnButtonClicked(const std::string& id, int button_index) {
  if (delegate_)
    delegate_->OnButtonClicked(id, button_index);
  if (HasPopupNotifications()) {
    notification_list_->MarkSinglePopupAsShown(id, true);
    NotifyMessageCenterChanged(false);
  }
}

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

void MessageCenter::Delegate::OnButtonClicked(const std::string& id,
                                              int button_index) {
}

//------------------------------------------------------------------------------
// Private.

void MessageCenter::NotifyMessageCenterChanged(bool new_notification) {
  FOR_EACH_OBSERVER(Observer,
                    observer_list_,
                    OnMessageCenterChanged(new_notification));
}


}  // namespace message_center