[WKTR] Allow changing the WKContextConfiguration between successive tests
[WebKit-https.git] / Tools / WebKitTestRunner / WebNotificationProvider.cpp
1 /*
2  * Copyright (C) 2012 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "WebNotificationProvider.h"
28
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>
34
35 namespace WTR {
36
37 static void showWebNotification(WKPageRef page, WKNotificationRef notification, const void* clientInfo)
38 {
39     static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->showWebNotification(page, notification);
40 }
41
42 static void closeWebNotification(WKNotificationRef notification, const void* clientInfo)
43 {
44     static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->closeWebNotification(notification);
45 }
46
47 static void addNotificationManager(WKNotificationManagerRef manager, const void* clientInfo)
48 {
49     static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->addNotificationManager(manager);
50 }
51
52 static void removeNotificationManager(WKNotificationManagerRef manager, const void* clientInfo)
53 {
54     static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->removeNotificationManager(manager);
55 }
56
57 static WKDictionaryRef notificationPermissions(const void* clientInfo)
58 {
59     return static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->notificationPermissions();
60 }
61
62 WebNotificationProvider::WebNotificationProvider()
63 {
64 }
65
66 WebNotificationProvider::~WebNotificationProvider()
67 {
68     if (m_currentNotificationManager)
69         WKNotificationManagerSetProvider(m_currentNotificationManager.get(), nullptr);
70 }
71
72 WKNotificationProviderV0 WebNotificationProvider::provider()
73 {
74     WKNotificationProviderV0 notificationProvider = {
75         { 0, this },
76         WTR::showWebNotification,
77         WTR::closeWebNotification,
78         0, // didDestroyNotification
79         WTR::addNotificationManager,
80         WTR::removeNotificationManager,
81         WTR::notificationPermissions,
82         0, // clearNotifications
83     };
84     return notificationProvider;
85 }
86
87 void WebNotificationProvider::showWebNotification(WKPageRef, WKNotificationRef notification)
88 {
89     if (!m_currentNotificationManager)
90         return;
91
92     uint64_t id = WKNotificationGetID(notification);
93     ASSERT(!m_shownNotifications.contains(id));
94     m_shownNotifications.add(id);
95
96     WKNotificationManagerProviderDidShowNotification(m_currentNotificationManager.get(), WKNotificationGetID(notification));
97 }
98
99 void WebNotificationProvider::closeWebNotification(WKNotificationRef notification)
100 {
101     if (!m_currentNotificationManager)
102         return;
103
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_currentNotificationManager.get(), array.get());
110 }
111
112 void WebNotificationProvider::addNotificationManager(WKNotificationManagerRef manager)
113 {
114     m_currentNotificationManager = manager;
115 }
116
117 void WebNotificationProvider::removeNotificationManager(WKNotificationManagerRef manager)
118 {
119 }
120
121 WKDictionaryRef WebNotificationProvider::notificationPermissions()
122 {
123     // Initial permissions are always empty.
124     return WKMutableDictionaryCreate();
125 }
126
127 void WebNotificationProvider::simulateWebNotificationClick(uint64_t notificationID)
128 {
129     if (!m_currentNotificationManager)
130         return;
131
132     ASSERT(m_shownNotifications.contains(notificationID));
133     WKNotificationManagerProviderDidClickNotification(m_currentNotificationManager.get(), notificationID);
134 }
135
136 void WebNotificationProvider::reset()
137 {
138     if (!m_currentNotificationManager) {
139         m_shownNotifications.clear();
140         return;
141     }
142
143     WKRetainPtr<WKMutableArrayRef> array(AdoptWK, WKMutableArrayCreate());
144     HashSet<uint64_t>::const_iterator itEnd = m_shownNotifications.end();
145     for (HashSet<uint64_t>::const_iterator it = m_shownNotifications.begin(); it != itEnd; ++it) {
146         WKRetainPtr<WKUInt64Ref> wkID = WKUInt64Create(*it);
147         WKArrayAppendItem(array.get(), wkID.get());
148     }
149
150     m_shownNotifications.clear();
151     WKNotificationManagerProviderDidCloseNotifications(m_currentNotificationManager.get(), array.get());
152 }
153
154 } // namespace WTR