Chromium Code Reviews| 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" | 7 #include "base/guid.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/desktop_notification_delegate.h" | 10 #include "content/public/browser/desktop_notification_delegate.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 LayoutTestNotificationManager::LayoutTestNotificationManager() | 28 LayoutTestNotificationManager::LayoutTestNotificationManager() |
| 29 : weak_factory_(this) {} | 29 : weak_factory_(this) {} |
| 30 | 30 |
| 31 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} | 31 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} |
| 32 | 32 |
| 33 void LayoutTestNotificationManager::DisplayNotification( | 33 void LayoutTestNotificationManager::DisplayNotification( |
| 34 BrowserContext* browser_context, | 34 BrowserContext* browser_context, |
| 35 const std::string& notification_id, | |
| 35 const GURL& origin, | 36 const GURL& origin, |
| 36 const PlatformNotificationData& notification_data, | 37 const PlatformNotificationData& notification_data, |
| 37 const NotificationResources& notification_resources, | 38 const NotificationResources& notification_resources, |
| 38 std::unique_ptr<DesktopNotificationDelegate> delegate, | 39 std::unique_ptr<DesktopNotificationDelegate> delegate, |
| 39 base::Closure* cancel_callback) { | 40 base::Closure* cancel_callback) { |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 41 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 41 std::string title = base::UTF16ToUTF8(notification_data.title); | 42 DCHECK(cancel_callback); |
| 42 | 43 |
| 43 DCHECK(cancel_callback); | |
| 44 *cancel_callback = base::Bind(&LayoutTestNotificationManager::Close, | 44 *cancel_callback = base::Bind(&LayoutTestNotificationManager::Close, |
| 45 weak_factory_.GetWeakPtr(), | 45 weak_factory_.GetWeakPtr(), notification_id); |
| 46 title); | |
| 47 | 46 |
| 48 ReplaceNotificationIfNeeded(notification_data); | 47 ReplaceNotificationIfNeeded(notification_id); |
| 49 | 48 |
| 50 page_notifications_[title] = delegate.release(); | 49 non_persistent_notifications_[notification_id] = delegate.release(); |
| 51 page_notifications_[title]->NotificationDisplayed(); | 50 non_persistent_notifications_[notification_id]->NotificationDisplayed(); |
| 51 | |
| 52 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] = | |
| 53 notification_id; | |
| 52 } | 54 } |
| 53 | 55 |
| 54 void LayoutTestNotificationManager::DisplayPersistentNotification( | 56 void LayoutTestNotificationManager::DisplayPersistentNotification( |
| 55 BrowserContext* browser_context, | 57 BrowserContext* browser_context, |
| 56 int64_t persistent_notification_id, | 58 const std::string& notification_id, |
| 57 const GURL& service_worker_scope, | 59 const GURL& service_worker_scope, |
| 58 const GURL& origin, | 60 const GURL& origin, |
| 59 const PlatformNotificationData& notification_data, | 61 const PlatformNotificationData& notification_data, |
| 60 const NotificationResources& notification_resources) { | 62 const NotificationResources& notification_resources) { |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 62 std::string title = base::UTF16ToUTF8(notification_data.title); | |
| 63 | 64 |
| 64 ReplaceNotificationIfNeeded(notification_data); | 65 ReplaceNotificationIfNeeded(notification_id); |
| 65 | 66 |
| 66 PersistentNotification notification; | 67 PersistentNotification notification; |
| 67 notification.browser_context = browser_context; | 68 notification.browser_context = browser_context; |
| 68 notification.origin = origin; | 69 notification.origin = origin; |
| 69 notification.persistent_id = persistent_notification_id; | |
| 70 | 70 |
| 71 persistent_notifications_[title] = notification; | 71 persistent_notifications_[notification_id] = notification; |
| 72 | |
| 73 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] = | |
| 74 notification_id; | |
| 72 } | 75 } |
| 73 | 76 |
| 74 void LayoutTestNotificationManager::ClosePersistentNotification( | 77 void LayoutTestNotificationManager::ClosePersistentNotification( |
| 75 BrowserContext* browser_context, | 78 BrowserContext* browser_context, |
| 76 int64_t persistent_notification_id) { | 79 const std::string& notification_id) { |
| 77 for (const auto& iter : persistent_notifications_) { | 80 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 78 if (iter.second.persistent_id != persistent_notification_id) | |
| 79 continue; | |
| 80 | 81 |
| 81 persistent_notifications_.erase(iter.first); | 82 const auto persistent_iter = persistent_notifications_.find(notification_id); |
|
johnme
2016/09/02 15:07:44
Just do: persistent_notifications_.erase(notificat
Peter Beverloo
2016/09/05 15:11:01
Done.
| |
| 82 return; | 83 if (persistent_iter != persistent_notifications_.end()) |
| 83 } | 84 persistent_notifications_.erase(persistent_iter); |
| 84 } | 85 } |
| 85 | 86 |
| 86 bool LayoutTestNotificationManager::GetDisplayedPersistentNotifications( | 87 bool LayoutTestNotificationManager::GetDisplayedPersistentNotifications( |
| 87 BrowserContext* browser_context, | 88 BrowserContext* browser_context, |
| 88 std::set<std::string>* displayed_notifications) { | 89 std::set<std::string>* displayed_notifications) { |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 89 DCHECK(displayed_notifications); | 91 DCHECK(displayed_notifications); |
| 90 | 92 |
| 91 // Notifications will never outlive the lifetime of running layout tests. | 93 // Notifications will never outlive the lifetime of running layout tests. |
| 92 return false; | 94 return false; |
| 93 } | 95 } |
| 94 | 96 |
| 95 void LayoutTestNotificationManager::SimulateClick(const std::string& title, | 97 void LayoutTestNotificationManager::SimulateClick(const std::string& title, |
| 96 int action_index) { | 98 int action_index) { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 99 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 98 | 100 |
| 99 // First check for page-notifications with the given title. | 101 const auto notification_id_iter = notification_id_map_.find(title); |
| 100 const auto& page_iterator = page_notifications_.find(title); | 102 if (notification_id_iter == notification_id_map_.end()) |
| 101 if (page_iterator != page_notifications_.end()) { | 103 return; |
| 104 | |
| 105 const std::string& notification_id = notification_id_iter->second; | |
| 106 | |
| 107 const auto persistent_iter = persistent_notifications_.find(notification_id); | |
| 108 const auto non_persistent_iter = | |
| 109 non_persistent_notifications_.find(notification_id); | |
| 110 | |
| 111 if (persistent_iter != persistent_notifications_.end()) { | |
| 112 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); | |
| 113 | |
| 114 const PersistentNotification& notification = persistent_iter->second; | |
| 115 content::NotificationEventDispatcher::GetInstance() | |
| 116 ->DispatchNotificationClickEvent( | |
| 117 notification.browser_context, notification_id, notification.origin, | |
| 118 action_index, base::Bind(&OnEventDispatchComplete)); | |
| 119 } else if (non_persistent_iter != non_persistent_notifications_.end()) { | |
| 102 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " | 120 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " |
| 103 "persistent notifications"; | 121 "persistent notifications"; |
| 104 page_iterator->second->NotificationClick(); | 122 |
| 105 return; | 123 non_persistent_iter->second->NotificationClick(); |
| 106 } | 124 } |
| 107 | |
| 108 // Then check for persistent notifications with the given title. | |
| 109 const auto& persistent_iterator = persistent_notifications_.find(title); | |
| 110 if (persistent_iterator == persistent_notifications_.end()) | |
| 111 return; | |
| 112 | |
| 113 const PersistentNotification& notification = persistent_iterator->second; | |
| 114 content::NotificationEventDispatcher::GetInstance() | |
| 115 ->DispatchNotificationClickEvent( | |
| 116 notification.browser_context, | |
| 117 notification.persistent_id, | |
| 118 notification.origin, | |
| 119 action_index, | |
| 120 base::Bind(&OnEventDispatchComplete)); | |
| 121 } | 125 } |
| 122 | 126 |
| 123 void LayoutTestNotificationManager::SimulateClose(const std::string& title, | 127 void LayoutTestNotificationManager::SimulateClose(const std::string& title, |
| 124 bool by_user) { | 128 bool by_user) { |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 126 | 130 |
| 127 const auto& persistent_iterator = persistent_notifications_.find(title); | 131 const auto notification_id_iter = notification_id_map_.find(title); |
| 128 if (persistent_iterator == persistent_notifications_.end()) | 132 if (notification_id_iter == notification_id_map_.end()) |
| 129 return; | 133 return; |
| 130 | 134 |
| 131 const PersistentNotification& notification = persistent_iterator->second; | 135 const std::string& notification_id = notification_id_iter->second; |
| 136 | |
| 137 const auto& persistent_iter = persistent_notifications_.find(notification_id); | |
| 138 if (persistent_iter == persistent_notifications_.end()) | |
| 139 return; | |
| 140 | |
| 141 const PersistentNotification& notification = persistent_iter->second; | |
| 132 content::NotificationEventDispatcher::GetInstance() | 142 content::NotificationEventDispatcher::GetInstance() |
| 133 ->DispatchNotificationCloseEvent( | 143 ->DispatchNotificationCloseEvent( |
| 134 notification.browser_context, | 144 notification.browser_context, notification_id, notification.origin, |
| 135 notification.persistent_id, | 145 by_user, base::Bind(&OnEventDispatchComplete)); |
| 136 notification.origin, | |
| 137 by_user, | |
| 138 base::Bind(&OnEventDispatchComplete)); | |
| 139 } | 146 } |
| 140 | 147 |
| 141 blink::mojom::PermissionStatus | 148 blink::mojom::PermissionStatus |
| 142 LayoutTestNotificationManager::CheckPermissionOnUIThread( | 149 LayoutTestNotificationManager::CheckPermissionOnUIThread( |
| 143 BrowserContext* browser_context, | 150 BrowserContext* browser_context, |
| 144 const GURL& origin, | 151 const GURL& origin, |
| 145 int render_process_id) { | 152 int render_process_id) { |
| 146 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 147 return CheckPermission(origin); | 154 return CheckPermission(origin); |
| 148 } | 155 } |
| 149 | 156 |
| 150 blink::mojom::PermissionStatus | 157 blink::mojom::PermissionStatus |
| 151 LayoutTestNotificationManager::CheckPermissionOnIOThread( | 158 LayoutTestNotificationManager::CheckPermissionOnIOThread( |
| 152 ResourceContext* resource_context, | 159 ResourceContext* resource_context, |
| 153 const GURL& origin, | 160 const GURL& origin, |
| 154 int render_process_id) { | 161 int render_process_id) { |
| 155 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 156 return CheckPermission(origin); | 163 return CheckPermission(origin); |
| 157 } | 164 } |
| 158 | 165 |
| 159 void LayoutTestNotificationManager::Close(const std::string& title) { | 166 void LayoutTestNotificationManager::Close(const std::string& notification_id) { |
| 160 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 167 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 161 auto iterator = page_notifications_.find(title); | 168 auto iterator = non_persistent_notifications_.find(notification_id); |
| 162 if (iterator == page_notifications_.end()) | 169 if (iterator == non_persistent_notifications_.end()) |
| 163 return; | 170 return; |
| 164 | 171 |
| 165 iterator->second->NotificationClosed(); | 172 iterator->second->NotificationClosed(); |
| 166 } | 173 } |
| 167 | 174 |
| 168 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( | 175 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( |
| 169 const PlatformNotificationData& notification_data) { | 176 const std::string& notification_id) { |
| 170 if (!notification_data.tag.length()) | 177 const auto persistent_iter = persistent_notifications_.find(notification_id); |
| 171 return; | 178 const auto non_persistent_iter = |
| 179 non_persistent_notifications_.find(notification_id); | |
| 172 | 180 |
| 173 std::string tag = notification_data.tag; | 181 if (persistent_iter != persistent_notifications_.end()) { |
| 174 const auto& replace_iter = replacements_.find(tag); | 182 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); |
| 175 if (replace_iter != replacements_.end()) { | 183 persistent_notifications_.erase(persistent_iter); |
| 176 const std::string& previous_title = replace_iter->second; | 184 } else if (non_persistent_iter != non_persistent_notifications_.end()) { |
| 185 DesktopNotificationDelegate* delegate = non_persistent_iter->second; | |
| 186 delegate->NotificationClosed(); | |
| 177 | 187 |
| 178 const auto& page_notification_iter = | 188 non_persistent_notifications_.erase(non_persistent_iter); |
| 179 page_notifications_.find(previous_title); | 189 delete delegate; |
|
johnme
2016/09/02 15:07:44
I realise these were being deleted before, but I'm
Peter Beverloo
2016/09/05 15:11:01
Done.
| |
| 180 if (page_notification_iter != page_notifications_.end()) { | |
| 181 DesktopNotificationDelegate* previous_delegate = | |
| 182 page_notification_iter->second; | |
| 183 | |
| 184 previous_delegate->NotificationClosed(); | |
| 185 | |
| 186 page_notifications_.erase(page_notification_iter); | |
| 187 delete previous_delegate; | |
| 188 } | |
| 189 | |
| 190 const auto& persistent_notification_iter = | |
| 191 persistent_notifications_.find(previous_title); | |
| 192 if (persistent_notification_iter != persistent_notifications_.end()) | |
| 193 persistent_notifications_.erase(persistent_notification_iter); | |
| 194 } | 190 } |
| 195 | |
| 196 replacements_[tag] = base::UTF16ToUTF8(notification_data.title); | |
| 197 } | 191 } |
| 198 | 192 |
| 199 blink::mojom::PermissionStatus | 193 blink::mojom::PermissionStatus |
| 200 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { | 194 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { |
| 201 return LayoutTestContentBrowserClient::Get() | 195 return LayoutTestContentBrowserClient::Get() |
| 202 ->GetLayoutTestBrowserContext() | 196 ->GetLayoutTestBrowserContext() |
| 203 ->GetLayoutTestPermissionManager() | 197 ->GetLayoutTestPermissionManager() |
| 204 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, | 198 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, |
| 205 origin, | 199 origin, |
| 206 origin); | 200 origin); |
| 207 } | 201 } |
| 208 | 202 |
| 209 } // namespace content | 203 } // namespace content |
| OLD | NEW |