1/*
2 * Copyright (C) 2010, 2011, 2013, 2015 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 "WKBundlePage.h"
28#include "WKBundlePagePrivate.h"
29
30#include "APIArray.h"
31#include "APIFrameHandle.h"
32#include "APIString.h"
33#include "APIURL.h"
34#include "APIURLRequest.h"
35#include "InjectedBundleNodeHandle.h"
36#include "InjectedBundlePageEditorClient.h"
37#include "InjectedBundlePageFormClient.h"
38#include "InjectedBundlePageLoaderClient.h"
39#include "InjectedBundlePageResourceLoadClient.h"
40#include "InjectedBundlePageUIClient.h"
41#include "PageBanner.h"
42#include "WKAPICast.h"
43#include "WKArray.h"
44#include "WKBundleAPICast.h"
45#include "WKRetainPtr.h"
46#include "WKString.h"
47#include "WebContextMenu.h"
48#include "WebContextMenuItem.h"
49#include "WebFrame.h"
50#include "WebFullScreenManager.h"
51#include "WebImage.h"
52#include "WebInspector.h"
53#include "WebPage.h"
54#include "WebPageGroupProxy.h"
55#include "WebPageOverlay.h"
56#include "WebRenderLayer.h"
57#include "WebRenderObject.h"
58#include <WebCore/AXObjectCache.h>
59#include <WebCore/AccessibilityObject.h>
60#include <WebCore/ApplicationCacheStorage.h>
61#include <WebCore/Frame.h>
62#include <WebCore/Page.h>
63#include <WebCore/PageOverlay.h>
64#include <WebCore/PageOverlayController.h>
65#include <WebCore/RenderLayerCompositor.h>
66#include <WebCore/ScriptExecutionContext.h>
67#include <WebCore/SecurityOriginData.h>
68#include <WebCore/WheelEventTestTrigger.h>
69#include <wtf/StdLibExtras.h>
70#include <wtf/URL.h>
71
72WKTypeID WKBundlePageGetTypeID()
73{
74 return WebKit::toAPI(WebKit::WebPage::APIType);
75}
76
77void WKBundlePageSetContextMenuClient(WKBundlePageRef pageRef, WKBundlePageContextMenuClientBase* wkClient)
78{
79#if ENABLE(CONTEXT_MENUS)
80 WebKit::toImpl(pageRef)->setInjectedBundleContextMenuClient(std::make_unique<WebKit::InjectedBundlePageContextMenuClient>(wkClient));
81#else
82 UNUSED_PARAM(pageRef);
83 UNUSED_PARAM(wkClient);
84#endif
85}
86
87void WKBundlePageSetEditorClient(WKBundlePageRef pageRef, WKBundlePageEditorClientBase* wkClient)
88{
89 WebKit::toImpl(pageRef)->setInjectedBundleEditorClient(wkClient ? std::make_unique<WebKit::InjectedBundlePageEditorClient>(*wkClient) : std::make_unique<API::InjectedBundle::EditorClient>());
90}
91
92void WKBundlePageSetFormClient(WKBundlePageRef pageRef, WKBundlePageFormClientBase* wkClient)
93{
94 WebKit::toImpl(pageRef)->setInjectedBundleFormClient(std::make_unique<WebKit::InjectedBundlePageFormClient>(wkClient));
95}
96
97void WKBundlePageSetPageLoaderClient(WKBundlePageRef pageRef, WKBundlePageLoaderClientBase* wkClient)
98{
99 WebKit::toImpl(pageRef)->setInjectedBundlePageLoaderClient(std::make_unique<WebKit::InjectedBundlePageLoaderClient>(wkClient));
100}
101
102void WKBundlePageSetResourceLoadClient(WKBundlePageRef pageRef, WKBundlePageResourceLoadClientBase* wkClient)
103{
104 WebKit::toImpl(pageRef)->setInjectedBundleResourceLoadClient(std::make_unique<WebKit::InjectedBundlePageResourceLoadClient>(wkClient));
105}
106
107void WKBundlePageSetPolicyClient(WKBundlePageRef pageRef, WKBundlePagePolicyClientBase* wkClient)
108{
109 WebKit::toImpl(pageRef)->initializeInjectedBundlePolicyClient(wkClient);
110}
111
112void WKBundlePageSetUIClient(WKBundlePageRef pageRef, WKBundlePageUIClientBase* wkClient)
113{
114 WebKit::toImpl(pageRef)->setInjectedBundleUIClient(std::make_unique<WebKit::InjectedBundlePageUIClient>(wkClient));
115}
116
117void WKBundlePageSetFullScreenClient(WKBundlePageRef pageRef, WKBundlePageFullScreenClientBase* wkClient)
118{
119#if defined(ENABLE_FULLSCREEN_API) && ENABLE_FULLSCREEN_API
120 WebKit::toImpl(pageRef)->initializeInjectedBundleFullScreenClient(wkClient);
121#else
122 UNUSED_PARAM(pageRef);
123 UNUSED_PARAM(wkClient);
124#endif
125}
126
127void WKBundlePageWillEnterFullScreen(WKBundlePageRef pageRef)
128{
129#if defined(ENABLE_FULLSCREEN_API) && ENABLE_FULLSCREEN_API
130 WebKit::toImpl(pageRef)->fullScreenManager()->willEnterFullScreen();
131#else
132 UNUSED_PARAM(pageRef);
133#endif
134}
135
136void WKBundlePageDidEnterFullScreen(WKBundlePageRef pageRef)
137{
138#if defined(ENABLE_FULLSCREEN_API) && ENABLE_FULLSCREEN_API
139 WebKit::toImpl(pageRef)->fullScreenManager()->didEnterFullScreen();
140#else
141 UNUSED_PARAM(pageRef);
142#endif
143}
144
145void WKBundlePageWillExitFullScreen(WKBundlePageRef pageRef)
146{
147#if defined(ENABLE_FULLSCREEN_API) && ENABLE_FULLSCREEN_API
148 WebKit::toImpl(pageRef)->fullScreenManager()->willExitFullScreen();
149#else
150 UNUSED_PARAM(pageRef);
151#endif
152}
153
154void WKBundlePageDidExitFullScreen(WKBundlePageRef pageRef)
155{
156#if defined(ENABLE_FULLSCREEN_API) && ENABLE_FULLSCREEN_API
157 WebKit::toImpl(pageRef)->fullScreenManager()->didExitFullScreen();
158#else
159 UNUSED_PARAM(pageRef);
160#endif
161}
162
163WKBundlePageGroupRef WKBundlePageGetPageGroup(WKBundlePageRef pageRef)
164{
165 return toAPI(WebKit::toImpl(pageRef)->pageGroup());
166}
167
168WKBundleFrameRef WKBundlePageGetMainFrame(WKBundlePageRef pageRef)
169{
170 return toAPI(WebKit::toImpl(pageRef)->mainWebFrame());
171}
172
173WKFrameHandleRef WKBundleFrameCreateFrameHandle(WKBundleFrameRef bundleFrameRef)
174{
175 return WebKit::toAPI(&API::FrameHandle::create(WebKit::toImpl(bundleFrameRef)->frameID()).leakRef());
176}
177
178void WKBundlePageClickMenuItem(WKBundlePageRef pageRef, WKContextMenuItemRef item)
179{
180#if ENABLE(CONTEXT_MENUS)
181 WebKit::toImpl(pageRef)->contextMenu()->itemSelected(WebKit::toImpl(item)->data());
182#else
183 UNUSED_PARAM(pageRef);
184 UNUSED_PARAM(item);
185#endif
186}
187
188#if ENABLE(CONTEXT_MENUS)
189static Ref<API::Array> contextMenuItems(const WebKit::WebContextMenu& contextMenu)
190{
191 auto items = contextMenu.items();
192
193 Vector<RefPtr<API::Object>> menuItems;
194 menuItems.reserveInitialCapacity(items.size());
195
196 for (const auto& item : items)
197 menuItems.uncheckedAppend(WebKit::WebContextMenuItem::create(item));
198
199 return API::Array::create(WTFMove(menuItems));
200}
201#endif
202
203WKArrayRef WKBundlePageCopyContextMenuItems(WKBundlePageRef pageRef)
204{
205#if ENABLE(CONTEXT_MENUS)
206 WebKit::WebContextMenu* contextMenu = WebKit::toImpl(pageRef)->contextMenu();
207
208 return WebKit::toAPI(&contextMenuItems(*contextMenu).leakRef());
209#else
210 UNUSED_PARAM(pageRef);
211 return nullptr;
212#endif
213}
214
215WKArrayRef WKBundlePageCopyContextMenuAtPointInWindow(WKBundlePageRef pageRef, WKPoint point)
216{
217#if ENABLE(CONTEXT_MENUS)
218 WebKit::WebContextMenu* contextMenu = WebKit::toImpl(pageRef)->contextMenuAtPointInWindow(WebKit::toIntPoint(point));
219 if (!contextMenu)
220 return nullptr;
221
222 return WebKit::toAPI(&contextMenuItems(*contextMenu).leakRef());
223#else
224 UNUSED_PARAM(pageRef);
225 UNUSED_PARAM(point);
226 return nullptr;
227#endif
228}
229
230void WKBundlePageInsertNewlineInQuotedContent(WKBundlePageRef pageRef)
231{
232 WebKit::toImpl(pageRef)->insertNewlineInQuotedContent();
233}
234
235void* WKAccessibilityRootObject(WKBundlePageRef pageRef)
236{
237#if HAVE(ACCESSIBILITY)
238 if (!pageRef)
239 return 0;
240
241 WebCore::Page* page = WebKit::toImpl(pageRef)->corePage();
242 if (!page)
243 return 0;
244
245 WebCore::Frame& core = page->mainFrame();
246 if (!core.document())
247 return 0;
248
249 WebCore::AXObjectCache::enableAccessibility();
250
251 WebCore::AccessibilityObject* root = core.document()->axObjectCache()->rootObject();
252 if (!root)
253 return 0;
254
255 return root->wrapper();
256#else
257 UNUSED_PARAM(pageRef);
258 return 0;
259#endif
260}
261
262void* WKAccessibilityFocusedObject(WKBundlePageRef pageRef)
263{
264#if HAVE(ACCESSIBILITY)
265 if (!pageRef)
266 return 0;
267
268 WebCore::Page* page = WebKit::toImpl(pageRef)->corePage();
269 if (!page)
270 return 0;
271
272 WebCore::AXObjectCache::enableAccessibility();
273
274 WebCore::AccessibilityObject* focusedObject = WebCore::AXObjectCache::focusedUIElementForPage(page);
275 if (!focusedObject)
276 return 0;
277
278 return focusedObject->wrapper();
279#else
280 UNUSED_PARAM(pageRef);
281 return 0;
282#endif
283}
284
285void WKAccessibilityEnableEnhancedAccessibility(bool enable)
286{
287#if HAVE(ACCESSIBILITY)
288 WebCore::AXObjectCache::setEnhancedUserInterfaceAccessibility(enable);
289#endif
290}
291
292bool WKAccessibilityEnhancedAccessibilityEnabled()
293{
294#if HAVE(ACCESSIBILITY)
295 return WebCore::AXObjectCache::accessibilityEnhancedUserInterfaceEnabled();
296#else
297 return false;
298#endif
299}
300
301void WKBundlePageStopLoading(WKBundlePageRef pageRef)
302{
303 WebKit::toImpl(pageRef)->stopLoading();
304}
305
306void WKBundlePageSetDefersLoading(WKBundlePageRef, bool)
307{
308}
309
310WKStringRef WKBundlePageCopyRenderTreeExternalRepresentation(WKBundlePageRef pageRef, RenderTreeExternalRepresentationBehavior options)
311{
312 // Convert to webcore options.
313 return WebKit::toCopiedAPI(WebKit::toImpl(pageRef)->renderTreeExternalRepresentation(options));
314}
315
316WKStringRef WKBundlePageCopyRenderTreeExternalRepresentationForPrinting(WKBundlePageRef pageRef)
317{
318 return WebKit::toCopiedAPI(WebKit::toImpl(pageRef)->renderTreeExternalRepresentationForPrinting());
319}
320
321void WKBundlePageExecuteEditingCommand(WKBundlePageRef pageRef, WKStringRef name, WKStringRef argument)
322{
323 WebKit::toImpl(pageRef)->executeEditingCommand(WebKit::toWTFString(name), WebKit::toWTFString(argument));
324}
325
326bool WKBundlePageIsEditingCommandEnabled(WKBundlePageRef pageRef, WKStringRef name)
327{
328 return WebKit::toImpl(pageRef)->isEditingCommandEnabled(WebKit::toWTFString(name));
329}
330
331void WKBundlePageClearMainFrameName(WKBundlePageRef pageRef)
332{
333 WebKit::toImpl(pageRef)->clearMainFrameName();
334}
335
336void WKBundlePageClose(WKBundlePageRef pageRef)
337{
338 WebKit::toImpl(pageRef)->sendClose();
339}
340
341double WKBundlePageGetTextZoomFactor(WKBundlePageRef pageRef)
342{
343 return WebKit::toImpl(pageRef)->textZoomFactor();
344}
345
346void WKBundlePageSetTextZoomFactor(WKBundlePageRef pageRef, double zoomFactor)
347{
348 WebKit::toImpl(pageRef)->setTextZoomFactor(zoomFactor);
349}
350
351double WKBundlePageGetPageZoomFactor(WKBundlePageRef pageRef)
352{
353 return WebKit::toImpl(pageRef)->pageZoomFactor();
354}
355
356void WKBundlePageSetPageZoomFactor(WKBundlePageRef pageRef, double zoomFactor)
357{
358 WebKit::toImpl(pageRef)->setPageZoomFactor(zoomFactor);
359}
360
361void WKBundlePageSetScaleAtOrigin(WKBundlePageRef pageRef, double scale, WKPoint origin)
362{
363 WebKit::toImpl(pageRef)->scalePage(scale, WebKit::toIntPoint(origin));
364}
365
366WKStringRef WKBundlePageDumpHistoryForTesting(WKBundlePageRef page, WKStringRef directory)
367{
368 return WebKit::toCopiedAPI(WebKit::toImpl(page)->dumpHistoryForTesting(WebKit::toWTFString(directory)));
369}
370
371void WKBundleClearHistoryForTesting(WKBundlePageRef page)
372{
373 WebKit::toImpl(page)->clearHistory();
374}
375
376WKBundleBackForwardListRef WKBundlePageGetBackForwardList(WKBundlePageRef pageRef)
377{
378 return nullptr;
379}
380
381void WKBundlePageInstallPageOverlay(WKBundlePageRef pageRef, WKBundlePageOverlayRef pageOverlayRef)
382{
383 WebKit::toImpl(pageRef)->corePage()->pageOverlayController().installPageOverlay(*WebKit::toImpl(pageOverlayRef)->coreOverlay(), WebCore::PageOverlay::FadeMode::DoNotFade);
384}
385
386void WKBundlePageUninstallPageOverlay(WKBundlePageRef pageRef, WKBundlePageOverlayRef pageOverlayRef)
387{
388 WebKit::toImpl(pageRef)->corePage()->pageOverlayController().uninstallPageOverlay(*WebKit::toImpl(pageOverlayRef)->coreOverlay(), WebCore::PageOverlay::FadeMode::DoNotFade);
389}
390
391void WKBundlePageInstallPageOverlayWithAnimation(WKBundlePageRef pageRef, WKBundlePageOverlayRef pageOverlayRef)
392{
393 WebKit::toImpl(pageRef)->corePage()->pageOverlayController().installPageOverlay(*WebKit::toImpl(pageOverlayRef)->coreOverlay(), WebCore::PageOverlay::FadeMode::Fade);
394}
395
396void WKBundlePageUninstallPageOverlayWithAnimation(WKBundlePageRef pageRef, WKBundlePageOverlayRef pageOverlayRef)
397{
398 WebKit::toImpl(pageRef)->corePage()->pageOverlayController().uninstallPageOverlay(*WebKit::toImpl(pageOverlayRef)->coreOverlay(), WebCore::PageOverlay::FadeMode::Fade);
399}
400
401void WKBundlePageSetTopOverhangImage(WKBundlePageRef pageRef, WKImageRef imageRef)
402{
403#if PLATFORM(MAC)
404 WebKit::toImpl(pageRef)->setTopOverhangImage(WebKit::toImpl(imageRef));
405#else
406 UNUSED_PARAM(pageRef);
407 UNUSED_PARAM(imageRef);
408#endif
409}
410
411void WKBundlePageSetBottomOverhangImage(WKBundlePageRef pageRef, WKImageRef imageRef)
412{
413#if PLATFORM(MAC)
414 WebKit::toImpl(pageRef)->setBottomOverhangImage(WebKit::toImpl(imageRef));
415#else
416 UNUSED_PARAM(pageRef);
417 UNUSED_PARAM(imageRef);
418#endif
419}
420
421#if !PLATFORM(IOS_FAMILY)
422void WKBundlePageSetHeaderBanner(WKBundlePageRef pageRef, WKBundlePageBannerRef bannerRef)
423{
424 WebKit::toImpl(pageRef)->setHeaderPageBanner(WebKit::toImpl(bannerRef));
425}
426
427void WKBundlePageSetFooterBanner(WKBundlePageRef pageRef, WKBundlePageBannerRef bannerRef)
428{
429 WebKit::toImpl(pageRef)->setFooterPageBanner(WebKit::toImpl(bannerRef));
430}
431#endif // !PLATFORM(IOS_FAMILY)
432
433bool WKBundlePageHasLocalDataForURL(WKBundlePageRef pageRef, WKURLRef urlRef)
434{
435 return WebKit::toImpl(pageRef)->hasLocalDataForURL(URL(URL(), WebKit::toWTFString(urlRef)));
436}
437
438bool WKBundlePageCanHandleRequest(WKURLRequestRef requestRef)
439{
440 if (!requestRef)
441 return false;
442 return WebKit::WebPage::canHandleRequest(WebKit::toImpl(requestRef)->resourceRequest());
443}
444
445bool WKBundlePageFindString(WKBundlePageRef pageRef, WKStringRef target, WKFindOptions findOptions)
446{
447 return WebKit::toImpl(pageRef)->findStringFromInjectedBundle(WebKit::toWTFString(target), WebKit::toFindOptions(findOptions));
448}
449
450void WKBundlePageFindStringMatches(WKBundlePageRef pageRef, WKStringRef target, WKFindOptions findOptions)
451{
452 WebKit::toImpl(pageRef)->findStringMatchesFromInjectedBundle(WebKit::toWTFString(target), WebKit::toFindOptions(findOptions));
453}
454
455void WKBundlePageReplaceStringMatches(WKBundlePageRef pageRef, WKArrayRef matchIndicesRef, WKStringRef replacementText, bool selectionOnly)
456{
457 auto* matchIndices = WebKit::toImpl(matchIndicesRef);
458
459 Vector<uint32_t> indices;
460 indices.reserveInitialCapacity(matchIndices->size());
461
462 auto numberOfMatchIndices = matchIndices->size();
463 for (size_t i = 0; i < numberOfMatchIndices; ++i) {
464 if (auto* indexAsObject = matchIndices->at<API::UInt64>(i))
465 indices.uncheckedAppend(indexAsObject->value());
466 }
467 WebKit::toImpl(pageRef)->replaceStringMatchesFromInjectedBundle(indices, WebKit::toWTFString(replacementText), selectionOnly);
468}
469
470WKImageRef WKBundlePageCreateSnapshotWithOptions(WKBundlePageRef pageRef, WKRect rect, WKSnapshotOptions options)
471{
472 RefPtr<WebKit::WebImage> webImage = WebKit::toImpl(pageRef)->scaledSnapshotWithOptions(WebKit::toIntRect(rect), 1, WebKit::toSnapshotOptions(options));
473 return toAPI(webImage.leakRef());
474}
475
476WKImageRef WKBundlePageCreateSnapshotInViewCoordinates(WKBundlePageRef pageRef, WKRect rect, WKImageOptions options)
477{
478 auto snapshotOptions = WebKit::snapshotOptionsFromImageOptions(options);
479 snapshotOptions |= WebKit::SnapshotOptionsInViewCoordinates;
480 RefPtr<WebKit::WebImage> webImage = WebKit::toImpl(pageRef)->scaledSnapshotWithOptions(WebKit::toIntRect(rect), 1, snapshotOptions);
481 return toAPI(webImage.leakRef());
482}
483
484WKImageRef WKBundlePageCreateSnapshotInDocumentCoordinates(WKBundlePageRef pageRef, WKRect rect, WKImageOptions options)
485{
486 RefPtr<WebKit::WebImage> webImage = WebKit::toImpl(pageRef)->scaledSnapshotWithOptions(WebKit::toIntRect(rect), 1, WebKit::snapshotOptionsFromImageOptions(options));
487 return toAPI(webImage.leakRef());
488}
489
490WKImageRef WKBundlePageCreateScaledSnapshotInDocumentCoordinates(WKBundlePageRef pageRef, WKRect rect, double scaleFactor, WKImageOptions options)
491{
492 RefPtr<WebKit::WebImage> webImage = WebKit::toImpl(pageRef)->scaledSnapshotWithOptions(WebKit::toIntRect(rect), scaleFactor, WebKit::snapshotOptionsFromImageOptions(options));
493 return toAPI(webImage.leakRef());
494}
495
496double WKBundlePageGetBackingScaleFactor(WKBundlePageRef pageRef)
497{
498 return WebKit::toImpl(pageRef)->deviceScaleFactor();
499}
500
501void WKBundlePageListenForLayoutMilestones(WKBundlePageRef pageRef, WKLayoutMilestones milestones)
502{
503 WebKit::toImpl(pageRef)->listenForLayoutMilestones(WebKit::toLayoutMilestones(milestones));
504}
505
506WKBundleInspectorRef WKBundlePageGetInspector(WKBundlePageRef pageRef)
507{
508 return WebKit::toAPI(WebKit::toImpl(pageRef)->inspector());
509}
510
511void WKBundlePageForceRepaint(WKBundlePageRef page)
512{
513 WebKit::toImpl(page)->forceRepaintWithoutCallback();
514}
515
516void WKBundlePageSimulateMouseDown(WKBundlePageRef page, int button, WKPoint position, int clickCount, WKEventModifiers modifiers, double time)
517{
518 WebKit::toImpl(page)->simulateMouseDown(button, WebKit::toIntPoint(position), clickCount, modifiers, WallTime::fromRawSeconds(time));
519}
520
521void WKBundlePageSimulateMouseUp(WKBundlePageRef page, int button, WKPoint position, int clickCount, WKEventModifiers modifiers, double time)
522{
523 WebKit::toImpl(page)->simulateMouseUp(button, WebKit::toIntPoint(position), clickCount, modifiers, WallTime::fromRawSeconds(time));
524}
525
526void WKBundlePageSimulateMouseMotion(WKBundlePageRef page, WKPoint position, double time)
527{
528 WebKit::toImpl(page)->simulateMouseMotion(WebKit::toIntPoint(position), WallTime::fromRawSeconds(time));
529}
530
531uint64_t WKBundlePageGetRenderTreeSize(WKBundlePageRef pageRef)
532{
533 return WebKit::toImpl(pageRef)->renderTreeSize();
534}
535
536WKRenderObjectRef WKBundlePageCopyRenderTree(WKBundlePageRef pageRef)
537{
538 return WebKit::toAPI(WebKit::WebRenderObject::create(WebKit::toImpl(pageRef)).leakRef());
539}
540
541WKRenderLayerRef WKBundlePageCopyRenderLayerTree(WKBundlePageRef pageRef)
542{
543 return WebKit::toAPI(WebKit::WebRenderLayer::create(WebKit::toImpl(pageRef)).leakRef());
544}
545
546void WKBundlePageSetPaintedObjectsCounterThreshold(WKBundlePageRef, uint64_t)
547{
548 // FIXME: This function is only still here to keep open source Mac builds building.
549 // We should remove it as soon as we can.
550}
551
552void WKBundlePageSetTracksRepaints(WKBundlePageRef pageRef, bool trackRepaints)
553{
554 WebKit::toImpl(pageRef)->setTracksRepaints(trackRepaints);
555}
556
557bool WKBundlePageIsTrackingRepaints(WKBundlePageRef pageRef)
558{
559 return WebKit::toImpl(pageRef)->isTrackingRepaints();
560}
561
562void WKBundlePageResetTrackedRepaints(WKBundlePageRef pageRef)
563{
564 WebKit::toImpl(pageRef)->resetTrackedRepaints();
565}
566
567WKArrayRef WKBundlePageCopyTrackedRepaintRects(WKBundlePageRef pageRef)
568{
569 return WebKit::toAPI(&WebKit::toImpl(pageRef)->trackedRepaintRects().leakRef());
570}
571
572void WKBundlePageSetComposition(WKBundlePageRef pageRef, WKStringRef text, int from, int length, bool suppressUnderline)
573{
574 WebKit::toImpl(pageRef)->setCompositionForTesting(WebKit::toWTFString(text), from, length, suppressUnderline);
575}
576
577bool WKBundlePageHasComposition(WKBundlePageRef pageRef)
578{
579 return WebKit::toImpl(pageRef)->hasCompositionForTesting();
580}
581
582void WKBundlePageConfirmComposition(WKBundlePageRef pageRef)
583{
584 WebKit::toImpl(pageRef)->confirmCompositionForTesting(String());
585}
586
587void WKBundlePageConfirmCompositionWithText(WKBundlePageRef pageRef, WKStringRef text)
588{
589 WebKit::toImpl(pageRef)->confirmCompositionForTesting(WebKit::toWTFString(text));
590}
591
592void WKBundlePageSetUseDarkAppearance(WKBundlePageRef pageRef, bool useDarkAppearance)
593{
594 WebKit::WebPage* webPage = WebKit::toImpl(pageRef);
595 if (WebCore::Page* page = webPage ? webPage->corePage() : nullptr)
596 page->effectiveAppearanceDidChange(useDarkAppearance, page->useInactiveAppearance());
597}
598
599bool WKBundlePageIsUsingDarkAppearance(WKBundlePageRef pageRef)
600{
601 WebKit::WebPage* webPage = WebKit::toImpl(pageRef);
602 if (WebCore::Page* page = webPage ? webPage->corePage() : nullptr)
603 return page->useDarkAppearance();
604 return false;
605}
606
607bool WKBundlePageCanShowMIMEType(WKBundlePageRef pageRef, WKStringRef mimeTypeRef)
608{
609 return WebKit::toImpl(pageRef)->canShowMIMEType(WebKit::toWTFString(mimeTypeRef));
610}
611
612WKRenderingSuppressionToken WKBundlePageExtendIncrementalRenderingSuppression(WKBundlePageRef pageRef)
613{
614 return WebKit::toImpl(pageRef)->extendIncrementalRenderingSuppression();
615}
616
617void WKBundlePageStopExtendingIncrementalRenderingSuppression(WKBundlePageRef pageRef, WKRenderingSuppressionToken token)
618{
619 WebKit::toImpl(pageRef)->stopExtendingIncrementalRenderingSuppression(token);
620}
621
622bool WKBundlePageIsUsingEphemeralSession(WKBundlePageRef pageRef)
623{
624 return WebKit::toImpl(pageRef)->usesEphemeralSession();
625}
626
627bool WKBundlePageIsControlledByAutomation(WKBundlePageRef pageRef)
628{
629 return WebKit::toImpl(pageRef)->isControlledByAutomation();
630}
631
632#if TARGET_OS_IPHONE
633void WKBundlePageSetUseTestingViewportConfiguration(WKBundlePageRef pageRef, bool useTestingViewportConfiguration)
634{
635 WebKit::toImpl(pageRef)->setUseTestingViewportConfiguration(useTestingViewportConfiguration);
636}
637#endif
638
639void WKBundlePageStartMonitoringScrollOperations(WKBundlePageRef pageRef)
640{
641 WebKit::WebPage* webPage = WebKit::toImpl(pageRef);
642 WebCore::Page* page = webPage ? webPage->corePage() : nullptr;
643
644 if (!page)
645 return;
646
647 page->ensureTestTrigger();
648}
649
650bool WKBundlePageRegisterScrollOperationCompletionCallback(WKBundlePageRef pageRef, WKBundlePageTestNotificationCallback callback, void* context)
651{
652 if (!callback)
653 return false;
654
655 WebKit::WebPage* webPage = WebKit::toImpl(pageRef);
656 WebCore::Page* page = webPage ? webPage->corePage() : nullptr;
657 if (!page || !page->expectsWheelEventTriggers())
658 return false;
659
660 page->ensureTestTrigger().setTestCallbackAndStartNotificationTimer([=]() {
661 callback(context);
662 });
663 return true;
664}
665
666void WKBundlePageCallAfterTasksAndTimers(WKBundlePageRef pageRef, WKBundlePageTestNotificationCallback callback, void* context)
667{
668 if (!callback)
669 return;
670
671 WebKit::WebPage* webPage = WebKit::toImpl(pageRef);
672 WebCore::Page* page = webPage ? webPage->corePage() : nullptr;
673 if (!page)
674 return;
675
676 WebCore::Document* document = page->mainFrame().document();
677 if (!document)
678 return;
679
680 class TimerOwner {
681 public:
682 TimerOwner(WTF::Function<void (void*)>&& callback, void* context)
683 : m_timer(*this, &TimerOwner::timerFired)
684 , m_callback(WTFMove(callback))
685 , m_context(context)
686 {
687 m_timer.startOneShot(0_s);
688 }
689
690 void timerFired()
691 {
692 m_callback(m_context);
693 delete this;
694 }
695
696 WebCore::Timer m_timer;
697 WTF::Function<void (void*)> m_callback;
698 void* m_context;
699 };
700
701 document->postTask([=] (WebCore::ScriptExecutionContext&) {
702 new TimerOwner(callback, context); // deletes itself when done.
703 });
704}
705
706void WKBundlePagePostMessage(WKBundlePageRef pageRef, WKStringRef messageNameRef, WKTypeRef messageBodyRef)
707{
708 WebKit::toImpl(pageRef)->postMessage(WebKit::toWTFString(messageNameRef), WebKit::toImpl(messageBodyRef));
709}
710
711void WKBundlePagePostMessageIgnoringFullySynchronousMode(WKBundlePageRef pageRef, WKStringRef messageNameRef, WKTypeRef messageBodyRef)
712{
713 WebKit::toImpl(pageRef)->postMessageIgnoringFullySynchronousMode(WebKit::toWTFString(messageNameRef), WebKit::toImpl(messageBodyRef));
714}
715
716void WKBundlePagePostSynchronousMessageForTesting(WKBundlePageRef pageRef, WKStringRef messageNameRef, WKTypeRef messageBodyRef, WKTypeRef* returnRetainedDataRef)
717{
718 WebKit::WebPage* page = WebKit::toImpl(pageRef);
719 page->layoutIfNeeded();
720
721 RefPtr<API::Object> returnData;
722 page->postSynchronousMessageForTesting(WebKit::toWTFString(messageNameRef), WebKit::toImpl(messageBodyRef), returnData);
723 if (returnRetainedDataRef)
724 *returnRetainedDataRef = WebKit::toAPI(returnData.leakRef());
725}
726
727void WKBundlePageAddUserScript(WKBundlePageRef pageRef, WKStringRef source, _WKUserScriptInjectionTime injectionTime, WKUserContentInjectedFrames injectedFrames)
728{
729 WebKit::toImpl(pageRef)->addUserScript(WebKit::toWTFString(source), WebKit::toUserContentInjectedFrames(injectedFrames), WebKit::toUserScriptInjectionTime(injectionTime));
730}
731
732void WKBundlePageAddUserStyleSheet(WKBundlePageRef pageRef, WKStringRef source, WKUserContentInjectedFrames injectedFrames)
733{
734 WebKit::toImpl(pageRef)->addUserStyleSheet(WebKit::toWTFString(source), WebKit::toUserContentInjectedFrames(injectedFrames));
735}
736
737void WKBundlePageRemoveAllUserContent(WKBundlePageRef pageRef)
738{
739 WebKit::toImpl(pageRef)->removeAllUserContent();
740}
741
742WKStringRef WKBundlePageCopyGroupIdentifier(WKBundlePageRef pageRef)
743{
744 return WebKit::toCopiedAPI(WebKit::toImpl(pageRef)->pageGroup()->identifier());
745}
746
747void WKBundlePageClearApplicationCache(WKBundlePageRef page)
748{
749 WebKit::toImpl(page)->corePage()->applicationCacheStorage().deleteAllEntries();
750}
751
752void WKBundlePageClearApplicationCacheForOrigin(WKBundlePageRef page, WKStringRef origin)
753{
754 WebKit::toImpl(page)->corePage()->applicationCacheStorage().deleteCacheForOrigin(WebCore::SecurityOrigin::createFromString(WebKit::toImpl(origin)->string()));
755}
756
757void WKBundlePageSetAppCacheMaximumSize(WKBundlePageRef page, uint64_t size)
758{
759 WebKit::toImpl(page)->corePage()->applicationCacheStorage().setMaximumSize(size);
760}
761
762uint64_t WKBundlePageGetAppCacheUsageForOrigin(WKBundlePageRef page, WKStringRef origin)
763{
764 return WebKit::toImpl(page)->corePage()->applicationCacheStorage().diskUsageForOrigin(WebCore::SecurityOrigin::createFromString(WebKit::toImpl(origin)->string()));
765}
766
767void WKBundlePageSetApplicationCacheOriginQuota(WKBundlePageRef page, WKStringRef origin, uint64_t bytes)
768{
769 WebKit::toImpl(page)->corePage()->applicationCacheStorage().storeUpdatedQuotaForOrigin(WebCore::SecurityOrigin::createFromString(WebKit::toImpl(origin)->string()).ptr(), bytes);
770}
771
772void WKBundlePageResetApplicationCacheOriginQuota(WKBundlePageRef page, WKStringRef origin)
773{
774 WebKit::toImpl(page)->corePage()->applicationCacheStorage().storeUpdatedQuotaForOrigin(WebCore::SecurityOrigin::createFromString(WebKit::toImpl(origin)->string()).ptr(), WebKit::toImpl(page)->corePage()->applicationCacheStorage().defaultOriginQuota());
775}
776
777WKArrayRef WKBundlePageCopyOriginsWithApplicationCache(WKBundlePageRef page)
778{
779 auto origins = WebKit::toImpl(page)->corePage()->applicationCacheStorage().originsWithCache();
780
781 Vector<RefPtr<API::Object>> originIdentifiers;
782 originIdentifiers.reserveInitialCapacity(origins.size());
783
784 for (const auto& origin : origins)
785 originIdentifiers.uncheckedAppend(API::String::create(origin->data().databaseIdentifier()));
786
787 return WebKit::toAPI(&API::Array::create(WTFMove(originIdentifiers)).leakRef());
788}
789
790void WKBundlePageSetEventThrottlingBehaviorOverride(WKBundlePageRef page, WKEventThrottlingBehavior* behavior)
791{
792 Optional<WebCore::EventThrottlingBehavior> behaviorValue;
793 if (behavior) {
794 switch (*behavior) {
795 case kWKEventThrottlingBehaviorResponsive:
796 behaviorValue = WebCore::EventThrottlingBehavior::Responsive;
797 break;
798 case kWKEventThrottlingBehaviorUnresponsive:
799 behaviorValue = WebCore::EventThrottlingBehavior::Unresponsive;
800 break;
801 }
802 }
803
804 WebKit::toImpl(page)->corePage()->setEventThrottlingBehaviorOverride(behaviorValue);
805}
806