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/profile_notification.h"
23 #include "chrome/browser/notifications/screen_lock_notification_blocker.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/common/extensions/api/notification_provider.h"
26 #include "chrome/common/pref_names.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/common/url_constants.h"
29 #include "extensions/browser/extension_registry.h"
30 #include "extensions/common/extension_set.h"
31 #include "extensions/common/permissions/permissions_data.h"
32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/message_center/message_center_style.h"
34 #include "ui/message_center/message_center_tray.h"
35 #include "ui/message_center/message_center_types.h"
36 #include "ui/message_center/notifier_settings.h"
38 #if defined(OS_CHROMEOS)
39 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
40 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
44 #include "ash/shell.h"
45 #include "ash/system/web_notification/web_notification_tray.h"
49 // The first-run balloon will be shown |kFirstRunIdleDelaySeconds| after all
50 // popups go away and the user has notifications in the message center.
51 const int kFirstRunIdleDelaySeconds
= 1;
54 MessageCenterNotificationManager::MessageCenterNotificationManager(
55 message_center::MessageCenter
* message_center
,
56 PrefService
* local_state
,
57 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider
)
58 : message_center_(message_center
),
60 first_run_idle_timeout_(
61 base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds
)),
64 settings_provider_(settings_provider
.Pass()),
65 system_observer_(this),
66 stats_collector_(message_center
),
67 google_now_stats_collector_(message_center
) {
69 first_run_pref_
.Init(prefs::kMessageCenterShowedFirstRunBalloon
, local_state
);
72 message_center_
->AddObserver(this);
73 message_center_
->SetNotifierSettingsProvider(settings_provider_
.get());
75 #if defined(OS_CHROMEOS)
76 #if !defined(USE_ATHENA)
77 // TODO(oshima|hashimoto): Support notification on athena. crbug.com/408755.
79 new LoginStateNotificationBlockerChromeOS(message_center
));
82 blockers_
.push_back(new ScreenLockNotificationBlocker(message_center
));
84 blockers_
.push_back(new FullscreenNotificationBlocker(message_center
));
86 #if defined(OS_WIN) || defined(OS_MACOSX) \
87 || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
88 // On Windows, Linux and Mac, the notification manager owns the tray icon and
89 // views.Other platforms have global ownership and Create will return NULL.
90 tray_
.reset(message_center::CreateMessageCenterTray());
94 MessageCenterNotificationManager::~MessageCenterNotificationManager() {
95 message_center_
->SetNotifierSettingsProvider(NULL
);
96 message_center_
->RemoveObserver(this);
98 STLDeleteContainerPairSecondPointers(profile_notifications_
.begin(),
99 profile_notifications_
.end());
100 profile_notifications_
.clear();
103 void MessageCenterNotificationManager::RegisterPrefs(
104 PrefRegistrySimple
* registry
) {
105 registry
->RegisterBooleanPref(prefs::kMessageCenterShowedFirstRunBalloon
,
107 registry
->RegisterBooleanPref(prefs::kMessageCenterShowIcon
, true);
108 registry
->RegisterBooleanPref(prefs::kMessageCenterForcedOnTaskbar
, false);
111 ////////////////////////////////////////////////////////////////////////////////
112 // NotificationUIManager
114 void MessageCenterNotificationManager::Add(const Notification
& notification
,
116 if (Update(notification
, profile
))
119 ProfileNotification
* profile_notification
=
120 new ProfileNotification(profile
, notification
);
122 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile
)->
123 ShowWelcomeNotificationIfNecessary(profile_notification
->notification());
125 // WARNING: You MUST use AddProfileNotification or update the message center
126 // via the notification within a ProfileNotification object or the profile ID
127 // will not be correctly set for ChromeOS.
128 // Takes ownership of profile_notification.
129 AddProfileNotification(profile_notification
);
131 // TODO(liyanhou): Change the logic to only send notifications to one party.
132 // Currently, if there is an app with notificationProvider permission,
133 // notifications will go to both message center and the app.
134 // Change it to send notifications to message center only when the user chose
135 // default message center (extension_id.empty()).
137 // If there exist apps/extensions that have notificationProvider permission,
138 // route notifications to one of the apps/extensions.
139 std::string extension_id
= GetExtensionTakingOverNotifications(profile
);
140 if (!extension_id
.empty())
141 AddNotificationToAlternateProvider(profile_notification
, extension_id
);
143 message_center_
->AddNotification(make_scoped_ptr(
144 new message_center::Notification(profile_notification
->notification())));
147 bool MessageCenterNotificationManager::Update(const Notification
& notification
,
149 const base::string16
& replace_id
= notification
.replace_id();
150 if (replace_id
.empty())
153 const GURL origin_url
= notification
.origin_url();
154 DCHECK(origin_url
.is_valid());
156 // Since replace_id is provided by arbitrary JS, we need to use origin_url
157 // (which is an app url in case of app/extension) to scope the replace ids
158 // in the given profile.
159 for (NotificationMap::iterator iter
= profile_notifications_
.begin();
160 iter
!= profile_notifications_
.end(); ++iter
) {
161 ProfileNotification
* old_notification
= (*iter
).second
;
162 if (old_notification
->notification().replace_id() == replace_id
&&
163 old_notification
->notification().origin_url() == origin_url
&&
164 old_notification
->profile() == profile
) {
165 // Changing the type from non-progress to progress does not count towards
166 // the immediate update allowed in the message center.
167 std::string old_id
= old_notification
->notification().id();
169 // Add/remove notification in the local list but just update the same
170 // one in MessageCenter.
171 delete old_notification
;
172 profile_notifications_
.erase(old_id
);
173 ProfileNotification
* new_notification
=
174 new ProfileNotification(profile
, notification
);
175 profile_notifications_
[new_notification
->notification().id()] =
178 // TODO(liyanhou): Add routing updated notifications to alternative
181 // WARNING: You MUST use AddProfileNotification or update the message
182 // center via the notification within a ProfileNotification object or the
183 // profile ID will not be correctly set for ChromeOS.
184 message_center_
->UpdateNotification(
186 make_scoped_ptr(new message_center::Notification(
187 new_notification
->notification())));
195 const Notification
* MessageCenterNotificationManager::FindById(
196 const std::string
& delegate_id
,
197 ProfileID profile_id
) const {
198 // The profile pointer can be weak, the instance may have been destroyed, so
199 // no profile method should be called inside this function.
201 std::string profile_notification_id
=
202 ProfileNotification::GetProfileNotificationId(delegate_id
, profile_id
);
203 NotificationMap::const_iterator iter
=
204 profile_notifications_
.find(profile_notification_id
);
205 if (iter
== profile_notifications_
.end())
207 return &(iter
->second
->notification());
210 bool MessageCenterNotificationManager::CancelById(
211 const std::string
& delegate_id
,
212 ProfileID profile_id
) {
213 // The profile pointer can be weak, the instance may have been destroyed, so
214 // no profile method should be called inside this function.
216 std::string profile_notification_id
=
217 ProfileNotification::GetProfileNotificationId(delegate_id
, profile_id
);
218 // See if this ID hasn't been shown yet.
219 // If it has been shown, remove it.
220 NotificationMap::iterator iter
=
221 profile_notifications_
.find(profile_notification_id
);
222 if (iter
== profile_notifications_
.end())
225 RemoveProfileNotification(iter
->second
);
226 message_center_
->RemoveNotification(profile_notification_id
,
227 /* by_user */ false);
231 std::set
<std::string
>
232 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
234 const GURL
& source
) {
235 // The profile pointer can be weak, the instance may have been destroyed, so
236 // no profile method should be called inside this function.
238 std::set
<std::string
> delegate_ids
;
239 for (NotificationMap::iterator iter
= profile_notifications_
.begin();
240 iter
!= profile_notifications_
.end(); iter
++) {
241 if ((*iter
).second
->notification().origin_url() == source
&&
242 profile
== (*iter
).second
->profile()) {
243 delegate_ids
.insert(iter
->second
->notification().delegate_id());
249 bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
250 const GURL
& source
) {
251 // Same pattern as CancelById, but more complicated than the above
252 // because there may be multiple notifications from the same source.
253 bool removed
= false;
255 for (NotificationMap::iterator loopiter
= profile_notifications_
.begin();
256 loopiter
!= profile_notifications_
.end(); ) {
257 NotificationMap::iterator curiter
= loopiter
++;
258 if ((*curiter
).second
->notification().origin_url() == source
) {
259 const std::string id
= curiter
->first
;
260 RemoveProfileNotification(curiter
->second
);
261 message_center_
->RemoveNotification(id
, /* by_user */ false);
268 bool MessageCenterNotificationManager::CancelAllByProfile(
269 ProfileID profile_id
) {
270 // Same pattern as CancelAllBySourceOrigin.
271 bool removed
= false;
273 for (NotificationMap::iterator loopiter
= profile_notifications_
.begin();
274 loopiter
!= profile_notifications_
.end(); ) {
275 NotificationMap::iterator curiter
= loopiter
++;
276 if (profile_id
== NotificationUIManager::GetProfileID(
277 (*curiter
).second
->profile())) {
278 const std::string id
= curiter
->first
;
279 RemoveProfileNotification(curiter
->second
);
280 message_center_
->RemoveNotification(id
, /* by_user */ false);
287 void MessageCenterNotificationManager::CancelAll() {
288 message_center_
->RemoveAllNotifications(/* by_user */ false);
291 ////////////////////////////////////////////////////////////////////////////////
292 // MessageCenter::Observer
293 void MessageCenterNotificationManager::OnNotificationRemoved(
294 const std::string
& id
,
296 NotificationMap::const_iterator iter
= profile_notifications_
.find(id
);
297 if (iter
!= profile_notifications_
.end())
298 RemoveProfileNotification(iter
->second
);
301 CheckFirstRunTimer();
305 void MessageCenterNotificationManager::OnCenterVisibilityChanged(
306 message_center::Visibility visibility
) {
308 if (visibility
== message_center::VISIBILITY_TRANSIENT
)
309 CheckFirstRunTimer();
313 void MessageCenterNotificationManager::OnNotificationUpdated(
314 const std::string
& id
) {
316 CheckFirstRunTimer();
320 void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
322 tray_
->GetMessageCenterTray()->HideMessageCenterBubble();
325 if (ash::Shell::HasInstance()) {
326 ash::WebNotificationTray
* tray
=
327 ash::Shell::GetInstance()->GetWebNotificationTray();
329 tray
->GetMessageCenterTray()->HideMessageCenterBubble();
334 void MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
335 message_center::MessageCenterTrayDelegate
* delegate
) {
336 tray_
.reset(delegate
);
340 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest(
341 const std::string
& delegate_id
,
343 return ProfileNotification::GetProfileNotificationId(delegate_id
,
344 GetProfileID(profile
));
347 ////////////////////////////////////////////////////////////////////////////////
350 void MessageCenterNotificationManager::AddNotificationToAlternateProvider(
351 ProfileNotification
* profile_notification
,
352 const std::string
& extension_id
) const {
353 const Notification
& notification
= profile_notification
->notification();
355 // Convert data from Notification type to NotificationOptions type.
356 extensions::api::notifications::NotificationOptions options
;
357 NotificationConversionHelper::NotificationToNotificationOptions(notification
,
360 // Send the notification to the alternate provider extension/app.
361 extensions::NotificationProviderEventRouter
event_router(
362 profile_notification
->profile());
363 event_router
.CreateNotification(extension_id
,
364 notification
.notifier_id().id
,
365 notification
.delegate_id(),
369 void MessageCenterNotificationManager::AddProfileNotification(
370 ProfileNotification
* profile_notification
) {
371 const Notification
& notification
= profile_notification
->notification();
372 std::string id
= notification
.id();
373 // Notification ids should be unique.
374 DCHECK(profile_notifications_
.find(id
) == profile_notifications_
.end());
375 profile_notifications_
[id
] = profile_notification
;
378 void MessageCenterNotificationManager::RemoveProfileNotification(
379 ProfileNotification
* profile_notification
) {
380 std::string id
= profile_notification
->notification().id();
381 profile_notifications_
.erase(id
);
382 delete profile_notification
;
385 ProfileNotification
* MessageCenterNotificationManager::FindProfileNotification(
386 const std::string
& id
) const {
387 NotificationMap::const_iterator iter
= profile_notifications_
.find(id
);
388 if (iter
== profile_notifications_
.end())
391 return (*iter
).second
;
395 MessageCenterNotificationManager::GetExtensionTakingOverNotifications(
397 // TODO(liyanhou): When additional settings in Chrome Settings is implemented,
398 // change choosing the last app with permission to a user selected app.
399 extensions::ExtensionRegistry
* registry
=
400 extensions::ExtensionRegistry::Get(profile
);
402 std::string extension_id
;
403 for (extensions::ExtensionSet::const_iterator it
=
404 registry
->enabled_extensions().begin();
405 it
!= registry
->enabled_extensions().end();
407 if ((*it
->get()).permissions_data()->HasAPIPermission(
408 extensions::APIPermission::ID::kNotificationProvider
)) {
409 extension_id
= (*it
->get()).id();