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
3 // found in the LICENSE file.
5 #include "content/shell/browser/layout_test/layout_test_notification_manager.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/desktop_notification_delegate.h"
11 #include "content/public/browser/notification_event_dispatcher.h"
12 #include "content/public/browser/permission_type.h"
13 #include "content/public/common/persistent_notification_status.h"
14 #include "content/public/common/platform_notification_data.h"
15 #include "content/shell/browser/layout_test/layout_test_browser_context.h"
16 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h"
17 #include "content/shell/browser/layout_test/layout_test_permission_manager.h"
22 // The Web Notification layout tests don't care about the lifetime of the
23 // Service Worker when a notificationclick event has been dispatched.
24 void OnEventDispatchComplete(PersistentNotificationStatus status
) {}
26 blink::WebNotificationPermission
ToWebNotificationPermission(
27 PermissionStatus status
) {
29 case PERMISSION_STATUS_GRANTED
:
30 return blink::WebNotificationPermissionAllowed
;
31 case PERMISSION_STATUS_DENIED
:
32 return blink::WebNotificationPermissionDenied
;
33 case PERMISSION_STATUS_ASK
:
34 return blink::WebNotificationPermissionDefault
;
38 return blink::WebNotificationPermissionLast
;
43 LayoutTestNotificationManager::LayoutTestNotificationManager()
44 : weak_factory_(this) {}
46 LayoutTestNotificationManager::~LayoutTestNotificationManager() {}
48 void LayoutTestNotificationManager::DisplayNotification(
49 BrowserContext
* browser_context
,
52 const PlatformNotificationData
& notification_data
,
53 scoped_ptr
<DesktopNotificationDelegate
> delegate
,
54 base::Closure
* cancel_callback
) {
55 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
56 std::string title
= base::UTF16ToUTF8(notification_data
.title
);
58 DCHECK(cancel_callback
);
59 *cancel_callback
= base::Bind(&LayoutTestNotificationManager::Close
,
60 weak_factory_
.GetWeakPtr(),
63 ReplaceNotificationIfNeeded(notification_data
);
65 page_notifications_
[title
] = delegate
.release();
66 page_notifications_
[title
]->NotificationDisplayed();
69 void LayoutTestNotificationManager::DisplayPersistentNotification(
70 BrowserContext
* browser_context
,
71 int64_t persistent_notification_id
,
74 const PlatformNotificationData
& notification_data
) {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
76 std::string title
= base::UTF16ToUTF8(notification_data
.title
);
78 ReplaceNotificationIfNeeded(notification_data
);
80 PersistentNotification notification
;
81 notification
.browser_context
= browser_context
;
82 notification
.origin
= origin
;
83 notification
.persistent_id
= persistent_notification_id
;
85 persistent_notifications_
[title
] = notification
;
88 void LayoutTestNotificationManager::ClosePersistentNotification(
89 BrowserContext
* browser_context
,
90 int64_t persistent_notification_id
) {
91 for (const auto& iter
: persistent_notifications_
) {
92 if (iter
.second
.persistent_id
!= persistent_notification_id
)
95 persistent_notifications_
.erase(iter
.first
);
100 bool LayoutTestNotificationManager::GetDisplayedPersistentNotifications(
101 BrowserContext
* browser_context
,
102 std::set
<std::string
>* displayed_notifications
) {
103 DCHECK(displayed_notifications
);
105 // Notifications will never outlive the lifetime of running layout tests.
109 void LayoutTestNotificationManager::SimulateClick(const std::string
& title
) {
110 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
112 // First check for page-notifications with the given title.
113 const auto& page_iterator
= page_notifications_
.find(title
);
114 if (page_iterator
!= page_notifications_
.end()) {
115 page_iterator
->second
->NotificationClick();
119 // Then check for persistent notifications with the given title.
120 const auto& persistent_iterator
= persistent_notifications_
.find(title
);
121 if (persistent_iterator
== persistent_notifications_
.end())
124 const PersistentNotification
& notification
= persistent_iterator
->second
;
125 content::NotificationEventDispatcher::GetInstance()
126 ->DispatchNotificationClickEvent(
127 notification
.browser_context
,
128 notification
.persistent_id
,
130 -1 /* action_index */,
131 base::Bind(&OnEventDispatchComplete
));
134 blink::WebNotificationPermission
135 LayoutTestNotificationManager::CheckPermissionOnUIThread(
136 BrowserContext
* browser_context
,
138 int render_process_id
) {
139 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
140 return CheckPermission(origin
);
143 blink::WebNotificationPermission
144 LayoutTestNotificationManager::CheckPermissionOnIOThread(
145 ResourceContext
* resource_context
,
147 int render_process_id
) {
148 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
149 return CheckPermission(origin
);
152 void LayoutTestNotificationManager::Close(const std::string
& title
) {
153 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
154 auto iterator
= page_notifications_
.find(title
);
155 if (iterator
== page_notifications_
.end())
158 iterator
->second
->NotificationClosed();
161 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded(
162 const PlatformNotificationData
& notification_data
) {
163 if (!notification_data
.tag
.length())
166 std::string tag
= notification_data
.tag
;
167 const auto& replace_iter
= replacements_
.find(tag
);
168 if (replace_iter
!= replacements_
.end()) {
169 const std::string
& previous_title
= replace_iter
->second
;
171 const auto& page_notification_iter
=
172 page_notifications_
.find(previous_title
);
173 if (page_notification_iter
!= page_notifications_
.end()) {
174 DesktopNotificationDelegate
* previous_delegate
=
175 page_notification_iter
->second
;
177 previous_delegate
->NotificationClosed();
179 page_notifications_
.erase(page_notification_iter
);
180 delete previous_delegate
;
183 const auto& persistent_notification_iter
=
184 persistent_notifications_
.find(previous_title
);
185 if (persistent_notification_iter
!= persistent_notifications_
.end())
186 persistent_notifications_
.erase(persistent_notification_iter
);
189 replacements_
[tag
] = base::UTF16ToUTF8(notification_data
.title
);
192 blink::WebNotificationPermission
193 LayoutTestNotificationManager::CheckPermission(const GURL
& origin
) {
194 return ToWebNotificationPermission(LayoutTestContentBrowserClient::Get()
195 ->GetLayoutTestBrowserContext()
196 ->GetLayoutTestPermissionManager()
197 ->GetPermissionStatus(PermissionType::NOTIFICATIONS
,
202 } // namespace content