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