2 * Copyright (C) 2012 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 "WebNotificationProvider.h"
29 #include <WebKit/WKMutableArray.h>
30 #include <WebKit/WKNotification.h>
31 #include <WebKit/WKNumber.h>
32 #include <WebKit/WKSecurityOriginRef.h>
33 #include <wtf/Assertions.h>
37 static void showWebNotification(WKPageRef page, WKNotificationRef notification, const void* clientInfo)
39 static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->showWebNotification(page, notification);
42 static void closeWebNotification(WKNotificationRef notification, const void* clientInfo)
44 static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->closeWebNotification(notification);
47 static void addNotificationManager(WKNotificationManagerRef manager, const void* clientInfo)
49 static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->addNotificationManager(manager);
52 static void removeNotificationManager(WKNotificationManagerRef manager, const void* clientInfo)
54 static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->removeNotificationManager(manager);
57 static WKDictionaryRef notificationPermissions(const void* clientInfo)
59 return static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->notificationPermissions();
62 WebNotificationProvider::WebNotificationProvider()
66 WebNotificationProvider::~WebNotificationProvider()
68 for (auto& manager : m_ownedNotifications)
69 WKNotificationManagerSetProvider(manager.key.get(), nullptr);
72 WKNotificationProviderV0 WebNotificationProvider::provider()
74 WKNotificationProviderV0 notificationProvider = {
76 WTR::showWebNotification,
77 WTR::closeWebNotification,
78 0, // didDestroyNotification
79 WTR::addNotificationManager,
80 WTR::removeNotificationManager,
81 WTR::notificationPermissions,
82 0, // clearNotifications
84 return notificationProvider;
87 void WebNotificationProvider::showWebNotification(WKPageRef page, WKNotificationRef notification)
89 auto context = WKPageGetContext(page);
90 auto notificationManager = WKContextGetNotificationManager(context);
91 uint64_t id = WKNotificationGetID(notification);
93 ASSERT(m_ownedNotifications.contains(notificationManager));
94 auto addResult = m_ownedNotifications.find(notificationManager)->value.add(id);
95 ASSERT_UNUSED(addResult, addResult.isNewEntry);
96 auto addResult2 = m_owningManager.set(id, notificationManager);
97 ASSERT_UNUSED(addResult2, addResult2.isNewEntry);
99 WKNotificationManagerProviderDidShowNotification(notificationManager, id);
102 void WebNotificationProvider::closeWebNotification(WKNotificationRef notification)
104 uint64_t id = WKNotificationGetID(notification);
105 ASSERT(m_owningManager.contains(id));
106 auto notificationManager = m_owningManager.get(id);
108 ASSERT(m_ownedNotifications.contains(notificationManager));
109 bool success = m_ownedNotifications.find(notificationManager)->value.remove(id);
110 ASSERT_UNUSED(success, success);
111 m_owningManager.remove(id);
113 WKRetainPtr<WKUInt64Ref> wkID = WKUInt64Create(id);
114 WKRetainPtr<WKMutableArrayRef> array(AdoptWK, WKMutableArrayCreate());
115 WKArrayAppendItem(array.get(), wkID.get());
116 WKNotificationManagerProviderDidCloseNotifications(notificationManager, array.get());
119 void WebNotificationProvider::addNotificationManager(WKNotificationManagerRef manager)
121 m_ownedNotifications.add(manager, HashSet<uint64_t>());
124 void WebNotificationProvider::removeNotificationManager(WKNotificationManagerRef manager)
126 auto iterator = m_ownedNotifications.find(manager);
127 ASSERT(iterator != m_ownedNotifications.end());
128 auto toRemove = iterator->value;
129 WKRetainPtr<WKNotificationManagerRef> guard(manager);
130 m_ownedNotifications.remove(iterator);
131 WKRetainPtr<WKMutableArrayRef> array = adoptWK(WKMutableArrayCreate());
132 for (uint64_t notificationID : toRemove) {
133 bool success = m_owningManager.remove(notificationID);
134 ASSERT_UNUSED(success, success);
135 WKArrayAppendItem(array.get(), adoptWK(WKUInt64Create(notificationID)).get());
137 WKNotificationManagerProviderDidCloseNotifications(manager, array.get());
140 WKDictionaryRef WebNotificationProvider::notificationPermissions()
142 // Initial permissions are always empty.
143 return WKMutableDictionaryCreate();
146 void WebNotificationProvider::simulateWebNotificationClick(uint64_t notificationID)
148 ASSERT(m_owningManager.contains(notificationID));
149 WKNotificationManagerProviderDidClickNotification(m_owningManager.get(notificationID), notificationID);
152 void WebNotificationProvider::reset()
154 for (auto& notificationPair : m_ownedNotifications) {
155 if (notificationPair.value.isEmpty())
157 WKRetainPtr<WKMutableArrayRef> array = adoptWK(WKMutableArrayCreate());
158 for (uint64_t notificationID : notificationPair.value)
159 WKArrayAppendItem(array.get(), adoptWK(WKUInt64Create(notificationID)).get());
161 notificationPair.value.clear();
162 WKNotificationManagerProviderDidCloseNotifications(notificationPair.key.get(), array.get());
164 m_owningManager.clear();