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/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
8 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
9 #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" |
| 12 #include "content/public/common/persistent_notification_status.h" |
10 #include "content/public/common/show_desktop_notification_params.h" | 13 #include "content/public/common/show_desktop_notification_params.h" |
11 | 14 |
12 namespace content { | 15 namespace content { |
| 16 namespace { |
| 17 |
| 18 // The Web Notification layout tests don't care about the lifetime of the |
| 19 // Service Worker when a notificationclick event has been dispatched. |
| 20 void OnEventDispatchComplete(PersistentNotificationStatus status) {} |
| 21 |
| 22 } // namespace |
13 | 23 |
14 LayoutTestNotificationManager::LayoutTestNotificationManager() | 24 LayoutTestNotificationManager::LayoutTestNotificationManager() |
15 : weak_factory_(this) {} | 25 : weak_factory_(this) {} |
16 | 26 |
| 27 LayoutTestNotificationManager::PersistentNotification::PersistentNotification() |
| 28 : browser_context(nullptr), |
| 29 service_worker_registration_id(0) {} |
| 30 |
17 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} | 31 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} |
18 | 32 |
19 blink::WebNotificationPermission | 33 blink::WebNotificationPermission |
20 LayoutTestNotificationManager::CheckPermission( | 34 LayoutTestNotificationManager::CheckPermission( |
21 ResourceContext* resource_context, | 35 ResourceContext* resource_context, |
22 const GURL& origin, | 36 const GURL& origin, |
23 int render_process_id) { | 37 int render_process_id) { |
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
25 NotificationPermissionMap::iterator iter = | 39 const auto& iter = permission_map_.find(origin); |
26 permission_map_.find(origin); | |
27 if (iter == permission_map_.end()) | 40 if (iter == permission_map_.end()) |
28 return blink::WebNotificationPermissionDefault; | 41 return blink::WebNotificationPermissionDefault; |
29 | 42 |
30 return iter->second; | 43 return iter->second; |
31 } | 44 } |
32 | 45 |
33 bool LayoutTestNotificationManager::RequestPermission(const GURL& origin) { | 46 bool LayoutTestNotificationManager::RequestPermission(const GURL& origin) { |
34 return CheckPermission(nullptr, origin, 0) == | 47 return CheckPermission(nullptr, origin, 0) == |
35 blink::WebNotificationPermissionAllowed; | 48 blink::WebNotificationPermissionAllowed; |
36 } | 49 } |
(...skipping 17 matching lines...) Expand all Loading... |
54 int render_process_id, | 67 int render_process_id, |
55 base::Closure* cancel_callback) { | 68 base::Closure* cancel_callback) { |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
57 std::string title = base::UTF16ToUTF8(params.title); | 70 std::string title = base::UTF16ToUTF8(params.title); |
58 | 71 |
59 DCHECK(cancel_callback); | 72 DCHECK(cancel_callback); |
60 *cancel_callback = base::Bind(&LayoutTestNotificationManager::Close, | 73 *cancel_callback = base::Bind(&LayoutTestNotificationManager::Close, |
61 weak_factory_.GetWeakPtr(), | 74 weak_factory_.GetWeakPtr(), |
62 title); | 75 title); |
63 | 76 |
64 if (params.replace_id.length()) { | 77 ReplaceNotificationIfNeeded(params); |
65 std::string replace_id = base::UTF16ToUTF8(params.replace_id); | |
66 auto replace_iter = replacements_.find(replace_id); | |
67 if (replace_iter != replacements_.end()) { | |
68 const std::string& previous_title = replace_iter->second; | |
69 auto notification_iter = notifications_.find(previous_title); | |
70 if (notification_iter != notifications_.end()) { | |
71 DesktopNotificationDelegate* previous_delegate = | |
72 notification_iter->second; | |
73 previous_delegate->NotificationClosed(false); | |
74 | 78 |
75 notifications_.erase(notification_iter); | 79 page_notifications_[title] = delegate.release(); |
76 delete previous_delegate; | 80 page_notifications_[title]->NotificationDisplayed(); |
77 } | |
78 } | |
79 | |
80 replacements_[replace_id] = title; | |
81 } | |
82 | |
83 notifications_[title] = delegate.release(); | |
84 notifications_[title]->NotificationDisplayed(); | |
85 } | 81 } |
86 | 82 |
87 void LayoutTestNotificationManager::DisplayPersistentNotification( | 83 void LayoutTestNotificationManager::DisplayPersistentNotification( |
88 BrowserContext* browser_context, | 84 BrowserContext* browser_context, |
89 int64 service_worker_registration_id, | 85 int64 service_worker_registration_id, |
90 const ShowDesktopNotificationHostMsgParams& params, | 86 const ShowDesktopNotificationHostMsgParams& params, |
91 int render_process_id) { | 87 int render_process_id) { |
92 // TODO(peter): Make persistent notifications layout testable. | 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
93 NOTIMPLEMENTED(); | 89 std::string title = base::UTF16ToUTF8(params.title); |
| 90 |
| 91 ReplaceNotificationIfNeeded(params); |
| 92 |
| 93 PersistentNotification notification; |
| 94 notification.browser_context = browser_context; |
| 95 notification.notification_data = params; |
| 96 notification.service_worker_registration_id = service_worker_registration_id; |
| 97 notification.persistent_id = base::GenerateGUID(); |
| 98 |
| 99 persistent_notifications_[title] = notification; |
94 } | 100 } |
95 | 101 |
96 void LayoutTestNotificationManager::ClosePersistentNotification( | 102 void LayoutTestNotificationManager::ClosePersistentNotification( |
97 BrowserContext* browser_context, | 103 BrowserContext* browser_context, |
98 const std::string& persistent_notification_id) { | 104 const std::string& persistent_notification_id) { |
99 // TODO(peter): Make persistent notifications layout testable. | 105 for (const auto& iter : persistent_notifications_) { |
100 NOTIMPLEMENTED(); | 106 if (iter.second.persistent_id != persistent_notification_id) |
| 107 continue; |
| 108 |
| 109 persistent_notifications_.erase(iter.first); |
| 110 return; |
| 111 } |
101 } | 112 } |
102 | 113 |
103 void LayoutTestNotificationManager::SimulateClick(const std::string& title) { | 114 void LayoutTestNotificationManager::SimulateClick(const std::string& title) { |
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
105 auto iterator = notifications_.find(title); | 116 |
106 if (iterator == notifications_.end()) | 117 // First check for page-notifications with the given title. |
| 118 const auto& page_iterator = page_notifications_.find(title); |
| 119 if (page_iterator != page_notifications_.end()) { |
| 120 page_iterator->second->NotificationClick(); |
| 121 return; |
| 122 } |
| 123 |
| 124 // Then check for persistent notifications with the given title. |
| 125 const auto& persistent_iterator = persistent_notifications_.find(title); |
| 126 if (persistent_iterator == persistent_notifications_.end()) |
107 return; | 127 return; |
108 | 128 |
109 iterator->second->NotificationClick(); | 129 const PersistentNotification& notification = persistent_iterator->second; |
| 130 content::NotificationEventDispatcher::GetInstance() |
| 131 ->DispatchNotificationClickEvent( |
| 132 notification.browser_context, |
| 133 notification.notification_data.origin, |
| 134 notification.service_worker_registration_id, |
| 135 notification.persistent_id, |
| 136 notification.notification_data, |
| 137 base::Bind(&OnEventDispatchComplete)); |
110 } | 138 } |
111 | 139 |
112 void LayoutTestNotificationManager::Close(const std::string& title) { | 140 void LayoutTestNotificationManager::Close(const std::string& title) { |
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
114 auto iterator = notifications_.find(title); | 142 auto iterator = page_notifications_.find(title); |
115 if (iterator == notifications_.end()) | 143 if (iterator == page_notifications_.end()) |
116 return; | 144 return; |
117 | 145 |
118 iterator->second->NotificationClosed(false); | 146 iterator->second->NotificationClosed(false); |
119 } | 147 } |
120 | 148 |
| 149 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( |
| 150 const ShowDesktopNotificationHostMsgParams& params) { |
| 151 if (!params.replace_id.length()) |
| 152 return; |
| 153 |
| 154 std::string replace_id = base::UTF16ToUTF8(params.replace_id); |
| 155 const auto& replace_iter = replacements_.find(replace_id); |
| 156 if (replace_iter != replacements_.end()) { |
| 157 const std::string& previous_title = replace_iter->second; |
| 158 |
| 159 const auto& page_notification_iter = |
| 160 page_notifications_.find(previous_title); |
| 161 if (page_notification_iter != page_notifications_.end()) { |
| 162 DesktopNotificationDelegate* previous_delegate = |
| 163 page_notification_iter->second; |
| 164 |
| 165 previous_delegate->NotificationClosed(false); |
| 166 |
| 167 page_notifications_.erase(page_notification_iter); |
| 168 delete previous_delegate; |
| 169 } |
| 170 |
| 171 const auto& persistent_notification_iter = |
| 172 persistent_notifications_.find(previous_title); |
| 173 if (persistent_notification_iter != persistent_notifications_.end()) |
| 174 persistent_notifications_.erase(persistent_notification_iter); |
| 175 } |
| 176 |
| 177 replacements_[replace_id] = base::UTF16ToUTF8(params.title); |
| 178 } |
| 179 |
121 } // namespace content | 180 } // namespace content |
OLD | NEW |