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