Chromium Code Reviews| Index: chrome/browser/notifications/native_notification_display_service.cc |
| diff --git a/chrome/browser/notifications/native_notification_display_service.cc b/chrome/browser/notifications/native_notification_display_service.cc |
| index 833fb970cd20a0cf3efb8710f50691c305674417..09b823fdf7aec0f8df151b4dea6f8eee81ddf20c 100644 |
| --- a/chrome/browser/notifications/native_notification_display_service.cc |
| +++ b/chrome/browser/notifications/native_notification_display_service.cc |
| @@ -7,6 +7,8 @@ |
| #include "base/memory/ptr_util.h" |
| #include "base/strings/nullable_string16.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/notifications/message_center_display_service.h" |
| #include "chrome/browser/notifications/non_persistent_notification_handler.h" |
| #include "chrome/browser/notifications/notification.h" |
| #include "chrome/browser/notifications/notification_delegate.h" |
| @@ -14,6 +16,7 @@ |
| #include "chrome/browser/notifications/notification_platform_bridge.h" |
| #include "chrome/browser/notifications/persistent_notification_handler.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/features.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "extensions/features/features.h" |
| @@ -21,6 +24,10 @@ |
| #include "chrome/browser/extensions/api/notifications/extension_notification_handler.h" |
| #endif |
| +#if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) && defined(OS_LINUX) |
| +#include "chrome/browser/notifications/notification_platform_bridge_linux.h" |
| +#endif |
| + |
| namespace { |
| std::string GetProfileId(Profile* profile) { |
| @@ -38,10 +45,24 @@ std::string GetProfileId(Profile* profile) { |
| NativeNotificationDisplayService::NativeNotificationDisplayService( |
| Profile* profile, |
| NotificationPlatformBridge* notification_bridge) |
| - : profile_(profile), notification_bridge_(notification_bridge) { |
| + : profile_(profile), |
| + notification_bridge_(notification_bridge), |
| + weak_factory_(this) { |
| DCHECK(profile_); |
| DCHECK(notification_bridge_); |
| +#if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) && defined(OS_LINUX) |
| + auto* platform_bridge_linux = static_cast<NotificationPlatformBridgeLinux*>( |
| + g_browser_process->notification_platform_bridge()); |
| + platform_bridge_linux->IsConnected( |
| + base::BindOnce(&NativeNotificationDisplayService:: |
| + OnNotificationPlatformBridgeInitialized, |
| + weak_factory_.GetWeakPtr())); |
| + notification_bridge_connected_ = false; |
| +#else |
| + notification_bridge_connected_ = true; |
| +#endif |
| + |
| AddNotificationHandler(NotificationCommon::NON_PERSISTENT, |
| base::MakeUnique<NonPersistentNotificationHandler>()); |
| AddNotificationHandler(NotificationCommon::PERSISTENT, |
| @@ -54,35 +75,75 @@ NativeNotificationDisplayService::NativeNotificationDisplayService( |
| NativeNotificationDisplayService::~NativeNotificationDisplayService() {} |
| +void NativeNotificationDisplayService::OnNotificationPlatformBridgeInitialized( |
| + bool success) { |
| + if (success) { |
| + notification_bridge_connected_ = true; |
| + } else { |
| + message_center_display_service_ = |
| + base::MakeUnique<MessageCenterDisplayService>( |
| + profile_, g_browser_process->notification_ui_manager()); |
| + } |
| + |
| + while (!actions_.empty()) { |
| + actions_.front().Run(); |
| + actions_.pop(); |
| + } |
| +} |
| + |
| void NativeNotificationDisplayService::Display( |
| NotificationCommon::Type notification_type, |
| const std::string& notification_id, |
| const Notification& notification) { |
| - notification_bridge_->Display(notification_type, notification_id, |
| - GetProfileId(profile_), |
| - profile_->IsOffTheRecord(), notification); |
| - notification.delegate()->Display(); |
| - NotificationHandler* handler = GetNotificationHandler(notification_type); |
| - handler->RegisterNotification(notification_id, notification.delegate()); |
| + if (notification_bridge_connected_) { |
| + notification_bridge_->Display(notification_type, notification_id, |
| + GetProfileId(profile_), |
| + profile_->IsOffTheRecord(), notification); |
| + notification.delegate()->Display(); |
| + NotificationHandler* handler = GetNotificationHandler(notification_type); |
| + handler->RegisterNotification(notification_id, notification.delegate()); |
| + } else if (message_center_display_service_) { |
| + message_center_display_service_->Display(notification_type, notification_id, |
| + notification); |
| + } else { |
| + actions_.push(base::Bind(&NativeNotificationDisplayService::Display, |
| + base::Unretained(this), notification_type, |
|
Lei Zhang
2017/04/21 22:45:17
Since you already have a WeakPtrFactory, just use
Tom (Use chromium acct)
2017/04/24 20:46:14
Done.
|
| + notification_id, notification)); |
| + } |
| } |
| void NativeNotificationDisplayService::Close( |
| NotificationCommon::Type notification_type, |
| const std::string& notification_id) { |
| - NotificationHandler* handler = GetNotificationHandler(notification_type); |
| - notification_bridge_->Close(GetProfileId(profile_), notification_id); |
| + if (notification_bridge_connected_) { |
| + NotificationHandler* handler = GetNotificationHandler(notification_type); |
| + notification_bridge_->Close(GetProfileId(profile_), notification_id); |
| - // TODO(miguelg): Figure out something better here, passing an empty |
| - // origin works because only non persistent notifications care about |
| - // this method for JS generated close calls and they don't require |
| - // the origin. |
| - handler->OnClose(profile_, "", notification_id, false /* by user */); |
| + // TODO(miguelg): Figure out something better here, passing an empty |
| + // origin works because only non persistent notifications care about |
| + // this method for JS generated close calls and they don't require |
| + // the origin. |
| + handler->OnClose(profile_, "", notification_id, false /* by user */); |
| + } else if (message_center_display_service_) { |
| + message_center_display_service_->Close(notification_type, notification_id); |
| + } else { |
| + actions_.push(base::Bind(&NativeNotificationDisplayService::Close, |
| + base::Unretained(this), notification_type, |
| + notification_id)); |
| + } |
| } |
| void NativeNotificationDisplayService::GetDisplayed( |
| const DisplayedNotificationsCallback& callback) { |
| - return notification_bridge_->GetDisplayed( |
| - GetProfileId(profile_), profile_->IsOffTheRecord(), callback); |
| + if (notification_bridge_connected_) { |
| + return notification_bridge_->GetDisplayed( |
| + GetProfileId(profile_), profile_->IsOffTheRecord(), callback); |
| + } else if (message_center_display_service_) { |
| + message_center_display_service_->GetDisplayed(callback); |
| + } else { |
| + actions_.push(base::Bind(&NativeNotificationDisplayService::GetDisplayed, |
| + base::Unretained(this), callback)); |
| + } |
| } |
| void NativeNotificationDisplayService::ProcessNotificationOperation( |