1/*
2 * Copyright (C) 2010-2016 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 "WKBundle.h"
28
29#include "APIArray.h"
30#include "APIData.h"
31#include "InjectedBundle.h"
32#include "InjectedBundleClient.h"
33#include "InjectedBundleScriptWorld.h"
34#include "WKAPICast.h"
35#include "WKBundleAPICast.h"
36#include "WKBundlePrivate.h"
37#include "WKMutableArray.h"
38#include "WKMutableDictionary.h"
39#include "WKNumber.h"
40#include "WKRetainPtr.h"
41#include "WKString.h"
42#include "WebConnection.h"
43#include "WebFrame.h"
44#include "WebPage.h"
45#include "WebPageGroupProxy.h"
46#include <WebCore/DatabaseTracker.h>
47#include <WebCore/MemoryRelease.h>
48#include <WebCore/ResourceLoadObserver.h>
49#include <WebCore/ServiceWorkerThreadProxy.h>
50
51WKTypeID WKBundleGetTypeID()
52{
53 return WebKit::toAPI(WebKit::InjectedBundle::APIType);
54}
55
56void WKBundleSetClient(WKBundleRef bundleRef, WKBundleClientBase *wkClient)
57{
58 WebKit::toImpl(bundleRef)->setClient(std::make_unique<WebKit::InjectedBundleClient>(wkClient));
59}
60
61void WKBundleSetServiceWorkerProxyCreationCallback(WKBundleRef bundleRef, void (*callback)(uint64_t))
62{
63 WebKit::toImpl(bundleRef)->setServiceWorkerProxyCreationCallback(callback);
64}
65
66void WKBundlePostMessage(WKBundleRef bundleRef, WKStringRef messageNameRef, WKTypeRef messageBodyRef)
67{
68 WebKit::toImpl(bundleRef)->postMessage(WebKit::toWTFString(messageNameRef), WebKit::toImpl(messageBodyRef));
69}
70
71void WKBundlePostSynchronousMessage(WKBundleRef bundleRef, WKStringRef messageNameRef, WKTypeRef messageBodyRef, WKTypeRef* returnRetainedDataRef)
72{
73 RefPtr<API::Object> returnData;
74 WebKit::toImpl(bundleRef)->postSynchronousMessage(WebKit::toWTFString(messageNameRef), WebKit::toImpl(messageBodyRef), returnData);
75 if (returnRetainedDataRef)
76 *returnRetainedDataRef = WebKit::toAPI(returnData.leakRef());
77}
78
79WKConnectionRef WKBundleGetApplicationConnection(WKBundleRef bundleRef)
80{
81 return toAPI(WebKit::toImpl(bundleRef)->webConnectionToUIProcess());
82}
83
84void WKBundleGarbageCollectJavaScriptObjects(WKBundleRef bundleRef)
85{
86 WebKit::toImpl(bundleRef)->garbageCollectJavaScriptObjects();
87}
88
89void WKBundleGarbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(WKBundleRef bundleRef, bool waitUntilDone)
90{
91 WebKit::toImpl(bundleRef)->garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(waitUntilDone);
92}
93
94size_t WKBundleGetJavaScriptObjectsCount(WKBundleRef bundleRef)
95{
96 return WebKit::toImpl(bundleRef)->javaScriptObjectsCount();
97}
98
99void WKBundleAddUserScript(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef, WKStringRef sourceRef, WKURLRef urlRef, WKArrayRef whitelistRef, WKArrayRef blacklistRef, _WKUserScriptInjectionTime injectionTimeRef, WKUserContentInjectedFrames injectedFramesRef)
100{
101 WebKit::toImpl(bundleRef)->addUserScript(WebKit::toImpl(pageGroupRef), WebKit::toImpl(scriptWorldRef), WebKit::toWTFString(sourceRef), WebKit::toWTFString(urlRef), WebKit::toImpl(whitelistRef), WebKit::toImpl(blacklistRef), WebKit::toUserScriptInjectionTime(injectionTimeRef), WebKit::toUserContentInjectedFrames(injectedFramesRef));
102}
103
104void WKBundleAddUserStyleSheet(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef, WKStringRef sourceRef, WKURLRef urlRef, WKArrayRef whitelistRef, WKArrayRef blacklistRef, WKUserContentInjectedFrames injectedFramesRef)
105{
106 WebKit::toImpl(bundleRef)->addUserStyleSheet(WebKit::toImpl(pageGroupRef), WebKit::toImpl(scriptWorldRef), WebKit::toWTFString(sourceRef), WebKit::toWTFString(urlRef), WebKit::toImpl(whitelistRef), WebKit::toImpl(blacklistRef), WebKit::toUserContentInjectedFrames(injectedFramesRef));
107}
108
109void WKBundleRemoveUserScript(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef, WKURLRef urlRef)
110{
111 WebKit::toImpl(bundleRef)->removeUserScript(WebKit::toImpl(pageGroupRef), WebKit::toImpl(scriptWorldRef), WebKit::toWTFString(urlRef));
112}
113
114void WKBundleRemoveUserStyleSheet(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef, WKURLRef urlRef)
115{
116 WebKit::toImpl(bundleRef)->removeUserStyleSheet(WebKit::toImpl(pageGroupRef), WebKit::toImpl(scriptWorldRef), WebKit::toWTFString(urlRef));
117}
118
119void WKBundleRemoveUserScripts(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef)
120{
121 WebKit::toImpl(bundleRef)->removeUserScripts(WebKit::toImpl(pageGroupRef), WebKit::toImpl(scriptWorldRef));
122}
123
124void WKBundleRemoveUserStyleSheets(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef)
125{
126 WebKit::toImpl(bundleRef)->removeUserStyleSheets(WebKit::toImpl(pageGroupRef), WebKit::toImpl(scriptWorldRef));
127}
128
129void WKBundleRemoveAllUserContent(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef)
130{
131 WebKit::toImpl(bundleRef)->removeAllUserContent(WebKit::toImpl(pageGroupRef));
132}
133
134void WKBundleOverrideBoolPreferenceForTestRunner(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKStringRef preference, bool enabled)
135{
136 WebKit::toImpl(bundleRef)->overrideBoolPreferenceForTestRunner(WebKit::toImpl(pageGroupRef), WebKit::toWTFString(preference), enabled);
137}
138
139void WKBundleSetAllowUniversalAccessFromFileURLs(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
140{
141 WebKit::toImpl(bundleRef)->setAllowUniversalAccessFromFileURLs(WebKit::toImpl(pageGroupRef), enabled);
142}
143
144void WKBundleSetAllowFileAccessFromFileURLs(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
145{
146 WebKit::toImpl(bundleRef)->setAllowFileAccessFromFileURLs(WebKit::toImpl(pageGroupRef), enabled);
147}
148
149void WKBundleSetAllowStorageAccessFromFileURLS(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool needsQuirk)
150{
151 WebKit::toImpl(bundleRef)->setNeedsStorageAccessFromFileURLsQuirk(WebKit::toImpl(pageGroupRef), needsQuirk);
152}
153
154void WKBundleSetMinimumLogicalFontSize(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, int size)
155{
156 WebKit::toImpl(bundleRef)->setMinimumLogicalFontSize(WebKit::toImpl(pageGroupRef), size);
157}
158
159void WKBundleSetFrameFlatteningEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
160{
161 WebKit::toImpl(bundleRef)->setFrameFlatteningEnabled(WebKit::toImpl(pageGroupRef), enabled);
162}
163
164void WKBundleSetJavaScriptCanAccessClipboard(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
165{
166 WebKit::toImpl(bundleRef)->setJavaScriptCanAccessClipboard(WebKit::toImpl(pageGroupRef), enabled);
167}
168
169void WKBundleSetPrivateBrowsingEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
170{
171 WebKit::toImpl(bundleRef)->setPrivateBrowsingEnabled(WebKit::toImpl(pageGroupRef), enabled);
172}
173
174void WKBundleSetUseDashboardCompatibilityMode(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
175{
176 WebKit::toImpl(bundleRef)->setUseDashboardCompatibilityMode(WebKit::toImpl(pageGroupRef), enabled);
177}
178
179void WKBundleSetPopupBlockingEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
180{
181 WebKit::toImpl(bundleRef)->setPopupBlockingEnabled(WebKit::toImpl(pageGroupRef), enabled);
182}
183
184void WKBundleSetAuthorAndUserStylesEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
185{
186 WebKit::toImpl(bundleRef)->setAuthorAndUserStylesEnabled(WebKit::toImpl(pageGroupRef), enabled);
187}
188
189void WKBundleSetSpatialNavigationEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
190{
191 WebKit::toImpl(bundleRef)->setSpatialNavigationEnabled(WebKit::toImpl(pageGroupRef), enabled);
192}
193
194void WKBundleAddOriginAccessWhitelistEntry(WKBundleRef bundleRef, WKStringRef sourceOrigin, WKStringRef destinationProtocol, WKStringRef destinationHost, bool allowDestinationSubdomains)
195{
196 WebKit::toImpl(bundleRef)->addOriginAccessWhitelistEntry(WebKit::toWTFString(sourceOrigin), WebKit::toWTFString(destinationProtocol), WebKit::toWTFString(destinationHost), allowDestinationSubdomains);
197}
198
199void WKBundleRemoveOriginAccessWhitelistEntry(WKBundleRef bundleRef, WKStringRef sourceOrigin, WKStringRef destinationProtocol, WKStringRef destinationHost, bool allowDestinationSubdomains)
200{
201 WebKit::toImpl(bundleRef)->removeOriginAccessWhitelistEntry(WebKit::toWTFString(sourceOrigin), WebKit::toWTFString(destinationProtocol), WebKit::toWTFString(destinationHost), allowDestinationSubdomains);
202}
203
204void WKBundleResetOriginAccessWhitelists(WKBundleRef bundleRef)
205{
206 WebKit::toImpl(bundleRef)->resetOriginAccessWhitelists();
207}
208
209void WKBundleSetAsynchronousSpellCheckingEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
210{
211 WebKit::toImpl(bundleRef)->setAsynchronousSpellCheckingEnabled(WebKit::toImpl(pageGroupRef), enabled);
212}
213
214WKArrayRef WKBundleGetLiveDocumentURLs(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool excludeDocumentsInPageGroupPages)
215{
216 auto liveDocuments = WebKit::toImpl(bundleRef)->liveDocumentURLs(WebKit::toImpl(pageGroupRef), excludeDocumentsInPageGroupPages);
217
218 auto liveURLs = adoptWK(WKMutableArrayCreate());
219
220 for (const auto& it : liveDocuments) {
221 auto urlInfo = adoptWK(WKMutableDictionaryCreate());
222
223 auto documentIDKey = adoptWK(WKStringCreateWithUTF8CString("id"));
224 auto documentURLKey = adoptWK(WKStringCreateWithUTF8CString("url"));
225
226 auto documentIDValue = adoptWK(WKUInt64Create(it.key));
227 auto documentURLValue = adoptWK(WebKit::toCopiedAPI(it.value));
228
229 WKDictionarySetItem(urlInfo.get(), documentIDKey.get(), documentIDValue.get());
230 WKDictionarySetItem(urlInfo.get(), documentURLKey.get(), documentURLValue.get());
231
232 WKArrayAppendItem(liveURLs.get(), urlInfo.get());
233 }
234
235 return liveURLs.leakRef();
236}
237
238void WKBundleReportException(JSContextRef context, JSValueRef exception)
239{
240 WebKit::InjectedBundle::reportException(context, exception);
241}
242
243void WKBundleClearAllDatabases(WKBundleRef)
244{
245 WebCore::DatabaseTracker::singleton().deleteAllDatabasesImmediately();
246}
247
248void WKBundleSetDatabaseQuota(WKBundleRef bundleRef, uint64_t quota)
249{
250 // Historically, we've used the following (somewhat nonsensical) string for the databaseIdentifier of local files.
251 WebCore::DatabaseTracker::singleton().setQuota(*WebCore::SecurityOriginData::fromDatabaseIdentifier("file__0"), quota);
252}
253
254void WKBundleReleaseMemory(WKBundleRef)
255{
256 WebCore::releaseMemory(WTF::Critical::Yes, WTF::Synchronous::Yes);
257}
258
259WKDataRef WKBundleCreateWKDataFromUInt8Array(WKBundleRef bundle, JSContextRef context, JSValueRef data)
260{
261 return WebKit::toAPI(&WebKit::toImpl(bundle)->createWebDataFromUint8Array(context, data).leakRef());
262}
263
264int WKBundleNumberOfPages(WKBundleRef bundleRef, WKBundleFrameRef frameRef, double pageWidthInPixels, double pageHeightInPixels)
265{
266 return WebKit::toImpl(bundleRef)->numberOfPages(WebKit::toImpl(frameRef), pageWidthInPixels, pageHeightInPixels);
267}
268
269int WKBundlePageNumberForElementById(WKBundleRef bundleRef, WKBundleFrameRef frameRef, WKStringRef idRef, double pageWidthInPixels, double pageHeightInPixels)
270{
271 return WebKit::toImpl(bundleRef)->pageNumberForElementById(WebKit::toImpl(frameRef), WebKit::toWTFString(idRef), pageWidthInPixels, pageHeightInPixels);
272}
273
274WKStringRef WKBundlePageSizeAndMarginsInPixels(WKBundleRef bundleRef, WKBundleFrameRef frameRef, int pageIndex, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
275{
276 return WebKit::toCopiedAPI(WebKit::toImpl(bundleRef)->pageSizeAndMarginsInPixels(WebKit::toImpl(frameRef), pageIndex, width, height, marginTop, marginRight, marginBottom, marginLeft));
277}
278
279bool WKBundleIsPageBoxVisible(WKBundleRef bundleRef, WKBundleFrameRef frameRef, int pageIndex)
280{
281 return WebKit::toImpl(bundleRef)->isPageBoxVisible(WebKit::toImpl(frameRef), pageIndex);
282}
283
284bool WKBundleIsProcessingUserGesture(WKBundleRef)
285{
286 return WebKit::InjectedBundle::isProcessingUserGesture();
287}
288
289void WKBundleSetUserStyleSheetLocation(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKStringRef location)
290{
291 WebKit::toImpl(bundleRef)->setUserStyleSheetLocation(WebKit::toImpl(pageGroupRef), WebKit::toWTFString(location));
292}
293
294void WKBundleSetWebNotificationPermission(WKBundleRef bundleRef, WKBundlePageRef pageRef, WKStringRef originStringRef, bool allowed)
295{
296 WebKit::toImpl(bundleRef)->setWebNotificationPermission(WebKit::toImpl(pageRef), WebKit::toWTFString(originStringRef), allowed);
297}
298
299void WKBundleRemoveAllWebNotificationPermissions(WKBundleRef bundleRef, WKBundlePageRef pageRef)
300{
301 WebKit::toImpl(bundleRef)->removeAllWebNotificationPermissions(WebKit::toImpl(pageRef));
302}
303
304uint64_t WKBundleGetWebNotificationID(WKBundleRef bundleRef, JSContextRef context, JSValueRef notification)
305{
306 return WebKit::toImpl(bundleRef)->webNotificationID(context, notification);
307}
308
309void WKBundleSetTabKeyCyclesThroughElements(WKBundleRef bundleRef, WKBundlePageRef pageRef, bool enabled)
310{
311 WebKit::toImpl(bundleRef)->setTabKeyCyclesThroughElements(WebKit::toImpl(pageRef), enabled);
312}
313
314void WKBundleClearResourceLoadStatistics(WKBundleRef)
315{
316 WebCore::ResourceLoadObserver::shared().clearState();
317}
318
319void WKBundleResourceLoadStatisticsNotifyObserver(WKBundleRef)
320{
321 WebCore::ResourceLoadObserver::shared().updateCentralStatisticsStore();
322}
323
324
325void WKBundleExtendClassesForParameterCoder(WKBundleRef bundle, WKArrayRef classes)
326{
327#if PLATFORM(COCOA)
328 auto classList = WebKit::toImpl(classes);
329 if (!classList)
330 return;
331
332 WebKit::toImpl(bundle)->extendClassesForParameterCoder(*classList);
333#endif
334}
335