Index: content/shell/browser/layout_test/layout_test_notification_manager.cc |
diff --git a/content/shell/browser/layout_test/layout_test_notification_manager.cc b/content/shell/browser/layout_test/layout_test_notification_manager.cc |
index ce4e86b931e07409d8d492b5fd59360dea87b09d..67330eb02979d99038610d59e2ceaf5a09bcc8c9 100644 |
--- a/content/shell/browser/layout_test/layout_test_notification_manager.cc |
+++ b/content/shell/browser/layout_test/layout_test_notification_manager.cc |
@@ -4,16 +4,30 @@ |
#include "content/shell/browser/layout_test/layout_test_notification_manager.h" |
+#include "base/guid.h" |
#include "base/strings/utf_string_conversions.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/desktop_notification_delegate.h" |
+#include "content/public/browser/notification_event_dispatcher.h" |
+#include "content/public/common/persistent_notification_status.h" |
#include "content/public/common/show_desktop_notification_params.h" |
namespace content { |
+namespace { |
+ |
+// The Web Notification layout tests don't care about the lifetime of the |
+// Service Worker when a notificationclick event has been dispatched. |
+void OnEventDispatchComplete(PersistentNotificationStatus status) {} |
+ |
+} // namespace |
LayoutTestNotificationManager::LayoutTestNotificationManager() |
: weak_factory_(this) {} |
+LayoutTestNotificationManager::PersistentNotification::PersistentNotification() |
+ : browser_context(nullptr), |
+ service_worker_registration_id(0) {} |
+ |
LayoutTestNotificationManager::~LayoutTestNotificationManager() {} |
blink::WebNotificationPermission |
@@ -22,8 +36,7 @@ LayoutTestNotificationManager::CheckPermission( |
const GURL& origin, |
int render_process_id) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- NotificationPermissionMap::iterator iter = |
- permission_map_.find(origin); |
+ const auto& iter = permission_map_.find(origin); |
if (iter == permission_map_.end()) |
return blink::WebNotificationPermissionDefault; |
@@ -61,27 +74,10 @@ void LayoutTestNotificationManager::DisplayNotification( |
weak_factory_.GetWeakPtr(), |
title); |
- if (params.replace_id.length()) { |
- std::string replace_id = base::UTF16ToUTF8(params.replace_id); |
- auto replace_iter = replacements_.find(replace_id); |
- if (replace_iter != replacements_.end()) { |
- const std::string& previous_title = replace_iter->second; |
- auto notification_iter = notifications_.find(previous_title); |
- if (notification_iter != notifications_.end()) { |
- DesktopNotificationDelegate* previous_delegate = |
- notification_iter->second; |
- previous_delegate->NotificationClosed(false); |
- |
- notifications_.erase(notification_iter); |
- delete previous_delegate; |
- } |
- } |
- |
- replacements_[replace_id] = title; |
- } |
+ ReplaceNotificationIfNeeded(params); |
- notifications_[title] = delegate.release(); |
- notifications_[title]->NotificationDisplayed(); |
+ page_notifications_[title] = delegate.release(); |
+ page_notifications_[title]->NotificationDisplayed(); |
} |
void LayoutTestNotificationManager::DisplayPersistentNotification( |
@@ -89,33 +85,96 @@ void LayoutTestNotificationManager::DisplayPersistentNotification( |
int64 service_worker_registration_id, |
const ShowDesktopNotificationHostMsgParams& params, |
int render_process_id) { |
- // TODO(peter): Make persistent notifications layout testable. |
- NOTIMPLEMENTED(); |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ std::string title = base::UTF16ToUTF8(params.title); |
+ |
+ ReplaceNotificationIfNeeded(params); |
+ |
+ PersistentNotification notification; |
+ notification.browser_context = browser_context; |
+ notification.notification_data = params; |
+ notification.service_worker_registration_id = service_worker_registration_id; |
+ notification.persistent_id = base::GenerateGUID(); |
+ |
+ persistent_notifications_[title] = notification; |
} |
void LayoutTestNotificationManager::ClosePersistentNotification( |
BrowserContext* browser_context, |
const std::string& persistent_notification_id) { |
- // TODO(peter): Make persistent notifications layout testable. |
- NOTIMPLEMENTED(); |
+ for (const auto& iter : persistent_notifications_) { |
+ if (iter.second.persistent_id != persistent_notification_id) |
+ continue; |
+ |
+ persistent_notifications_.erase(iter.first); |
+ return; |
+ } |
} |
void LayoutTestNotificationManager::SimulateClick(const std::string& title) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- auto iterator = notifications_.find(title); |
- if (iterator == notifications_.end()) |
+ |
+ // First check for page-notifications with the given title. |
+ const auto& page_iterator = page_notifications_.find(title); |
+ if (page_iterator != page_notifications_.end()) { |
+ page_iterator->second->NotificationClick(); |
+ return; |
+ } |
+ |
+ // Then check for persistent notifications with the given title. |
+ const auto& persistent_iterator = persistent_notifications_.find(title); |
+ if (persistent_iterator == persistent_notifications_.end()) |
return; |
- iterator->second->NotificationClick(); |
+ const PersistentNotification& notification = persistent_iterator->second; |
+ content::NotificationEventDispatcher::GetInstance() |
+ ->DispatchNotificationClickEvent( |
+ notification.browser_context, |
+ notification.notification_data.origin, |
+ notification.service_worker_registration_id, |
+ notification.persistent_id, |
+ notification.notification_data, |
+ base::Bind(&OnEventDispatchComplete)); |
} |
void LayoutTestNotificationManager::Close(const std::string& title) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- auto iterator = notifications_.find(title); |
- if (iterator == notifications_.end()) |
+ auto iterator = page_notifications_.find(title); |
+ if (iterator == page_notifications_.end()) |
return; |
iterator->second->NotificationClosed(false); |
} |
+void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( |
+ const ShowDesktopNotificationHostMsgParams& params) { |
+ if (!params.replace_id.length()) |
+ return; |
+ |
+ std::string replace_id = base::UTF16ToUTF8(params.replace_id); |
+ const auto& replace_iter = replacements_.find(replace_id); |
+ if (replace_iter != replacements_.end()) { |
+ const std::string& previous_title = replace_iter->second; |
+ |
+ const auto& page_notification_iter = |
+ page_notifications_.find(previous_title); |
+ if (page_notification_iter != page_notifications_.end()) { |
+ DesktopNotificationDelegate* previous_delegate = |
+ page_notification_iter->second; |
+ |
+ previous_delegate->NotificationClosed(false); |
+ |
+ page_notifications_.erase(page_notification_iter); |
+ delete previous_delegate; |
+ } |
+ |
+ const auto& persistent_notification_iter = |
+ persistent_notifications_.find(previous_title); |
+ if (persistent_notification_iter != persistent_notifications_.end()) |
+ persistent_notifications_.erase(persistent_notification_iter); |
+ } |
+ |
+ replacements_[replace_id] = base::UTF16ToUTF8(params.title); |
+} |
+ |
} // namespace content |