1/*
2 * Copyright (C) 2010 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 "ImmutableArray.h"
30#include "InjectedBundle.h"
31#include "WKAPICast.h"
32#include "WKBundleAPICast.h"
33#include "WKBundlePrivate.h"
34#include "WebData.h"
35
36using namespace WebKit;
37
38WKTypeID WKBundleGetTypeID()
39{
40    return toAPI(InjectedBundle::APIType);
41}
42
43void WKBundleSetClient(WKBundleRef bundleRef, WKBundleClient * wkClient)
44{
45    toImpl(bundleRef)->initializeClient(wkClient);
46}
47
48void WKBundlePostMessage(WKBundleRef bundleRef, WKStringRef messageNameRef, WKTypeRef messageBodyRef)
49{
50    toImpl(bundleRef)->postMessage(toWTFString(messageNameRef), toImpl(messageBodyRef));
51}
52
53void WKBundlePostSynchronousMessage(WKBundleRef bundleRef, WKStringRef messageNameRef, WKTypeRef messageBodyRef, WKTypeRef* returnDataRef)
54{
55    RefPtr<APIObject> returnData;
56    toImpl(bundleRef)->postSynchronousMessage(toWTFString(messageNameRef), toImpl(messageBodyRef), returnData);
57    if (returnDataRef)
58        *returnDataRef = toAPI(returnData.release().leakRef());
59}
60
61WKConnectionRef WKBundleGetApplicationConnection(WKBundleRef bundleRef)
62{
63    return toAPI(toImpl(bundleRef)->webConnectionToUIProcess());
64}
65
66void WKBundleSetShouldTrackVisitedLinks(WKBundleRef bundleRef, bool shouldTrackVisitedLinks)
67{
68    toImpl(bundleRef)->setShouldTrackVisitedLinks(shouldTrackVisitedLinks);
69}
70
71void WKBundleRemoveAllVisitedLinks(WKBundleRef bundleRef)
72{
73    toImpl(bundleRef)->removeAllVisitedLinks();
74}
75
76void WKBundleActivateMacFontAscentHack(WKBundleRef bundleRef)
77{
78    toImpl(bundleRef)->activateMacFontAscentHack();
79}
80
81void WKBundleSetCacheModel(WKBundleRef bundleRef, uint32_t cacheModel)
82{
83    toImpl(bundleRef)->setCacheModel(cacheModel);
84}
85
86void WKBundleGarbageCollectJavaScriptObjects(WKBundleRef bundleRef)
87{
88    toImpl(bundleRef)->garbageCollectJavaScriptObjects();
89}
90
91void WKBundleGarbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(WKBundleRef bundleRef, bool waitUntilDone)
92{
93    toImpl(bundleRef)->garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(waitUntilDone);
94}
95
96size_t WKBundleGetJavaScriptObjectsCount(WKBundleRef bundleRef)
97{
98    return toImpl(bundleRef)->javaScriptObjectsCount();
99}
100
101void WKBundleSetAlwaysAcceptCookies(WKBundleRef bundleRef, bool accept)
102{
103    toImpl(bundleRef)->setAlwaysAcceptCookies(accept);
104}
105
106void WKBundleAddUserScript(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef, WKStringRef sourceRef, WKURLRef urlRef, WKArrayRef whitelistRef, WKArrayRef blacklistRef, WKUserScriptInjectionTime injectionTimeRef, WKUserContentInjectedFrames injectedFramesRef)
107{
108    toImpl(bundleRef)->addUserScript(toImpl(pageGroupRef), toImpl(scriptWorldRef), toWTFString(sourceRef), toWTFString(urlRef), toImpl(whitelistRef), toImpl(blacklistRef), toUserScriptInjectionTime(injectionTimeRef), toUserContentInjectedFrames(injectedFramesRef));
109}
110
111void WKBundleAddUserStyleSheet(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef, WKStringRef sourceRef, WKURLRef urlRef, WKArrayRef whitelistRef, WKArrayRef blacklistRef, WKUserContentInjectedFrames injectedFramesRef)
112{
113    toImpl(bundleRef)->addUserStyleSheet(toImpl(pageGroupRef), toImpl(scriptWorldRef), toWTFString(sourceRef), toWTFString(urlRef), toImpl(whitelistRef), toImpl(blacklistRef), toUserContentInjectedFrames(injectedFramesRef));
114}
115
116void WKBundleRemoveUserScript(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef, WKURLRef urlRef)
117{
118    toImpl(bundleRef)->removeUserScript(toImpl(pageGroupRef), toImpl(scriptWorldRef), toWTFString(urlRef));
119}
120
121void WKBundleRemoveUserStyleSheet(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef, WKURLRef urlRef)
122{
123    toImpl(bundleRef)->removeUserStyleSheet(toImpl(pageGroupRef), toImpl(scriptWorldRef), toWTFString(urlRef));
124}
125
126void WKBundleRemoveUserScripts(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef)
127{
128    toImpl(bundleRef)->removeUserScripts(toImpl(pageGroupRef), toImpl(scriptWorldRef));
129}
130
131void WKBundleRemoveUserStyleSheets(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKBundleScriptWorldRef scriptWorldRef)
132{
133    toImpl(bundleRef)->removeUserStyleSheets(toImpl(pageGroupRef), toImpl(scriptWorldRef));
134}
135
136void WKBundleRemoveAllUserContent(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef)
137{
138    toImpl(bundleRef)->removeAllUserContent(toImpl(pageGroupRef));
139}
140
141void WKBundleOverrideBoolPreferenceForTestRunner(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKStringRef preference, bool enabled)
142{
143    toImpl(bundleRef)->overrideBoolPreferenceForTestRunner(toImpl(pageGroupRef), toWTFString(preference), enabled);
144}
145
146void WKBundleSetAllowUniversalAccessFromFileURLs(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
147{
148    toImpl(bundleRef)->setAllowUniversalAccessFromFileURLs(toImpl(pageGroupRef), enabled);
149}
150
151void WKBundleSetAllowFileAccessFromFileURLs(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
152{
153    toImpl(bundleRef)->setAllowFileAccessFromFileURLs(toImpl(pageGroupRef), enabled);
154}
155
156void WKBundleSetMinimumLogicalFontSize(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, int size)
157{
158    toImpl(bundleRef)->setMinimumLogicalFontSize(toImpl(pageGroupRef), size);
159}
160
161void WKBundleSetFrameFlatteningEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
162{
163    toImpl(bundleRef)->setFrameFlatteningEnabled(toImpl(pageGroupRef), enabled);
164}
165
166void WKBundleSetPluginsEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
167{
168    toImpl(bundleRef)->setPluginsEnabled(toImpl(pageGroupRef), enabled);
169}
170
171void WKBundleSetJavaScriptCanAccessClipboard(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
172{
173    toImpl(bundleRef)->setJavaScriptCanAccessClipboard(toImpl(pageGroupRef), enabled);
174}
175
176void WKBundleSetPrivateBrowsingEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
177{
178    toImpl(bundleRef)->setPrivateBrowsingEnabled(toImpl(pageGroupRef), enabled);
179}
180
181void WKBundleSetPopupBlockingEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
182{
183    toImpl(bundleRef)->setPopupBlockingEnabled(toImpl(pageGroupRef), enabled);
184}
185
186void WKBundleSwitchNetworkLoaderToNewTestingSession(WKBundleRef bundleRef)
187{
188    toImpl(bundleRef)->switchNetworkLoaderToNewTestingSession();
189}
190
191void WKBundleSetAuthorAndUserStylesEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
192{
193    toImpl(bundleRef)->setAuthorAndUserStylesEnabled(toImpl(pageGroupRef), enabled);
194}
195
196void WKBundleSetSpatialNavigationEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
197{
198    toImpl(bundleRef)->setSpatialNavigationEnabled(toImpl(pageGroupRef), enabled);
199}
200
201void WKBundleAddOriginAccessWhitelistEntry(WKBundleRef bundleRef, WKStringRef sourceOrigin, WKStringRef destinationProtocol, WKStringRef destinationHost, bool allowDestinationSubdomains)
202{
203    toImpl(bundleRef)->addOriginAccessWhitelistEntry(toWTFString(sourceOrigin), toWTFString(destinationProtocol), toWTFString(destinationHost), allowDestinationSubdomains);
204}
205
206void WKBundleRemoveOriginAccessWhitelistEntry(WKBundleRef bundleRef, WKStringRef sourceOrigin, WKStringRef destinationProtocol, WKStringRef destinationHost, bool allowDestinationSubdomains)
207{
208    toImpl(bundleRef)->removeOriginAccessWhitelistEntry(toWTFString(sourceOrigin), toWTFString(destinationProtocol), toWTFString(destinationHost), allowDestinationSubdomains);
209}
210
211void WKBundleResetOriginAccessWhitelists(WKBundleRef bundleRef)
212{
213    toImpl(bundleRef)->resetOriginAccessWhitelists();
214}
215
216void WKBundleSetAsynchronousSpellCheckingEnabled(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, bool enabled)
217{
218    toImpl(bundleRef)->setAsynchronousSpellCheckingEnabled(toImpl(pageGroupRef), enabled);
219}
220
221void WKBundleReportException(JSContextRef context, JSValueRef exception)
222{
223    InjectedBundle::reportException(context, exception);
224}
225
226void WKBundleClearAllDatabases(WKBundleRef bundleRef)
227{
228    toImpl(bundleRef)->clearAllDatabases();
229}
230
231void WKBundleSetDatabaseQuota(WKBundleRef bundleRef, uint64_t quota)
232{
233    toImpl(bundleRef)->setDatabaseQuota(quota);
234}
235
236void WKBundleClearApplicationCache(WKBundleRef bundleRef)
237{
238    toImpl(bundleRef)->clearApplicationCache();
239}
240
241void WKBundleClearApplicationCacheForOrigin(WKBundleRef bundleRef, WKStringRef origin)
242{
243    toImpl(bundleRef)->clearApplicationCacheForOrigin(toWTFString(origin));
244}
245
246void WKBundleSetAppCacheMaximumSize(WKBundleRef bundleRef, uint64_t size)
247{
248    toImpl(bundleRef)->setAppCacheMaximumSize(size);
249}
250
251uint64_t WKBundleGetAppCacheUsageForOrigin(WKBundleRef bundleRef, WKStringRef origin)
252{
253    return toImpl(bundleRef)->appCacheUsageForOrigin(toWTFString(origin));
254}
255
256void WKBundleSetApplicationCacheOriginQuota(WKBundleRef bundleRef, WKStringRef origin, uint64_t bytes)
257{
258    toImpl(bundleRef)->setApplicationCacheOriginQuota(toWTFString(origin), bytes);
259}
260
261void WKBundleResetApplicationCacheOriginQuota(WKBundleRef bundleRef, WKStringRef origin)
262{
263    toImpl(bundleRef)->resetApplicationCacheOriginQuota(toWTFString(origin));
264}
265
266WKArrayRef WKBundleCopyOriginsWithApplicationCache(WKBundleRef bundleRef)
267{
268    RefPtr<ImmutableArray> origins = toImpl(bundleRef)->originsWithApplicationCache();
269    return toAPI(origins.release().leakRef());
270}
271
272WKDataRef WKBundleCreateWKDataFromUInt8Array(WKBundleRef bundle, JSContextRef context, JSValueRef data)
273{
274    RefPtr<WebData> webData = toImpl(bundle)->createWebDataFromUint8Array(context, data);
275    return toAPI(webData.release().leakRef());
276}
277
278int WKBundleNumberOfPages(WKBundleRef bundleRef, WKBundleFrameRef frameRef, double pageWidthInPixels, double pageHeightInPixels)
279{
280    return toImpl(bundleRef)->numberOfPages(toImpl(frameRef), pageWidthInPixels, pageHeightInPixels);
281}
282
283int WKBundlePageNumberForElementById(WKBundleRef bundleRef, WKBundleFrameRef frameRef, WKStringRef idRef, double pageWidthInPixels, double pageHeightInPixels)
284{
285    return toImpl(bundleRef)->pageNumberForElementById(toImpl(frameRef), toWTFString(idRef), pageWidthInPixels, pageHeightInPixels);
286}
287
288WKStringRef WKBundlePageSizeAndMarginsInPixels(WKBundleRef bundleRef, WKBundleFrameRef frameRef, int pageIndex, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
289{
290    return toCopiedAPI(toImpl(bundleRef)->pageSizeAndMarginsInPixels(toImpl(frameRef), pageIndex, width, height, marginTop, marginRight, marginBottom, marginLeft));
291}
292
293bool WKBundleIsPageBoxVisible(WKBundleRef bundleRef, WKBundleFrameRef frameRef, int pageIndex)
294{
295    return toImpl(bundleRef)->isPageBoxVisible(toImpl(frameRef), pageIndex);
296}
297
298bool WKBundleIsProcessingUserGesture(WKBundleRef)
299{
300    return InjectedBundle::isProcessingUserGesture();
301}
302
303void WKBundleSetUserStyleSheetLocation(WKBundleRef bundleRef, WKBundlePageGroupRef pageGroupRef, WKStringRef location)
304{
305    toImpl(bundleRef)->setUserStyleSheetLocation(toImpl(pageGroupRef), toWTFString(location));
306}
307
308void WKBundleSetWebNotificationPermission(WKBundleRef bundleRef, WKBundlePageRef pageRef, WKStringRef originStringRef, bool allowed)
309{
310    toImpl(bundleRef)->setWebNotificationPermission(toImpl(pageRef), toWTFString(originStringRef), allowed);
311}
312
313void WKBundleRemoveAllWebNotificationPermissions(WKBundleRef bundleRef, WKBundlePageRef pageRef)
314{
315    toImpl(bundleRef)->removeAllWebNotificationPermissions(toImpl(pageRef));
316}
317
318uint64_t WKBundleGetWebNotificationID(WKBundleRef bundleRef, JSContextRef context, JSValueRef notification)
319{
320    return toImpl(bundleRef)->webNotificationID(context, notification);
321}
322
323void WKBundleSetTabKeyCyclesThroughElements(WKBundleRef bundleRef, WKBundlePageRef pageRef, bool enabled)
324{
325    toImpl(bundleRef)->setTabKeyCyclesThroughElements(toImpl(pageRef), enabled);
326}
327
328void WKBundleSetSerialLoadingEnabled(WKBundleRef bundleRef, bool enabled)
329{
330    toImpl(bundleRef)->setSerialLoadingEnabled(enabled);
331}
332
333void WKBundleSetShadowDOMEnabled(WKBundleRef bundleRef, bool enabled)
334{
335    toImpl(bundleRef)->setShadowDOMEnabled(enabled);
336}
337
338void WKBundleSetSeamlessIFramesEnabled(WKBundleRef bundleRef, bool enabled)
339{
340    toImpl(bundleRef)->setSeamlessIFramesEnabled(enabled);
341}
342
343void WKBundleDispatchPendingLoadRequests(WKBundleRef bundleRef)
344{
345    toImpl(bundleRef)->dispatchPendingLoadRequests();
346}
347