Roll src/third_party/skia 2440fcd:4de8c3a
[chromium-blink-merge.git] / content / shell / browser / layout_test / layout_test_notification_manager.cc
blob3a0433b380db744a5acc548df565a4bda247303f
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"
7 #include "base/guid.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"
19 namespace content {
20 namespace {
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) {
28 switch (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;
37 NOTREACHED();
38 return blink::WebNotificationPermissionLast;
41 } // namespace
43 LayoutTestNotificationManager::LayoutTestNotificationManager()
44 : weak_factory_(this) {}
46 LayoutTestNotificationManager::~LayoutTestNotificationManager() {}
48 void LayoutTestNotificationManager::DisplayNotification(
49 BrowserContext* browser_context,
50 const GURL& origin,
51 const SkBitmap& icon,
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(),
61 title);
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,
72 const GURL& origin,
73 const SkBitmap& icon,
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)
93 continue;
95 persistent_notifications_.erase(iter.first);
96 return;
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.
106 return false;
109 void LayoutTestNotificationManager::SimulateClick(const std::string& title,
110 int action_index) {
111 DCHECK_CURRENTLY_ON(BrowserThread::UI);
113 // First check for page-notifications with the given title.
114 const auto& page_iterator = page_notifications_.find(title);
115 if (page_iterator != page_notifications_.end()) {
116 // TODO(johnme): Pass action_index once it's supported.
117 page_iterator->second->NotificationClick();
118 return;
121 // Then check for persistent notifications with the given title.
122 const auto& persistent_iterator = persistent_notifications_.find(title);
123 if (persistent_iterator == persistent_notifications_.end())
124 return;
126 const PersistentNotification& notification = persistent_iterator->second;
127 content::NotificationEventDispatcher::GetInstance()
128 ->DispatchNotificationClickEvent(
129 notification.browser_context,
130 notification.persistent_id,
131 notification.origin,
132 action_index,
133 base::Bind(&OnEventDispatchComplete));
136 blink::WebNotificationPermission
137 LayoutTestNotificationManager::CheckPermissionOnUIThread(
138 BrowserContext* browser_context,
139 const GURL& origin,
140 int render_process_id) {
141 DCHECK_CURRENTLY_ON(BrowserThread::UI);
142 return CheckPermission(origin);
145 blink::WebNotificationPermission
146 LayoutTestNotificationManager::CheckPermissionOnIOThread(
147 ResourceContext* resource_context,
148 const GURL& origin,
149 int render_process_id) {
150 DCHECK_CURRENTLY_ON(BrowserThread::IO);
151 return CheckPermission(origin);
154 void LayoutTestNotificationManager::Close(const std::string& title) {
155 DCHECK_CURRENTLY_ON(BrowserThread::UI);
156 auto iterator = page_notifications_.find(title);
157 if (iterator == page_notifications_.end())
158 return;
160 iterator->second->NotificationClosed();
163 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded(
164 const PlatformNotificationData& notification_data) {
165 if (!notification_data.tag.length())
166 return;
168 std::string tag = notification_data.tag;
169 const auto& replace_iter = replacements_.find(tag);
170 if (replace_iter != replacements_.end()) {
171 const std::string& previous_title = replace_iter->second;
173 const auto& page_notification_iter =
174 page_notifications_.find(previous_title);
175 if (page_notification_iter != page_notifications_.end()) {
176 DesktopNotificationDelegate* previous_delegate =
177 page_notification_iter->second;
179 previous_delegate->NotificationClosed();
181 page_notifications_.erase(page_notification_iter);
182 delete previous_delegate;
185 const auto& persistent_notification_iter =
186 persistent_notifications_.find(previous_title);
187 if (persistent_notification_iter != persistent_notifications_.end())
188 persistent_notifications_.erase(persistent_notification_iter);
191 replacements_[tag] = base::UTF16ToUTF8(notification_data.title);
194 blink::WebNotificationPermission
195 LayoutTestNotificationManager::CheckPermission(const GURL& origin) {
196 return ToWebNotificationPermission(LayoutTestContentBrowserClient::Get()
197 ->GetLayoutTestBrowserContext()
198 ->GetLayoutTestPermissionManager()
199 ->GetPermissionStatus(PermissionType::NOTIFICATIONS,
200 origin,
201 origin));
204 } // namespace content