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 WKBundleSetPopupBlockingEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
175{
176 WebKit::toImpl(bundleRef)->setPopupBlockingEnabled(WebKit::toImpl(pageGroupRef), enabled);
177}
178
179void WKBundleSetAuthorAndUserStylesEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
180{
181 WebKit::toImpl(bundleRef)->setAuthorAndUserStylesEnabled(WebKit::toImpl(pageGroupRef), enabled);
182}
183
184void WKBundleSetSpatialNavigationEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
185{
186 WebKit::toImpl(bundleRef)->setSpatialNavigationEnabled(WebKit::toImpl(pageGroupRef), enabled);
187}
188
189void WKBundleAddOriginAccessWhitelistEntry(WKBundleRef bundleRef, WKStringRef sourceOrigin, WKStringRef destinationProtocol, WKStringRef destinationHost, bool allowDestinationSubdomains)
190{
191 WebKit::toImpl(bundleRef)->addOriginAccessWhitelistEntry(WebKit::toWTFString(sourceOrigin), WebKit::toWTFString(destinationProtocol), WebKit::toWTFString(destinationHost), allowDestinationSubdomains);
192}
193
194void WKBundleRemoveOriginAccessWhitelistEntry(WKBundleRef bundleRef, WKStringRef sourceOrigin, WKStringRef destinationProtocol, WKStringRef destinationHost, bool allowDestinationSubdomains)
195{
196 WebKit::toImpl(bundleRef)->removeOriginAccessWhitelistEntry(WebKit::toWTFString(sourceOrigin), WebKit::toWTFString(destinationProtocol), WebKit::toWTFString(destinationHost), allowDestinationSubdomains);
197}
198
199void WKBundleResetOriginAccessWhitelists(WKBundleRef bundleRef)
200{
201 WebKit::toImpl(bundleRef)->resetOriginAccessWhitelists();
202}
203
204void WKBundleSetAsynchronousSpellCheckingEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
205{
206 WebKit::toImpl(bundleRef)->setAsynchronousSpellCheckingEnabled(WebKit::toImpl(pageGroupRef), enabled);
207}
208
209WKArrayRef WKBundleGetLiveDocumentURLs(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool excludeDocumentsInPageGroupPages)
210{
211 auto liveDocuments = WebKit::toImpl(bundleRef)->liveDocumentURLs(WebKit::toImpl(pageGroupRef), excludeDocumentsInPageGroupPages);
212
213 auto liveURLs = adoptWK(WKMutableArrayCreate());
214
215 for (const auto& it : liveDocuments) {
216 auto urlInfo = adoptWK(WKMutableDictionaryCreate());
217
218 auto documentIDKey = adoptWK(WKStringCreateWithUTF8CString("id"));
219 auto documentURLKey = adoptWK(WKStringCreateWithUTF8CString("url"));
220
221 auto documentIDValue = adoptWK(WKUInt64Create(it.key));
222 auto documentURLValue = adoptWK(WebKit::toCopiedAPI(it.value));
223
224 WKDictionarySetItem(urlInfo.get(), documentIDKey.get(), documentIDValue.get());
225 WKDictionarySetItem(urlInfo.get(), documentURLKey.get(), documentURLValue.get());
226
227 WKArrayAppendItem(liveURLs.get(), urlInfo.get());
228 }
229
230 return liveURLs.leakRef();
231}
232
233void WKBundleReportException(JSContextRef context, JSValueRef exception)
234{
235 WebKit::InjectedBundle::reportException(context, exception);
236}
237
238void WKBundleClearAllDatabases(WKBundleRef)
239{
240 WebCore::DatabaseTracker::singleton().deleteAllDatabasesImmediately();
241}
242
243void WKBundleSetDatabaseQuota(WKBundleRef bundleRef, uint64_t quota)
244{
245 // Historically, we've used the following (somewhat nonsensical) string for the databaseIdentifier of local files.
246 WebCore::DatabaseTracker::singleton().setQuota(*WebCore::SecurityOriginData::fromDatabaseIdentifier("file__0"), quota);
247}
248
249void WKBundleReleaseMemory(WKBundleRef)
250{
251 WebCore::releaseMemory(WTF::Critical::Yes, WTF::Synchronous::Yes);
252}
253
254WKDataRef WKBundleCreateWKDataFromUInt8Array(WKBundleRef bundle, JSContextRef context, JSValueRef data)
255{
256 return WebKit::toAPI(&WebKit::toImpl(bundle)->createWebDataFromUint8Array(context, data).leakRef());
257}
258
259int WKBundleNumberOfPages(WKBundleRef bundleRef, WKBundleFrameRef frameRef, double pageWidthInPixels, double pageHeightInPixels)
260{
261 return WebKit::toImpl(bundleRef)->numberOfPages(WebKit::toImpl(frameRef), pageWidthInPixels, pageHeightInPixels);
262}
263
264int WKBundlePageNumberForElementById(WKBundleRef bundleRef, WKBundleFrameRef frameRef, WKStringRef idRef, double pageWidthInPixels, double pageHeightInPixels)
265{
266 return WebKit::toImpl(bundleRef)->pageNumberForElementById(WebKit::toImpl(frameRef), WebKit::toWTFString(idRef), pageWidthInPixels, pageHeightInPixels);
267}
268
269WKStringRef WKBundlePageSizeAndMarginsInPixels(WKBundleRef bundleRef, WKBundleFrameRef frameRef, int pageIndex, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
270{
271 return WebKit::toCopiedAPI(WebKit::toImpl(bundleRef)->pageSizeAndMarginsInPixels(WebKit::toImpl(frameRef), pageIndex, width, height, marginTop, marginRight, marginBottom, marginLeft));
272}
273
274bool WKBundleIsPageBoxVisible(WKBundleRef bundleRef, WKBundleFrameRef frameRef, int pageIndex)
275{
276 return WebKit::toImpl(bundleRef)->isPageBoxVisible(WebKit::toImpl(frameRef), pageIndex);
277}
278
279bool WKBundleIsProcessingUserGesture(WKBundleRef)
280{
281 return WebKit::InjectedBundle::isProcessingUserGesture();
282}
283
284void WKBundleSetUserStyleSheetLocation(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKStringRef location)
285{
286 WebKit::toImpl(bundleRef)->setUserStyleSheetLocation(WebKit::toImpl(pageGroupRef), WebKit::toWTFString(location));
287}
288
289void WKBundleSetWebNotificationPermission(WKBundleRef bundleRef, WKBundlePageRef pageRef, WKStringRef originStringRef, bool allowed)
290{
291 WebKit::toImpl(bundleRef)->setWebNotificationPermission(WebKit::toImpl(pageRef), WebKit::toWTFString(originStringRef), allowed);
292}
293
294void WKBundleRemoveAllWebNotificationPermissions(WKBundleRef bundleRef, WKBundlePageRef pageRef)
295{
296 WebKit::toImpl(bundleRef)->removeAllWebNotificationPermissions(WebKit::toImpl(pageRef));
297}
298
299uint64_t WKBundleGetWebNotificationID(WKBundleRef bundleRef, JSContextRef context, JSValueRef notification)
300{
301 return WebKit::toImpl(bundleRef)->webNotificationID(context, notification);
302}
303
304void WKBundleSetTabKeyCyclesThroughElements(WKBundleRef bundleRef, WKBundlePageRef pageRef, bool enabled)
305{
306 WebKit::toImpl(bundleRef)->setTabKeyCyclesThroughElements(WebKit::toImpl(pageRef), enabled);
307}
308
309void WKBundleClearResourceLoadStatistics(WKBundleRef)
310{
311 WebCore::ResourceLoadObserver::shared().clearState();
312}
313
314void WKBundleResourceLoadStatisticsNotifyObserver(WKBundleRef)
315{
316 WebCore::ResourceLoadObserver::shared().updateCentralStatisticsStore();
317}
318
319
320void WKBundleExtendClassesForParameterCoder(WKBundleRef bundle, WKArrayRef classes)
321{
322#if PLATFORM(COCOA)
323 auto classList = WebKit::toImpl(classes);
324 if (!classList)
325 return;
326
327 WebKit::toImpl(bundle)->extendClassesForParameterCoder(*classList);
328#endif
329}
330