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] = std::move(delegate); |
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 persistent_notifications_.erase(notification_id); |
82 return; | |
83 } | |
84 } | 83 } |
85 | 84 |
86 bool LayoutTestNotificationManager::GetDisplayedPersistentNotifications( | 85 bool LayoutTestNotificationManager::GetDisplayedPersistentNotifications( |
87 BrowserContext* browser_context, | 86 BrowserContext* browser_context, |
88 std::set<std::string>* displayed_notifications) { | 87 std::set<std::string>* displayed_notifications) { |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
89 DCHECK(displayed_notifications); | 89 DCHECK(displayed_notifications); |
90 | 90 |
91 // Notifications will never outlive the lifetime of running layout tests. | 91 // Notifications will never outlive the lifetime of running layout tests. |
92 return false; | 92 return false; |
93 } | 93 } |
94 | 94 |
95 void LayoutTestNotificationManager::SimulateClick(const std::string& title, | 95 void LayoutTestNotificationManager::SimulateClick(const std::string& title, |
96 int action_index) { | 96 int action_index) { |
97 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
98 | 98 |
99 // First check for page-notifications with the given title. | 99 const auto notification_id_iter = notification_id_map_.find(title); |
100 const auto& page_iterator = page_notifications_.find(title); | 100 if (notification_id_iter == notification_id_map_.end()) |
101 if (page_iterator != page_notifications_.end()) { | 101 return; |
| 102 |
| 103 const std::string& notification_id = notification_id_iter->second; |
| 104 |
| 105 const auto persistent_iter = persistent_notifications_.find(notification_id); |
| 106 const auto non_persistent_iter = |
| 107 non_persistent_notifications_.find(notification_id); |
| 108 |
| 109 if (persistent_iter != persistent_notifications_.end()) { |
| 110 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); |
| 111 |
| 112 const PersistentNotification& notification = persistent_iter->second; |
| 113 content::NotificationEventDispatcher::GetInstance() |
| 114 ->DispatchNotificationClickEvent( |
| 115 notification.browser_context, notification_id, notification.origin, |
| 116 action_index, base::Bind(&OnEventDispatchComplete)); |
| 117 } else if (non_persistent_iter != non_persistent_notifications_.end()) { |
102 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " | 118 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " |
103 "persistent notifications"; | 119 "persistent notifications"; |
104 page_iterator->second->NotificationClick(); | 120 |
105 return; | 121 non_persistent_iter->second->NotificationClick(); |
106 } | 122 } |
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 } | 123 } |
122 | 124 |
123 void LayoutTestNotificationManager::SimulateClose(const std::string& title, | 125 void LayoutTestNotificationManager::SimulateClose(const std::string& title, |
124 bool by_user) { | 126 bool by_user) { |
125 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
126 | 128 |
127 const auto& persistent_iterator = persistent_notifications_.find(title); | 129 const auto notification_id_iter = notification_id_map_.find(title); |
128 if (persistent_iterator == persistent_notifications_.end()) | 130 if (notification_id_iter == notification_id_map_.end()) |
129 return; | 131 return; |
130 | 132 |
131 const PersistentNotification& notification = persistent_iterator->second; | 133 const std::string& notification_id = notification_id_iter->second; |
| 134 |
| 135 const auto& persistent_iter = persistent_notifications_.find(notification_id); |
| 136 if (persistent_iter == persistent_notifications_.end()) |
| 137 return; |
| 138 |
| 139 const PersistentNotification& notification = persistent_iter->second; |
132 content::NotificationEventDispatcher::GetInstance() | 140 content::NotificationEventDispatcher::GetInstance() |
133 ->DispatchNotificationCloseEvent( | 141 ->DispatchNotificationCloseEvent( |
134 notification.browser_context, | 142 notification.browser_context, notification_id, notification.origin, |
135 notification.persistent_id, | 143 by_user, base::Bind(&OnEventDispatchComplete)); |
136 notification.origin, | |
137 by_user, | |
138 base::Bind(&OnEventDispatchComplete)); | |
139 } | 144 } |
140 | 145 |
141 blink::mojom::PermissionStatus | 146 blink::mojom::PermissionStatus |
142 LayoutTestNotificationManager::CheckPermissionOnUIThread( | 147 LayoutTestNotificationManager::CheckPermissionOnUIThread( |
143 BrowserContext* browser_context, | 148 BrowserContext* browser_context, |
144 const GURL& origin, | 149 const GURL& origin, |
145 int render_process_id) { | 150 int render_process_id) { |
146 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 151 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
147 return CheckPermission(origin); | 152 return CheckPermission(origin); |
148 } | 153 } |
149 | 154 |
150 blink::mojom::PermissionStatus | 155 blink::mojom::PermissionStatus |
151 LayoutTestNotificationManager::CheckPermissionOnIOThread( | 156 LayoutTestNotificationManager::CheckPermissionOnIOThread( |
152 ResourceContext* resource_context, | 157 ResourceContext* resource_context, |
153 const GURL& origin, | 158 const GURL& origin, |
154 int render_process_id) { | 159 int render_process_id) { |
155 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 160 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
156 return CheckPermission(origin); | 161 return CheckPermission(origin); |
157 } | 162 } |
158 | 163 |
159 void LayoutTestNotificationManager::Close(const std::string& title) { | 164 void LayoutTestNotificationManager::Close(const std::string& notification_id) { |
160 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 165 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
161 auto iterator = page_notifications_.find(title); | 166 auto iterator = non_persistent_notifications_.find(notification_id); |
162 if (iterator == page_notifications_.end()) | 167 if (iterator == non_persistent_notifications_.end()) |
163 return; | 168 return; |
164 | 169 |
165 iterator->second->NotificationClosed(); | 170 iterator->second->NotificationClosed(); |
166 } | 171 } |
167 | 172 |
168 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( | 173 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( |
169 const PlatformNotificationData& notification_data) { | 174 const std::string& notification_id) { |
170 if (!notification_data.tag.length()) | 175 const auto persistent_iter = persistent_notifications_.find(notification_id); |
171 return; | 176 const auto non_persistent_iter = |
| 177 non_persistent_notifications_.find(notification_id); |
172 | 178 |
173 std::string tag = notification_data.tag; | 179 if (persistent_iter != persistent_notifications_.end()) { |
174 const auto& replace_iter = replacements_.find(tag); | 180 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); |
175 if (replace_iter != replacements_.end()) { | 181 persistent_notifications_.erase(persistent_iter); |
176 const std::string& previous_title = replace_iter->second; | 182 } else if (non_persistent_iter != non_persistent_notifications_.end()) { |
177 | 183 non_persistent_iter->second->NotificationClosed(); |
178 const auto& page_notification_iter = | 184 non_persistent_notifications_.erase(non_persistent_iter); |
179 page_notifications_.find(previous_title); | |
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 } | 185 } |
195 | |
196 replacements_[tag] = base::UTF16ToUTF8(notification_data.title); | |
197 } | 186 } |
198 | 187 |
199 blink::mojom::PermissionStatus | 188 blink::mojom::PermissionStatus |
200 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { | 189 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { |
201 return LayoutTestContentBrowserClient::Get() | 190 return LayoutTestContentBrowserClient::Get() |
202 ->GetLayoutTestBrowserContext() | 191 ->GetLayoutTestBrowserContext() |
203 ->GetLayoutTestPermissionManager() | 192 ->GetLayoutTestPermissionManager() |
204 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, | 193 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, |
205 origin, | 194 origin, |
206 origin); | 195 origin); |
207 } | 196 } |
208 | 197 |
209 } // namespace content | 198 } // namespace content |
OLD | NEW |