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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
// 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 "chrome/browser/notifications/balloon_notification_ui_manager.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/stl_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/fullscreen.h"
#include "chrome/browser/idle.h"
#include "chrome/browser/notifications/balloon_collection.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
// A class which represents a notification waiting to be shown.
class QueuedNotification {
public:
QueuedNotification(const Notification& notification, Profile* profile)
: notification_(notification),
profile_(profile) {
}
const Notification& notification() const { return notification_; }
Profile* profile() const { return profile_; }
void Replace(const Notification& new_notification) {
notification_ = new_notification;
}
private:
// The notification to be shown.
Notification notification_;
// Non owned pointer to the user's profile.
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(QueuedNotification);
};
BalloonNotificationUIManager::BalloonNotificationUIManager(
PrefService* local_state)
: NotificationPrefsManager(local_state),
// Passes NULL to blockers since |message_center| is not used from balloon
// notifications.
screen_lock_blocker_(NULL),
fullscreen_blocker_(NULL),
system_observer_(this) {
position_pref_.Init(
prefs::kDesktopNotificationPosition,
local_state,
base::Bind(
&BalloonNotificationUIManager::OnDesktopNotificationPositionChanged,
base::Unretained(this)));
}
BalloonNotificationUIManager::~BalloonNotificationUIManager() {
}
void BalloonNotificationUIManager::SetBalloonCollection(
BalloonCollection* balloon_collection) {
DCHECK(!balloon_collection_.get() ||
balloon_collection_->GetActiveBalloons().size() == 0);
DCHECK(balloon_collection);
balloon_collection_.reset(balloon_collection);
balloon_collection_->SetPositionPreference(
static_cast<BalloonCollection::PositionPreference>(
position_pref_.GetValue()));
balloon_collection_->set_space_change_listener(this);
}
void BalloonNotificationUIManager::Add(const Notification& notification,
Profile* profile) {
if (Update(notification, profile)) {
return;
}
VLOG(1) << "Added notification. URL: "
<< notification.content_url().spec();
show_queue_.push_back(linked_ptr<QueuedNotification>(
new QueuedNotification(notification, profile)));
CheckAndShowNotifications();
}
bool BalloonNotificationUIManager::Update(const Notification& notification,
Profile* profile) {
const GURL& origin = notification.origin_url();
const string16& replace_id = notification.replace_id();
if (replace_id.empty())
return false;
// First check the queue of pending notifications for replacement.
// Then check the list of notifications already being shown.
for (NotificationDeque::const_iterator iter = show_queue_.begin();
iter != show_queue_.end(); ++iter) {
if (profile == (*iter)->profile() &&
origin == (*iter)->notification().origin_url() &&
replace_id == (*iter)->notification().replace_id()) {
(*iter)->Replace(notification);
return true;
}
}
return UpdateNotification(notification, profile);
}
const Notification* BalloonNotificationUIManager::FindById(
const std::string& id) const {
for (NotificationDeque::const_iterator iter = show_queue_.begin();
iter != show_queue_.end(); ++iter) {
if ((*iter)->notification().notification_id() == id) {
return &((*iter)->notification());
}
}
return balloon_collection_->FindById(id);
}
bool BalloonNotificationUIManager::CancelById(const std::string& id) {
// See if this ID hasn't been shown yet.
for (NotificationDeque::iterator iter = show_queue_.begin();
iter != show_queue_.end(); ++iter) {
if ((*iter)->notification().notification_id() == id) {
show_queue_.erase(iter);
return true;
}
}
// If it has been shown, remove it from the balloon collections.
return balloon_collection_->RemoveById(id);
}
std::set<std::string>
BalloonNotificationUIManager::GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) {
std::set<std::string> notification_ids;
for (NotificationDeque::iterator iter = show_queue_.begin();
iter != show_queue_.end(); iter++) {
if ((*iter)->notification().origin_url() == source &&
profile->IsSameProfile((*iter)->profile())) {
notification_ids.insert((*iter)->notification().notification_id());
}
}
const BalloonCollection::Balloons& balloons =
balloon_collection_->GetActiveBalloons();
for (BalloonCollection::Balloons::const_iterator iter = balloons.begin();
iter != balloons.end(); ++iter) {
if (profile->IsSameProfile((*iter)->profile()) &&
source == (*iter)->notification().origin_url()) {
notification_ids.insert((*iter)->notification().notification_id());
}
}
return notification_ids;
}
bool BalloonNotificationUIManager::CancelAllBySourceOrigin(const GURL& source) {
// Same pattern as CancelById, but more complicated than the above
// because there may be multiple notifications from the same source.
bool removed = false;
for (NotificationDeque::iterator loopiter = show_queue_.begin();
loopiter != show_queue_.end(); ) {
if ((*loopiter)->notification().origin_url() != source) {
++loopiter;
continue;
}
loopiter = show_queue_.erase(loopiter);
removed = true;
}
return balloon_collection_->RemoveBySourceOrigin(source) || removed;
}
bool BalloonNotificationUIManager::CancelAllByProfile(Profile* profile) {
// Same pattern as CancelAllBySourceOrigin.
bool removed = false;
for (NotificationDeque::iterator loopiter = show_queue_.begin();
loopiter != show_queue_.end(); ) {
if ((*loopiter)->profile() != profile) {
++loopiter;
continue;
}
loopiter = show_queue_.erase(loopiter);
removed = true;
}
return balloon_collection_->RemoveByProfile(profile) || removed;
}
void BalloonNotificationUIManager::CancelAll() {
balloon_collection_->RemoveAll();
}
BalloonCollection* BalloonNotificationUIManager::balloon_collection() {
return balloon_collection_.get();
}
NotificationPrefsManager* BalloonNotificationUIManager::prefs_manager() {
return this;
}
bool BalloonNotificationUIManager::ShowNotification(
const Notification& notification,
Profile* profile) {
if (!balloon_collection_->HasSpace())
return false;
balloon_collection_->Add(notification, profile);
return true;
}
void BalloonNotificationUIManager::OnBalloonSpaceChanged() {
CheckAndShowNotifications();
}
void BalloonNotificationUIManager::OnBlockingStateChanged() {
CheckAndShowNotifications();
}
bool BalloonNotificationUIManager::UpdateNotification(
const Notification& notification,
Profile* profile) {
const GURL& origin = notification.origin_url();
const string16& replace_id = notification.replace_id();
DCHECK(!replace_id.empty());
const BalloonCollection::Balloons& balloons =
balloon_collection_->GetActiveBalloons();
for (BalloonCollection::Balloons::const_iterator iter = balloons.begin();
iter != balloons.end(); ++iter) {
if (profile == (*iter)->profile() &&
origin == (*iter)->notification().origin_url() &&
replace_id == (*iter)->notification().replace_id()) {
(*iter)->Update(notification);
return true;
}
}
return false;
}
BalloonCollection::PositionPreference
BalloonNotificationUIManager::GetPositionPreference() const {
LOG(INFO) << "Current position preference: " << position_pref_.GetValue();
return static_cast<BalloonCollection::PositionPreference>(
position_pref_.GetValue());
}
void BalloonNotificationUIManager::SetPositionPreference(
BalloonCollection::PositionPreference preference) {
LOG(INFO) << "Setting position preference: " << preference;
position_pref_.SetValue(static_cast<int>(preference));
balloon_collection_->SetPositionPreference(preference);
}
void BalloonNotificationUIManager::CheckAndShowNotifications() {
screen_lock_blocker_.CheckState();
fullscreen_blocker_.CheckState();
if (screen_lock_blocker_.is_locked() ||
fullscreen_blocker_.is_fullscreen_mode()) {
return;
}
ShowNotifications();
}
void BalloonNotificationUIManager::OnDesktopNotificationPositionChanged() {
balloon_collection_->SetPositionPreference(
static_cast<BalloonCollection::PositionPreference>(
position_pref_.GetValue()));
}
void BalloonNotificationUIManager::ShowNotifications() {
while (!show_queue_.empty()) {
linked_ptr<QueuedNotification> queued_notification(show_queue_.front());
show_queue_.pop_front();
if (!ShowNotification(queued_notification->notification(),
queued_notification->profile())) {
show_queue_.push_front(queued_notification);
return;
}
}
}
// static
BalloonNotificationUIManager*
BalloonNotificationUIManager::GetInstanceForTesting() {
if (NotificationUIManager::DelegatesToMessageCenter()) {
LOG(ERROR) << "Attempt to run a test that requires "
<< "BalloonNotificationUIManager while delegating to a "
<< "native MessageCenter. Test will fail. Ask dimich@";
return NULL;
}
return static_cast<BalloonNotificationUIManager*>(
g_browser_process->notification_ui_manager());
}
void BalloonNotificationUIManager::GetQueuedNotificationsForTesting(
std::vector<const Notification*>* notifications) {
for (NotificationDeque::const_iterator iter = show_queue_.begin();
iter != show_queue_.end(); ++iter) {
notifications->push_back(&(*iter)->notification());
}
}
|