OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/notifications/message_center_notification_manager.h" | 5 #include "chrome/browser/notifications/message_center_notification_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "chrome/browser/extensions/api/notification_provider/notification_provi
der_api.h" | |
14 #include "chrome/browser/notifications/extension_welcome_notification.h" | 13 #include "chrome/browser/notifications/extension_welcome_notification.h" |
15 #include "chrome/browser/notifications/extension_welcome_notification_factory.h" | 14 #include "chrome/browser/notifications/extension_welcome_notification_factory.h" |
16 #include "chrome/browser/notifications/fullscreen_notification_blocker.h" | 15 #include "chrome/browser/notifications/fullscreen_notification_blocker.h" |
17 #include "chrome/browser/notifications/message_center_settings_controller.h" | 16 #include "chrome/browser/notifications/message_center_settings_controller.h" |
18 #include "chrome/browser/notifications/notification.h" | 17 #include "chrome/browser/notifications/notification.h" |
19 #include "chrome/browser/notifications/notification_conversion_helper.h" | |
20 #include "chrome/browser/notifications/profile_notification.h" | 18 #include "chrome/browser/notifications/profile_notification.h" |
21 #include "chrome/browser/notifications/screen_lock_notification_blocker.h" | 19 #include "chrome/browser/notifications/screen_lock_notification_blocker.h" |
22 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/common/extensions/api/notification_provider.h" | |
24 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/url_constants.h" | 22 #include "content/public/common/url_constants.h" |
26 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
27 #include "extensions/common/extension_set.h" | 24 #include "extensions/common/extension_set.h" |
28 #include "extensions/common/permissions/permissions_data.h" | 25 #include "extensions/common/permissions/permissions_data.h" |
29 #include "ui/gfx/image/image_skia.h" | 26 #include "ui/gfx/image/image_skia.h" |
30 #include "ui/message_center/message_center_style.h" | 27 #include "ui/message_center/message_center_style.h" |
31 #include "ui/message_center/message_center_tray.h" | 28 #include "ui/message_center/message_center_tray.h" |
32 #include "ui/message_center/message_center_types.h" | 29 #include "ui/message_center/message_center_types.h" |
33 #include "ui/message_center/notifier_settings.h" | 30 #include "ui/message_center/notifier_settings.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 91 |
95 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile)-> | 92 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile)-> |
96 ShowWelcomeNotificationIfNecessary(profile_notification->notification()); | 93 ShowWelcomeNotificationIfNecessary(profile_notification->notification()); |
97 | 94 |
98 // WARNING: You MUST use AddProfileNotification or update the message center | 95 // WARNING: You MUST use AddProfileNotification or update the message center |
99 // via the notification within a ProfileNotification object or the profile ID | 96 // via the notification within a ProfileNotification object or the profile ID |
100 // will not be correctly set for ChromeOS. | 97 // will not be correctly set for ChromeOS. |
101 // Takes ownership of profile_notification. | 98 // Takes ownership of profile_notification. |
102 AddProfileNotification(std::move(profile_notification_ptr)); | 99 AddProfileNotification(std::move(profile_notification_ptr)); |
103 | 100 |
104 // TODO(liyanhou): Change the logic to only send notifications to one party. | |
105 // Currently, if there is an app with notificationProvider permission, | |
106 // notifications will go to both message center and the app. | |
107 // Change it to send notifications to message center only when the user chose | |
108 // default message center (extension_id.empty()). | |
109 | |
110 // If there exist apps/extensions that have notificationProvider permission, | |
111 // route notifications to one of the apps/extensions. | |
112 std::string extension_id = GetExtensionTakingOverNotifications(profile); | |
113 if (!extension_id.empty()) | |
114 AddNotificationToAlternateProvider(profile_notification->notification(), | |
115 profile, extension_id); | |
116 | |
117 message_center_->AddNotification( | 101 message_center_->AddNotification( |
118 base::MakeUnique<message_center::Notification>( | 102 base::MakeUnique<message_center::Notification>( |
119 profile_notification->notification())); | 103 profile_notification->notification())); |
120 } | 104 } |
121 | 105 |
122 bool MessageCenterNotificationManager::Update(const Notification& notification, | 106 bool MessageCenterNotificationManager::Update(const Notification& notification, |
123 Profile* profile) { | 107 Profile* profile) { |
124 const std::string& tag = notification.tag(); | 108 const std::string& tag = notification.tag(); |
125 if (tag.empty()) | 109 if (tag.empty()) |
126 return false; | 110 return false; |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest( | 300 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest( |
317 const std::string& delegate_id, | 301 const std::string& delegate_id, |
318 Profile* profile) { | 302 Profile* profile) { |
319 return ProfileNotification::GetProfileNotificationId(delegate_id, | 303 return ProfileNotification::GetProfileNotificationId(delegate_id, |
320 GetProfileID(profile)); | 304 GetProfileID(profile)); |
321 } | 305 } |
322 | 306 |
323 //////////////////////////////////////////////////////////////////////////////// | 307 //////////////////////////////////////////////////////////////////////////////// |
324 // private | 308 // private |
325 | 309 |
326 void MessageCenterNotificationManager::AddNotificationToAlternateProvider( | |
327 const Notification& notification, | |
328 Profile* profile, | |
329 const std::string& extension_id) const { | |
330 // Convert data from Notification type to NotificationOptions type. | |
331 extensions::api::notifications::NotificationOptions options; | |
332 NotificationConversionHelper::NotificationToNotificationOptions(notification, | |
333 &options); | |
334 | |
335 // Send the notification to the alternate provider extension/app. | |
336 extensions::NotificationProviderEventRouter event_router(profile); | |
337 event_router.CreateNotification(extension_id, | |
338 notification.notifier_id().id, | |
339 notification.delegate_id(), | |
340 options); | |
341 } | |
342 | |
343 void MessageCenterNotificationManager::AddProfileNotification( | 310 void MessageCenterNotificationManager::AddProfileNotification( |
344 std::unique_ptr<ProfileNotification> profile_notification) { | 311 std::unique_ptr<ProfileNotification> profile_notification) { |
345 const Notification& notification = profile_notification->notification(); | 312 const Notification& notification = profile_notification->notification(); |
346 std::string id = notification.id(); | 313 std::string id = notification.id(); |
347 // Notification ids should be unique. | 314 // Notification ids should be unique. |
348 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); | 315 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); |
349 profile_notifications_[id] = std::move(profile_notification); | 316 profile_notifications_[id] = std::move(profile_notification); |
350 } | 317 } |
351 | 318 |
352 void MessageCenterNotificationManager::RemoveProfileNotification( | 319 void MessageCenterNotificationManager::RemoveProfileNotification( |
(...skipping 17 matching lines...) Expand all Loading... |
370 } | 337 } |
371 | 338 |
372 ProfileNotification* MessageCenterNotificationManager::FindProfileNotification( | 339 ProfileNotification* MessageCenterNotificationManager::FindProfileNotification( |
373 const std::string& id) const { | 340 const std::string& id) const { |
374 auto iter = profile_notifications_.find(id); | 341 auto iter = profile_notifications_.find(id); |
375 if (iter == profile_notifications_.end()) | 342 if (iter == profile_notifications_.end()) |
376 return nullptr; | 343 return nullptr; |
377 | 344 |
378 return (*iter).second.get(); | 345 return (*iter).second.get(); |
379 } | 346 } |
380 | |
381 std::string | |
382 MessageCenterNotificationManager::GetExtensionTakingOverNotifications( | |
383 Profile* profile) { | |
384 // TODO(liyanhou): When additional settings in Chrome Settings is implemented, | |
385 // change choosing the last app with permission to a user selected app. | |
386 extensions::ExtensionRegistry* registry = | |
387 extensions::ExtensionRegistry::Get(profile); | |
388 DCHECK(registry); | |
389 std::string extension_id; | |
390 for (extensions::ExtensionSet::const_iterator it = | |
391 registry->enabled_extensions().begin(); | |
392 it != registry->enabled_extensions().end(); | |
393 ++it) { | |
394 if ((*it->get()).permissions_data()->HasAPIPermission( | |
395 extensions::APIPermission::ID::kNotificationProvider)) { | |
396 extension_id = (*it->get()).id(); | |
397 return extension_id; | |
398 } | |
399 } | |
400 return extension_id; | |
401 } | |
OLD | NEW |