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/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 content::NotificationEventDispatcher::GetInstance() |
|
Peter Beverloo
2017/06/08 06:48:11
nit: drop content::, elsewhere too (I see that the
Miguel Garcia
2017/06/09 12:24:52
Done.
| |
| 48 non_persistent_notifications_[notification_id]->NotificationDisplayed(); | 47 ->DispatchNonPersistentShowEvent(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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 content::BrowserThread::UI, FROM_HERE, | 91 content::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 content::NotificationEventDispatcher::GetInstance() |
| 119 ->DispatchNotificationClickEvent( | 116 ->DispatchNotificationClickEvent( |
| 120 notification.browser_context, notification_id, notification.origin, | 117 notification.browser_context, notification_id, notification.origin, |
| 121 action_index, reply, base::Bind(&OnEventDispatchComplete)); | 118 action_index, reply, base::Bind(&OnEventDispatchComplete)); |
| 122 } else if (non_persistent_iter != non_persistent_notifications_.end()) { | 119 } else if (non_persistent_iter != non_persistent_notifications_.end()) { |
| 123 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " | 120 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " |
| 124 "persistent notifications"; | 121 "persistent notifications"; |
| 125 | 122 content::NotificationEventDispatcher::GetInstance() |
| 126 non_persistent_iter->second->NotificationClick(); | 123 ->DispatchNonPersistentClickEvent(notification_id); |
| 127 } | 124 } |
| 128 } | 125 } |
| 129 | 126 |
| 130 void MockPlatformNotificationService::SimulateClose(const std::string& title, | 127 void MockPlatformNotificationService::SimulateClose(const std::string& title, |
| 131 bool by_user) { | 128 bool by_user) { |
| 132 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 133 | 130 |
| 134 const auto notification_id_iter = notification_id_map_.find(title); | 131 const auto notification_id_iter = notification_id_map_.find(title); |
| 135 if (notification_id_iter == notification_id_map_.end()) | 132 if (notification_id_iter == notification_id_map_.end()) |
| 136 return; | 133 return; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 162 ResourceContext* resource_context, | 159 ResourceContext* resource_context, |
| 163 const GURL& origin, | 160 const GURL& origin, |
| 164 int render_process_id) { | 161 int render_process_id) { |
| 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 166 return CheckPermission(origin); | 163 return CheckPermission(origin); |
| 167 } | 164 } |
| 168 | 165 |
| 169 void MockPlatformNotificationService::Close( | 166 void MockPlatformNotificationService::Close( |
| 170 const std::string& notification_id) { | 167 const std::string& notification_id) { |
| 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 172 auto iterator = non_persistent_notifications_.find(notification_id); | 169 const auto non_persistent_iter = |
| 173 if (iterator == non_persistent_notifications_.end()) | 170 non_persistent_notifications_.find(notification_id); |
| 174 return; | 171 content::NotificationEventDispatcher::GetInstance() |
| 175 | 172 ->DispatchNonPersistentCloseEvent(notification_id); |
|
Peter Beverloo
2017/06/08 06:48:11
Should we check for having a valid |non_persistent
Miguel Garcia
2017/06/09 12:24:52
Done.
| |
| 176 iterator->second->NotificationClosed(); | 173 non_persistent_notifications_.erase(non_persistent_iter); |
| 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 const auto persistent_iter = persistent_notifications_.find(notification_id); |
| 182 const auto non_persistent_iter = | 179 const auto non_persistent_iter = |
| 183 non_persistent_notifications_.find(notification_id); | 180 non_persistent_notifications_.find(notification_id); |
| 184 | 181 |
| 185 if (persistent_iter != persistent_notifications_.end()) { | 182 if (persistent_iter != persistent_notifications_.end()) { |
| 186 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); | 183 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); |
| 187 persistent_notifications_.erase(persistent_iter); | 184 persistent_notifications_.erase(persistent_iter); |
| 188 } else if (non_persistent_iter != non_persistent_notifications_.end()) { | 185 } else if (non_persistent_iter != non_persistent_notifications_.end()) { |
| 189 non_persistent_iter->second->NotificationClosed(); | |
| 190 non_persistent_notifications_.erase(non_persistent_iter); | 186 non_persistent_notifications_.erase(non_persistent_iter); |
| 191 } | 187 } |
|
Peter Beverloo
2017/06/08 06:48:11
Hmm. Since calling erase() with a key that's in in
Miguel Garcia
2017/06/09 12:24:52
Good point
| |
| 192 } | 188 } |
| 193 | 189 |
| 194 blink::mojom::PermissionStatus MockPlatformNotificationService::CheckPermission( | 190 blink::mojom::PermissionStatus MockPlatformNotificationService::CheckPermission( |
| 195 const GURL& origin) { | 191 const GURL& origin) { |
| 196 return blink::mojom::PermissionStatus::GRANTED; | 192 return blink::mojom::PermissionStatus::GRANTED; |
| 197 } | 193 } |
| 198 | 194 |
| 199 } // namespace content | 195 } // namespace content |
| OLD | NEW |