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" | 7 #include "base/guid.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/desktop_notification_delegate.h" | 10 #include "content/public/browser/desktop_notification_delegate.h" |
11 #include "content/public/browser/notification_event_dispatcher.h" | 11 #include "content/public/browser/notification_event_dispatcher.h" |
12 #include "content/public/browser/permission_type.h" | 12 #include "content/public/browser/permission_type.h" |
13 #include "content/public/common/persistent_notification_status.h" | 13 #include "content/public/common/persistent_notification_status.h" |
14 #include "content/public/common/platform_notification_data.h" | 14 #include "content/public/common/platform_notification_data.h" |
15 #include "content/shell/browser/layout_test/layout_test_browser_context.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
" | 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" | 17 #include "content/shell/browser/layout_test/layout_test_permission_manager.h" |
18 | 18 |
| 19 using message_center::Notification; |
| 20 |
19 namespace content { | 21 namespace content { |
20 namespace { | 22 namespace { |
21 | 23 |
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) {} | |
25 | |
26 blink::WebNotificationPermission ToWebNotificationPermission( | 24 blink::WebNotificationPermission ToWebNotificationPermission( |
27 PermissionStatus status) { | 25 PermissionStatus status) { |
28 switch (status) { | 26 switch (status) { |
29 case PERMISSION_STATUS_GRANTED: | 27 case PERMISSION_STATUS_GRANTED: |
30 return blink::WebNotificationPermissionAllowed; | 28 return blink::WebNotificationPermissionAllowed; |
31 case PERMISSION_STATUS_DENIED: | 29 case PERMISSION_STATUS_DENIED: |
32 return blink::WebNotificationPermissionDenied; | 30 return blink::WebNotificationPermissionDenied; |
33 case PERMISSION_STATUS_ASK: | 31 case PERMISSION_STATUS_ASK: |
34 return blink::WebNotificationPermissionDefault; | 32 return blink::WebNotificationPermissionDefault; |
35 } | 33 } |
36 | 34 |
37 NOTREACHED(); | 35 NOTREACHED(); |
38 return blink::WebNotificationPermissionLast; | 36 return blink::WebNotificationPermissionLast; |
39 } | 37 } |
40 | 38 |
41 } // namespace | 39 } // namespace |
42 | 40 |
43 LayoutTestNotificationManager::LayoutTestNotificationManager() | 41 LayoutTestNotificationManager::LayoutTestNotificationManager() {} |
44 : weak_factory_(this) {} | |
45 | 42 |
46 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} | 43 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} |
47 | 44 |
48 void LayoutTestNotificationManager::DisplayNotification( | 45 void LayoutTestNotificationManager::DisplayNotification( |
49 BrowserContext* browser_context, | 46 BrowserContext* browser_context, |
50 const GURL& origin, | 47 const Notification& notification) { |
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); | 48 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
56 std::string title = base::UTF16ToUTF8(notification_data.title); | |
57 | 49 |
58 DCHECK(cancel_callback); | 50 const std::string& notification_id = notification.id(); |
59 *cancel_callback = base::Bind(&LayoutTestNotificationManager::Close, | |
60 weak_factory_.GetWeakPtr(), | |
61 title); | |
62 | 51 |
63 ReplaceNotificationIfNeeded(notification_data); | 52 // Deterministic notification id generation allows us to simply check for |
| 53 // notifications sharing an id with |notification| rather than having to |
| 54 // consider the tag when closing to-be-replaced notifications. |
| 55 if (notifications_.contains(notification_id)) |
| 56 CloseNotification(browser_context, notification_id); |
64 | 57 |
65 page_notifications_[title] = delegate.release(); | 58 // Makes sure that the |onshow| event fires for non-persistent notifications. |
66 page_notifications_[title]->NotificationDisplayed(); | 59 notification.delegate()->Display(); |
| 60 |
| 61 notifications_.add(notification_id, |
| 62 make_scoped_ptr(new Notification(notification))); |
67 } | 63 } |
68 | 64 |
69 void LayoutTestNotificationManager::DisplayPersistentNotification( | 65 void LayoutTestNotificationManager::CloseNotification( |
70 BrowserContext* browser_context, | 66 BrowserContext* browser_context, |
71 int64_t persistent_notification_id, | 67 const std::string& notification_id) { |
72 const GURL& origin, | 68 Notification* notification = notifications_.get(notification_id); |
73 const SkBitmap& icon, | 69 if (!notification) |
74 const PlatformNotificationData& notification_data) { | 70 return; |
75 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
76 std::string title = base::UTF16ToUTF8(notification_data.title); | |
77 | 71 |
78 ReplaceNotificationIfNeeded(notification_data); | 72 // Makes sure that the |onclose| event fires for non-persistent notifications. |
| 73 notification->delegate()->Close(false /* by_user */); |
79 | 74 |
80 PersistentNotification notification; | 75 notifications_.erase(notification_id); |
81 notification.browser_context = browser_context; | |
82 notification.origin = origin; | |
83 notification.persistent_id = persistent_notification_id; | |
84 | |
85 persistent_notifications_[title] = notification; | |
86 } | |
87 | |
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; | |
94 | |
95 persistent_notifications_.erase(iter.first); | |
96 return; | |
97 } | |
98 } | 76 } |
99 | 77 |
100 void LayoutTestNotificationManager::SimulateClick(const std::string& title) { | 78 void LayoutTestNotificationManager::SimulateClick(const std::string& title) { |
101 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 79 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
102 | 80 |
103 // First check for page-notifications with the given title. | 81 base::string16 title16 = base::UTF8ToUTF16(title); |
104 const auto& page_iterator = page_notifications_.find(title); | 82 for (const auto& iter : notifications_) { |
105 if (page_iterator != page_notifications_.end()) { | 83 if (iter.second->title() != title16) |
106 page_iterator->second->NotificationClick(); | 84 continue; |
107 return; | 85 |
| 86 // Makes sure that the |onclick| event fires for non-persistent |
| 87 // notifications, or the |onnotificationclick| event for persistent ones. |
| 88 iter.second->delegate()->Click(); |
108 } | 89 } |
109 | |
110 // Then check for persistent notifications with the given title. | |
111 const auto& persistent_iterator = persistent_notifications_.find(title); | |
112 if (persistent_iterator == persistent_notifications_.end()) | |
113 return; | |
114 | |
115 const PersistentNotification& notification = persistent_iterator->second; | |
116 content::NotificationEventDispatcher::GetInstance() | |
117 ->DispatchNotificationClickEvent( | |
118 notification.browser_context, | |
119 notification.persistent_id, | |
120 notification.origin, | |
121 base::Bind(&OnEventDispatchComplete)); | |
122 } | 90 } |
123 | 91 |
124 blink::WebNotificationPermission | 92 blink::WebNotificationPermission |
125 LayoutTestNotificationManager::CheckPermissionOnUIThread( | 93 LayoutTestNotificationManager::CheckPermissionOnUIThread( |
126 BrowserContext* browser_context, | 94 BrowserContext* browser_context, |
127 const GURL& origin, | 95 const GURL& origin, |
128 int render_process_id) { | 96 int render_process_id) { |
129 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
130 return CheckPermission(origin); | 98 return CheckPermission(origin); |
131 } | 99 } |
132 | 100 |
133 blink::WebNotificationPermission | 101 blink::WebNotificationPermission |
134 LayoutTestNotificationManager::CheckPermissionOnIOThread( | 102 LayoutTestNotificationManager::CheckPermissionOnIOThread( |
135 ResourceContext* resource_context, | 103 ResourceContext* resource_context, |
136 const GURL& origin, | 104 const GURL& origin, |
137 int render_process_id) { | 105 int render_process_id) { |
138 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
139 return CheckPermission(origin); | 107 return CheckPermission(origin); |
140 } | 108 } |
141 | 109 |
142 void LayoutTestNotificationManager::Close(const std::string& title) { | |
143 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
144 auto iterator = page_notifications_.find(title); | |
145 if (iterator == page_notifications_.end()) | |
146 return; | |
147 | |
148 iterator->second->NotificationClosed(); | |
149 } | |
150 | |
151 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( | |
152 const PlatformNotificationData& notification_data) { | |
153 if (!notification_data.tag.length()) | |
154 return; | |
155 | |
156 std::string tag = notification_data.tag; | |
157 const auto& replace_iter = replacements_.find(tag); | |
158 if (replace_iter != replacements_.end()) { | |
159 const std::string& previous_title = replace_iter->second; | |
160 | |
161 const auto& page_notification_iter = | |
162 page_notifications_.find(previous_title); | |
163 if (page_notification_iter != page_notifications_.end()) { | |
164 DesktopNotificationDelegate* previous_delegate = | |
165 page_notification_iter->second; | |
166 | |
167 previous_delegate->NotificationClosed(); | |
168 | |
169 page_notifications_.erase(page_notification_iter); | |
170 delete previous_delegate; | |
171 } | |
172 | |
173 const auto& persistent_notification_iter = | |
174 persistent_notifications_.find(previous_title); | |
175 if (persistent_notification_iter != persistent_notifications_.end()) | |
176 persistent_notifications_.erase(persistent_notification_iter); | |
177 } | |
178 | |
179 replacements_[tag] = base::UTF16ToUTF8(notification_data.title); | |
180 } | |
181 | |
182 blink::WebNotificationPermission | 110 blink::WebNotificationPermission |
183 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { | 111 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { |
184 return ToWebNotificationPermission(LayoutTestContentBrowserClient::Get() | 112 return ToWebNotificationPermission(LayoutTestContentBrowserClient::Get() |
185 ->GetLayoutTestBrowserContext() | 113 ->GetLayoutTestBrowserContext() |
186 ->GetLayoutTestPermissionManager() | 114 ->GetLayoutTestPermissionManager() |
187 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, | 115 ->GetPermissionStatus(PermissionType::NOTIFICATIONS, |
188 origin, | 116 origin, |
189 origin)); | 117 origin)); |
190 } | 118 } |
191 | 119 |
192 } // namespace content | 120 } // namespace content |
OLD | NEW |