blob: f49ec6a2b663d1e2e8fb69c7e589cc5f843e951b (
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
|
// 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/stl_util.h"
#include "chrome/browser/browser_process.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/prefs/pref_service.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
BalloonNotificationUIManager::BalloonNotificationUIManager(
PrefService* local_state)
: NotificationUIManagerImpl(),
balloon_collection_(NULL) {
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);
}
bool BalloonNotificationUIManager::DoesIdExist(const std::string& id) {
if (NotificationUIManagerImpl::DoesIdExist(id))
return true;
return balloon_collection_->DoesIdExist(id);
}
bool BalloonNotificationUIManager::CancelById(const std::string& id) {
// See if this ID hasn't been shown yet.
if (NotificationUIManagerImpl::CancelById(id))
return true;
// If it has been shown, remove it from the balloon collections.
return balloon_collection_->RemoveById(id);
}
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 = NotificationUIManagerImpl::CancelAllBySourceOrigin(source);
return balloon_collection_->RemoveBySourceOrigin(source) || removed;
}
bool BalloonNotificationUIManager::CancelAllByProfile(Profile* profile) {
// Same pattern as CancelAllBySourceOrigin.
bool removed = NotificationUIManagerImpl::CancelAllByProfile(profile);
return balloon_collection_->RemoveByProfile(profile) || removed;
}
void BalloonNotificationUIManager::CancelAll() {
NotificationUIManagerImpl::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();
}
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::OnDesktopNotificationPositionChanged() {
balloon_collection_->SetPositionPreference(
static_cast<BalloonCollection::PositionPreference>(
position_pref_.GetValue()));
}
// 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());
}
|