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