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/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/browser/extensions/api/notification_provider/notification_provider_api.h"
14 #include "chrome/browser/notifications/desktop_notification_service.h"
15 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
16 #include "chrome/browser/notifications/extension_welcome_notification.h"
17 #include "chrome/browser/notifications/extension_welcome_notification_factory.h"
18 #include "chrome/browser/notifications/fullscreen_notification_blocker.h"
19 #include "chrome/browser/notifications/message_center_settings_controller.h"
20 #include "chrome/browser/notifications/notification.h"
21 #include "chrome/browser/notifications/notification_conversion_helper.h"
22 #include "chrome/browser/notifications/screen_lock_notification_blocker.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/common/extensions/api/notification_provider.h"
25 #include "chrome/common/pref_names.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/url_constants.h"
28 #include "extensions/browser/extension_registry.h"
29 #include "extensions/common/extension_set.h"
30 #include "extensions/common/permissions/permissions_data.h"
31 #include "ui/gfx/image/image_skia.h"
32 #include "ui/message_center/message_center_style.h"
33 #include "ui/message_center/message_center_tray.h"
34 #include "ui/message_center/message_center_types.h"
35 #include "ui/message_center/notifier_settings.h"
37 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
39 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
43 #include "ash/shell.h"
44 #include "ash/system/web_notification/web_notification_tray.h"
48 // The first-run balloon will be shown |kFirstRunIdleDelaySeconds| after all
49 // popups go away and the user has notifications in the message center.
50 const int kFirstRunIdleDelaySeconds
= 1;
55 // Returns a string that uniquely identifies a profile + delegate_id pair.
56 // The profile_id is used as an identifier to identify a profile instance; it
57 // cannot be NULL. The returned ID becomes invalid when a profile is destroyed,
58 // therefore CancelAllByProfile() must be called in that case.
59 std::string
GetProfileNotificationId(const std::string
& delegate_id
,
60 ProfileID profile_id
) {
62 return base::StringPrintf("notification-ui-manager#%p#%s",
63 profile_id
, // Each profile has its unique instance
64 // including incognito profile.
70 MessageCenterNotificationManager::MessageCenterNotificationManager(
71 message_center::MessageCenter
* message_center
,
72 PrefService
* local_state
,
73 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider
)
74 : message_center_(message_center
),
76 first_run_idle_timeout_(
77 base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds
)),
80 settings_provider_(settings_provider
.Pass()),
81 system_observer_(this),
82 stats_collector_(message_center
),
83 google_now_stats_collector_(message_center
) {
85 first_run_pref_
.Init(prefs::kMessageCenterShowedFirstRunBalloon
, local_state
);
88 message_center_
->AddObserver(this);
89 message_center_
->SetNotifierSettingsProvider(settings_provider_
.get());
91 #if defined(OS_CHROMEOS)
92 #if !defined(USE_ATHENA)
93 // TODO(oshima|hashimoto): Support notification on athena. crbug.com/408755.
95 new LoginStateNotificationBlockerChromeOS(message_center
));
98 blockers_
.push_back(new ScreenLockNotificationBlocker(message_center
));
100 blockers_
.push_back(new FullscreenNotificationBlocker(message_center
));
102 #if defined(OS_WIN) || defined(OS_MACOSX) \
103 || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
104 // On Windows, Linux and Mac, the notification manager owns the tray icon and
105 // views.Other platforms have global ownership and Create will return NULL.
106 tray_
.reset(message_center::CreateMessageCenterTray());
110 MessageCenterNotificationManager::~MessageCenterNotificationManager() {
111 message_center_
->SetNotifierSettingsProvider(NULL
);
112 message_center_
->RemoveObserver(this);
114 STLDeleteContainerPairSecondPointers(profile_notifications_
.begin(),
115 profile_notifications_
.end());
116 profile_notifications_
.clear();
119 void MessageCenterNotificationManager::RegisterPrefs(
120 PrefRegistrySimple
* registry
) {
121 registry
->RegisterBooleanPref(prefs::kMessageCenterShowedFirstRunBalloon
,
123 registry
->RegisterBooleanPref(prefs::kMessageCenterShowIcon
, true);
124 registry
->RegisterBooleanPref(prefs::kMessageCenterForcedOnTaskbar
, false);
127 ////////////////////////////////////////////////////////////////////////////////
128 // NotificationUIManager
130 void MessageCenterNotificationManager::Add(const Notification
& notification
,
132 if (Update(notification
, profile
))
135 ProfileNotification
* profile_notification
=
136 new ProfileNotification(profile
, notification
, message_center_
);
138 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile
)->
139 ShowWelcomeNotificationIfNecessary(profile_notification
->notification());
141 // WARNING: You MUST use AddProfileNotification or update the message center
142 // via the notification within a ProfileNotification object or the profile ID
143 // will not be correctly set for ChromeOS.
144 // Takes ownership of profile_notification.
145 AddProfileNotification(profile_notification
);
147 // TODO(liyanhou): Change the logic to only send notifications to one party.
148 // Currently, if there is an app with notificationProvider permission,
149 // notifications will go to both message center and the app.
150 // Change it to send notifications to message center only when the user chose
151 // default message center (extension_id.empty()).
153 // If there exist apps/extensions that have notificationProvider permission,
154 // route notifications to one of the apps/extensions.
155 std::string extension_id
= GetExtensionTakingOverNotifications(profile
);
156 if (!extension_id
.empty())
157 profile_notification
->AddToAlternateProvider(extension_id
);
159 message_center_
->AddNotification(make_scoped_ptr(
160 new message_center::Notification(profile_notification
->notification())));
163 bool MessageCenterNotificationManager::Update(const Notification
& notification
,
165 const base::string16
& replace_id
= notification
.replace_id();
166 if (replace_id
.empty())
169 const GURL origin_url
= notification
.origin_url();
170 DCHECK(origin_url
.is_valid());
172 // Since replace_id is provided by arbitrary JS, we need to use origin_url
173 // (which is an app url in case of app/extension) to scope the replace ids
174 // in the given profile.
175 for (NotificationMap::iterator iter
= profile_notifications_
.begin();
176 iter
!= profile_notifications_
.end(); ++iter
) {
177 ProfileNotification
* old_notification
= (*iter
).second
;
178 if (old_notification
->notification().replace_id() == replace_id
&&
179 old_notification
->notification().origin_url() == origin_url
&&
180 old_notification
->profile() == profile
) {
181 // Changing the type from non-progress to progress does not count towards
182 // the immediate update allowed in the message center.
183 std::string old_id
= old_notification
->notification().id();
185 // Add/remove notification in the local list but just update the same
186 // one in MessageCenter.
187 delete old_notification
;
188 profile_notifications_
.erase(old_id
);
189 ProfileNotification
* new_notification
=
190 new ProfileNotification(profile
, notification
, message_center_
);
191 profile_notifications_
[new_notification
->notification().id()] =
194 // TODO(liyanhou): Add routing updated notifications to alternative
197 // WARNING: You MUST use AddProfileNotification or update the message
198 // center via the notification within a ProfileNotification object or the
199 // profile ID will not be correctly set for ChromeOS.
200 message_center_
->UpdateNotification(
202 make_scoped_ptr(new message_center::Notification(
203 new_notification
->notification())));
211 const Notification
* MessageCenterNotificationManager::FindById(
212 const std::string
& delegate_id
,
213 ProfileID profile_id
) const {
214 // The profile pointer can be weak, the instance may have been destroyed, so
215 // no profile method should be called inside this function.
217 std::string profile_notification_id
=
218 GetProfileNotificationId(delegate_id
, profile_id
);
219 NotificationMap::const_iterator iter
=
220 profile_notifications_
.find(profile_notification_id
);
221 if (iter
== profile_notifications_
.end())
223 return &(iter
->second
->notification());
226 bool MessageCenterNotificationManager::CancelById(
227 const std::string
& delegate_id
,
228 ProfileID profile_id
) {
229 // The profile pointer can be weak, the instance may have been destroyed, so
230 // no profile method should be called inside this function.
232 std::string profile_notification_id
=
233 GetProfileNotificationId(delegate_id
, profile_id
);
234 // See if this ID hasn't been shown yet.
235 // If it has been shown, remove it.
236 NotificationMap::iterator iter
=
237 profile_notifications_
.find(profile_notification_id
);
238 if (iter
== profile_notifications_
.end())
241 RemoveProfileNotification(iter
->second
);
242 message_center_
->RemoveNotification(profile_notification_id
,
243 /* by_user */ false);
247 std::set
<std::string
>
248 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
250 const GURL
& source
) {
251 // The profile pointer can be weak, the instance may have been destroyed, so
252 // no profile method should be called inside this function.
254 std::set
<std::string
> delegate_ids
;
255 for (NotificationMap::iterator iter
= profile_notifications_
.begin();
256 iter
!= profile_notifications_
.end(); iter
++) {
257 if ((*iter
).second
->notification().origin_url() == source
&&
258 profile
== (*iter
).second
->profile()) {
259 delegate_ids
.insert(iter
->second
->notification().delegate_id());
265 bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
266 const GURL
& source
) {
267 // Same pattern as CancelById, but more complicated than the above
268 // because there may be multiple notifications from the same source.
269 bool removed
= false;
271 for (NotificationMap::iterator loopiter
= profile_notifications_
.begin();
272 loopiter
!= profile_notifications_
.end(); ) {
273 NotificationMap::iterator curiter
= loopiter
++;
274 if ((*curiter
).second
->notification().origin_url() == source
) {
275 const std::string id
= curiter
->first
;
276 RemoveProfileNotification(curiter
->second
);
277 message_center_
->RemoveNotification(id
, /* by_user */ false);
284 bool MessageCenterNotificationManager::CancelAllByProfile(
285 ProfileID profile_id
) {
286 // Same pattern as CancelAllBySourceOrigin.
287 bool removed
= false;
289 for (NotificationMap::iterator loopiter
= profile_notifications_
.begin();
290 loopiter
!= profile_notifications_
.end(); ) {
291 NotificationMap::iterator curiter
= loopiter
++;
292 if (profile_id
== NotificationUIManager::GetProfileID(
293 (*curiter
).second
->profile())) {
294 const std::string id
= curiter
->first
;
295 RemoveProfileNotification(curiter
->second
);
296 message_center_
->RemoveNotification(id
, /* by_user */ false);
303 void MessageCenterNotificationManager::CancelAll() {
304 message_center_
->RemoveAllNotifications(/* by_user */ false);
307 ////////////////////////////////////////////////////////////////////////////////
308 // MessageCenter::Observer
309 void MessageCenterNotificationManager::OnNotificationRemoved(
310 const std::string
& id
,
312 NotificationMap::const_iterator iter
= profile_notifications_
.find(id
);
313 if (iter
!= profile_notifications_
.end())
314 RemoveProfileNotification(iter
->second
);
317 CheckFirstRunTimer();
321 void MessageCenterNotificationManager::OnCenterVisibilityChanged(
322 message_center::Visibility visibility
) {
324 if (visibility
== message_center::VISIBILITY_TRANSIENT
)
325 CheckFirstRunTimer();
329 void MessageCenterNotificationManager::OnNotificationUpdated(
330 const std::string
& id
) {
332 CheckFirstRunTimer();
336 void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
338 tray_
->GetMessageCenterTray()->HideMessageCenterBubble();
341 if (ash::Shell::HasInstance()) {
342 ash::WebNotificationTray
* tray
=
343 ash::Shell::GetInstance()->GetWebNotificationTray();
345 tray
->GetMessageCenterTray()->HideMessageCenterBubble();
350 void MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
351 message_center::MessageCenterTrayDelegate
* delegate
) {
352 tray_
.reset(delegate
);
356 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest(
357 const std::string
& delegate_id
,
359 return GetProfileNotificationId(delegate_id
, GetProfileID(profile
));
362 ////////////////////////////////////////////////////////////////////////////////
363 // ProfileNotification
365 MessageCenterNotificationManager::ProfileNotification::ProfileNotification(
367 const Notification
& notification
,
368 message_center::MessageCenter
* message_center
)
371 // Uses Notification's copy constructor to assign the message center
372 // id, which should be unique for every profile + Notification pair.
373 GetProfileNotificationId(notification
.delegate_id(),
374 GetProfileID(profile
)),
377 #if defined(OS_CHROMEOS)
378 notification_
.set_profile_id(multi_user_util::GetUserIDFromProfile(profile
));
382 MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() {
386 MessageCenterNotificationManager::ProfileNotification::AddToAlternateProvider(
387 const std::string extension_id
) {
388 // Convert data from Notification type to NotificationOptions type.
389 extensions::api::notifications::NotificationOptions options
;
390 NotificationConversionHelper::NotificationToNotificationOptions(notification_
,
393 // Send the notification to the alternate provider extension/app.
394 extensions::NotificationProviderEventRouter
event_router(profile_
);
395 event_router
.CreateNotification(extension_id
,
396 notification_
.notifier_id().id
,
397 notification_
.delegate_id(),
401 ////////////////////////////////////////////////////////////////////////////////
404 void MessageCenterNotificationManager::AddProfileNotification(
405 ProfileNotification
* profile_notification
) {
406 const Notification
& notification
= profile_notification
->notification();
407 std::string id
= notification
.id();
408 // Notification ids should be unique.
409 DCHECK(profile_notifications_
.find(id
) == profile_notifications_
.end());
410 profile_notifications_
[id
] = profile_notification
;
413 void MessageCenterNotificationManager::RemoveProfileNotification(
414 ProfileNotification
* profile_notification
) {
415 std::string id
= profile_notification
->notification().id();
416 profile_notifications_
.erase(id
);
417 delete profile_notification
;
420 MessageCenterNotificationManager::ProfileNotification
*
421 MessageCenterNotificationManager::FindProfileNotification(
422 const std::string
& id
) const {
423 NotificationMap::const_iterator iter
= profile_notifications_
.find(id
);
424 if (iter
== profile_notifications_
.end())
427 return (*iter
).second
;
431 MessageCenterNotificationManager::GetExtensionTakingOverNotifications(
433 // TODO(liyanhou): When additional settings in Chrome Settings is implemented,
434 // change choosing the last app with permission to a user selected app.
435 extensions::ExtensionRegistry
* registry
=
436 extensions::ExtensionRegistry::Get(profile
);
438 std::string extension_id
;
439 for (extensions::ExtensionSet::const_iterator it
=
440 registry
->enabled_extensions().begin();
441 it
!= registry
->enabled_extensions().end();
443 if ((*it
->get()).permissions_data()->HasAPIPermission(
444 extensions::APIPermission::ID::kNotificationProvider
)) {
445 extension_id
= (*it
->get()).id();