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" | |
13 #include "content/public/browser/permission_type.h" | 7 #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" | |
16 #include "content/shell/browser/layout_test/layout_test_browser_context.h" | 8 #include "content/shell/browser/layout_test/layout_test_browser_context.h" |
17 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h
" | 9 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h
" |
18 #include "content/shell/browser/layout_test/layout_test_permission_manager.h" | 10 #include "content/shell/browser/layout_test/layout_test_permission_manager.h" |
19 | 11 |
20 namespace content { | 12 namespace content { |
21 namespace { | |
22 | 13 |
23 // The Web Notification layout tests don't care about the lifetime of the | 14 LayoutTestNotificationManager::LayoutTestNotificationManager() {} |
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 | |
32 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} | 15 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} |
33 | 16 |
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 void LayoutTestNotificationManager::GetDisplayedNotifications( | |
87 BrowserContext* browser_context, | |
88 const DisplayedNotificationsCallback& callback) { | |
89 auto displayed_notifications = base::MakeUnique<std::set<std::string>>(); | |
90 BrowserThread::PostTask( | |
91 BrowserThread::UI, FROM_HERE, | |
92 base::Bind(callback, base::Passed(&displayed_notifications), | |
93 false /* supports_synchronization */)); | |
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 NotificationEventDispatcher::GetInstance()->DispatchNotificationClickEvent( | |
117 notification.browser_context, notification_id, notification.origin, | |
118 action_index, reply, base::Bind(&OnEventDispatchComplete)); | |
119 } else if (non_persistent_iter != non_persistent_notifications_.end()) { | |
120 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " | |
121 "persistent notifications"; | |
122 | |
123 non_persistent_iter->second->NotificationClick(); | |
124 } | |
125 } | |
126 | |
127 void LayoutTestNotificationManager::SimulateClose(const std::string& title, | |
128 bool by_user) { | |
129 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
130 | |
131 const auto notification_id_iter = notification_id_map_.find(title); | |
132 if (notification_id_iter == notification_id_map_.end()) | |
133 return; | |
134 | |
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; | |
142 NotificationEventDispatcher::GetInstance()->DispatchNotificationCloseEvent( | |
143 notification.browser_context, notification_id, notification.origin, | |
144 by_user, base::Bind(&OnEventDispatchComplete)); | |
145 } | |
146 | |
147 blink::mojom::PermissionStatus | |
148 LayoutTestNotificationManager::CheckPermissionOnUIThread( | |
149 BrowserContext* browser_context, | |
150 const GURL& origin, | |
151 int render_process_id) { | |
152 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
153 return CheckPermission(origin); | |
154 } | |
155 | |
156 blink::mojom::PermissionStatus | |
157 LayoutTestNotificationManager::CheckPermissionOnIOThread( | |
158 ResourceContext* resource_context, | |
159 const GURL& origin, | |
160 int render_process_id) { | |
161 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
162 return CheckPermission(origin); | |
163 } | |
164 | |
165 void LayoutTestNotificationManager::Close(const std::string& notification_id) { | |
166 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
167 auto iterator = non_persistent_notifications_.find(notification_id); | |
168 if (iterator == non_persistent_notifications_.end()) | |
169 return; | |
170 | |
171 iterator->second->NotificationClosed(); | |
172 } | |
173 | |
174 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( | |
175 const std::string& notification_id) { | |
176 const auto persistent_iter = persistent_notifications_.find(notification_id); | |
177 const auto non_persistent_iter = | |
178 non_persistent_notifications_.find(notification_id); | |
179 | |
180 if (persistent_iter != persistent_notifications_.end()) { | |
181 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); | |
182 persistent_notifications_.erase(persistent_iter); | |
183 } else if (non_persistent_iter != non_persistent_notifications_.end()) { | |
184 non_persistent_iter->second->NotificationClosed(); | |
185 non_persistent_notifications_.erase(non_persistent_iter); | |
186 } | |
187 } | |
188 | 17 |
189 blink::mojom::PermissionStatus | 18 blink::mojom::PermissionStatus |
190 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { | 19 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { |
191 return LayoutTestContentBrowserClient::Get() | 20 return LayoutTestContentBrowserClient::Get() |
192 ->GetLayoutTestBrowserContext() | 21 ->GetLayoutTestBrowserContext() |
193 ->GetLayoutTestPermissionManager() | 22 ->GetLayoutTestPermissionManager() |
194 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, | 23 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, |
195 origin, | 24 origin, |
196 origin); | 25 origin); |
197 } | 26 } |
198 | 27 |
199 } // namespace content | 28 } // namespace content |
OLD | NEW |