2 * Copyright (C) 2011, 2012, 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WebNotificationManager.h"
29 #include "WebCoreArgumentCoders.h"
31 #include "WebProcess.h"
32 #include "WebProcessCreationParameters.h"
34 #if ENABLE(NOTIFICATIONS)
35 #include "WebNotification.h"
36 #include "WebNotificationManagerMessages.h"
37 #include "WebPageProxyMessages.h"
38 #include <WebCore/Document.h>
39 #include <WebCore/Notification.h>
40 #include <WebCore/Page.h>
41 #include <WebCore/ScriptExecutionContext.h>
42 #include <WebCore/SecurityOrigin.h>
43 #include <WebCore/Settings.h>
44 #include <WebCore/UserGestureIndicator.h>
47 using namespace WebCore;
51 #if ENABLE(NOTIFICATIONS)
52 static uint64_t generateNotificationID()
54 static uint64_t uniqueNotificationID = 1;
55 return uniqueNotificationID++;
59 const char* WebNotificationManager::supplementName()
61 return "WebNotificationManager";
64 WebNotificationManager::WebNotificationManager(WebProcess* process)
67 #if ENABLE(NOTIFICATIONS)
68 m_process->addMessageReceiver(Messages::WebNotificationManager::messageReceiverName(), *this);
72 WebNotificationManager::~WebNotificationManager()
76 void WebNotificationManager::initialize(const WebProcessCreationParameters& parameters)
78 #if ENABLE(NOTIFICATIONS)
79 m_permissionsMap = parameters.notificationPermissions;
81 UNUSED_PARAM(parameters);
85 void WebNotificationManager::didUpdateNotificationDecision(const String& originString, bool allowed)
87 #if ENABLE(NOTIFICATIONS)
88 m_permissionsMap.set(originString, allowed);
90 UNUSED_PARAM(originString);
91 UNUSED_PARAM(allowed);
95 void WebNotificationManager::didRemoveNotificationDecisions(const Vector<String>& originStrings)
97 #if ENABLE(NOTIFICATIONS)
98 for (auto& originString : originStrings)
99 m_permissionsMap.remove(originString);
101 UNUSED_PARAM(originStrings);
105 NotificationClient::Permission WebNotificationManager::policyForOrigin(WebCore::SecurityOrigin* origin) const
107 #if ENABLE(NOTIFICATIONS)
109 return NotificationClient::Permission::Default;
111 ASSERT(!origin->isUnique());
112 auto it = m_permissionsMap.find(origin->toRawString());
113 if (it != m_permissionsMap.end())
114 return it->value ? NotificationClient::Permission::Granted : NotificationClient::Permission::Denied;
116 UNUSED_PARAM(origin);
119 return NotificationClient::Permission::Default;
122 void WebNotificationManager::removeAllPermissionsForTesting()
124 #if ENABLE(NOTIFICATIONS)
125 m_permissionsMap.clear();
129 uint64_t WebNotificationManager::notificationIDForTesting(Notification* notification)
131 #if ENABLE(NOTIFICATIONS)
134 return m_notificationMap.get(notification);
136 UNUSED_PARAM(notification);
141 bool WebNotificationManager::show(Notification* notification, WebPage* page)
143 #if ENABLE(NOTIFICATIONS)
144 if (!notification || !page->corePage()->settings().notificationsEnabled())
147 uint64_t notificationID = generateNotificationID();
148 m_notificationMap.set(notification, notificationID);
149 m_notificationIDMap.set(notificationID, notification);
151 auto it = m_notificationContextMap.add(notification->scriptExecutionContext(), Vector<uint64_t>()).iterator;
152 it->value.append(notificationID);
154 m_process->parentProcessConnection()->send(Messages::WebPageProxy::ShowNotification(notification->title(), notification->body(), notification->icon().string(), notification->tag(), notification->lang(), notification->dir(), notification->scriptExecutionContext()->securityOrigin()->toString(), notificationID), page->pageID());
157 UNUSED_PARAM(notification);
163 void WebNotificationManager::cancel(Notification* notification, WebPage* page)
165 #if ENABLE(NOTIFICATIONS)
166 if (!notification || !page->corePage()->settings().notificationsEnabled())
169 uint64_t notificationID = m_notificationMap.get(notification);
173 m_process->parentProcessConnection()->send(Messages::WebPageProxy::CancelNotification(notificationID), page->pageID());
175 UNUSED_PARAM(notification);
180 void WebNotificationManager::clearNotifications(WebCore::ScriptExecutionContext* context, WebPage* page)
182 #if ENABLE(NOTIFICATIONS)
183 NotificationContextMap::iterator it = m_notificationContextMap.find(context);
184 if (it == m_notificationContextMap.end())
187 Vector<uint64_t>& notificationIDs = it->value;
188 m_process->parentProcessConnection()->send(Messages::WebPageProxy::ClearNotifications(notificationIDs), page->pageID());
189 size_t count = notificationIDs.size();
190 for (size_t i = 0; i < count; ++i) {
191 RefPtr<Notification> notification = m_notificationIDMap.take(notificationIDs[i]);
194 notification->finalize();
195 m_notificationMap.remove(notification);
198 m_notificationContextMap.remove(it);
200 UNUSED_PARAM(context);
205 void WebNotificationManager::didDestroyNotification(Notification* notification, WebPage* page)
207 #if ENABLE(NOTIFICATIONS)
208 uint64_t notificationID = m_notificationMap.take(notification);
212 m_notificationIDMap.remove(notificationID);
213 removeNotificationFromContextMap(notificationID, notification);
214 m_process->parentProcessConnection()->send(Messages::WebPageProxy::DidDestroyNotification(notificationID), page->pageID());
216 UNUSED_PARAM(notification);
221 void WebNotificationManager::didShowNotification(uint64_t notificationID)
223 #if ENABLE(NOTIFICATIONS)
224 if (!isNotificationIDValid(notificationID))
227 RefPtr<Notification> notification = m_notificationIDMap.get(notificationID);
231 notification->dispatchShowEvent();
233 UNUSED_PARAM(notificationID);
237 void WebNotificationManager::didClickNotification(uint64_t notificationID)
239 #if ENABLE(NOTIFICATIONS)
240 if (!isNotificationIDValid(notificationID))
243 RefPtr<Notification> notification = m_notificationIDMap.get(notificationID);
247 // Indicate that this event is being dispatched in reaction to a user's interaction with a platform notification.
248 UserGestureIndicator indicator(ProcessingUserGesture);
249 notification->dispatchClickEvent();
251 UNUSED_PARAM(notificationID);
255 void WebNotificationManager::didCloseNotifications(const Vector<uint64_t>& notificationIDs)
257 #if ENABLE(NOTIFICATIONS)
258 size_t count = notificationIDs.size();
259 for (size_t i = 0; i < count; ++i) {
260 uint64_t notificationID = notificationIDs[i];
261 if (!isNotificationIDValid(notificationID))
264 RefPtr<Notification> notification = m_notificationIDMap.take(notificationID);
268 m_notificationMap.remove(notification);
269 removeNotificationFromContextMap(notificationID, notification.get());
271 notification->dispatchCloseEvent();
274 UNUSED_PARAM(notificationIDs);
278 #if ENABLE(NOTIFICATIONS)
279 void WebNotificationManager::removeNotificationFromContextMap(uint64_t notificationID, Notification* notification)
281 // This is a helper function for managing the hash maps.
282 NotificationContextMap::iterator it = m_notificationContextMap.find(notification->scriptExecutionContext());
283 ASSERT(it != m_notificationContextMap.end());
284 size_t index = it->value.find(notificationID);
285 ASSERT(index != notFound);
286 it->value.remove(index);
287 if (it->value.isEmpty())
288 m_notificationContextMap.remove(it);
292 } // namespace WebKit