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)
77 new LoginStateNotificationBlockerChromeOS(message_center
));
79 blockers_
.push_back(new ScreenLockNotificationBlocker(message_center
));
81 blockers_
.push_back(new FullscreenNotificationBlocker(message_center
));
83 #if defined(OS_WIN) || defined(OS_MACOSX) \
84 || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
85 // On Windows, Linux and Mac, the notification manager owns the tray icon and
86 // views.Other platforms have global ownership and Create will return NULL.
87 tray_
.reset(message_center::CreateMessageCenterTray());
91 MessageCenterNotificationManager::~MessageCenterNotificationManager() {
92 message_center_
->SetNotifierSettingsProvider(NULL
);
93 message_center_
->RemoveObserver(this);
95 STLDeleteContainerPairSecondPointers(profile_notifications_
.begin(),
96 profile_notifications_
.end());
97 profile_notifications_
.clear();
100 void MessageCenterNotificationManager::RegisterPrefs(
101 PrefRegistrySimple
* registry
) {
102 registry
->RegisterBooleanPref(prefs::kMessageCenterShowedFirstRunBalloon
,
104 registry
->RegisterBooleanPref(prefs::kMessageCenterShowIcon
, true);
105 registry
->RegisterBooleanPref(prefs::kMessageCenterForcedOnTaskbar
, false);
108 ////////////////////////////////////////////////////////////////////////////////
109 // NotificationUIManager
111 void MessageCenterNotificationManager::Add(const Notification
& notification
,
113 if (Update(notification
, profile
))
116 ProfileNotification
* profile_notification
=
117 new ProfileNotification(profile
, notification
);
119 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile
)->
120 ShowWelcomeNotificationIfNecessary(profile_notification
->notification());
122 // WARNING: You MUST use AddProfileNotification or update the message center
123 // via the notification within a ProfileNotification object or the profile ID
124 // will not be correctly set for ChromeOS.
125 // Takes ownership of profile_notification.
126 AddProfileNotification(profile_notification
);
128 // TODO(liyanhou): Change the logic to only send notifications to one party.
129 // Currently, if there is an app with notificationProvider permission,
130 // notifications will go to both message center and the app.
131 // Change it to send notifications to message center only when the user chose
132 // default message center (extension_id.empty()).
134 // If there exist apps/extensions that have notificationProvider permission,
135 // route notifications to one of the apps/extensions.
136 std::string extension_id
= GetExtensionTakingOverNotifications(profile
);
137 if (!extension_id
.empty())
138 AddNotificationToAlternateProvider(profile_notification
, extension_id
);
140 message_center_
->AddNotification(make_scoped_ptr(
141 new message_center::Notification(profile_notification
->notification())));
144 bool MessageCenterNotificationManager::Update(const Notification
& notification
,
146 const base::string16
& replace_id
= notification
.replace_id();
147 if (replace_id
.empty())
150 const GURL origin_url
= notification
.origin_url();
151 DCHECK(origin_url
.is_valid());
153 // Since replace_id is provided by arbitrary JS, we need to use origin_url
154 // (which is an app url in case of app/extension) to scope the replace ids
155 // in the given profile.
156 for (NotificationMap::iterator iter
= profile_notifications_
.begin();
157 iter
!= profile_notifications_
.end(); ++iter
) {
158 ProfileNotification
* old_notification
= (*iter
).second
;
159 if (old_notification
->notification().replace_id() == replace_id
&&
160 old_notification
->notification().origin_url() == origin_url
&&
161 old_notification
->profile() == profile
) {
162 // Changing the type from non-progress to progress does not count towards
163 // the immediate update allowed in the message center.
164 std::string old_id
= old_notification
->notification().id();
166 // Add/remove notification in the local list but just update the same
167 // one in MessageCenter.
168 delete old_notification
;
169 profile_notifications_
.erase(old_id
);
170 ProfileNotification
* new_notification
=
171 new ProfileNotification(profile
, notification
);
172 profile_notifications_
[new_notification
->notification().id()] =
175 // TODO(liyanhou): Add routing updated notifications to alternative
178 // WARNING: You MUST use AddProfileNotification or update the message
179 // center via the notification within a ProfileNotification object or the
180 // profile ID will not be correctly set for ChromeOS.
181 message_center_
->UpdateNotification(
183 make_scoped_ptr(new message_center::Notification(
184 new_notification
->notification())));
192 const Notification
* MessageCenterNotificationManager::FindById(
193 const std::string
& delegate_id
,
194 ProfileID profile_id
) const {
195 // The profile pointer can be weak, the instance may have been destroyed, so
196 // no profile method should be called inside this function.
198 std::string profile_notification_id
=
199 ProfileNotification::GetProfileNotificationId(delegate_id
, profile_id
);
200 NotificationMap::const_iterator iter
=
201 profile_notifications_
.find(profile_notification_id
);
202 if (iter
== profile_notifications_
.end())
204 return &(iter
->second
->notification());
207 bool MessageCenterNotificationManager::CancelById(
208 const std::string
& delegate_id
,
209 ProfileID profile_id
) {
210 // The profile pointer can be weak, the instance may have been destroyed, so
211 // no profile method should be called inside this function.
213 std::string profile_notification_id
=
214 ProfileNotification::GetProfileNotificationId(delegate_id
, profile_id
);
215 // See if this ID hasn't been shown yet.
216 // If it has been shown, remove it.
217 NotificationMap::iterator iter
=
218 profile_notifications_
.find(profile_notification_id
);
219 if (iter
== profile_notifications_
.end())
222 RemoveProfileNotification(iter
->second
);
223 message_center_
->RemoveNotification(profile_notification_id
,
224 /* by_user */ false);
228 std::set
<std::string
>
229 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
231 const GURL
& source
) {
232 // The profile pointer can be weak, the instance may have been destroyed, so
233 // no profile method should be called inside this function.
235 std::set
<std::string
> delegate_ids
;
236 for (NotificationMap::iterator iter
= profile_notifications_
.begin();
237 iter
!= profile_notifications_
.end(); iter
++) {
238 if ((*iter
).second
->notification().origin_url() == source
&&
239 profile
== (*iter
).second
->profile()) {
240 delegate_ids
.insert(iter
->second
->notification().delegate_id());
246 bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
247 const GURL
& source
) {
248 // Same pattern as CancelById, but more complicated than the above
249 // because there may be multiple notifications from the same source.
250 bool removed
= false;
252 for (NotificationMap::iterator loopiter
= profile_notifications_
.begin();
253 loopiter
!= profile_notifications_
.end(); ) {
254 NotificationMap::iterator curiter
= loopiter
++;
255 if ((*curiter
).second
->notification().origin_url() == source
) {
256 const std::string id
= curiter
->first
;
257 RemoveProfileNotification(curiter
->second
);
258 message_center_
->RemoveNotification(id
, /* by_user */ false);
265 bool MessageCenterNotificationManager::CancelAllByProfile(
266 ProfileID profile_id
) {
267 // Same pattern as CancelAllBySourceOrigin.
268 bool removed
= false;
270 for (NotificationMap::iterator loopiter
= profile_notifications_
.begin();
271 loopiter
!= profile_notifications_
.end(); ) {
272 NotificationMap::iterator curiter
= loopiter
++;
273 if (profile_id
== NotificationUIManager::GetProfileID(
274 (*curiter
).second
->profile())) {
275 const std::string id
= curiter
->first
;
276 RemoveProfileNotification(curiter
->second
);
277 message_center_
->RemoveNotification(id
, /* by_user */ false);
284 void MessageCenterNotificationManager::CancelAll() {
285 message_center_
->RemoveAllNotifications(/* by_user */ false);
288 ////////////////////////////////////////////////////////////////////////////////
289 // MessageCenter::Observer
290 void MessageCenterNotificationManager::OnNotificationRemoved(
291 const std::string
& id
,
293 NotificationMap::const_iterator iter
= profile_notifications_
.find(id
);
294 if (iter
!= profile_notifications_
.end())
295 RemoveProfileNotification(iter
->second
);
298 CheckFirstRunTimer();
302 void MessageCenterNotificationManager::OnCenterVisibilityChanged(
303 message_center::Visibility visibility
) {
305 if (visibility
== message_center::VISIBILITY_TRANSIENT
)
306 CheckFirstRunTimer();
310 void MessageCenterNotificationManager::OnNotificationUpdated(
311 const std::string
& id
) {
313 CheckFirstRunTimer();
317 void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
319 tray_
->GetMessageCenterTray()->HideMessageCenterBubble();
322 if (ash::Shell::HasInstance()) {
323 ash::WebNotificationTray
* tray
=
324 ash::Shell::GetInstance()->GetWebNotificationTray();
326 tray
->GetMessageCenterTray()->HideMessageCenterBubble();
331 void MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
332 message_center::MessageCenterTrayDelegate
* delegate
) {
333 tray_
.reset(delegate
);
337 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest(
338 const std::string
& delegate_id
,
340 return ProfileNotification::GetProfileNotificationId(delegate_id
,
341 GetProfileID(profile
));
344 ////////////////////////////////////////////////////////////////////////////////
347 void MessageCenterNotificationManager::AddNotificationToAlternateProvider(
348 ProfileNotification
* profile_notification
,
349 const std::string
& extension_id
) const {
350 const Notification
& notification
= profile_notification
->notification();
352 // Convert data from Notification type to NotificationOptions type.
353 extensions::api::notifications::NotificationOptions options
;
354 NotificationConversionHelper::NotificationToNotificationOptions(notification
,
357 // Send the notification to the alternate provider extension/app.
358 extensions::NotificationProviderEventRouter
event_router(
359 profile_notification
->profile());
360 event_router
.CreateNotification(extension_id
,
361 notification
.notifier_id().id
,
362 notification
.delegate_id(),
366 void MessageCenterNotificationManager::AddProfileNotification(
367 ProfileNotification
* profile_notification
) {
368 const Notification
& notification
= profile_notification
->notification();
369 std::string id
= notification
.id();
370 // Notification ids should be unique.
371 DCHECK(profile_notifications_
.find(id
) == profile_notifications_
.end());
372 profile_notifications_
[id
] = profile_notification
;
375 void MessageCenterNotificationManager::RemoveProfileNotification(
376 ProfileNotification
* profile_notification
) {
377 std::string id
= profile_notification
->notification().id();
378 profile_notifications_
.erase(id
);
379 delete profile_notification
;
382 ProfileNotification
* MessageCenterNotificationManager::FindProfileNotification(
383 const std::string
& id
) const {
384 NotificationMap::const_iterator iter
= profile_notifications_
.find(id
);
385 if (iter
== profile_notifications_
.end())
388 return (*iter
).second
;
392 MessageCenterNotificationManager::GetExtensionTakingOverNotifications(
394 // TODO(liyanhou): When additional settings in Chrome Settings is implemented,
395 // change choosing the last app with permission to a user selected app.
396 extensions::ExtensionRegistry
* registry
=
397 extensions::ExtensionRegistry::Get(profile
);
399 std::string extension_id
;
400 for (extensions::ExtensionSet::const_iterator it
=
401 registry
->enabled_extensions().begin();
402 it
!= registry
->enabled_extensions().end();
404 if ((*it
->get()).permissions_data()->HasAPIPermission(
405 extensions::APIPermission::ID::kNotificationProvider
)) {
406 extension_id
= (*it
->get()).id();