Chromium Code Reviews| Index: chrome/browser/notifications/message_center_notification_manager.cc |
| diff --git a/chrome/browser/notifications/message_center_notification_manager.cc b/chrome/browser/notifications/message_center_notification_manager.cc |
| index 8afa5a1714d82d9df32ebe9b709082e145d3dd02..f65bcea9d07b361fd6e86418faac96b2ba0dc3e2 100644 |
| --- a/chrome/browser/notifications/message_center_notification_manager.cc |
| +++ b/chrome/browser/notifications/message_center_notification_manager.cc |
| @@ -10,23 +10,28 @@ |
| #include "base/prefs/pref_service.h" |
| #include "base/stl_util.h" |
| #include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/extensions/api/notification_provider/notification_provider_api.h" |
| #include "chrome/browser/notifications/desktop_notification_service.h" |
| #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| #include "chrome/browser/notifications/fullscreen_notification_blocker.h" |
| #include "chrome/browser/notifications/message_center_settings_controller.h" |
| #include "chrome/browser/notifications/notification.h" |
| +#include "chrome/browser/notifications/notification_conversion_helper.h" |
| #include "chrome/browser/notifications/screen_lock_notification_blocker.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/browser_finder.h" |
| #include "chrome/browser/ui/chrome_pages.h" |
| #include "chrome/browser/ui/host_desktop.h" |
| +#include "chrome/common/extensions/api/notification_provider.h" |
| #include "chrome/common/pref_names.h" |
| #include "content/public/browser/notification_service.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/common/url_constants.h" |
| +#include "extensions/browser/extension_registry.h" |
| #include "extensions/browser/extension_system.h" |
| #include "extensions/browser/info_map.h" |
| #include "extensions/common/extension_set.h" |
| +#include "extensions/common/permissions/permissions_data.h" |
| #include "ui/gfx/image/image_skia.h" |
| #include "ui/message_center/message_center_style.h" |
| #include "ui/message_center/message_center_tray.h" |
| @@ -117,11 +122,29 @@ void MessageCenterNotificationManager::Add(const Notification& notification, |
| DesktopNotificationServiceFactory::GetForProfile(profile)-> |
| ShowWelcomeNotificationIfNecessary(notification); |
| - // WARNING: You MUST use AddProfileNotification or update the message center |
| - // via the notification within a ProfileNotification object or the profile ID |
| - // will not be correctly set for ChromeOS. |
| - AddProfileNotification( |
| + // WARNING: You MUST update the message center via the notification within a |
| + // ProfileNotification object or the profile ID will not be correctly set for |
| + // ChromeOS. |
| + ProfileNotification* profile_notification( |
| new ProfileNotification(profile, notification, message_center_)); |
| + AddProfileNotification(profile_notification); |
| + |
| + // TODO(liyanhou): Change the logic to only send notifications to one party. |
| + // Currently, if there is an app with notificationProvider permission, |
| + // notifications will go to both message center and the app. |
| + // Change it to send notifications to message center only when the user chose |
| + // default message center (extension_id.empty()). |
| + |
| + // If there exist apps/extensions that have notificationProvider permission, |
| + // route notifications to one of the apps/extensions. |
| + std::string extension_id = GetExtensionTakingOverNotifications(profile); |
| + if (!extension_id.empty()) { |
|
dewittj
2014/08/13 18:30:17
nit: no braces for 1-line if statement
liyanhou
2014/08/13 20:28:56
Done.
|
| + profile_notification->AddToAlternateProvider(extension_id); |
| + } |
| + |
| + message_center_->AddNotification(make_scoped_ptr( |
| + new message_center::Notification(profile_notification->notification()))); |
| + profile_notification->StartDownloads(); |
| } |
| bool MessageCenterNotificationManager::Update(const Notification& notification, |
| @@ -155,6 +178,9 @@ bool MessageCenterNotificationManager::Update(const Notification& notification, |
| new ProfileNotification(profile, notification, message_center_); |
| profile_notifications_[notification.delegate_id()] = new_notification; |
| + // TODO(liyanhou): Add routing updated notifications to alternative |
| + // providers. |
| + |
| // WARNING: You MUST use AddProfileNotification or update the message |
| // center via the notification within a ProfileNotification object or the |
| // profile ID will not be correctly set for ChromeOS. |
| @@ -477,23 +503,33 @@ std::string |
| return std::string(); |
| } |
| +void |
| +MessageCenterNotificationManager::ProfileNotification::AddToAlternateProvider( |
| + std::string extension_id) { |
|
dewittj
2014/08/13 18:30:17
nit: const ref
liyanhou
2014/08/13 20:28:56
Done.
|
| + // Convert data from Notification type to NotificationOptions type. |
| + scoped_ptr<extensions::api::notifications::NotificationOptions> options( |
|
dewittj
2014/08/13 18:30:17
nit: just stack allocate this.
extensions::api::n
liyanhou
2014/08/13 20:28:56
Done.
|
| + new extensions::api::notifications::NotificationOptions()); |
| + NotificationConversionHelper::NotificationToNotificationOptions( |
| + notification_, options.get()); |
| + |
| + // Send the notification to the alternate provider extension/app. |
| + scoped_ptr<extensions::NotificationProviderEventRouter> event_router( |
| + new extensions::NotificationProviderEventRouter(profile_)); |
|
dewittj
2014/08/13 18:30:17
same, stack allocate this too
liyanhou
2014/08/13 20:28:57
Done.
|
| + event_router->CreateNotification(extension_id, |
| + notification_.notifier_id().id, |
| + notification_.delegate_id(), |
| + *options); |
| +} |
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // private |
| void MessageCenterNotificationManager::AddProfileNotification( |
| ProfileNotification* profile_notification) { |
| - const Notification& notification = profile_notification->notification(); |
| - std::string id = notification.delegate_id(); |
| + std::string id = profile_notification->notification().delegate_id(); |
| // Notification ids should be unique. |
| DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); |
| profile_notifications_[id] = profile_notification; |
| - |
| - // Create the copy for message center, and ensure the extension ID is correct. |
| - scoped_ptr<message_center::Notification> message_center_notification( |
| - new message_center::Notification(notification)); |
| - message_center_->AddNotification(message_center_notification.Pass()); |
| - |
| - profile_notification->StartDownloads(); |
| } |
| void MessageCenterNotificationManager::RemoveProfileNotification( |
| @@ -512,3 +548,23 @@ MessageCenterNotificationManager::ProfileNotification* |
| return (*iter).second; |
| } |
| + |
| +std::string |
| +MessageCenterNotificationManager::GetExtensionTakingOverNotifications( |
| + Profile* profile) { |
| + // TODO(liyanhou): When additional settings in Chrome Settings is implemented, |
| + // change choosing the last app with permission to a user selected app. |
| + extensions::ExtensionRegistry* registry = |
| + extensions::ExtensionRegistry::Get(profile); |
| + DCHECK(registry); |
| + std::string extension_id; |
| + for (extensions::ExtensionSet::const_iterator it = |
|
dewittj
2014/08/13 18:30:17
nit: rewrite this loop condition so that it fits o
liyanhou
2014/08/13 20:28:57
Done.
|
| + registry->enabled_extensions().begin(); |
| + it != registry->enabled_extensions().end(); |
| + ++it) { |
| + if ((*it->get()).permissions_data()->HasAPIPermission( |
| + extensions::APIPermission::ID::kNotificationProvider)) |
| + extension_id = (*it->get()).id(); |
|
dewittj
2014/08/13 18:30:17
nit: return from here so you don't always iterate
liyanhou
2014/08/13 20:28:57
Done.
|
| + } |
| + return extension_id; |
| +} |