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