1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/notifications/message_center_notification_manager.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/stl_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/extensions/api/notification_provider/notification_provider_api.h"
12 #include "chrome/browser/notifications/extension_welcome_notification.h"
13 #include "chrome/browser/notifications/extension_welcome_notification_factory.h"
14 #include "chrome/browser/notifications/fullscreen_notification_blocker.h"
15 #include "chrome/browser/notifications/message_center_settings_controller.h"
16 #include "chrome/browser/notifications/notification.h"
17 #include "chrome/browser/notifications/notification_conversion_helper.h"
18 #include "chrome/browser/notifications/profile_notification.h"
19 #include "chrome/browser/notifications/screen_lock_notification_blocker.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/extensions/api/notification_provider.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/url_constants.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "extensions/common/extension_set.h"
26 #include "extensions/common/permissions/permissions_data.h"
27 #include "ui/gfx/image/image_skia.h"
28 #include "ui/message_center/message_center_style.h"
29 #include "ui/message_center/message_center_tray.h"
30 #include "ui/message_center/message_center_types.h"
31 #include "ui/message_center/notifier_settings.h"
33 #if defined(OS_CHROMEOS)
34 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
35 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
39 #include "ash/shell.h"
40 #include "ash/system/web_notification/web_notification_tray.h"
43 MessageCenterNotificationManager::MessageCenterNotificationManager(
44 message_center::MessageCenter
* message_center
,
45 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider
)
46 : message_center_(message_center
),
47 settings_provider_(settings_provider
.Pass()),
48 system_observer_(this),
49 stats_collector_(message_center
),
50 google_now_stats_collector_(message_center
)
52 message_center_
->AddObserver(this);
53 message_center_
->SetNotifierSettingsProvider(settings_provider_
.get());
55 #if defined(OS_CHROMEOS)
57 new LoginStateNotificationBlockerChromeOS(message_center
));
59 blockers_
.push_back(new ScreenLockNotificationBlocker(message_center
));
61 blockers_
.push_back(new FullscreenNotificationBlocker(message_center
));
63 #if defined(OS_WIN) || defined(OS_MACOSX) \
64 || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
65 // On Windows, Linux and Mac, the notification manager owns the tray icon and
66 // views.Other platforms have global ownership and Create will return NULL.
67 tray_
.reset(message_center::CreateMessageCenterTray());
71 MessageCenterNotificationManager::~MessageCenterNotificationManager() {
72 message_center_
->SetNotifierSettingsProvider(NULL
);
73 message_center_
->RemoveObserver(this);
75 STLDeleteContainerPairSecondPointers(profile_notifications_
.begin(),
76 profile_notifications_
.end());
77 profile_notifications_
.clear();
80 ////////////////////////////////////////////////////////////////////////////////
81 // NotificationUIManager
83 void MessageCenterNotificationManager::Add(const Notification
& notification
,
85 if (Update(notification
, profile
))
88 ProfileNotification
* profile_notification
=
89 new ProfileNotification(profile
, notification
);
91 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile
)->
92 ShowWelcomeNotificationIfNecessary(profile_notification
->notification());
94 // WARNING: You MUST use AddProfileNotification or update the message center
95 // via the notification within a ProfileNotification object or the profile ID
96 // will not be correctly set for ChromeOS.
97 // Takes ownership of profile_notification.
98 AddProfileNotification(profile_notification
);
100 // TODO(liyanhou): Change the logic to only send notifications to one party.
101 // Currently, if there is an app with notificationProvider permission,
102 // notifications will go to both message center and the app.
103 // Change it to send notifications to message center only when the user chose
104 // default message center (extension_id.empty()).
106 // If there exist apps/extensions that have notificationProvider permission,
107 // route notifications to one of the apps/extensions.
108 std::string extension_id
= GetExtensionTakingOverNotifications(profile
);
109 if (!extension_id
.empty())
110 AddNotificationToAlternateProvider(profile_notification
->notification(),
111 profile
, extension_id
);
113 message_center_
->AddNotification(make_scoped_ptr(
114 new message_center::Notification(profile_notification
->notification())));
117 bool MessageCenterNotificationManager::Update(const Notification
& notification
,
119 const std::string
& tag
= notification
.tag();
123 const GURL origin_url
= notification
.origin_url();
124 DCHECK(origin_url
.is_valid());
126 // Since tag is provided by arbitrary JS, we need to use origin_url
127 // (which is an app url in case of app/extension) to scope the tags
128 // in the given profile.
129 for (NotificationMap::iterator iter
= profile_notifications_
.begin();
130 iter
!= profile_notifications_
.end(); ++iter
) {
131 ProfileNotification
* old_notification
= (*iter
).second
;
132 if (old_notification
->notification().tag() == tag
&&
133 old_notification
->notification().origin_url() == origin_url
&&
134 old_notification
->profile_id() ==
135 NotificationUIManager::GetProfileID(profile
)) {
136 // Changing the type from non-progress to progress does not count towards
137 // the immediate update allowed in the message center.
138 std::string old_id
= old_notification
->notification().id();
140 // Add/remove notification in the local list but just update the same
141 // one in MessageCenter.
142 delete old_notification
;
143 profile_notifications_
.erase(old_id
);
144 ProfileNotification
* new_notification
=
145 new ProfileNotification(profile
, notification
);
146 profile_notifications_
[new_notification
->notification().id()] =
149 // TODO(liyanhou): Add routing updated notifications to alternative
152 // WARNING: You MUST use AddProfileNotification or update the message
153 // center via the notification within a ProfileNotification object or the
154 // profile ID will not be correctly set for ChromeOS.
155 message_center_
->UpdateNotification(
157 make_scoped_ptr(new message_center::Notification(
158 new_notification
->notification())));
166 const Notification
* MessageCenterNotificationManager::FindById(
167 const std::string
& delegate_id
,
168 ProfileID profile_id
) const {
169 // The profile pointer can be weak, the instance may have been destroyed, so
170 // no profile method should be called inside this function.
172 std::string profile_notification_id
=
173 ProfileNotification::GetProfileNotificationId(delegate_id
, profile_id
);
174 NotificationMap::const_iterator iter
=
175 profile_notifications_
.find(profile_notification_id
);
176 if (iter
== profile_notifications_
.end())
178 return &(iter
->second
->notification());
181 bool MessageCenterNotificationManager::CancelById(
182 const std::string
& delegate_id
,
183 ProfileID profile_id
) {
184 // The profile pointer can be weak, the instance may have been destroyed, so
185 // no profile method should be called inside this function.
187 std::string profile_notification_id
=
188 ProfileNotification::GetProfileNotificationId(delegate_id
, profile_id
);
189 // See if this ID hasn't been shown yet.
190 // If it has been shown, remove it.
191 NotificationMap::iterator iter
=
192 profile_notifications_
.find(profile_notification_id
);
193 if (iter
== profile_notifications_
.end())
196 RemoveProfileNotification(iter
->second
);
197 message_center_
->RemoveNotification(profile_notification_id
,
198 /* by_user */ false);
202 std::set
<std::string
>
203 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
204 ProfileID profile_id
,
205 const GURL
& source
) {
206 std::set
<std::string
> delegate_ids
;
207 for (const auto& pair
: profile_notifications_
) {
208 const Notification
& notification
= pair
.second
->notification();
209 if (pair
.second
->profile_id() == profile_id
&&
210 notification
.origin_url() == source
) {
211 delegate_ids
.insert(notification
.delegate_id());
218 std::set
<std::string
> MessageCenterNotificationManager::GetAllIdsByProfile(
219 ProfileID profile_id
) {
220 std::set
<std::string
> delegate_ids
;
221 for (const auto& pair
: profile_notifications_
) {
222 if (pair
.second
->profile_id() == profile_id
)
223 delegate_ids
.insert(pair
.second
->notification().delegate_id());
229 bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
230 const GURL
& source
) {
231 // Same pattern as CancelById, but more complicated than the above
232 // because there may be multiple notifications from the same source.
233 bool removed
= false;
235 for (NotificationMap::iterator loopiter
= profile_notifications_
.begin();
236 loopiter
!= profile_notifications_
.end(); ) {
237 NotificationMap::iterator curiter
= loopiter
++;
238 if ((*curiter
).second
->notification().origin_url() == source
) {
239 const std::string id
= curiter
->first
;
240 RemoveProfileNotification(curiter
->second
);
241 message_center_
->RemoveNotification(id
, /* by_user */ false);
248 bool MessageCenterNotificationManager::CancelAllByProfile(
249 ProfileID profile_id
) {
250 // Same pattern as CancelAllBySourceOrigin.
251 bool removed
= false;
253 for (NotificationMap::iterator loopiter
= profile_notifications_
.begin();
254 loopiter
!= profile_notifications_
.end(); ) {
255 NotificationMap::iterator curiter
= loopiter
++;
256 if (profile_id
== (*curiter
).second
->profile_id()) {
257 const std::string id
= curiter
->first
;
258 RemoveProfileNotification(curiter
->second
);
259 message_center_
->RemoveNotification(id
, /* by_user */ false);
266 void MessageCenterNotificationManager::CancelAll() {
267 message_center_
->RemoveAllNotifications(/* by_user */ false);
270 ////////////////////////////////////////////////////////////////////////////////
271 // MessageCenter::Observer
272 void MessageCenterNotificationManager::OnNotificationRemoved(
273 const std::string
& id
,
275 NotificationMap::const_iterator iter
= profile_notifications_
.find(id
);
276 if (iter
!= profile_notifications_
.end())
277 RemoveProfileNotification(iter
->second
);
280 void MessageCenterNotificationManager::OnCenterVisibilityChanged(
281 message_center::Visibility visibility
) {
284 void MessageCenterNotificationManager::OnNotificationUpdated(
285 const std::string
& id
) {
288 void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
289 if (tray_
.get() && tray_
->GetMessageCenterTray())
290 tray_
->GetMessageCenterTray()->HideMessageCenterBubble();
293 if (ash::Shell::HasInstance()) {
294 ash::WebNotificationTray
* tray
=
295 ash::Shell::GetInstance()->GetWebNotificationTray();
297 tray
->GetMessageCenterTray()->HideMessageCenterBubble();
302 void MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
303 message_center::MessageCenterTrayDelegate
* delegate
) {
304 tray_
.reset(delegate
);
308 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest(
309 const std::string
& delegate_id
,
311 return ProfileNotification::GetProfileNotificationId(delegate_id
,
312 GetProfileID(profile
));
315 ////////////////////////////////////////////////////////////////////////////////
318 void MessageCenterNotificationManager::AddNotificationToAlternateProvider(
319 const Notification
& notification
,
321 const std::string
& extension_id
) const {
322 // Convert data from Notification type to NotificationOptions type.
323 extensions::api::notifications::NotificationOptions options
;
324 NotificationConversionHelper::NotificationToNotificationOptions(notification
,
327 // Send the notification to the alternate provider extension/app.
328 extensions::NotificationProviderEventRouter
event_router(profile
);
329 event_router
.CreateNotification(extension_id
,
330 notification
.notifier_id().id
,
331 notification
.delegate_id(),
335 void MessageCenterNotificationManager::AddProfileNotification(
336 ProfileNotification
* profile_notification
) {
337 const Notification
& notification
= profile_notification
->notification();
338 std::string id
= notification
.id();
339 // Notification ids should be unique.
340 DCHECK(profile_notifications_
.find(id
) == profile_notifications_
.end());
341 profile_notifications_
[id
] = profile_notification
;
344 void MessageCenterNotificationManager::RemoveProfileNotification(
345 ProfileNotification
* profile_notification
) {
346 std::string id
= profile_notification
->notification().id();
347 profile_notifications_
.erase(id
);
348 delete profile_notification
;
351 ProfileNotification
* MessageCenterNotificationManager::FindProfileNotification(
352 const std::string
& id
) const {
353 NotificationMap::const_iterator iter
= profile_notifications_
.find(id
);
354 if (iter
== profile_notifications_
.end())
357 return (*iter
).second
;
361 MessageCenterNotificationManager::GetExtensionTakingOverNotifications(
363 // TODO(liyanhou): When additional settings in Chrome Settings is implemented,
364 // change choosing the last app with permission to a user selected app.
365 extensions::ExtensionRegistry
* registry
=
366 extensions::ExtensionRegistry::Get(profile
);
368 std::string extension_id
;
369 for (extensions::ExtensionSet::const_iterator it
=
370 registry
->enabled_extensions().begin();
371 it
!= registry
->enabled_extensions().end();
373 if ((*it
->get()).permissions_data()->HasAPIPermission(
374 extensions::APIPermission::ID::kNotificationProvider
)) {
375 extension_id
= (*it
->get()).id();