| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/shell/browser/layout_test/layout_test_notification_manager.h" | 5 #include "content/test/mock_platform_notification_service.h" |
| 6 | 6 |
| 7 #include "base/guid.h" | 7 #include "base/guid.h" |
| 8 #include "base/strings/nullable_string16.h" | 8 #include "base/strings/nullable_string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/desktop_notification_delegate.h" | 11 #include "content/public/browser/desktop_notification_delegate.h" |
| 12 #include "content/public/browser/notification_event_dispatcher.h" | 12 #include "content/public/browser/notification_event_dispatcher.h" |
| 13 #include "content/public/browser/permission_type.h" | 13 #include "content/public/browser/permission_type.h" |
| 14 #include "content/public/common/persistent_notification_status.h" | 14 #include "content/public/common/persistent_notification_status.h" |
| 15 #include "content/public/common/platform_notification_data.h" | 15 #include "content/public/common/platform_notification_data.h" |
| 16 #include "content/shell/browser/layout_test/layout_test_browser_context.h" | |
| 17 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h
" | |
| 18 #include "content/shell/browser/layout_test/layout_test_permission_manager.h" | |
| 19 | 16 |
| 20 namespace content { | 17 namespace content { |
| 21 namespace { | 18 namespace { |
| 22 | 19 |
| 23 // The Web Notification layout tests don't care about the lifetime of the | 20 // The Web Notification layout tests don't care about the lifetime of the |
| 24 // Service Worker when a notificationclick event has been dispatched. | 21 // Service Worker when a notificationclick event has been dispatched. |
| 25 void OnEventDispatchComplete(PersistentNotificationStatus status) {} | 22 void OnEventDispatchComplete(PersistentNotificationStatus status) {} |
| 26 | 23 |
| 27 } // namespace | 24 } // namespace |
| 28 | 25 |
| 29 LayoutTestNotificationManager::LayoutTestNotificationManager() | 26 MockPlatformNotificationService::MockPlatformNotificationService() |
| 30 : weak_factory_(this) {} | 27 : weak_factory_(this) {} |
| 31 | 28 |
| 32 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} | 29 MockPlatformNotificationService::~MockPlatformNotificationService() {} |
| 33 | 30 |
| 34 void LayoutTestNotificationManager::DisplayNotification( | 31 void MockPlatformNotificationService::DisplayNotification( |
| 35 BrowserContext* browser_context, | 32 BrowserContext* browser_context, |
| 36 const std::string& notification_id, | 33 const std::string& notification_id, |
| 37 const GURL& origin, | 34 const GURL& origin, |
| 38 const PlatformNotificationData& notification_data, | 35 const PlatformNotificationData& notification_data, |
| 39 const NotificationResources& notification_resources, | 36 const NotificationResources& notification_resources, |
| 40 std::unique_ptr<DesktopNotificationDelegate> delegate, | 37 std::unique_ptr<DesktopNotificationDelegate> delegate, |
| 41 base::Closure* cancel_callback) { | 38 base::Closure* cancel_callback) { |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 39 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 43 DCHECK(cancel_callback); | 40 DCHECK(cancel_callback); |
| 44 | 41 |
| 45 *cancel_callback = base::Bind(&LayoutTestNotificationManager::Close, | 42 *cancel_callback = base::Bind(&MockPlatformNotificationService::Close, |
| 46 weak_factory_.GetWeakPtr(), notification_id); | 43 weak_factory_.GetWeakPtr(), notification_id); |
| 47 | 44 |
| 48 ReplaceNotificationIfNeeded(notification_id); | 45 ReplaceNotificationIfNeeded(notification_id); |
| 49 | 46 |
| 50 non_persistent_notifications_[notification_id] = std::move(delegate); | 47 non_persistent_notifications_[notification_id] = std::move(delegate); |
| 51 non_persistent_notifications_[notification_id]->NotificationDisplayed(); | 48 non_persistent_notifications_[notification_id]->NotificationDisplayed(); |
| 52 | 49 |
| 53 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] = | 50 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] = |
| 54 notification_id; | 51 notification_id; |
| 55 } | 52 } |
| 56 | 53 |
| 57 void LayoutTestNotificationManager::DisplayPersistentNotification( | 54 void MockPlatformNotificationService::DisplayPersistentNotification( |
| 58 BrowserContext* browser_context, | 55 BrowserContext* browser_context, |
| 59 const std::string& notification_id, | 56 const std::string& notification_id, |
| 60 const GURL& service_worker_scope, | 57 const GURL& service_worker_scope, |
| 61 const GURL& origin, | 58 const GURL& origin, |
| 62 const PlatformNotificationData& notification_data, | 59 const PlatformNotificationData& notification_data, |
| 63 const NotificationResources& notification_resources) { | 60 const NotificationResources& notification_resources) { |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 65 | 62 |
| 66 ReplaceNotificationIfNeeded(notification_id); | 63 ReplaceNotificationIfNeeded(notification_id); |
| 67 | 64 |
| 68 PersistentNotification notification; | 65 PersistentNotification notification; |
| 69 notification.browser_context = browser_context; | 66 notification.browser_context = browser_context; |
| 70 notification.origin = origin; | 67 notification.origin = origin; |
| 71 | 68 |
| 72 persistent_notifications_[notification_id] = notification; | 69 persistent_notifications_[notification_id] = notification; |
| 73 | 70 |
| 74 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] = | 71 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] = |
| 75 notification_id; | 72 notification_id; |
| 76 } | 73 } |
| 77 | 74 |
| 78 void LayoutTestNotificationManager::ClosePersistentNotification( | 75 void MockPlatformNotificationService::ClosePersistentNotification( |
| 79 BrowserContext* browser_context, | 76 BrowserContext* browser_context, |
| 80 const std::string& notification_id) { | 77 const std::string& notification_id) { |
| 81 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 78 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 82 | 79 |
| 83 persistent_notifications_.erase(notification_id); | 80 persistent_notifications_.erase(notification_id); |
| 84 } | 81 } |
| 85 | 82 |
| 86 bool LayoutTestNotificationManager::GetDisplayedNotifications( | 83 bool MockPlatformNotificationService::GetDisplayedNotifications( |
| 87 BrowserContext* browser_context, | 84 BrowserContext* browser_context, |
| 88 std::set<std::string>* displayed_notifications) { | 85 std::set<std::string>* displayed_notifications) { |
| 89 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 86 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 90 DCHECK(displayed_notifications); | 87 DCHECK(displayed_notifications); |
| 91 | 88 |
| 92 // Notifications will never outlive the lifetime of running layout tests. | 89 for (const auto& kv : persistent_notifications_) |
| 93 return false; | 90 displayed_notifications->insert(kv.first); |
| 91 |
| 92 return true; |
| 94 } | 93 } |
| 95 | 94 |
| 96 void LayoutTestNotificationManager::SimulateClick( | 95 void MockPlatformNotificationService::SimulateClick( |
| 97 const std::string& title, | 96 const std::string& title, |
| 98 int action_index, | 97 int action_index, |
| 99 const base::NullableString16& reply) { | 98 const base::NullableString16& reply) { |
| 100 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 99 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 101 | 100 |
| 102 const auto notification_id_iter = notification_id_map_.find(title); | 101 const auto notification_id_iter = notification_id_map_.find(title); |
| 103 if (notification_id_iter == notification_id_map_.end()) | 102 if (notification_id_iter == notification_id_map_.end()) |
| 104 return; | 103 return; |
| 105 | 104 |
| 106 const std::string& notification_id = notification_id_iter->second; | 105 const std::string& notification_id = notification_id_iter->second; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 118 notification.browser_context, notification_id, notification.origin, | 117 notification.browser_context, notification_id, notification.origin, |
| 119 action_index, reply, base::Bind(&OnEventDispatchComplete)); | 118 action_index, reply, base::Bind(&OnEventDispatchComplete)); |
| 120 } else if (non_persistent_iter != non_persistent_notifications_.end()) { | 119 } else if (non_persistent_iter != non_persistent_notifications_.end()) { |
| 121 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " | 120 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " |
| 122 "persistent notifications"; | 121 "persistent notifications"; |
| 123 | 122 |
| 124 non_persistent_iter->second->NotificationClick(); | 123 non_persistent_iter->second->NotificationClick(); |
| 125 } | 124 } |
| 126 } | 125 } |
| 127 | 126 |
| 128 void LayoutTestNotificationManager::SimulateClose(const std::string& title, | 127 void MockPlatformNotificationService::SimulateClose(const std::string& title, |
| 129 bool by_user) { | 128 bool by_user) { |
| 130 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 131 | 130 |
| 132 const auto notification_id_iter = notification_id_map_.find(title); | 131 const auto notification_id_iter = notification_id_map_.find(title); |
| 133 if (notification_id_iter == notification_id_map_.end()) | 132 if (notification_id_iter == notification_id_map_.end()) |
| 134 return; | 133 return; |
| 135 | 134 |
| 136 const std::string& notification_id = notification_id_iter->second; | 135 const std::string& notification_id = notification_id_iter->second; |
| 137 | 136 |
| 138 const auto& persistent_iter = persistent_notifications_.find(notification_id); | 137 const auto& persistent_iter = persistent_notifications_.find(notification_id); |
| 139 if (persistent_iter == persistent_notifications_.end()) | 138 if (persistent_iter == persistent_notifications_.end()) |
| 140 return; | 139 return; |
| 141 | 140 |
| 142 const PersistentNotification& notification = persistent_iter->second; | 141 const PersistentNotification& notification = persistent_iter->second; |
| 143 content::NotificationEventDispatcher::GetInstance() | 142 content::NotificationEventDispatcher::GetInstance() |
| 144 ->DispatchNotificationCloseEvent( | 143 ->DispatchNotificationCloseEvent( |
| 145 notification.browser_context, notification_id, notification.origin, | 144 notification.browser_context, notification_id, notification.origin, |
| 146 by_user, base::Bind(&OnEventDispatchComplete)); | 145 by_user, base::Bind(&OnEventDispatchComplete)); |
| 147 } | 146 } |
| 148 | 147 |
| 149 blink::mojom::PermissionStatus | 148 blink::mojom::PermissionStatus |
| 150 LayoutTestNotificationManager::CheckPermissionOnUIThread( | 149 MockPlatformNotificationService::CheckPermissionOnUIThread( |
| 151 BrowserContext* browser_context, | 150 BrowserContext* browser_context, |
| 152 const GURL& origin, | 151 const GURL& origin, |
| 153 int render_process_id) { | 152 int render_process_id) { |
| 154 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 155 return CheckPermission(origin); | 154 return CheckPermission(origin); |
| 156 } | 155 } |
| 157 | 156 |
| 158 blink::mojom::PermissionStatus | 157 blink::mojom::PermissionStatus |
| 159 LayoutTestNotificationManager::CheckPermissionOnIOThread( | 158 MockPlatformNotificationService::CheckPermissionOnIOThread( |
| 160 ResourceContext* resource_context, | 159 ResourceContext* resource_context, |
| 161 const GURL& origin, | 160 const GURL& origin, |
| 162 int render_process_id) { | 161 int render_process_id) { |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 164 return CheckPermission(origin); | 163 return CheckPermission(origin); |
| 165 } | 164 } |
| 166 | 165 |
| 167 void LayoutTestNotificationManager::Close(const std::string& notification_id) { | 166 void MockPlatformNotificationService::Close( |
| 167 const std::string& notification_id) { |
| 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 169 auto iterator = non_persistent_notifications_.find(notification_id); | 169 auto iterator = non_persistent_notifications_.find(notification_id); |
| 170 if (iterator == non_persistent_notifications_.end()) | 170 if (iterator == non_persistent_notifications_.end()) |
| 171 return; | 171 return; |
| 172 | 172 |
| 173 iterator->second->NotificationClosed(); | 173 iterator->second->NotificationClosed(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( | 176 void MockPlatformNotificationService::ReplaceNotificationIfNeeded( |
| 177 const std::string& notification_id) { | 177 const std::string& notification_id) { |
| 178 const auto persistent_iter = persistent_notifications_.find(notification_id); | 178 const auto persistent_iter = persistent_notifications_.find(notification_id); |
| 179 const auto non_persistent_iter = | 179 const auto non_persistent_iter = |
| 180 non_persistent_notifications_.find(notification_id); | 180 non_persistent_notifications_.find(notification_id); |
| 181 | 181 |
| 182 if (persistent_iter != persistent_notifications_.end()) { | 182 if (persistent_iter != persistent_notifications_.end()) { |
| 183 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); | 183 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); |
| 184 persistent_notifications_.erase(persistent_iter); | 184 persistent_notifications_.erase(persistent_iter); |
| 185 } else if (non_persistent_iter != non_persistent_notifications_.end()) { | 185 } else if (non_persistent_iter != non_persistent_notifications_.end()) { |
| 186 non_persistent_iter->second->NotificationClosed(); | 186 non_persistent_iter->second->NotificationClosed(); |
| 187 non_persistent_notifications_.erase(non_persistent_iter); | 187 non_persistent_notifications_.erase(non_persistent_iter); |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 | 190 |
| 191 blink::mojom::PermissionStatus | 191 blink::mojom::PermissionStatus MockPlatformNotificationService::CheckPermission( |
| 192 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { | 192 const GURL& origin) { |
| 193 return LayoutTestContentBrowserClient::Get() | 193 return blink::mojom::PermissionStatus::GRANTED; |
| 194 ->GetLayoutTestBrowserContext() | |
| 195 ->GetLayoutTestPermissionManager() | |
| 196 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, | |
| 197 origin, | |
| 198 origin); | |
| 199 } | 194 } |
| 200 | 195 |
| 201 } // namespace content | 196 } // namespace content |
| OLD | NEW |