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 if (m_notificationManager)
69 WKNotificationManagerSetProvider(m_notificationManager.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, WKNotificationRef notification)
89 if (!m_notificationManager)
92 uint64_t id = WKNotificationGetID(notification);
93 ASSERT(!m_shownNotifications.contains(id));
94 m_shownNotifications.add(id);
96 WKNotificationManagerProviderDidShowNotification(m_notificationManager.get(), WKNotificationGetID(notification));
99 void WebNotificationProvider::closeWebNotification(WKNotificationRef notification)
101 if (!m_notificationManager)
104 uint64_t id = WKNotificationGetID(notification);
105 WKRetainPtr<WKUInt64Ref> wkID = WKUInt64Create(id);
106 WKRetainPtr<WKMutableArrayRef> array(AdoptWK, WKMutableArrayCreate());
107 WKArrayAppendItem(array.get(), wkID.get());
108 m_shownNotifications.remove(id);
109 WKNotificationManagerProviderDidCloseNotifications(m_notificationManager.get(), array.get());
112 void WebNotificationProvider::addNotificationManager(WKNotificationManagerRef manager)
114 // We assume there is only one for testing.
115 ASSERT(!m_notificationManager);
116 m_notificationManager = manager;
119 void WebNotificationProvider::removeNotificationManager(WKNotificationManagerRef manager)
121 // We assume there is only one for testing.
122 ASSERT(m_notificationManager);
123 ASSERT(m_notificationManager.get() == manager);
124 m_notificationManager = 0;
127 WKDictionaryRef WebNotificationProvider::notificationPermissions()
129 // Initial permissions are always empty.
130 return WKMutableDictionaryCreate();
133 void WebNotificationProvider::simulateWebNotificationClick(uint64_t notificationID)
135 if (!m_notificationManager)
138 ASSERT(m_shownNotifications.contains(notificationID));
139 WKNotificationManagerProviderDidClickNotification(m_notificationManager.get(), notificationID);
142 void WebNotificationProvider::reset()
144 if (!m_notificationManager) {
145 m_shownNotifications.clear();
149 WKRetainPtr<WKMutableArrayRef> array(AdoptWK, WKMutableArrayCreate());
150 HashSet<uint64_t>::const_iterator itEnd = m_shownNotifications.end();
151 for (HashSet<uint64_t>::const_iterator it = m_shownNotifications.begin(); it != itEnd; ++it) {
152 WKRetainPtr<WKUInt64Ref> wkID = WKUInt64Create(*it);
153 WKArrayAppendItem(array.get(), wkID.get());
156 m_shownNotifications.clear();
157 WKNotificationManagerProviderDidCloseNotifications(m_notificationManager.get(), array.get());