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
|
// 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/ui/views/ash/balloon_collection_impl_ash.h"
#include "ash/ash_switches.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "base/command_line.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/notifications/balloon.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/views/ash/balloon_view_ash.h"
#include "chrome/browser/ui/views/notifications/balloon_view_host.h"
#include "chrome/browser/ui/views/notifications/balloon_view_views.h"
namespace {
bool IsAshNotifyEnabled() {
return CommandLine::ForCurrentProcess()->HasSwitch(ash::switches::kAshNotify);
}
} // namespace
BalloonCollectionImplAsh::BalloonCollectionImplAsh() {
if (IsAshNotifyEnabled()) {
ash::Shell::GetInstance()->status_area_widget()->
web_notification_tray()->SetDelegate(this);
}
}
BalloonCollectionImplAsh::~BalloonCollectionImplAsh() {
}
bool BalloonCollectionImplAsh::HasSpace() const {
if (!IsAshNotifyEnabled())
return BalloonCollectionImpl::HasSpace();
return true; // Overflow is handled by ash::WebNotificationTray.
}
void BalloonCollectionImplAsh::Add(const Notification& notification,
Profile* profile) {
if (IsAshNotifyEnabled()) {
if (notification.is_html())
return; // HTML notifications are not supported in Ash.
if (notification.title().empty() && notification.body().empty())
return; // Empty notification, don't show.
}
return BalloonCollectionImpl::Add(notification, profile);
}
void BalloonCollectionImplAsh::DisableExtension(
const std::string& notifcation_id) {
Balloon* balloon = base().FindBalloonById(notifcation_id);
if (!balloon)
return;
ExtensionService* extension_service =
balloon->profile()->GetExtensionService();
const GURL& origin = balloon->notification().origin_url();
const extensions::Extension* extension =
extension_service->extensions()->GetExtensionOrAppByURL(
ExtensionURLInfo(origin));
if (!extension)
return;
extension_service->DisableExtension(
extension->id(), extensions::Extension::DISABLE_USER_ACTION);
}
void BalloonCollectionImplAsh::DisableNotificationsFromSource(
const std::string& notifcation_id) {
Balloon* balloon = base().FindBalloonById(notifcation_id);
if (!balloon)
return;
DesktopNotificationService* service =
DesktopNotificationServiceFactory::GetForProfile(balloon->profile());
service->DenyPermission(balloon->notification().origin_url());
}
void BalloonCollectionImplAsh::NotificationRemoved(
const std::string& notifcation_id) {
RemoveById(notifcation_id);
}
void BalloonCollectionImplAsh::ShowSettings(const std::string& notifcation_id) {
Balloon* balloon = base().FindBalloonById(notifcation_id);
Profile* profile =
balloon ? balloon->profile() : ProfileManager::GetDefaultProfile();
Browser* browser = browser::FindOrCreateTabbedBrowser(profile);
browser->ShowContentSettingsPage(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
}
void BalloonCollectionImplAsh::OnClicked(const std::string& notifcation_id) {
Balloon* balloon = base().FindBalloonById(notifcation_id);
if (!balloon)
return;
balloon->OnClick();
}
bool BalloonCollectionImplAsh::AddWebUIMessageCallback(
const Notification& notification,
const std::string& message,
const chromeos::BalloonViewHost::MessageCallback& callback) {
#if defined(OS_CHROMEOS)
Balloon* balloon = base().FindBalloon(notification);
if (!balloon)
return false;
BalloonHost* balloon_host = balloon->balloon_view()->GetHost();
if (!balloon_host)
return false;
chromeos::BalloonViewHost* balloon_view_host =
static_cast<chromeos::BalloonViewHost*>(balloon_host);
return balloon_view_host->AddWebUIMessageCallback(message, callback);
#else
return false;
#endif
}
void BalloonCollectionImplAsh::AddSystemNotification(
const Notification& notification,
Profile* profile,
bool sticky) {
system_notifications_.insert(notification.notification_id());
// Add balloons to the front of the stack. This ensures that system
// notifications will always be displayed. NOTE: This has the side effect
// that system notifications are displayed in inverse order, with the most
// recent notification always at the front of the list.
AddImpl(notification, profile, true /* add to front*/);
}
bool BalloonCollectionImplAsh::UpdateNotification(
const Notification& notification) {
Balloon* balloon = base().FindBalloon(notification);
if (!balloon)
return false;
balloon->Update(notification);
return true;
}
bool BalloonCollectionImplAsh::UpdateAndShowNotification(
const Notification& notification) {
return UpdateNotification(notification);
}
Balloon* BalloonCollectionImplAsh::MakeBalloon(
const Notification& notification, Profile* profile) {
Balloon* balloon = new Balloon(notification, profile, this);
if (!IsAshNotifyEnabled()) {
::BalloonViewImpl* balloon_view = new ::BalloonViewImpl(this);
if (system_notifications_.find(notification.notification_id()) !=
system_notifications_.end())
balloon_view->set_enable_web_ui(true);
balloon->set_view(balloon_view);
gfx::Size size(layout().min_balloon_width(), layout().min_balloon_height());
balloon->set_content_size(size);
} else {
BalloonViewAsh* balloon_view = new BalloonViewAsh(this);
balloon->set_view(balloon_view);
}
return balloon;
}
// For now, only use BalloonCollectionImplAsh on ChromeOS, until
// system_notifications_ is replaced with status area notifications.
#if defined(OS_CHROMEOS)
// static
BalloonCollection* BalloonCollection::Create() {
return new BalloonCollectionImplAsh();
}
#endif
|