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"
30 #include "WebProcess.h"
31 #include "WebProcessCreationParameters.h"
33 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
34 #include "WebNotification.h"
35 #include "WebNotificationManagerMessages.h"
36 #include "WebPageProxyMessages.h"
37 #include <WebCore/Document.h>
38 #include <WebCore/Notification.h>
39 #include <WebCore/Page.h>
40 #include <WebCore/ScriptExecutionContext.h>
41 #include <WebCore/SecurityOrigin.h>
42 #include <WebCore/Settings.h>
43 #include <WebCore/UserGestureIndicator.h>
46 using namespace WebCore;
50 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
51 static uint64_t generateNotificationID()
53 static uint64_t uniqueNotificationID = 1;
54 return uniqueNotificationID++;
58 const char* WebNotificationManager::supplementName()
60 return "WebNotificationManager";
63 WebNotificationManager::WebNotificationManager(WebProcess* process)
66 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
67 m_process->addMessageReceiver(Messages::WebNotificationManager::messageReceiverName(), *this);
71 WebNotificationManager::~WebNotificationManager()
75 void WebNotificationManager::initialize(const WebProcessCreationParameters& parameters)
77 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
78 m_permissionsMap = parameters.notificationPermissions;
80 UNUSED_PARAM(parameters);
84 void WebNotificationManager::didUpdateNotificationDecision(const String& originString, bool allowed)
86 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
87 m_permissionsMap.set(originString, allowed);
89 UNUSED_PARAM(originString);
90 UNUSED_PARAM(allowed);
94 void WebNotificationManager::didRemoveNotificationDecisions(const Vector<String>& originStrings)
96 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
97 size_t count = originStrings.size();
98 for (size_t i = 0; i < count; ++i)
99 m_permissionsMap.remove(originStrings[i]);
101 UNUSED_PARAM(originStrings);
105 NotificationClient::Permission WebNotificationManager::policyForOrigin(WebCore::SecurityOrigin *origin) const
107 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
109 return NotificationClient::PermissionNotAllowed;
111 ASSERT(!origin->isUnique());
112 HashMap<String, bool>::const_iterator it = m_permissionsMap.find(origin->toRawString());
113 if (it != m_permissionsMap.end())
114 return it->value ? NotificationClient::PermissionAllowed : NotificationClient::PermissionDenied;
116 UNUSED_PARAM(origin);
119 return NotificationClient::PermissionNotAllowed;
122 void WebNotificationManager::removeAllPermissionsForTesting()
124 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
125 m_permissionsMap.clear();
129 uint64_t WebNotificationManager::notificationIDForTesting(Notification* notification)
131 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
134 return m_notificationMap.get(notification);
136 UNUSED_PARAM(notification);
141 bool WebNotificationManager::show(Notification* notification, WebPage* page)
143 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_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 NotificationContextMap::iterator it = m_notificationContextMap.add(notification->scriptExecutionContext(), Vector<uint64_t>()).iterator;
152 it->value.append(notificationID);
154 #if ENABLE(NOTIFICATIONS)
155 m_process->parentProcessConnection()->send(Messages::WebPageProxy::ShowNotification(notification->title(), notification->body(), notification->iconURL().string(), notification->tag(), notification->lang(), notification->dir(), notification->scriptExecutionContext()->securityOrigin()->toString(), notificationID), page->pageID());
157 m_process->parentProcessConnection()->send(Messages::WebPageProxy::ShowNotification(notification->title(), notification->body(), notification->iconURL().string(), notification->replaceId(), notification->lang(), notification->dir(), notification->scriptExecutionContext()->securityOrigin()->toString(), notificationID), page->pageID());
161 UNUSED_PARAM(notification);
167 void WebNotificationManager::cancel(Notification* notification, WebPage* page)
169 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
170 if (!notification || !page->corePage()->settings().notificationsEnabled())
173 uint64_t notificationID = m_notificationMap.get(notification);
177 m_process->parentProcessConnection()->send(Messages::WebPageProxy::CancelNotification(notificationID), page->pageID());
179 UNUSED_PARAM(notification);
184 void WebNotificationManager::clearNotifications(WebCore::ScriptExecutionContext* context, WebPage* page)
186 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
187 NotificationContextMap::iterator it = m_notificationContextMap.find(context);
188 if (it == m_notificationContextMap.end())
191 Vector<uint64_t>& notificationIDs = it->value;
192 m_process->parentProcessConnection()->send(Messages::WebPageProxy::ClearNotifications(notificationIDs), page->pageID());
193 size_t count = notificationIDs.size();
194 for (size_t i = 0; i < count; ++i) {
195 RefPtr<Notification> notification = m_notificationIDMap.take(notificationIDs[i]);
198 notification->finalize();
199 m_notificationMap.remove(notification);
202 m_notificationContextMap.remove(it);
204 UNUSED_PARAM(context);
209 void WebNotificationManager::didDestroyNotification(Notification* notification, WebPage* page)
211 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
212 uint64_t notificationID = m_notificationMap.take(notification);
216 m_notificationIDMap.remove(notificationID);
217 removeNotificationFromContextMap(notificationID, notification);
218 m_process->parentProcessConnection()->send(Messages::WebPageProxy::DidDestroyNotification(notificationID), page->pageID());
220 UNUSED_PARAM(notification);
225 void WebNotificationManager::didShowNotification(uint64_t notificationID)
227 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
228 if (!isNotificationIDValid(notificationID))
231 RefPtr<Notification> notification = m_notificationIDMap.get(notificationID);
235 notification->dispatchShowEvent();
237 UNUSED_PARAM(notificationID);
241 void WebNotificationManager::didClickNotification(uint64_t notificationID)
243 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
244 if (!isNotificationIDValid(notificationID))
247 RefPtr<Notification> notification = m_notificationIDMap.get(notificationID);
251 // Indicate that this event is being dispatched in reaction to a user's interaction with a platform notification.
252 UserGestureIndicator indicator(ProcessingUserGesture);
253 notification->dispatchClickEvent();
255 UNUSED_PARAM(notificationID);
259 void WebNotificationManager::didCloseNotifications(const Vector<uint64_t>& notificationIDs)
261 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
262 size_t count = notificationIDs.size();
263 for (size_t i = 0; i < count; ++i) {
264 uint64_t notificationID = notificationIDs[i];
265 if (!isNotificationIDValid(notificationID))
268 RefPtr<Notification> notification = m_notificationIDMap.take(notificationID);
272 m_notificationMap.remove(notification);
273 removeNotificationFromContextMap(notificationID, notification.get());
275 notification->dispatchCloseEvent();
278 UNUSED_PARAM(notificationIDs);
282 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
283 void WebNotificationManager::removeNotificationFromContextMap(uint64_t notificationID, Notification* notification)
285 // This is a helper function for managing the hash maps.
286 NotificationContextMap::iterator it = m_notificationContextMap.find(notification->scriptExecutionContext());
287 ASSERT(it != m_notificationContextMap.end());
288 size_t index = it->value.find(notificationID);
289 ASSERT(index != notFound);
290 it->value.remove(index);
291 if (it->value.isEmpty())
292 m_notificationContextMap.remove(it);
296 } // namespace WebKit