59f1a6b4f8ac8cc694af7416c00a92dcd3b7cf61
[WebKit-https.git] / Tools / WebKitTestRunner / InjectedBundle / InjectedBundle.cpp
1 /*
2  * Copyright (C) 2010-2018 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 "InjectedBundle.h"
28
29 #include "ActivateFonts.h"
30 #include "InjectedBundlePage.h"
31 #include "StringFunctions.h"
32 #include "WebCoreTestSupport.h"
33 #include <JavaScriptCore/Options.h>
34 #include <WebKit/WKBundle.h>
35 #include <WebKit/WKBundlePage.h>
36 #include <WebKit/WKBundlePagePrivate.h>
37 #include <WebKit/WKBundlePrivate.h>
38 #include <WebKit/WKRetainPtr.h>
39 #include <WebKit/WebKit2_C.h>
40 #include <wtf/text/CString.h>
41 #include <wtf/text/StringBuilder.h>
42 #include <wtf/Vector.h>
43
44 namespace WTR {
45
46 static void handleTextDidChangeInTextField(WKBundlePageRef, WKBundleNodeHandleRef, WKBundleFrameRef, const void* context)
47 {
48     static_cast<InjectedBundle*>(const_cast<void*>(context))->textDidChangeInTextField();
49 }
50
51 static void handleTextFieldDidBeginEditing(WKBundlePageRef, WKBundleNodeHandleRef, WKBundleFrameRef, const void* context)
52 {
53     static_cast<InjectedBundle*>(const_cast<void*>(context))->textFieldDidBeginEditing();
54 }
55
56 static void handleTextFieldDidEndEditing(WKBundlePageRef, WKBundleNodeHandleRef, WKBundleFrameRef, const void* context)
57 {
58     static_cast<InjectedBundle*>(const_cast<void*>(context))->textFieldDidEndEditing();
59 }
60
61 InjectedBundle& InjectedBundle::singleton()
62 {
63     static InjectedBundle& shared = *new InjectedBundle;
64     return shared;
65 }
66
67 InjectedBundle::InjectedBundle()
68     : m_bundle(0)
69     , m_topLoadingFrame(0)
70     , m_state(Idle)
71     , m_dumpPixels(false)
72     , m_useWaitToDumpWatchdogTimer(true)
73     , m_useWorkQueue(false)
74     , m_timeout(0)
75 {
76 }
77
78 void InjectedBundle::didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
79 {
80     static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didCreatePage(page);
81 }
82
83 void InjectedBundle::willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
84 {
85     static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->willDestroyPage(page);
86 }
87
88 void InjectedBundle::didInitializePageGroup(WKBundleRef bundle, WKBundlePageGroupRef pageGroup, const void* clientInfo)
89 {
90     static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didInitializePageGroup(pageGroup);
91 }
92
93 void InjectedBundle::didReceiveMessage(WKBundleRef bundle, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
94 {
95     static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didReceiveMessage(messageName, messageBody);
96 }
97
98 void InjectedBundle::didReceiveMessageToPage(WKBundleRef bundle, WKBundlePageRef page, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
99 {
100     static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didReceiveMessageToPage(page, messageName, messageBody);
101 }
102
103 void InjectedBundle::initialize(WKBundleRef bundle, WKTypeRef initializationUserData)
104 {
105     m_bundle = bundle;
106
107     WKBundleClientV1 client = {
108         { 1, this },
109         didCreatePage,
110         willDestroyPage,
111         didInitializePageGroup,
112         didReceiveMessage,
113         didReceiveMessageToPage
114     };
115     WKBundleSetClient(m_bundle, &client.base);
116     WKBundleSetServiceWorkerProxyCreationCallback(m_bundle, WebCoreTestSupport::setupNewlyCreatedServiceWorker);
117     platformInitialize(initializationUserData);
118
119     activateFonts();
120 }
121
122 void InjectedBundle::didCreatePage(WKBundlePageRef page)
123 {
124     bool isMainPage = m_pages.isEmpty();
125     m_pages.append(std::make_unique<InjectedBundlePage>(page));
126
127     setUpInjectedBundleClients(page);
128
129     if (!isMainPage)
130         return;
131
132     WKRetainPtr<WKStringRef> messsageName(AdoptWK, WKStringCreateWithUTF8CString("Initialization"));
133     WKTypeRef result = 0;
134     WKBundlePostSynchronousMessage(m_bundle, messsageName.get(), 0, &result);
135     ASSERT(WKGetTypeID(result) == WKDictionaryGetTypeID());
136     WKDictionaryRef initializationDictionary = static_cast<WKDictionaryRef>(result);
137
138     WKRetainPtr<WKStringRef> resumeTestingKey(AdoptWK, WKStringCreateWithUTF8CString("ResumeTesting"));
139     WKTypeRef resumeTestingValue = WKDictionaryGetItemForKey(initializationDictionary, resumeTestingKey.get());
140     ASSERT(WKGetTypeID(resumeTestingValue) == WKBooleanGetTypeID());
141     if (WKBooleanGetValue(static_cast<WKBooleanRef>(resumeTestingValue)))
142         beginTesting(initializationDictionary, BegingTestingMode::Resume);
143 }
144
145 void InjectedBundle::willDestroyPage(WKBundlePageRef page)
146 {
147     m_pages.removeFirstMatching([page](auto& current) {
148         return current->page() == page;
149     });
150 }
151
152 void InjectedBundle::didInitializePageGroup(WKBundlePageGroupRef pageGroup)
153 {
154     m_pageGroup = pageGroup;
155 }
156
157 void InjectedBundle::setUpInjectedBundleClients(WKBundlePageRef page)
158 {
159     WKBundlePageFormClientV2 formClient = {
160         { 2, this },
161         handleTextFieldDidBeginEditing,
162         handleTextFieldDidEndEditing,
163         handleTextDidChangeInTextField,
164         0, // textDidChangeInTextArea
165         0, // shouldPerformActionInTextField
166         0, // willSubmitForm
167         0, // willSendSubmitEvent
168         0, // didFocusTextField
169         0, // shouldNotifyOnFormChanges
170         0, // didAssociateFormControls
171     };
172     WKBundlePageSetFormClient(page, &formClient.base);
173 }
174
175 InjectedBundlePage* InjectedBundle::page() const
176 {
177     // It might be better to have the UI process send over a reference to the main
178     // page instead of just assuming it's the first one.
179     return m_pages[0].get();
180 }
181
182 void InjectedBundle::resetLocalSettings()
183 {
184     setlocale(LC_ALL, "");
185 }
186
187 void InjectedBundle::didReceiveMessage(WKStringRef messageName, WKTypeRef messageBody)
188 {
189     WKRetainPtr<WKStringRef> errorMessageName(AdoptWK, WKStringCreateWithUTF8CString("Error"));
190     WKRetainPtr<WKStringRef> errorMessageBody(AdoptWK, WKStringCreateWithUTF8CString("Unknown"));
191     WKBundlePostMessage(m_bundle, errorMessageName.get(), errorMessageBody.get());
192 }
193
194 void InjectedBundle::didReceiveMessageToPage(WKBundlePageRef page, WKStringRef messageName, WKTypeRef messageBody)
195 {
196     if (WKStringIsEqualToUTF8CString(messageName, "BeginTest")) {
197         ASSERT(messageBody);
198         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
199         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
200
201         WKRetainPtr<WKStringRef> dumpPixelsKey(AdoptWK, WKStringCreateWithUTF8CString("DumpPixels"));
202         m_dumpPixels = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, dumpPixelsKey.get())));
203
204         WKRetainPtr<WKStringRef> useWaitToDumpWatchdogTimerKey(AdoptWK, WKStringCreateWithUTF8CString("UseWaitToDumpWatchdogTimer"));
205         m_useWaitToDumpWatchdogTimer = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, useWaitToDumpWatchdogTimerKey.get())));
206
207         WKRetainPtr<WKStringRef> timeoutKey(AdoptWK, WKStringCreateWithUTF8CString("Timeout"));
208         m_timeout = (int)WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, timeoutKey.get())));
209
210         WKRetainPtr<WKStringRef> dumpJSConsoleLogInStdErrKey(AdoptWK, WKStringCreateWithUTF8CString("DumpJSConsoleLogInStdErr"));
211         m_dumpJSConsoleLogInStdErr = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, dumpJSConsoleLogInStdErrKey.get())));
212
213         WKRetainPtr<WKStringRef> ackMessageName(AdoptWK, WKStringCreateWithUTF8CString("Ack"));
214         WKRetainPtr<WKStringRef> ackMessageBody(AdoptWK, WKStringCreateWithUTF8CString("BeginTest"));
215         WKBundlePagePostMessage(page, ackMessageName.get(), ackMessageBody.get());
216
217         beginTesting(messageBodyDictionary, BegingTestingMode::New);
218         return;
219     }
220
221     if (WKStringIsEqualToUTF8CString(messageName, "Reset")) {
222         ASSERT(messageBody);
223         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
224         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
225         WKRetainPtr<WKStringRef> jscOptionsKey(AdoptWK, WKStringCreateWithUTF8CString("JSCOptions"));
226         WKRetainPtr<WKStringRef> jscOptionsString = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, jscOptionsKey.get()));
227         if (jscOptionsString) {
228             String options = toWTFString(jscOptionsString);
229             JSC::Options::setOptions(options.utf8().data());
230         }
231
232         WKRetainPtr<WKStringRef> shouldGCKey(AdoptWK, WKStringCreateWithUTF8CString("ShouldGC"));
233         bool shouldGC = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, shouldGCKey.get())));
234
235         if (shouldGC)
236             WKBundleGarbageCollectJavaScriptObjects(m_bundle);
237
238         WKRetainPtr<WKStringRef> allowedHostsKey(AdoptWK, WKStringCreateWithUTF8CString("AllowedHosts"));
239         WKTypeRef allowedHostsValue = WKDictionaryGetItemForKey(messageBodyDictionary, allowedHostsKey.get());
240         if (allowedHostsValue && WKGetTypeID(allowedHostsValue) == WKArrayGetTypeID()) {
241             m_allowedHosts.clear();
242
243             WKArrayRef allowedHostsArray = static_cast<WKArrayRef>(allowedHostsValue);
244             for (size_t i = 0, size = WKArrayGetSize(allowedHostsArray); i < size; ++i) {
245                 WKTypeRef item = WKArrayGetItemAtIndex(allowedHostsArray, i);
246                 if (item && WKGetTypeID(item) == WKStringGetTypeID())
247                     m_allowedHosts.append(toWTFString(static_cast<WKStringRef>(item)));
248             }
249         }
250
251         m_state = Idle;
252         m_dumpPixels = false;
253         m_pixelResultIsPending = false;
254
255         resetLocalSettings();
256         TestRunner::removeAllWebNotificationPermissions();
257
258         InjectedBundle::page()->resetAfterTest();
259
260         return;
261     }
262
263     if (WKStringIsEqualToUTF8CString(messageName, "CallAddChromeInputFieldCallback")) {
264         m_testRunner->callAddChromeInputFieldCallback();
265         return;
266     }
267
268     if (WKStringIsEqualToUTF8CString(messageName, "CallRemoveChromeInputFieldCallback")) {
269         m_testRunner->callRemoveChromeInputFieldCallback();
270         return;
271     }
272
273     if (WKStringIsEqualToUTF8CString(messageName, "CallFocusWebViewCallback")) {
274         m_testRunner->callFocusWebViewCallback();
275         return;
276     }
277
278     if (WKStringIsEqualToUTF8CString(messageName, "CallSetBackingScaleFactorCallback")) {
279         m_testRunner->callSetBackingScaleFactorCallback();
280         return;
281     }
282
283     if (WKStringIsEqualToUTF8CString(messageName, "CallDidBeginSwipeCallback")) {
284         m_testRunner->callDidBeginSwipeCallback();
285         return;
286     }
287
288     if (WKStringIsEqualToUTF8CString(messageName, "CallWillEndSwipeCallback")) {
289         m_testRunner->callWillEndSwipeCallback();
290         return;
291     }
292
293     if (WKStringIsEqualToUTF8CString(messageName, "CallDidEndSwipeCallback")) {
294         m_testRunner->callDidEndSwipeCallback();
295         return;
296     }
297
298     if (WKStringIsEqualToUTF8CString(messageName, "CallDidRemoveSwipeSnapshotCallback")) {
299         m_testRunner->callDidRemoveSwipeSnapshotCallback();
300         return;
301     }
302
303     if (WKStringIsEqualToUTF8CString(messageName, "CallDidClearStatisticsThroughWebsiteDataRemoval")) {
304         m_testRunner->statisticsCallClearThroughWebsiteDataRemovalCallback();
305         return;
306     }
307
308     if (WKStringIsEqualToUTF8CString(messageName, "CallDidResetStatisticsToConsistentState")) {
309         m_testRunner->statisticsCallDidResetToConsistentStateCallback();
310         return;
311     }
312     
313     if (WKStringIsEqualToUTF8CString(messageName, "CallDidSetPartitionOrBlockCookiesForHost")) {
314         m_testRunner->statisticsCallDidSetPartitionOrBlockCookiesForHostCallback();
315         return;
316     }
317
318     if (WKStringIsEqualToUTF8CString(messageName, "CallDidSetLastSeen")) {
319         m_testRunner->statisticsCallDidSetLastSeenCallback();
320         return;
321     }
322
323     if (WKStringIsEqualToUTF8CString(messageName, "CallDidSetPrevalentResource")) {
324         m_testRunner->statisticsCallDidSetPrevalentResourceCallback();
325         return;
326     }
327
328     if (WKStringIsEqualToUTF8CString(messageName, "CallDidSetVeryPrevalentResource")) {
329         m_testRunner->statisticsCallDidSetVeryPrevalentResourceCallback();
330         return;
331     }
332
333     if (WKStringIsEqualToUTF8CString(messageName, "CallDidSetHasHadUserInteraction")) {
334         m_testRunner->statisticsCallDidSetHasHadUserInteractionCallback();
335         return;
336     }
337     
338     if (WKStringIsEqualToUTF8CString(messageName, "CallDidSetHasHadNonRecentUserInteraction")) {
339         m_testRunner->statisticsCallDidSetHasHadUserInteractionCallback();
340         return;
341     }
342     
343     if (WKStringIsEqualToUTF8CString(messageName, "CallDidReceiveAllStorageAccessEntries")) {
344         ASSERT(messageBody);
345         ASSERT(WKGetTypeID(messageBody) == WKArrayGetTypeID());
346
347         WKArrayRef domainsArray = static_cast<WKArrayRef>(messageBody);
348         auto size = WKArrayGetSize(domainsArray);
349         Vector<String> domains;
350         domains.reserveInitialCapacity(size);
351         for (size_t i = 0; i < size; ++i) {
352             WKTypeRef item = WKArrayGetItemAtIndex(domainsArray, i);
353             if (item && WKGetTypeID(item) == WKStringGetTypeID())
354                 domains.append(toWTFString(static_cast<WKStringRef>(item)));
355         }
356
357         m_testRunner->callDidReceiveAllStorageAccessEntriesCallback(domains);
358         return;
359     }
360
361     if (WKStringIsEqualToUTF8CString(messageName, "CallDidRemoveAllSessionCredentialsCallback")) {
362         m_testRunner->callDidRemoveAllSessionCredentialsCallback();
363         return;
364     }
365
366     if (WKStringIsEqualToUTF8CString(messageName, "NotifyDownloadDone")) {
367         if (m_testRunner->shouldFinishAfterDownload())
368             m_testRunner->notifyDone();
369         return;
370     }
371
372     if (WKStringIsEqualToUTF8CString(messageName, "CallUISideScriptCallback")) {
373         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
374
375         WKRetainPtr<WKStringRef> resultKey(AdoptWK, WKStringCreateWithUTF8CString("Result"));
376         WKRetainPtr<WKStringRef> callbackIDKey(AdoptWK, WKStringCreateWithUTF8CString("CallbackID"));
377
378         unsigned callbackID = (unsigned)WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, callbackIDKey.get())));
379
380         WKStringRef resultString = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, resultKey.get()));
381         auto resultJSString = toJS(resultString);
382
383         m_testRunner->runUIScriptCallback(callbackID, resultJSString.get());
384         return;
385     }
386
387     if (WKStringIsEqualToUTF8CString(messageName, "WorkQueueProcessedCallback")) {
388         if (!topLoadingFrame() && !m_testRunner->shouldWaitUntilDone())
389             InjectedBundle::page()->dump();
390         return;
391     }
392
393     if (WKStringIsEqualToUTF8CString(messageName, "WebsiteDataDeletionForTopPrivatelyOwnedDomainsFinished")) {
394         m_testRunner->statisticsDidModifyDataRecordsCallback();
395         return;
396     }
397
398     if (WKStringIsEqualToUTF8CString(messageName, "WebsiteDataScanForTopPrivatelyControlledDomainsFinished")) {
399         m_testRunner->statisticsDidScanDataRecordsCallback();
400         return;
401     }
402
403     if (WKStringIsEqualToUTF8CString(messageName, "ResourceLoadStatisticsTelemetryFinished")) {
404         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
405         
406         unsigned totalPrevalentResources = (unsigned)WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, WKStringCreateWithUTF8CString("TotalPrevalentResources"))));
407         unsigned totalPrevalentResourcesWithUserInteraction = (unsigned)WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, WKStringCreateWithUTF8CString("TotalPrevalentResourcesWithUserInteraction"))));
408         unsigned top3SubframeUnderTopFrameOrigins = (unsigned)WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, WKStringCreateWithUTF8CString("Top3SubframeUnderTopFrameOrigins"))));
409         
410         m_testRunner->statisticsDidRunTelemetryCallback(totalPrevalentResources, totalPrevalentResourcesWithUserInteraction, top3SubframeUnderTopFrameOrigins);
411         return;
412     }
413     
414     if (WKStringIsEqualToUTF8CString(messageName, "DidGetApplicationManifest")) {
415         m_testRunner->didGetApplicationManifest();
416         return;
417     }
418     
419     WKRetainPtr<WKStringRef> errorMessageName(AdoptWK, WKStringCreateWithUTF8CString("Error"));
420     WKRetainPtr<WKStringRef> errorMessageBody(AdoptWK, WKStringCreateWithUTF8CString("Unknown"));
421     WKBundlePagePostMessage(page, errorMessageName.get(), errorMessageBody.get());
422 }
423
424 bool InjectedBundle::booleanForKey(WKDictionaryRef dictionary, const char* key)
425 {
426     WKRetainPtr<WKStringRef> wkKey(AdoptWK, WKStringCreateWithUTF8CString(key));
427     WKTypeRef value = WKDictionaryGetItemForKey(dictionary, wkKey.get());
428     if (WKGetTypeID(value) != WKBooleanGetTypeID()) {
429         outputText(makeString("Boolean value for key", key, " not found in dictionary\n"));
430         return false;
431     }
432     return WKBooleanGetValue(static_cast<WKBooleanRef>(value));
433 }
434
435 void InjectedBundle::beginTesting(WKDictionaryRef settings, BegingTestingMode testingMode)
436 {
437     m_state = Testing;
438
439     m_pixelResult.clear();
440     m_repaintRects.clear();
441
442     m_testRunner = TestRunner::create();
443     m_gcController = GCController::create();
444     m_eventSendingController = EventSendingController::create();
445     m_textInputController = TextInputController::create();
446 #if HAVE(ACCESSIBILITY)
447     m_accessibilityController = AccessibilityController::create();
448 #endif
449
450     WKBundleSetAllowUniversalAccessFromFileURLs(m_bundle, m_pageGroup, true);
451     WKBundleSetJavaScriptCanAccessClipboard(m_bundle, m_pageGroup, true);
452     WKBundleSetPrivateBrowsingEnabled(m_bundle, m_pageGroup, false);
453     WKBundleSetUseDashboardCompatibilityMode(m_bundle, m_pageGroup, false);
454     WKBundleSetAuthorAndUserStylesEnabled(m_bundle, m_pageGroup, true);
455     WKBundleSetFrameFlatteningEnabled(m_bundle, m_pageGroup, false);
456     WKBundleSetAsyncFrameScrollingEnabled(m_bundle, m_pageGroup, false);
457     WKBundleSetMinimumLogicalFontSize(m_bundle, m_pageGroup, 9);
458     WKBundleSetSpatialNavigationEnabled(m_bundle, m_pageGroup, false);
459     WKBundleSetAllowFileAccessFromFileURLs(m_bundle, m_pageGroup, true);
460     WKBundleSetPopupBlockingEnabled(m_bundle, m_pageGroup, false);
461     WKBundleSetAllowStorageAccessFromFileURLS(m_bundle, m_pageGroup, false);
462
463 #if PLATFORM(IOS)
464     WKBundlePageSetUseTestingViewportConfiguration(page()->page(), !booleanForKey(settings, "UseFlexibleViewport"));
465 #endif
466
467     m_testRunner->setPluginsEnabled(true);
468
469     m_testRunner->setUserStyleSheetEnabled(false);
470     m_testRunner->setXSSAuditorEnabled(false);
471
472     m_testRunner->setWebGL2Enabled(true);
473     m_testRunner->setWebGPUEnabled(true);
474
475     m_testRunner->setWritableStreamAPIEnabled(true);
476     m_testRunner->setReadableByteStreamAPIEnabled(true);
477
478     m_testRunner->setEncryptedMediaAPIEnabled(true);
479
480     m_testRunner->setCloseRemainingWindowsWhenComplete(false);
481     m_testRunner->setAcceptsEditing(true);
482     m_testRunner->setTabKeyCyclesThroughElements(true);
483     m_testRunner->clearTestRunnerCallbacks();
484
485     if (m_timeout > 0)
486         m_testRunner->setCustomTimeout(m_timeout);
487
488     page()->prepare();
489
490     if (testingMode != BegingTestingMode::New)
491         return;
492
493     WKBundleClearAllDatabases(m_bundle);
494     WKBundlePageClearApplicationCache(page()->page());
495     WKBundleResetOriginAccessWhitelists(m_bundle);
496     WKBundleClearResourceLoadStatistics(m_bundle);
497
498     // [WK2] REGRESSION(r128623): It made layout tests extremely slow
499     // https://bugs.webkit.org/show_bug.cgi?id=96862
500     // WKBundleSetDatabaseQuota(m_bundle, 5 * 1024 * 1024);
501 }
502
503 void InjectedBundle::done()
504 {
505     m_state = Stopping;
506
507     m_useWorkQueue = false;
508
509     page()->stopLoading();
510     setTopLoadingFrame(0);
511
512     m_testRunner->invalidateWaitToDumpWatchdogTimer();
513
514 #if HAVE(ACCESSIBILITY)
515     m_accessibilityController->resetToConsistentState();
516 #endif
517
518     WKRetainPtr<WKStringRef> doneMessageName(AdoptWK, WKStringCreateWithUTF8CString("Done"));
519     WKRetainPtr<WKMutableDictionaryRef> doneMessageBody(AdoptWK, WKMutableDictionaryCreate());
520
521     WKRetainPtr<WKStringRef> pixelResultIsPendingKey = adoptWK(WKStringCreateWithUTF8CString("PixelResultIsPending"));
522     WKRetainPtr<WKBooleanRef> pixelResultIsPending(AdoptWK, WKBooleanCreate(m_pixelResultIsPending));
523     WKDictionarySetItem(doneMessageBody.get(), pixelResultIsPendingKey.get(), pixelResultIsPending.get());
524
525     if (!m_pixelResultIsPending) {
526         WKRetainPtr<WKStringRef> pixelResultKey = adoptWK(WKStringCreateWithUTF8CString("PixelResult"));
527         WKDictionarySetItem(doneMessageBody.get(), pixelResultKey.get(), m_pixelResult.get());
528     }
529
530     WKRetainPtr<WKStringRef> repaintRectsKey = adoptWK(WKStringCreateWithUTF8CString("RepaintRects"));
531     WKDictionarySetItem(doneMessageBody.get(), repaintRectsKey.get(), m_repaintRects.get());
532
533     WKRetainPtr<WKStringRef> audioResultKey = adoptWK(WKStringCreateWithUTF8CString("AudioResult"));
534     WKDictionarySetItem(doneMessageBody.get(), audioResultKey.get(), m_audioResult.get());
535
536     WKBundlePagePostMessage(page()->page(), doneMessageName.get(), doneMessageBody.get());
537
538     closeOtherPages();
539
540     m_state = Idle;
541 }
542
543 void InjectedBundle::closeOtherPages()
544 {
545     Vector<WKBundlePageRef> pagesToClose;
546     size_t size = m_pages.size();
547     for (size_t i = 1; i < size; ++i)
548         pagesToClose.append(m_pages[i]->page());
549     size = pagesToClose.size();
550     for (size_t i = 0; i < size; ++i)
551         WKBundlePageClose(pagesToClose[i]);
552 }
553
554 void InjectedBundle::dumpBackForwardListsForAllPages(StringBuilder& stringBuilder)
555 {
556     size_t size = m_pages.size();
557     for (size_t i = 0; i < size; ++i)
558         m_pages[i]->dumpBackForwardList(stringBuilder);
559 }
560
561 void InjectedBundle::dumpToStdErr(const String& output)
562 {
563     if (m_state != Testing)
564         return;
565     if (output.isEmpty())
566         return;
567     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("DumpToStdErr"));
568     WKRetainPtr<WKStringRef> messageBody(AdoptWK, WKStringCreateWithUTF8CString(output.utf8().data()));
569     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
570 }
571
572 void InjectedBundle::outputText(const String& output)
573 {
574     if (m_state != Testing)
575         return;
576     if (output.isEmpty())
577         return;
578     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("TextOutput"));
579     WKRetainPtr<WKStringRef> messageBody(AdoptWK, WKStringCreateWithUTF8CString(output.utf8().data()));
580     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
581 }
582
583 void InjectedBundle::postNewBeforeUnloadReturnValue(bool value)
584 {
585     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("BeforeUnloadReturnValue"));
586     WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(value));
587     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
588 }
589
590 void InjectedBundle::postAddChromeInputField()
591 {
592     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("AddChromeInputField"));
593     WKBundlePagePostMessage(page()->page(), messageName.get(), 0);
594 }
595
596 void InjectedBundle::postRemoveChromeInputField()
597 {
598     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("RemoveChromeInputField"));
599     WKBundlePagePostMessage(page()->page(), messageName.get(), 0);
600 }
601
602 void InjectedBundle::postFocusWebView()
603 {
604     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("FocusWebView"));
605     WKBundlePagePostMessage(page()->page(), messageName.get(), 0);
606 }
607
608 void InjectedBundle::postSetBackingScaleFactor(double backingScaleFactor)
609 {
610     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetBackingScaleFactor"));
611     WKRetainPtr<WKDoubleRef> messageBody(AdoptWK, WKDoubleCreate(backingScaleFactor));
612     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
613 }
614
615 void InjectedBundle::postSetWindowIsKey(bool isKey)
616 {
617     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetWindowIsKey"));
618     WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(isKey));
619     WKBundlePagePostSynchronousMessageForTesting(page()->page(), messageName.get(), messageBody.get(), 0);
620 }
621
622 void InjectedBundle::postSetViewSize(double width, double height)
623 {
624     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetViewSize"));
625
626     WKRetainPtr<WKStringRef> widthKey(AdoptWK, WKStringCreateWithUTF8CString("width"));
627     WKRetainPtr<WKStringRef> heightKey(AdoptWK, WKStringCreateWithUTF8CString("height"));
628
629     WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
630
631     WKRetainPtr<WKDoubleRef> widthWK(AdoptWK, WKDoubleCreate(width));
632     WKDictionarySetItem(messageBody.get(), widthKey.get(), widthWK.get());
633
634     WKRetainPtr<WKDoubleRef> heightWK(AdoptWK, WKDoubleCreate(height));
635     WKDictionarySetItem(messageBody.get(), heightKey.get(), heightWK.get());
636
637     WKBundlePagePostSynchronousMessageForTesting(page()->page(), messageName.get(), messageBody.get(), 0);
638 }
639
640 void InjectedBundle::postSimulateWebNotificationClick(uint64_t notificationID)
641 {
642     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SimulateWebNotificationClick"));
643     WKRetainPtr<WKUInt64Ref> messageBody(AdoptWK, WKUInt64Create(notificationID));
644     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
645 }
646
647 void InjectedBundle::postSetAddsVisitedLinks(bool addsVisitedLinks)
648 {
649     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetAddsVisitedLinks"));
650     WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(addsVisitedLinks));
651     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
652 }
653
654 void InjectedBundle::setGeolocationPermission(bool enabled)
655 {
656     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetGeolocationPermission"));
657     WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(enabled));
658     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
659 }
660
661 void InjectedBundle::setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed, bool providesFloorLevel, double floorLevel)
662 {
663     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetMockGeolocationPosition"));
664
665     WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
666
667     WKRetainPtr<WKStringRef> latitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("latitude"));
668     WKRetainPtr<WKDoubleRef> latitudeWK(AdoptWK, WKDoubleCreate(latitude));
669     WKDictionarySetItem(messageBody.get(), latitudeKeyWK.get(), latitudeWK.get());
670
671     WKRetainPtr<WKStringRef> longitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("longitude"));
672     WKRetainPtr<WKDoubleRef> longitudeWK(AdoptWK, WKDoubleCreate(longitude));
673     WKDictionarySetItem(messageBody.get(), longitudeKeyWK.get(), longitudeWK.get());
674
675     WKRetainPtr<WKStringRef> accuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("accuracy"));
676     WKRetainPtr<WKDoubleRef> accuracyWK(AdoptWK, WKDoubleCreate(accuracy));
677     WKDictionarySetItem(messageBody.get(), accuracyKeyWK.get(), accuracyWK.get());
678
679     WKRetainPtr<WKStringRef> providesAltitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesAltitude"));
680     WKRetainPtr<WKBooleanRef> providesAltitudeWK(AdoptWK, WKBooleanCreate(providesAltitude));
681     WKDictionarySetItem(messageBody.get(), providesAltitudeKeyWK.get(), providesAltitudeWK.get());
682
683     WKRetainPtr<WKStringRef> altitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("altitude"));
684     WKRetainPtr<WKDoubleRef> altitudeWK(AdoptWK, WKDoubleCreate(altitude));
685     WKDictionarySetItem(messageBody.get(), altitudeKeyWK.get(), altitudeWK.get());
686
687     WKRetainPtr<WKStringRef> providesAltitudeAccuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesAltitudeAccuracy"));
688     WKRetainPtr<WKBooleanRef> providesAltitudeAccuracyWK(AdoptWK, WKBooleanCreate(providesAltitudeAccuracy));
689     WKDictionarySetItem(messageBody.get(), providesAltitudeAccuracyKeyWK.get(), providesAltitudeAccuracyWK.get());
690
691     WKRetainPtr<WKStringRef> altitudeAccuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("altitudeAccuracy"));
692     WKRetainPtr<WKDoubleRef> altitudeAccuracyWK(AdoptWK, WKDoubleCreate(altitudeAccuracy));
693     WKDictionarySetItem(messageBody.get(), altitudeAccuracyKeyWK.get(), altitudeAccuracyWK.get());
694
695     WKRetainPtr<WKStringRef> providesHeadingKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesHeading"));
696     WKRetainPtr<WKBooleanRef> providesHeadingWK(AdoptWK, WKBooleanCreate(providesHeading));
697     WKDictionarySetItem(messageBody.get(), providesHeadingKeyWK.get(), providesHeadingWK.get());
698
699     WKRetainPtr<WKStringRef> headingKeyWK(AdoptWK, WKStringCreateWithUTF8CString("heading"));
700     WKRetainPtr<WKDoubleRef> headingWK(AdoptWK, WKDoubleCreate(heading));
701     WKDictionarySetItem(messageBody.get(), headingKeyWK.get(), headingWK.get());
702
703     WKRetainPtr<WKStringRef> providesSpeedKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesSpeed"));
704     WKRetainPtr<WKBooleanRef> providesSpeedWK(AdoptWK, WKBooleanCreate(providesSpeed));
705     WKDictionarySetItem(messageBody.get(), providesSpeedKeyWK.get(), providesSpeedWK.get());
706
707     WKRetainPtr<WKStringRef> speedKeyWK(AdoptWK, WKStringCreateWithUTF8CString("speed"));
708     WKRetainPtr<WKDoubleRef> speedWK(AdoptWK, WKDoubleCreate(speed));
709     WKDictionarySetItem(messageBody.get(), speedKeyWK.get(), speedWK.get());
710
711     WKRetainPtr<WKStringRef> providesFloorLevelKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesFloorLevel"));
712     WKRetainPtr<WKBooleanRef> providesFloorLevelWK(AdoptWK, WKBooleanCreate(providesFloorLevel));
713     WKDictionarySetItem(messageBody.get(), providesFloorLevelKeyWK.get(), providesFloorLevelWK.get());
714
715     WKRetainPtr<WKStringRef> floorLevelKeyWK(AdoptWK, WKStringCreateWithUTF8CString("floorLevel"));
716     WKRetainPtr<WKDoubleRef> floorLevelWK(AdoptWK, WKDoubleCreate(floorLevel));
717     WKDictionarySetItem(messageBody.get(), floorLevelKeyWK.get(), floorLevelWK.get());
718
719     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
720 }
721
722 void InjectedBundle::setMockGeolocationPositionUnavailableError(WKStringRef errorMessage)
723 {
724     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetMockGeolocationPositionUnavailableError"));
725     WKBundlePagePostMessage(page()->page(), messageName.get(), errorMessage);
726 }
727
728 bool InjectedBundle::isGeolocationProviderActive() const
729 {
730     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("IsGeolocationClientActive"));
731     WKTypeRef resultToPass = 0;
732     WKBundlePagePostSynchronousMessageForTesting(page()->page(), messageName.get(), 0, &resultToPass);
733     WKRetainPtr<WKBooleanRef> isActive(AdoptWK, static_cast<WKBooleanRef>(resultToPass));
734
735     return WKBooleanGetValue(isActive.get());
736 }
737
738 unsigned InjectedBundle::imageCountInGeneralPasteboard() const
739 {
740     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("ImageCountInGeneralPasteboard"));
741     WKTypeRef resultToPass = 0;
742     WKBundlePagePostSynchronousMessageForTesting(page()->page(), messageName.get(), 0, &resultToPass);
743     WKRetainPtr<WKUInt64Ref> imageCount(AdoptWK, static_cast<WKUInt64Ref>(resultToPass));
744     
745     return static_cast<unsigned>(WKUInt64GetValue(imageCount.get()));
746 }
747
748 void InjectedBundle::setUserMediaPermission(bool enabled)
749 {
750     auto messageName = adoptWK(WKStringCreateWithUTF8CString("SetUserMediaPermission"));
751     auto messageBody = adoptWK(WKBooleanCreate(enabled));
752     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
753 }
754
755 void InjectedBundle::resetUserMediaPermission()
756 {
757     auto messageName = adoptWK(WKStringCreateWithUTF8CString("ResetUserMediaPermission"));
758     WKBundlePagePostMessage(page()->page(), messageName.get(), 0);
759 }
760
761 void InjectedBundle::setUserMediaPersistentPermissionForOrigin(bool permission, WKStringRef origin, WKStringRef parentOrigin)
762 {
763     auto messageName = adoptWK(WKStringCreateWithUTF8CString("SetUserMediaPersistentPermissionForOrigin"));
764     WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
765
766     WKRetainPtr<WKStringRef> permissionKeyWK(AdoptWK, WKStringCreateWithUTF8CString("permission"));
767     WKRetainPtr<WKBooleanRef> permissionWK(AdoptWK, WKBooleanCreate(permission));
768     WKDictionarySetItem(messageBody.get(), permissionKeyWK.get(), permissionWK.get());
769
770     WKRetainPtr<WKStringRef> originKeyWK(AdoptWK, WKStringCreateWithUTF8CString("origin"));
771     WKDictionarySetItem(messageBody.get(), originKeyWK.get(), origin);
772
773     WKRetainPtr<WKStringRef> parentOriginKeyWK(AdoptWK, WKStringCreateWithUTF8CString("parentOrigin"));
774     WKDictionarySetItem(messageBody.get(), parentOriginKeyWK.get(), parentOrigin);
775
776     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
777 }
778
779 unsigned InjectedBundle::userMediaPermissionRequestCountForOrigin(WKStringRef origin, WKStringRef parentOrigin) const
780 {
781     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("UserMediaPermissionRequestCountForOrigin"));
782     WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
783
784     WKRetainPtr<WKStringRef> originKeyWK(AdoptWK, WKStringCreateWithUTF8CString("origin"));
785     WKDictionarySetItem(messageBody.get(), originKeyWK.get(), origin);
786
787     WKRetainPtr<WKStringRef> parentOriginKeyWK(AdoptWK, WKStringCreateWithUTF8CString("parentOrigin"));
788     WKDictionarySetItem(messageBody.get(), parentOriginKeyWK.get(), parentOrigin);
789
790     WKTypeRef resultToPass = 0;
791     WKBundlePagePostSynchronousMessageForTesting(page()->page(), messageName.get(), messageBody.get(), &resultToPass);
792     WKRetainPtr<WKUInt64Ref> count(AdoptWK, static_cast<WKUInt64Ref>(resultToPass));
793
794     return static_cast<unsigned>(WKUInt64GetValue(count.get()));
795 }
796
797 void InjectedBundle::resetUserMediaPermissionRequestCountForOrigin(WKStringRef origin, WKStringRef parentOrigin)
798 {
799     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("ResetUserMediaPermissionRequestCountForOrigin"));
800     WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
801
802     WKRetainPtr<WKStringRef> originKeyWK(AdoptWK, WKStringCreateWithUTF8CString("origin"));
803     WKDictionarySetItem(messageBody.get(), originKeyWK.get(), origin);
804
805     WKRetainPtr<WKStringRef> parentOriginKeyWK(AdoptWK, WKStringCreateWithUTF8CString("parentOrigin"));
806     WKDictionarySetItem(messageBody.get(), parentOriginKeyWK.get(), parentOrigin);
807
808     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
809 }
810
811 void InjectedBundle::setCustomPolicyDelegate(bool enabled, bool permissive)
812 {
813     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetCustomPolicyDelegate"));
814
815     WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
816
817     WKRetainPtr<WKStringRef> enabledKeyWK(AdoptWK, WKStringCreateWithUTF8CString("enabled"));
818     WKRetainPtr<WKBooleanRef> enabledWK(AdoptWK, WKBooleanCreate(enabled));
819     WKDictionarySetItem(messageBody.get(), enabledKeyWK.get(), enabledWK.get());
820
821     WKRetainPtr<WKStringRef> permissiveKeyWK(AdoptWK, WKStringCreateWithUTF8CString("permissive"));
822     WKRetainPtr<WKBooleanRef> permissiveWK(AdoptWK, WKBooleanCreate(permissive));
823     WKDictionarySetItem(messageBody.get(), permissiveKeyWK.get(), permissiveWK.get());
824
825     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
826 }
827
828 void InjectedBundle::setHidden(bool hidden)
829 {
830     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetHidden"));
831     WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
832
833     WKRetainPtr<WKStringRef> isInitialKeyWK(AdoptWK, WKStringCreateWithUTF8CString("hidden"));
834     WKRetainPtr<WKBooleanRef> isInitialWK(AdoptWK, WKBooleanCreate(hidden));
835     WKDictionarySetItem(messageBody.get(), isInitialKeyWK.get(), isInitialWK.get());
836
837     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
838 }
839
840 void InjectedBundle::setCacheModel(int model)
841 {
842     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetCacheModel"));
843     WKRetainPtr<WKUInt64Ref> messageBody(AdoptWK, WKUInt64Create(model));
844     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
845 }
846
847 bool InjectedBundle::shouldProcessWorkQueue() const
848 {
849     if (!m_useWorkQueue)
850         return false;
851
852     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("IsWorkQueueEmpty"));
853     WKTypeRef resultToPass = 0;
854     WKBundlePagePostSynchronousMessageForTesting(page()->page(), messageName.get(), 0, &resultToPass);
855     WKRetainPtr<WKBooleanRef> isEmpty(AdoptWK, static_cast<WKBooleanRef>(resultToPass));
856
857     return !WKBooleanGetValue(isEmpty.get());
858 }
859
860 void InjectedBundle::processWorkQueue()
861 {
862     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("ProcessWorkQueue"));
863     WKBundlePagePostMessage(page()->page(), messageName.get(), 0);
864 }
865
866 void InjectedBundle::queueBackNavigation(unsigned howFarBackward)
867 {
868     m_useWorkQueue = true;
869
870     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueBackNavigation"));
871     WKRetainPtr<WKUInt64Ref> messageBody(AdoptWK, WKUInt64Create(howFarBackward));
872     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
873 }
874
875 void InjectedBundle::queueForwardNavigation(unsigned howFarForward)
876 {
877     m_useWorkQueue = true;
878
879     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueForwardNavigation"));
880     WKRetainPtr<WKUInt64Ref> messageBody(AdoptWK, WKUInt64Create(howFarForward));
881     WKBundlePagePostMessage(page()->page(), messageName.get(), messageBody.get());
882 }
883
884 void InjectedBundle::queueLoad(WKStringRef url, WKStringRef target, bool shouldOpenExternalURLs)
885 {
886     m_useWorkQueue = true;
887
888     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueLoad"));
889
890     WKRetainPtr<WKMutableDictionaryRef> loadData(AdoptWK, WKMutableDictionaryCreate());
891
892     WKRetainPtr<WKStringRef> urlKey(AdoptWK, WKStringCreateWithUTF8CString("url"));
893     WKDictionarySetItem(loadData.get(), urlKey.get(), url);
894
895     WKRetainPtr<WKStringRef> targetKey(AdoptWK, WKStringCreateWithUTF8CString("target"));
896     WKDictionarySetItem(loadData.get(), targetKey.get(), target);
897
898     WKRetainPtr<WKStringRef> shouldOpenExternalURLsKey(AdoptWK, WKStringCreateWithUTF8CString("shouldOpenExternalURLs"));
899     WKRetainPtr<WKBooleanRef> shouldOpenExternalURLsValue(AdoptWK, WKBooleanCreate(shouldOpenExternalURLs));
900     WKDictionarySetItem(loadData.get(), shouldOpenExternalURLsKey.get(), shouldOpenExternalURLsValue.get());
901
902     WKBundlePagePostMessage(page()->page(), messageName.get(), loadData.get());
903 }
904
905 void InjectedBundle::queueLoadHTMLString(WKStringRef content, WKStringRef baseURL, WKStringRef unreachableURL)
906 {
907     m_useWorkQueue = true;
908
909     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueLoadHTMLString"));
910
911     WKRetainPtr<WKMutableDictionaryRef> loadData(AdoptWK, WKMutableDictionaryCreate());
912
913     WKRetainPtr<WKStringRef> contentKey(AdoptWK, WKStringCreateWithUTF8CString("content"));
914     WKDictionarySetItem(loadData.get(), contentKey.get(), content);
915
916     if (baseURL) {
917         WKRetainPtr<WKStringRef> baseURLKey(AdoptWK, WKStringCreateWithUTF8CString("baseURL"));
918         WKDictionarySetItem(loadData.get(), baseURLKey.get(), baseURL);
919     }
920
921     if (unreachableURL) {
922         WKRetainPtr<WKStringRef> unreachableURLKey(AdoptWK, WKStringCreateWithUTF8CString("unreachableURL"));
923         WKDictionarySetItem(loadData.get(), unreachableURLKey.get(), unreachableURL);
924     }
925
926     WKBundlePagePostMessage(page()->page(), messageName.get(), loadData.get());
927 }
928
929 void InjectedBundle::queueReload()
930 {
931     m_useWorkQueue = true;
932
933     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueReload"));
934     WKBundlePagePostMessage(page()->page(), messageName.get(), 0);
935 }
936
937 void InjectedBundle::queueLoadingScript(WKStringRef script)
938 {
939     m_useWorkQueue = true;
940
941     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueLoadingScript"));
942     WKBundlePagePostMessage(page()->page(), messageName.get(), script);
943 }
944
945 void InjectedBundle::queueNonLoadingScript(WKStringRef script)
946 {
947     m_useWorkQueue = true;
948
949     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueNonLoadingScript"));
950     WKBundlePagePostMessage(page()->page(), messageName.get(), script);
951 }
952
953 bool InjectedBundle::isAllowedHost(WKStringRef host)
954 {
955     if (m_allowedHosts.isEmpty())
956         return false;
957     return m_allowedHosts.contains(toWTFString(host));
958 }
959
960 void InjectedBundle::setAllowsAnySSLCertificate(bool allowsAnySSLCertificate)
961 {
962     WebCoreTestSupport::setAllowsAnySSLCertificate(allowsAnySSLCertificate);
963 }
964
965 void InjectedBundle::statisticsNotifyObserver()
966 {
967     WKBundleResourceLoadStatisticsNotifyObserver(m_bundle);
968 }
969
970 void InjectedBundle::textDidChangeInTextField()
971 {
972     m_testRunner->textDidChangeInTextFieldCallback();
973 }
974
975 void InjectedBundle::textFieldDidBeginEditing()
976 {
977     m_testRunner->textFieldDidBeginEditingCallback();
978 }
979
980 void InjectedBundle::textFieldDidEndEditing()
981 {
982     m_testRunner->textFieldDidEndEditingCallback();
983 }
984
985 } // namespace WTR