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