Domain relationships in the ITP Database should be inserted in a single query and...
[WebKit-https.git] / Tools / WebKitTestRunner / TestInvocation.cpp
1 /*
2  * Copyright (C) 2010-2017 Apple Inc. All rights reserved.
3  * Copyright (C) 2012 Intel Corporation. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "TestInvocation.h"
29
30 #include "PlatformWebView.h"
31 #include "StringFunctions.h"
32 #include "TestController.h"
33 #include "UIScriptController.h"
34 #include "WebCoreTestSupport.h"
35 #include <WebKit/WKContextPrivate.h>
36 #include <WebKit/WKData.h>
37 #include <WebKit/WKDictionary.h>
38 #include <WebKit/WKHTTPCookieStoreRef.h>
39 #include <WebKit/WKInspector.h>
40 #include <WebKit/WKPagePrivate.h>
41 #include <WebKit/WKRetainPtr.h>
42 #include <WebKit/WKWebsiteDataStoreRef.h>
43 #include <climits>
44 #include <cstdio>
45 #include <wtf/StdLibExtras.h>
46 #include <wtf/text/CString.h>
47
48 #if PLATFORM(MAC) && !PLATFORM(IOS_FAMILY)
49 #include <Carbon/Carbon.h>
50 #endif
51
52 #if PLATFORM(COCOA)
53 #include <WebKit/WKPagePrivateMac.h>
54 #endif
55
56 #if PLATFORM(WIN)
57 #include <io.h>
58 #define isatty _isatty
59 #else
60 #include <unistd.h>
61 #endif
62
63 using namespace JSC;
64 using namespace WebKit;
65 using namespace std;
66
67 namespace WTR {
68
69 TestInvocation::TestInvocation(WKURLRef url, const TestOptions& options)
70     : m_options(options)
71     , m_url(url)
72     , m_waitToDumpWatchdogTimer(RunLoop::main(), this, &TestInvocation::waitToDumpWatchdogTimerFired)
73 {
74     WKRetainPtr<WKStringRef> urlString = adoptWK(WKURLCopyString(m_url.get()));
75
76     size_t stringLength = WKStringGetLength(urlString.get());
77
78     Vector<char> urlVector;
79     urlVector.resize(stringLength + 1);
80
81     WKStringGetUTF8CString(urlString.get(), urlVector.data(), stringLength + 1);
82
83     m_urlString = String(urlVector.data(), stringLength);
84
85     // FIXME: Avoid mutating the setting via a test directory like this.
86     m_dumpFrameLoadCallbacks = urlContains("loading/") && !urlContains("://localhost");
87 }
88
89 TestInvocation::~TestInvocation()
90 {
91     if (m_pendingUIScriptInvocationData)
92         m_pendingUIScriptInvocationData->testInvocation = nullptr;
93 }
94
95 WKURLRef TestInvocation::url() const
96 {
97     return m_url.get();
98 }
99
100 bool TestInvocation::urlContains(const char* searchString) const
101 {
102     return m_urlString.containsIgnoringASCIICase(searchString);
103 }
104
105 void TestInvocation::setIsPixelTest(const std::string& expectedPixelHash)
106 {
107     m_dumpPixels = true;
108     m_expectedPixelHash = expectedPixelHash;
109 }
110
111 WTF::Seconds TestInvocation::shortTimeout() const
112 {
113     if (!m_timeout) {
114         // Running WKTR directly, without webkitpy.
115         return TestController::defaultShortTimeout;
116     }
117
118     // This is not exactly correct for the way short timeout is used - it should not depend on whether a test is "slow",
119     // but it currently does. There is no way to know what a normal test's timeout is, as webkitpy only passes timeouts
120     // for each test individually.
121     // But there shouldn't be any observable negative consequences from this.
122     return m_timeout / 4;
123 }
124
125 bool TestInvocation::shouldLogHistoryClientCallbacks() const
126 {
127     return urlContains("globalhistory/");
128 }
129
130 WKRetainPtr<WKMutableDictionaryRef> TestInvocation::createTestSettingsDictionary()
131 {
132     WKRetainPtr<WKMutableDictionaryRef> beginTestMessageBody = adoptWK(WKMutableDictionaryCreate());
133
134     WKRetainPtr<WKStringRef> useFlexibleViewportKey = adoptWK(WKStringCreateWithUTF8CString("UseFlexibleViewport"));
135     WKRetainPtr<WKBooleanRef> useFlexibleViewportValue = adoptWK(WKBooleanCreate(options().useFlexibleViewport));
136     WKDictionarySetItem(beginTestMessageBody.get(), useFlexibleViewportKey.get(), useFlexibleViewportValue.get());
137
138     WKRetainPtr<WKStringRef> dumpPixelsKey = adoptWK(WKStringCreateWithUTF8CString("DumpPixels"));
139     WKRetainPtr<WKBooleanRef> dumpPixelsValue = adoptWK(WKBooleanCreate(m_dumpPixels));
140     WKDictionarySetItem(beginTestMessageBody.get(), dumpPixelsKey.get(), dumpPixelsValue.get());
141
142     WKRetainPtr<WKStringRef> timeoutKey = adoptWK(WKStringCreateWithUTF8CString("Timeout"));
143     WKRetainPtr<WKUInt64Ref> timeoutValue = adoptWK(WKUInt64Create(m_timeout.milliseconds()));
144     WKDictionarySetItem(beginTestMessageBody.get(), timeoutKey.get(), timeoutValue.get());
145
146     WKRetainPtr<WKStringRef> dumpJSConsoleLogInStdErrKey = adoptWK(WKStringCreateWithUTF8CString("DumpJSConsoleLogInStdErr"));
147     WKRetainPtr<WKBooleanRef> dumpJSConsoleLogInStdErrValue = adoptWK(WKBooleanCreate(m_dumpJSConsoleLogInStdErr));
148     WKDictionarySetItem(beginTestMessageBody.get(), dumpJSConsoleLogInStdErrKey.get(), dumpJSConsoleLogInStdErrValue.get());
149
150     WKRetainPtr<WKStringRef> additionalSupportedImageTypesKey = adoptWK(WKStringCreateWithUTF8CString("additionalSupportedImageTypes"));
151     WKRetainPtr<WKStringRef> additionalSupportedImageTypesValue = adoptWK(WKStringCreateWithUTF8CString(options().additionalSupportedImageTypes.c_str()));
152     WKDictionarySetItem(beginTestMessageBody.get(), additionalSupportedImageTypesKey.get(), additionalSupportedImageTypesValue.get());
153
154     return beginTestMessageBody;
155 }
156
157 void TestInvocation::invoke()
158 {
159     TestController::singleton().configureViewForTest(*this);
160
161     WKPageSetAddsVisitedLinks(TestController::singleton().mainWebView()->page(), false);
162
163     m_textOutput.clear();
164
165     TestController::singleton().setShouldLogHistoryClientCallbacks(shouldLogHistoryClientCallbacks());
166
167     WKHTTPCookieStoreSetHTTPCookieAcceptPolicy(WKWebsiteDataStoreGetHTTPCookieStore(TestController::websiteDataStore()), kWKHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain, nullptr, nullptr);
168
169     // FIXME: We should clear out visited links here.
170
171     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("BeginTest"));
172     auto beginTestMessageBody = createTestSettingsDictionary();
173     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), beginTestMessageBody.get());
174
175     m_startedTesting = true;
176
177     bool shouldOpenExternalURLs = false;
178
179     TestController::singleton().runUntil(m_gotInitialResponse, TestController::noTimeout);
180     if (m_error)
181         goto end;
182
183     WKPageLoadURLWithShouldOpenExternalURLsPolicy(TestController::singleton().mainWebView()->page(), m_url.get(), shouldOpenExternalURLs);
184
185     TestController::singleton().runUntil(m_gotFinalMessage, TestController::noTimeout);
186     if (m_error)
187         goto end;
188
189     dumpResults();
190
191 end:
192 #if !PLATFORM(IOS_FAMILY)
193     if (m_gotInitialResponse)
194         WKInspectorClose(WKPageGetInspector(TestController::singleton().mainWebView()->page()));
195 #endif // !PLATFORM(IOS_FAMILY)
196
197     if (TestController::singleton().resetStateToConsistentValues(m_options, TestController::ResetStage::AfterTest))
198         return;
199
200     // The process is unresponsive, so let's start a new one.
201     TestController::singleton().terminateWebContentProcess();
202     // Make sure that we have a process, as invoke() will need one to send bundle messages for the next test.
203     TestController::singleton().reattachPageToWebProcess();
204 }
205
206 void TestInvocation::dumpWebProcessUnresponsiveness(const char* errorMessage)
207 {
208     fprintf(stderr, "%s", errorMessage);
209     char buffer[1024] = { };
210 #if PLATFORM(COCOA)
211     pid_t pid = WKPageGetProcessIdentifier(TestController::singleton().mainWebView()->page());
212     snprintf(buffer, sizeof(buffer), "#PROCESS UNRESPONSIVE - %s (pid %ld)\n", TestController::webProcessName(), static_cast<long>(pid));
213 #else
214     snprintf(buffer, sizeof(buffer), "#PROCESS UNRESPONSIVE - %s\n", TestController::webProcessName());
215 #endif
216
217     dump(errorMessage, buffer, true);
218     
219     if (!TestController::singleton().usingServerMode())
220         return;
221     
222     if (isatty(fileno(stdin)) || isatty(fileno(stderr)))
223         fputs("Grab an image of the stack, then hit enter...\n", stderr);
224     
225     if (!fgets(buffer, sizeof(buffer), stdin) || strcmp(buffer, "#SAMPLE FINISHED\n"))
226         fprintf(stderr, "Failed receive expected sample response, got:\n\t\"%s\"\nContinuing...\n", buffer);
227 }
228
229 void TestInvocation::dump(const char* textToStdout, const char* textToStderr, bool seenError)
230 {
231     printf("Content-Type: text/plain\n");
232     if (textToStdout)
233         fputs(textToStdout, stdout);
234     if (textToStderr)
235         fputs(textToStderr, stderr);
236
237     fputs("#EOF\n", stdout);
238     fputs("#EOF\n", stderr);
239     if (seenError)
240         fputs("#EOF\n", stdout);
241     fflush(stdout);
242     fflush(stderr);
243 }
244
245 void TestInvocation::forceRepaintDoneCallback(WKErrorRef error, void* context)
246 {
247     // The context may not be valid any more, e.g. if WebKit is invalidating callbacks at process exit.
248     if (error)
249         return;
250
251     TestInvocation* testInvocation = static_cast<TestInvocation*>(context);
252     RELEASE_ASSERT(TestController::singleton().isCurrentInvocation(testInvocation));
253
254     testInvocation->m_gotRepaint = true;
255     TestController::singleton().notifyDone();
256 }
257
258 void TestInvocation::dumpResults()
259 {
260     if (m_shouldDumpResourceLoadStatistics)
261         m_textOutput.append(m_savedResourceLoadStatistics.isNull() ? TestController::singleton().dumpResourceLoadStatistics() : m_savedResourceLoadStatistics);
262
263     if (m_shouldDumpAdClickAttribution)
264         m_textOutput.append(TestController::singleton().dumpAdClickAttribution());
265     
266     if (m_textOutput.length() || !m_audioResult)
267         dump(m_textOutput.toString().utf8().data());
268     else
269         dumpAudio(m_audioResult.get());
270
271     if (m_dumpPixels) {
272         if (m_pixelResult)
273             dumpPixelsAndCompareWithExpected(SnapshotResultType::WebContents, m_repaintRects.get(), m_pixelResult.get());
274         else if (m_pixelResultIsPending) {
275             m_gotRepaint = false;
276             WKPageForceRepaint(TestController::singleton().mainWebView()->page(), this, TestInvocation::forceRepaintDoneCallback);
277             TestController::singleton().runUntil(m_gotRepaint, TestController::noTimeout);
278             dumpPixelsAndCompareWithExpected(SnapshotResultType::WebView, m_repaintRects.get());
279         }
280     }
281
282     fputs("#EOF\n", stdout);
283     fflush(stdout);
284     fflush(stderr);
285 }
286
287 void TestInvocation::dumpAudio(WKDataRef audioData)
288 {
289     size_t length = WKDataGetSize(audioData);
290     if (!length)
291         return;
292
293     const unsigned char* data = WKDataGetBytes(audioData);
294
295     printf("Content-Type: audio/wav\n");
296     printf("Content-Length: %lu\n", static_cast<unsigned long>(length));
297
298     fwrite(data, 1, length, stdout);
299     printf("#EOF\n");
300     fprintf(stderr, "#EOF\n");
301 }
302
303 bool TestInvocation::compareActualHashToExpectedAndDumpResults(const char actualHash[33])
304 {
305     // Compute the hash of the bitmap context pixels
306     fprintf(stdout, "\nActualHash: %s\n", actualHash);
307
308     if (!m_expectedPixelHash.length())
309         return false;
310
311     ASSERT(m_expectedPixelHash.length() == 32);
312     fprintf(stdout, "\nExpectedHash: %s\n", m_expectedPixelHash.c_str());
313
314     // FIXME: Do case insensitive compare.
315     return m_expectedPixelHash == actualHash;
316 }
317
318 void TestInvocation::didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
319 {
320     if (WKStringIsEqualToUTF8CString(messageName, "Error")) {
321         // Set all states to true to stop spinning the runloop.
322         m_gotInitialResponse = true;
323         m_gotFinalMessage = true;
324         m_error = true;
325         TestController::singleton().notifyDone();
326         return;
327     }
328
329     if (WKStringIsEqualToUTF8CString(messageName, "Ack")) {
330         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
331         WKStringRef messageBodyString = static_cast<WKStringRef>(messageBody);
332         if (WKStringIsEqualToUTF8CString(messageBodyString, "BeginTest")) {
333             m_gotInitialResponse = true;
334             TestController::singleton().notifyDone();
335             return;
336         }
337
338         ASSERT_NOT_REACHED();
339     }
340
341     if (WKStringIsEqualToUTF8CString(messageName, "Done")) {
342         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
343         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
344
345         WKRetainPtr<WKStringRef> pixelResultIsPendingKey = adoptWK(WKStringCreateWithUTF8CString("PixelResultIsPending"));
346         WKBooleanRef pixelResultIsPending = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, pixelResultIsPendingKey.get()));
347         m_pixelResultIsPending = WKBooleanGetValue(pixelResultIsPending);
348
349         if (!m_pixelResultIsPending) {
350             WKRetainPtr<WKStringRef> pixelResultKey = adoptWK(WKStringCreateWithUTF8CString("PixelResult"));
351             m_pixelResult = static_cast<WKImageRef>(WKDictionaryGetItemForKey(messageBodyDictionary, pixelResultKey.get()));
352             ASSERT(!m_pixelResult || m_dumpPixels);
353         }
354
355         WKRetainPtr<WKStringRef> repaintRectsKey = adoptWK(WKStringCreateWithUTF8CString("RepaintRects"));
356         m_repaintRects = static_cast<WKArrayRef>(WKDictionaryGetItemForKey(messageBodyDictionary, repaintRectsKey.get()));
357
358         WKRetainPtr<WKStringRef> audioResultKey =  adoptWK(WKStringCreateWithUTF8CString("AudioResult"));
359         m_audioResult = static_cast<WKDataRef>(WKDictionaryGetItemForKey(messageBodyDictionary, audioResultKey.get()));
360
361         done();
362         return;
363     }
364
365     if (WKStringIsEqualToUTF8CString(messageName, "TextOutput")) {
366         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
367         WKStringRef textOutput = static_cast<WKStringRef>(messageBody);
368         m_textOutput.append(toWTFString(textOutput));
369         return;
370     }
371
372     if (WKStringIsEqualToUTF8CString(messageName, "DumpToStdErr")) {
373         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
374         WKStringRef textOutput = static_cast<WKStringRef>(messageBody);
375         fprintf(stderr, "%s", toWTFString(textOutput).utf8().data());
376         return;
377     }
378
379     if (WKStringIsEqualToUTF8CString(messageName, "BeforeUnloadReturnValue")) {
380         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
381         WKBooleanRef beforeUnloadReturnValue = static_cast<WKBooleanRef>(messageBody);
382         TestController::singleton().setBeforeUnloadReturnValue(WKBooleanGetValue(beforeUnloadReturnValue));
383         return;
384     }
385     
386     if (WKStringIsEqualToUTF8CString(messageName, "AddChromeInputField")) {
387         TestController::singleton().mainWebView()->addChromeInputField();
388         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallAddChromeInputFieldCallback"));
389         WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
390         return;
391     }
392
393     if (WKStringIsEqualToUTF8CString(messageName, "RemoveChromeInputField")) {
394         TestController::singleton().mainWebView()->removeChromeInputField();
395         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallRemoveChromeInputFieldCallback"));
396         WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
397         return;
398     }
399     
400     if (WKStringIsEqualToUTF8CString(messageName, "FocusWebView")) {
401         TestController::singleton().mainWebView()->makeWebViewFirstResponder();
402         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallFocusWebViewCallback"));
403         WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
404         return;
405     }
406
407     if (WKStringIsEqualToUTF8CString(messageName, "SetBackingScaleFactor")) {
408         ASSERT(WKGetTypeID(messageBody) == WKDoubleGetTypeID());
409         double backingScaleFactor = WKDoubleGetValue(static_cast<WKDoubleRef>(messageBody));
410         WKPageSetCustomBackingScaleFactor(TestController::singleton().mainWebView()->page(), backingScaleFactor);
411
412         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallSetBackingScaleFactorCallback"));
413         WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
414         return;
415     }
416
417     if (WKStringIsEqualToUTF8CString(messageName, "SimulateWebNotificationClick")) {
418         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
419         uint64_t notificationID = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
420         TestController::singleton().simulateWebNotificationClick(notificationID);
421         return;
422     }
423
424     if (WKStringIsEqualToUTF8CString(messageName, "SetAddsVisitedLinks")) {
425         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
426         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(messageBody);
427         WKPageSetAddsVisitedLinks(TestController::singleton().mainWebView()->page(), WKBooleanGetValue(enabledWK));
428         return;
429     }
430
431     if (WKStringIsEqualToUTF8CString(messageName, "SetGeolocationPermission")) {
432         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
433         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(messageBody);
434         TestController::singleton().setGeolocationPermission(WKBooleanGetValue(enabledWK));
435         return;
436     }
437
438     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGeolocationPosition")) {
439         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
440         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
441
442         WKRetainPtr<WKStringRef> latitudeKeyWK = adoptWK(WKStringCreateWithUTF8CString("latitude"));
443         WKDoubleRef latitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, latitudeKeyWK.get()));
444         double latitude = WKDoubleGetValue(latitudeWK);
445
446         WKRetainPtr<WKStringRef> longitudeKeyWK = adoptWK(WKStringCreateWithUTF8CString("longitude"));
447         WKDoubleRef longitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, longitudeKeyWK.get()));
448         double longitude = WKDoubleGetValue(longitudeWK);
449
450         WKRetainPtr<WKStringRef> accuracyKeyWK = adoptWK(WKStringCreateWithUTF8CString("accuracy"));
451         WKDoubleRef accuracyWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, accuracyKeyWK.get()));
452         double accuracy = WKDoubleGetValue(accuracyWK);
453
454         WKRetainPtr<WKStringRef> providesAltitudeKeyWK = adoptWK(WKStringCreateWithUTF8CString("providesAltitude"));
455         WKBooleanRef providesAltitudeWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesAltitudeKeyWK.get()));
456         bool providesAltitude = WKBooleanGetValue(providesAltitudeWK);
457
458         WKRetainPtr<WKStringRef> altitudeKeyWK = adoptWK(WKStringCreateWithUTF8CString("altitude"));
459         WKDoubleRef altitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, altitudeKeyWK.get()));
460         double altitude = WKDoubleGetValue(altitudeWK);
461
462         WKRetainPtr<WKStringRef> providesAltitudeAccuracyKeyWK = adoptWK(WKStringCreateWithUTF8CString("providesAltitudeAccuracy"));
463         WKBooleanRef providesAltitudeAccuracyWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesAltitudeAccuracyKeyWK.get()));
464         bool providesAltitudeAccuracy = WKBooleanGetValue(providesAltitudeAccuracyWK);
465
466         WKRetainPtr<WKStringRef> altitudeAccuracyKeyWK = adoptWK(WKStringCreateWithUTF8CString("altitudeAccuracy"));
467         WKDoubleRef altitudeAccuracyWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, altitudeAccuracyKeyWK.get()));
468         double altitudeAccuracy = WKDoubleGetValue(altitudeAccuracyWK);
469
470         WKRetainPtr<WKStringRef> providesHeadingKeyWK = adoptWK(WKStringCreateWithUTF8CString("providesHeading"));
471         WKBooleanRef providesHeadingWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesHeadingKeyWK.get()));
472         bool providesHeading = WKBooleanGetValue(providesHeadingWK);
473
474         WKRetainPtr<WKStringRef> headingKeyWK = adoptWK(WKStringCreateWithUTF8CString("heading"));
475         WKDoubleRef headingWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, headingKeyWK.get()));
476         double heading = WKDoubleGetValue(headingWK);
477
478         WKRetainPtr<WKStringRef> providesSpeedKeyWK = adoptWK(WKStringCreateWithUTF8CString("providesSpeed"));
479         WKBooleanRef providesSpeedWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesSpeedKeyWK.get()));
480         bool providesSpeed = WKBooleanGetValue(providesSpeedWK);
481
482         WKRetainPtr<WKStringRef> speedKeyWK = adoptWK(WKStringCreateWithUTF8CString("speed"));
483         WKDoubleRef speedWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, speedKeyWK.get()));
484         double speed = WKDoubleGetValue(speedWK);
485
486         WKRetainPtr<WKStringRef> providesFloorLevelKeyWK = adoptWK(WKStringCreateWithUTF8CString("providesFloorLevel"));
487         WKBooleanRef providesFloorLevelWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesFloorLevelKeyWK.get()));
488         bool providesFloorLevel = WKBooleanGetValue(providesFloorLevelWK);
489
490         WKRetainPtr<WKStringRef> floorLevelKeyWK = adoptWK(WKStringCreateWithUTF8CString("floorLevel"));
491         WKDoubleRef floorLevelWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, floorLevelKeyWK.get()));
492         double floorLevel = WKDoubleGetValue(floorLevelWK);
493
494         TestController::singleton().setMockGeolocationPosition(latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed, providesFloorLevel, floorLevel);
495         return;
496     }
497
498     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGeolocationPositionUnavailableError")) {
499         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
500         WKStringRef errorMessage = static_cast<WKStringRef>(messageBody);
501         TestController::singleton().setMockGeolocationPositionUnavailableError(errorMessage);
502         return;
503     }
504
505     if (WKStringIsEqualToUTF8CString(messageName, "SetUserMediaPermission")) {
506         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
507         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(messageBody);
508         TestController::singleton().setUserMediaPermission(WKBooleanGetValue(enabledWK));
509         return;
510     }
511
512     if (WKStringIsEqualToUTF8CString(messageName, "ResetUserMediaPermission")) {
513         TestController::singleton().resetUserMediaPermission();
514         return;
515     }
516
517     if (WKStringIsEqualToUTF8CString(messageName, "SetUserMediaPersistentPermissionForOrigin")) {
518         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
519         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
520
521         WKRetainPtr<WKStringRef> permissionKeyWK = adoptWK(WKStringCreateWithUTF8CString("permission"));
522         WKBooleanRef permissionWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, permissionKeyWK.get()));
523         bool permission = WKBooleanGetValue(permissionWK);
524
525         WKRetainPtr<WKStringRef> originKey = adoptWK(WKStringCreateWithUTF8CString("origin"));
526         WKStringRef originWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, originKey.get()));
527
528         WKRetainPtr<WKStringRef> parentOriginKey = adoptWK(WKStringCreateWithUTF8CString("parentOrigin"));
529         WKStringRef parentOriginWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, parentOriginKey.get()));
530
531         TestController::singleton().setUserMediaPersistentPermissionForOrigin(permission, originWK, parentOriginWK);
532         return;
533     }
534
535     if (WKStringIsEqualToUTF8CString(messageName, "ResetUserMediaPermissionRequestCountForOrigin")) {
536         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
537         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
538
539         WKRetainPtr<WKStringRef> originKey = adoptWK(WKStringCreateWithUTF8CString("origin"));
540         WKStringRef originWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, originKey.get()));
541
542         WKRetainPtr<WKStringRef> parentOriginKey = adoptWK(WKStringCreateWithUTF8CString("parentOrigin"));
543         WKStringRef parentOriginWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, parentOriginKey.get()));
544         
545         TestController::singleton().resetUserMediaPermissionRequestCountForOrigin(originWK, parentOriginWK);
546         return;
547     }
548
549     if (WKStringIsEqualToUTF8CString(messageName, "SetCacheModel")) {
550         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
551         uint64_t model = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
552         WKContextSetCacheModel(TestController::singleton().context(), model);
553         return;
554     }
555
556     if (WKStringIsEqualToUTF8CString(messageName, "SetCustomPolicyDelegate")) {
557         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
558         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
559
560         WKRetainPtr<WKStringRef> enabledKeyWK = adoptWK(WKStringCreateWithUTF8CString("enabled"));
561         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, enabledKeyWK.get()));
562         bool enabled = WKBooleanGetValue(enabledWK);
563
564         WKRetainPtr<WKStringRef> permissiveKeyWK = adoptWK(WKStringCreateWithUTF8CString("permissive"));
565         WKBooleanRef permissiveWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, permissiveKeyWK.get()));
566         bool permissive = WKBooleanGetValue(permissiveWK);
567
568         TestController::singleton().setCustomPolicyDelegate(enabled, permissive);
569         return;
570     }
571
572     if (WKStringIsEqualToUTF8CString(messageName, "SetHidden")) {
573         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
574         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
575
576         WKRetainPtr<WKStringRef> isInitialKeyWK = adoptWK(WKStringCreateWithUTF8CString("hidden"));
577         WKBooleanRef hiddenWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, isInitialKeyWK.get()));
578         bool hidden = WKBooleanGetValue(hiddenWK);
579
580         TestController::singleton().setHidden(hidden);
581         return;
582     }
583
584     if (WKStringIsEqualToUTF8CString(messageName, "ProcessWorkQueue")) {
585         if (TestController::singleton().workQueueManager().processWorkQueue()) {
586             WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("WorkQueueProcessedCallback"));
587             WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
588         }
589         return;
590     }
591
592     if (WKStringIsEqualToUTF8CString(messageName, "QueueBackNavigation")) {
593         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
594         uint64_t stepCount = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
595         TestController::singleton().workQueueManager().queueBackNavigation(stepCount);
596         return;
597     }
598
599     if (WKStringIsEqualToUTF8CString(messageName, "QueueForwardNavigation")) {
600         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
601         uint64_t stepCount = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
602         TestController::singleton().workQueueManager().queueForwardNavigation(stepCount);
603         return;
604     }
605
606     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoad")) {
607         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
608         WKDictionaryRef loadDataDictionary = static_cast<WKDictionaryRef>(messageBody);
609
610         WKRetainPtr<WKStringRef> urlKey = adoptWK(WKStringCreateWithUTF8CString("url"));
611         WKStringRef urlWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, urlKey.get()));
612
613         WKRetainPtr<WKStringRef> targetKey = adoptWK(WKStringCreateWithUTF8CString("target"));
614         WKStringRef targetWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, targetKey.get()));
615
616         WKRetainPtr<WKStringRef> shouldOpenExternalURLsKey = adoptWK(WKStringCreateWithUTF8CString("shouldOpenExternalURLs"));
617         WKBooleanRef shouldOpenExternalURLsValueWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(loadDataDictionary, shouldOpenExternalURLsKey.get()));
618
619         TestController::singleton().workQueueManager().queueLoad(toWTFString(urlWK), toWTFString(targetWK), WKBooleanGetValue(shouldOpenExternalURLsValueWK));
620         return;
621     }
622
623     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoadHTMLString")) {
624         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
625         WKDictionaryRef loadDataDictionary = static_cast<WKDictionaryRef>(messageBody);
626
627         WKRetainPtr<WKStringRef> contentKey = adoptWK(WKStringCreateWithUTF8CString("content"));
628         WKStringRef contentWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, contentKey.get()));
629
630         WKRetainPtr<WKStringRef> baseURLKey = adoptWK(WKStringCreateWithUTF8CString("baseURL"));
631         WKStringRef baseURLWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, baseURLKey.get()));
632
633         WKRetainPtr<WKStringRef> unreachableURLKey = adoptWK(WKStringCreateWithUTF8CString("unreachableURL"));
634         WKStringRef unreachableURLWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, unreachableURLKey.get()));
635
636         TestController::singleton().workQueueManager().queueLoadHTMLString(toWTFString(contentWK), baseURLWK ? toWTFString(baseURLWK) : String(), unreachableURLWK ? toWTFString(unreachableURLWK) : String());
637         return;
638     }
639
640     if (WKStringIsEqualToUTF8CString(messageName, "QueueReload")) {
641         TestController::singleton().workQueueManager().queueReload();
642         return;
643     }
644
645     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoadingScript")) {
646         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
647         WKStringRef script = static_cast<WKStringRef>(messageBody);
648         TestController::singleton().workQueueManager().queueLoadingScript(toWTFString(script));
649         return;
650     }
651
652     if (WKStringIsEqualToUTF8CString(messageName, "QueueNonLoadingScript")) {
653         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
654         WKStringRef script = static_cast<WKStringRef>(messageBody);
655         TestController::singleton().workQueueManager().queueNonLoadingScript(toWTFString(script));
656         return;
657     }
658
659     if (WKStringIsEqualToUTF8CString(messageName, "SetRejectsProtectionSpaceAndContinueForAuthenticationChallenges")) {
660         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
661         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
662         TestController::singleton().setRejectsProtectionSpaceAndContinueForAuthenticationChallenges(WKBooleanGetValue(value));
663         return;
664     }
665
666     if (WKStringIsEqualToUTF8CString(messageName, "SetHandlesAuthenticationChallenges")) {
667         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
668         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
669         TestController::singleton().setHandlesAuthenticationChallenges(WKBooleanGetValue(value));
670         return;
671     }
672
673     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldLogCanAuthenticateAgainstProtectionSpace")) {
674         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
675         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
676         TestController::singleton().setShouldLogCanAuthenticateAgainstProtectionSpace(WKBooleanGetValue(value));
677         return;
678     }
679
680     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldLogDownloadCallbacks")) {
681         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
682         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
683         TestController::singleton().setShouldLogDownloadCallbacks(WKBooleanGetValue(value));
684         return;
685     }
686
687     if (WKStringIsEqualToUTF8CString(messageName, "SetAuthenticationUsername")) {
688         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
689         WKStringRef username = static_cast<WKStringRef>(messageBody);
690         TestController::singleton().setAuthenticationUsername(toWTFString(username));
691         return;
692     }
693
694     if (WKStringIsEqualToUTF8CString(messageName, "SetAuthenticationPassword")) {
695         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
696         WKStringRef password = static_cast<WKStringRef>(messageBody);
697         TestController::singleton().setAuthenticationPassword(toWTFString(password));
698         return;
699     }
700
701     if (WKStringIsEqualToUTF8CString(messageName, "SetBlockAllPlugins")) {
702         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
703         WKBooleanRef shouldBlock = static_cast<WKBooleanRef>(messageBody);
704         TestController::singleton().setBlockAllPlugins(WKBooleanGetValue(shouldBlock));
705         return;
706     }
707
708     if (WKStringIsEqualToUTF8CString(messageName, "SetPluginSupportedMode")) {
709         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
710         WKStringRef mode = static_cast<WKStringRef>(messageBody);
711         TestController::singleton().setPluginSupportedMode(toWTFString(mode));
712         return;
713     }
714
715     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldDecideNavigationPolicyAfterDelay")) {
716         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
717         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
718         TestController::singleton().setShouldDecideNavigationPolicyAfterDelay(WKBooleanGetValue(value));
719         return;
720     }
721
722     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldDecideResponsePolicyAfterDelay")) {
723         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
724         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
725         TestController::singleton().setShouldDecideResponsePolicyAfterDelay(WKBooleanGetValue(value));
726         return;
727     }
728
729     if (WKStringIsEqualToUTF8CString(messageName, "SetNavigationGesturesEnabled")) {
730         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
731         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
732         TestController::singleton().setNavigationGesturesEnabled(WKBooleanGetValue(value));
733         return;
734     }
735     
736     if (WKStringIsEqualToUTF8CString(messageName, "SetIgnoresViewportScaleLimits")) {
737         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
738         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
739         TestController::singleton().setIgnoresViewportScaleLimits(WKBooleanGetValue(value));
740         return;
741     }
742
743     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldDownloadUndisplayableMIMETypes")) {
744         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
745         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
746         TestController::singleton().setShouldDownloadUndisplayableMIMETypes(WKBooleanGetValue(value));
747         return;
748     }
749
750     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldAllowDeviceOrientationAndMotionAccess")) {
751         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
752         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
753         TestController::singleton().setShouldAllowDeviceOrientationAndMotionAccess(WKBooleanGetValue(value));
754         return;
755     }
756
757     if (WKStringIsEqualToUTF8CString(messageName, "RunUIProcessScript")) {
758         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
759         WKRetainPtr<WKStringRef> scriptKey = adoptWK(WKStringCreateWithUTF8CString("Script"));
760         WKRetainPtr<WKStringRef> callbackIDKey = adoptWK(WKStringCreateWithUTF8CString("CallbackID"));
761
762         UIScriptInvocationData* invocationData = new UIScriptInvocationData();
763         invocationData->testInvocation = this;
764         invocationData->callbackID = (unsigned)WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, callbackIDKey.get())));
765         invocationData->scriptString = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, scriptKey.get()));
766         m_pendingUIScriptInvocationData = invocationData;
767         WKPageCallAfterNextPresentationUpdate(TestController::singleton().mainWebView()->page(), invocationData, runUISideScriptAfterUpdateCallback);
768         return;
769     }
770
771     if (WKStringIsEqualToUTF8CString(messageName, "InstallCustomMenuAction")) {
772         auto messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
773         WKRetainPtr<WKStringRef> nameKey = adoptWK(WKStringCreateWithUTF8CString("name"));
774         WKRetainPtr<WKStringRef> name = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, nameKey.get()));
775         WKRetainPtr<WKStringRef> dismissesAutomaticallyKey = adoptWK(WKStringCreateWithUTF8CString("dismissesAutomatically"));
776         auto dismissesAutomatically = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, dismissesAutomaticallyKey.get()));
777         TestController::singleton().installCustomMenuAction(toWTFString(name.get()), WKBooleanGetValue(dismissesAutomatically));
778         return;
779     }
780
781     if (WKStringIsEqualToUTF8CString(messageName, "SetAllowedMenuActions")) {
782         auto messageBodyArray = static_cast<WKArrayRef>(messageBody);
783         auto size = WKArrayGetSize(messageBodyArray);
784         Vector<String> actions;
785         actions.reserveInitialCapacity(size);
786         for (size_t index = 0; index < size; ++index)
787             actions.append(toWTFString(static_cast<WKStringRef>(WKArrayGetItemAtIndex(messageBodyArray, index))));
788         TestController::singleton().setAllowedMenuActions(actions);
789         return;
790     }
791
792     if (WKStringIsEqualToUTF8CString(messageName, "SetOpenPanelFileURLs")) {
793         TestController::singleton().setOpenPanelFileURLs(static_cast<WKArrayRef>(messageBody));
794         return;
795     }
796
797 #if PLATFORM(IOS_FAMILY)
798     if (WKStringIsEqualToUTF8CString(messageName, "SetOpenPanelFileURLsMediaIcon")) {
799         TestController::singleton().setOpenPanelFileURLsMediaIcon(static_cast<WKDataRef>(messageBody));
800         return;
801     }
802 #endif
803
804     ASSERT_NOT_REACHED();
805 }
806
807 WKRetainPtr<WKTypeRef> TestInvocation::didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
808 {
809     if (WKStringIsEqualToUTF8CString(messageName, "Initialization")) {
810         auto settings = createTestSettingsDictionary();
811         WKRetainPtr<WKStringRef> resumeTestingKey = adoptWK(WKStringCreateWithUTF8CString("ResumeTesting"));
812         WKRetainPtr<WKBooleanRef> resumeTestingValue = adoptWK(WKBooleanCreate(m_startedTesting));
813         WKDictionarySetItem(settings.get(), resumeTestingKey.get(), resumeTestingValue.get());
814         return settings;
815     }
816
817     if (WKStringIsEqualToUTF8CString(messageName, "SetDumpPixels")) {
818         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
819         m_dumpPixels = WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody));
820         return nullptr;
821     }
822     if (WKStringIsEqualToUTF8CString(messageName, "GetDumpPixels"))
823         return adoptWK(WKBooleanCreate(m_dumpPixels));
824
825     if (WKStringIsEqualToUTF8CString(messageName, "SetWhatToDump")) {
826         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
827         m_whatToDump = static_cast<WhatToDump>(WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody)));
828         return nullptr;
829     }
830     if (WKStringIsEqualToUTF8CString(messageName, "GetWhatToDump"))
831         return adoptWK(WKUInt64Create(static_cast<uint64_t>(m_whatToDump)));
832
833     if (WKStringIsEqualToUTF8CString(messageName, "SetWaitUntilDone")) {
834         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
835         setWaitUntilDone(static_cast<unsigned char>(WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody))));
836         return nullptr;
837     }
838     if (WKStringIsEqualToUTF8CString(messageName, "GetWaitUntilDone"))
839         return adoptWK(WKBooleanCreate(m_waitUntilDone));
840
841     if (WKStringIsEqualToUTF8CString(messageName, "SetDumpFrameLoadCallbacks")) {
842         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
843         m_dumpFrameLoadCallbacks = static_cast<unsigned char>(WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody)));
844         return nullptr;
845     }
846     if (WKStringIsEqualToUTF8CString(messageName, "GetDumpFrameLoadCallbacks"))
847         return adoptWK(WKBooleanCreate(m_dumpFrameLoadCallbacks));
848
849     if (WKStringIsEqualToUTF8CString(messageName, "SetCanOpenWindows")) {
850         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
851         m_canOpenWindows = static_cast<unsigned char>(WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody)));
852         return nullptr;
853     }
854
855     if (WKStringIsEqualToUTF8CString(messageName, "SetWindowIsKey")) {
856         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
857         WKBooleanRef isKeyValue = static_cast<WKBooleanRef>(messageBody);
858         TestController::singleton().mainWebView()->setWindowIsKey(WKBooleanGetValue(isKeyValue));
859         return nullptr;
860     }
861
862     if (WKStringIsEqualToUTF8CString(messageName, "SetViewSize")) {
863         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
864
865         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
866         WKRetainPtr<WKStringRef> widthKey = adoptWK(WKStringCreateWithUTF8CString("width"));
867         WKRetainPtr<WKStringRef> heightKey = adoptWK(WKStringCreateWithUTF8CString("height"));
868
869         WKDoubleRef widthWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, widthKey.get()));
870         WKDoubleRef heightWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, heightKey.get()));
871
872         TestController::singleton().mainWebView()->resizeTo(WKDoubleGetValue(widthWK), WKDoubleGetValue(heightWK));
873         return nullptr;
874     }
875
876     if (WKStringIsEqualToUTF8CString(messageName, "IsGeolocationClientActive")) {
877         bool isActive = TestController::singleton().isGeolocationProviderActive();
878         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(isActive));
879         return result;
880     }
881
882     if (WKStringIsEqualToUTF8CString(messageName, "IsWorkQueueEmpty")) {
883         bool isEmpty = TestController::singleton().workQueueManager().isWorkQueueEmpty();
884         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(isEmpty));
885         return result;
886     }
887
888     if (WKStringIsEqualToUTF8CString(messageName, "DidReceiveServerRedirectForProvisionalNavigation")) {
889         WKRetainPtr<WKBooleanRef> result = adoptWK(WKBooleanCreate(TestController::singleton().didReceiveServerRedirectForProvisionalNavigation()));
890         return result;
891     }
892
893     if (WKStringIsEqualToUTF8CString(messageName, "ClearDidReceiveServerRedirectForProvisionalNavigation")) {
894         TestController::singleton().clearDidReceiveServerRedirectForProvisionalNavigation();
895         return nullptr;
896     }
897
898     if (WKStringIsEqualToUTF8CString(messageName, "SecureEventInputIsEnabled")) {
899 #if PLATFORM(MAC) && !PLATFORM(IOS_FAMILY)
900         WKRetainPtr<WKBooleanRef> result = adoptWK(WKBooleanCreate(IsSecureEventInputEnabled()));
901 #else
902         WKRetainPtr<WKBooleanRef> result = adoptWK(WKBooleanCreate(false));
903 #endif
904         return result;
905     }
906
907     if (WKStringIsEqualToUTF8CString(messageName, "SetCustomUserAgent")) {
908         WKStringRef userAgent = static_cast<WKStringRef>(messageBody);
909         WKPageSetCustomUserAgent(TestController::singleton().mainWebView()->page(), userAgent);
910         return nullptr;
911     }
912
913     if (WKStringIsEqualToUTF8CString(messageName, "SetStorageAccessAPIEnabled")) {
914         WKBooleanRef accept = static_cast<WKBooleanRef>(messageBody);
915         WKContextSetStorageAccessAPIEnabled(TestController::singleton().context(), WKBooleanGetValue(accept));
916         return nullptr;
917     }
918
919     if (WKStringIsEqualToUTF8CString(messageName, "GetAllStorageAccessEntries")) {
920         TestController::singleton().getAllStorageAccessEntries();
921         return nullptr;
922     }
923
924     if (WKStringIsEqualToUTF8CString(messageName, "SetAllowsAnySSLCertificate")) {
925         TestController::singleton().setAllowsAnySSLCertificate(WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody)));
926         return nullptr;
927     }
928     
929     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldSwapToEphemeralSessionOnNextNavigation")) {
930         TestController::singleton().setShouldSwapToEphemeralSessionOnNextNavigation(WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody)));
931         return nullptr;
932     }
933     
934     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldSwapToDefaultSessionOnNextNavigation")) {
935         TestController::singleton().setShouldSwapToDefaultSessionOnNextNavigation(WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody)));
936         return nullptr;
937     }
938
939     if (WKStringIsEqualToUTF8CString(messageName, "ImageCountInGeneralPasteboard")) {
940         unsigned count = TestController::singleton().imageCountInGeneralPasteboard();
941         WKRetainPtr<WKUInt64Ref> result = adoptWK(WKUInt64Create(count));
942         return result;
943     }
944     
945     if (WKStringIsEqualToUTF8CString(messageName, "DeleteAllIndexedDatabases")) {
946         WKWebsiteDataStoreRemoveAllIndexedDatabases(TestController::websiteDataStore(), nullptr, { });
947         return nullptr;
948     }
949
950     if (WKStringIsEqualToUTF8CString(messageName, "AddMockMediaDevice")) {
951         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
952
953         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
954         WKRetainPtr<WKStringRef> persistentIDKey = adoptWK(WKStringCreateWithUTF8CString("PersistentID"));
955         WKRetainPtr<WKStringRef> labelKey = adoptWK(WKStringCreateWithUTF8CString("Label"));
956         WKRetainPtr<WKStringRef> typeKey = adoptWK(WKStringCreateWithUTF8CString("Type"));
957
958         auto persistentID = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, persistentIDKey.get()));
959         auto label = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, labelKey.get()));
960         auto type = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, typeKey.get()));
961
962         TestController::singleton().addMockMediaDevice(persistentID, label, type);
963         return nullptr;
964     }
965
966     if (WKStringIsEqualToUTF8CString(messageName, "ClearMockMediaDevices")) {
967         TestController::singleton().clearMockMediaDevices();
968         return nullptr;
969     }
970
971     if (WKStringIsEqualToUTF8CString(messageName, "RemoveMockMediaDevice")) {
972         WKStringRef persistentId = static_cast<WKStringRef>(messageBody);
973
974         TestController::singleton().removeMockMediaDevice(persistentId);
975         return nullptr;
976     }
977
978     if (WKStringIsEqualToUTF8CString(messageName, "ResetMockMediaDevices")) {
979         TestController::singleton().resetMockMediaDevices();
980         return nullptr;
981     }
982
983 #if PLATFORM(MAC)
984     if (WKStringIsEqualToUTF8CString(messageName, "ConnectMockGamepad")) {
985         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
986
987         uint64_t index = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
988         WebCoreTestSupport::connectMockGamepad(index);
989         
990         return nullptr;
991     }
992
993     if (WKStringIsEqualToUTF8CString(messageName, "DisconnectMockGamepad")) {
994         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
995
996         uint64_t index = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
997         WebCoreTestSupport::disconnectMockGamepad(index);
998
999         return nullptr;
1000     }
1001
1002     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGamepadDetails")) {
1003         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1004
1005         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1006         WKRetainPtr<WKStringRef> gamepadIndexKey = adoptWK(WKStringCreateWithUTF8CString("GamepadIndex"));
1007         WKRetainPtr<WKStringRef> gamepadIDKey = adoptWK(WKStringCreateWithUTF8CString("GamepadID"));
1008         WKRetainPtr<WKStringRef> axisCountKey = adoptWK(WKStringCreateWithUTF8CString("AxisCount"));
1009         WKRetainPtr<WKStringRef> buttonCountKey = adoptWK(WKStringCreateWithUTF8CString("ButtonCount"));
1010
1011         WKUInt64Ref gamepadIndex = static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, gamepadIndexKey.get()));
1012         WKStringRef gamepadID = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, gamepadIDKey.get()));
1013         WKUInt64Ref axisCount = static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, axisCountKey.get()));
1014         WKUInt64Ref buttonCount = static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, buttonCountKey.get()));
1015
1016         WebCoreTestSupport::setMockGamepadDetails(WKUInt64GetValue(gamepadIndex), toWTFString(gamepadID), WKUInt64GetValue(axisCount), WKUInt64GetValue(buttonCount));
1017         return nullptr;
1018     }
1019
1020     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGamepadAxisValue")) {
1021         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1022
1023         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1024         WKRetainPtr<WKStringRef> gamepadIndexKey = adoptWK(WKStringCreateWithUTF8CString("GamepadIndex"));
1025         WKRetainPtr<WKStringRef> axisIndexKey = adoptWK(WKStringCreateWithUTF8CString("AxisIndex"));
1026         WKRetainPtr<WKStringRef> valueKey = adoptWK(WKStringCreateWithUTF8CString("Value"));
1027
1028         WKUInt64Ref gamepadIndex = static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, gamepadIndexKey.get()));
1029         WKUInt64Ref axisIndex = static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, axisIndexKey.get()));
1030         WKDoubleRef value = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, valueKey.get()));
1031
1032         WebCoreTestSupport::setMockGamepadAxisValue(WKUInt64GetValue(gamepadIndex), WKUInt64GetValue(axisIndex), WKDoubleGetValue(value));
1033
1034         return nullptr;
1035     }
1036
1037     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGamepadButtonValue")) {
1038         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1039
1040         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1041         WKRetainPtr<WKStringRef> gamepadIndexKey = adoptWK(WKStringCreateWithUTF8CString("GamepadIndex"));
1042         WKRetainPtr<WKStringRef> buttonIndexKey = adoptWK(WKStringCreateWithUTF8CString("ButtonIndex"));
1043         WKRetainPtr<WKStringRef> valueKey = adoptWK(WKStringCreateWithUTF8CString("Value"));
1044
1045         WKUInt64Ref gamepadIndex = static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, gamepadIndexKey.get()));
1046         WKUInt64Ref buttonIndex = static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, buttonIndexKey.get()));
1047         WKDoubleRef value = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, valueKey.get()));
1048
1049         WebCoreTestSupport::setMockGamepadButtonValue(WKUInt64GetValue(gamepadIndex), WKUInt64GetValue(buttonIndex), WKDoubleGetValue(value));
1050
1051         return nullptr;
1052     }
1053 #endif // PLATFORM(MAC)
1054
1055     if (WKStringIsEqualToUTF8CString(messageName, "UserMediaPermissionRequestCountForOrigin")) {
1056         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1057         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1058
1059         WKRetainPtr<WKStringRef> originKey = adoptWK(WKStringCreateWithUTF8CString("origin"));
1060         WKStringRef originWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, originKey.get()));
1061
1062         WKRetainPtr<WKStringRef> parentOriginKey = adoptWK(WKStringCreateWithUTF8CString("parentOrigin"));
1063         WKStringRef parentOriginWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, parentOriginKey.get()));
1064         
1065         unsigned count = TestController::singleton().userMediaPermissionRequestCountForOrigin(originWK, parentOriginWK);
1066         WKRetainPtr<WKUInt64Ref> result = adoptWK(WKUInt64Create(count));
1067         return result;
1068     }
1069     if (WKStringIsEqualToUTF8CString(messageName, "IsDoingMediaCapture")) {
1070         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(TestController::singleton().isDoingMediaCapture()));
1071         return result;
1072     }
1073
1074     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsDebugMode")) {
1075         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1076         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1077         TestController::singleton().setStatisticsDebugMode(WKBooleanGetValue(value));
1078         return nullptr;
1079     }
1080     
1081     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsPrevalentResourceForDebugMode")) {
1082         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1083         WKStringRef hostName = static_cast<WKStringRef>(messageBody);
1084         TestController::singleton().setStatisticsPrevalentResourceForDebugMode(hostName);
1085         return nullptr;
1086     }
1087     
1088     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsLastSeen")) {
1089         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1090         
1091         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1092         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1093         WKRetainPtr<WKStringRef> valueKey = adoptWK(WKStringCreateWithUTF8CString("Value"));
1094         
1095         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1096         WKDoubleRef value = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, valueKey.get()));
1097         
1098         TestController::singleton().setStatisticsLastSeen(hostName, WKDoubleGetValue(value));
1099         
1100         return nullptr;
1101     }
1102     
1103     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsMergeStatistic")) {
1104         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1105
1106         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1107         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1108         WKRetainPtr<WKStringRef> topFrameDomain1Key = adoptWK(WKStringCreateWithUTF8CString("TopFrameDomain1"));
1109         WKRetainPtr<WKStringRef> topFrameDomain2Key = adoptWK(WKStringCreateWithUTF8CString("TopFrameDomain2"));
1110         WKRetainPtr<WKStringRef> lastSeenKey = adoptWK(WKStringCreateWithUTF8CString("LastSeen"));
1111         WKRetainPtr<WKStringRef> hadUserInteractionKey = adoptWK(WKStringCreateWithUTF8CString("HadUserInteraction"));
1112         WKRetainPtr<WKStringRef> mostRecentUserInteractionKey = adoptWK(WKStringCreateWithUTF8CString("MostRecentUserInteraction"));
1113         WKRetainPtr<WKStringRef> isGrandfatheredKey = adoptWK(WKStringCreateWithUTF8CString("IsGrandfathered"));
1114         WKRetainPtr<WKStringRef> isPrevalentKey = adoptWK(WKStringCreateWithUTF8CString("IsPrevalent"));
1115         WKRetainPtr<WKStringRef> isVeryPrevalentKey = adoptWK(WKStringCreateWithUTF8CString("IsVeryPrevalent"));
1116         WKRetainPtr<WKStringRef> dataRecordsRemovedKey = adoptWK(WKStringCreateWithUTF8CString("DataRecordsRemoved"));
1117         WKRetainPtr<WKStringRef> timesAccessedFirstPartyInteractionKey = adoptWK(WKStringCreateWithUTF8CString("TimesAccessedFirstPartyInteraction"));
1118
1119         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1120         WKStringRef topFrameDomain1 = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, topFrameDomain1Key.get()));
1121         WKStringRef topFrameDomain2 = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, topFrameDomain2Key.get()));
1122         WKDoubleRef lastSeen = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, lastSeenKey.get()));
1123         WKBooleanRef hadUserInteraction = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hadUserInteractionKey.get()));
1124         WKDoubleRef mostRecentUserInteraction = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, mostRecentUserInteractionKey.get()));
1125         WKBooleanRef isGrandfathered = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, isGrandfatheredKey.get()));
1126         WKBooleanRef isPrevalent = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, isPrevalentKey.get()));
1127         WKBooleanRef isVeryPrevalent = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, isVeryPrevalentKey.get()));
1128         WKUInt64Ref dataRecordsRemoved = static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, dataRecordsRemovedKey.get()));
1129         
1130         TestController::singleton().setStatisticsMergeStatistic(hostName, topFrameDomain1, topFrameDomain2, WKDoubleGetValue(lastSeen), WKBooleanGetValue(hadUserInteraction), WKDoubleGetValue(mostRecentUserInteraction), WKBooleanGetValue(isGrandfathered), WKBooleanGetValue(isPrevalent), WKBooleanGetValue(isVeryPrevalent), WKUInt64GetValue(dataRecordsRemoved));
1131
1132         return nullptr;
1133     }
1134     
1135     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsPrevalentResource")) {
1136         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1137
1138         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1139         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1140         WKRetainPtr<WKStringRef> valueKey = adoptWK(WKStringCreateWithUTF8CString("Value"));
1141
1142         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1143         WKBooleanRef value = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, valueKey.get()));
1144
1145         TestController::singleton().setStatisticsPrevalentResource(hostName, WKBooleanGetValue(value));
1146         return nullptr;
1147     }
1148
1149     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsVeryPrevalentResource")) {
1150         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1151         
1152         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1153         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1154         WKRetainPtr<WKStringRef> valueKey = adoptWK(WKStringCreateWithUTF8CString("Value"));
1155         
1156         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1157         WKBooleanRef value = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, valueKey.get()));
1158         
1159         TestController::singleton().setStatisticsVeryPrevalentResource(hostName, WKBooleanGetValue(value));
1160         return nullptr;
1161     }
1162     
1163     if (WKStringIsEqualToUTF8CString(messageName, "dumpResourceLoadStatistics")) {
1164         dumpResourceLoadStatistics();
1165         return nullptr;
1166     }
1167
1168     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsPrevalentResource")) {
1169         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1170
1171         WKStringRef hostName = static_cast<WKStringRef>(messageBody);
1172         bool isPrevalent = TestController::singleton().isStatisticsPrevalentResource(hostName);
1173         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(isPrevalent));
1174         return result;
1175     }
1176
1177     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsVeryPrevalentResource")) {
1178         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1179         
1180         WKStringRef hostName = static_cast<WKStringRef>(messageBody);
1181         bool isPrevalent = TestController::singleton().isStatisticsVeryPrevalentResource(hostName);
1182         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(isPrevalent));
1183         return result;
1184     }
1185     
1186     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsRegisteredAsSubresourceUnder")) {
1187         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1188         
1189         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1190         WKRetainPtr<WKStringRef> subresourceHostKey = adoptWK(WKStringCreateWithUTF8CString("SubresourceHost"));
1191         WKRetainPtr<WKStringRef> topFrameHostKey = adoptWK(WKStringCreateWithUTF8CString("TopFrameHost"));
1192         
1193         WKStringRef subresourceHost = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, subresourceHostKey.get()));
1194         WKStringRef topFrameHost = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, topFrameHostKey.get()));
1195         
1196         bool isRegisteredAsSubresourceUnder = TestController::singleton().isStatisticsRegisteredAsSubresourceUnder(subresourceHost, topFrameHost);
1197         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(isRegisteredAsSubresourceUnder));
1198         return result;
1199     }
1200     
1201     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsRegisteredAsSubFrameUnder")) {
1202         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1203         
1204         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1205         WKRetainPtr<WKStringRef> subFrameHostKey = adoptWK(WKStringCreateWithUTF8CString("SubFrameHost"));
1206         WKRetainPtr<WKStringRef> topFrameHostKey = adoptWK(WKStringCreateWithUTF8CString("TopFrameHost"));
1207         
1208         WKStringRef subFrameHost = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, subFrameHostKey.get()));
1209         WKStringRef topFrameHost = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, topFrameHostKey.get()));
1210
1211         bool isRegisteredAsSubFrameUnder = TestController::singleton().isStatisticsRegisteredAsSubFrameUnder(subFrameHost, topFrameHost);
1212         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(isRegisteredAsSubFrameUnder));
1213         return result;
1214     }
1215     
1216     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsRegisteredAsRedirectingTo")) {
1217         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1218         
1219         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1220         WKRetainPtr<WKStringRef> hostRedirectedFromKey = adoptWK(WKStringCreateWithUTF8CString("HostRedirectedFrom"));
1221         WKRetainPtr<WKStringRef> hostRedirectedToKey = adoptWK(WKStringCreateWithUTF8CString("HostRedirectedTo"));
1222         
1223         WKStringRef hostRedirectedFrom = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostRedirectedFromKey.get()));
1224         WKStringRef hostRedirectedTo = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostRedirectedToKey.get()));
1225         
1226         bool isRegisteredAsRedirectingTo = TestController::singleton().isStatisticsRegisteredAsRedirectingTo(hostRedirectedFrom, hostRedirectedTo);
1227         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(isRegisteredAsRedirectingTo));
1228         return result;
1229     }
1230     
1231     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsHasHadUserInteraction")) {
1232         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1233         
1234         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1235         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1236         WKRetainPtr<WKStringRef> valueKey = adoptWK(WKStringCreateWithUTF8CString("Value"));
1237         
1238         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1239         WKBooleanRef value = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, valueKey.get()));
1240         
1241         TestController::singleton().setStatisticsHasHadUserInteraction(hostName, WKBooleanGetValue(value));
1242         return nullptr;
1243     }
1244
1245     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsHasHadUserInteraction")) {
1246         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1247         
1248         WKStringRef hostName = static_cast<WKStringRef>(messageBody);
1249         bool hasHadUserInteraction = TestController::singleton().isStatisticsHasHadUserInteraction(hostName);
1250         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(hasHadUserInteraction));
1251         return result;
1252     }
1253     
1254     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsOnlyInDatabaseOnce")) {
1255         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1256         
1257         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1258         auto subHostKey = adoptWK(WKStringCreateWithUTF8CString("SubHost"));
1259         auto topHostKey = adoptWK(WKStringCreateWithUTF8CString("TopHost"));
1260         
1261         WKStringRef subHost = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, subHostKey.get()));
1262         WKStringRef topHost = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, topHostKey.get()));
1263
1264         bool statisticInDatabaseOnce = TestController::singleton().isStatisticsOnlyInDatabaseOnce(subHost, topHost);
1265         return adoptWK(WKBooleanCreate(statisticInDatabaseOnce));
1266     }
1267     
1268     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsGrandfathered")) {
1269         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1270         
1271         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1272         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1273         WKRetainPtr<WKStringRef> valueKey = adoptWK(WKStringCreateWithUTF8CString("Value"));
1274         
1275         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1276         WKBooleanRef value = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, valueKey.get()));
1277         
1278         TestController::singleton().setStatisticsGrandfathered(hostName, WKBooleanGetValue(value));
1279         return nullptr;
1280     }
1281     
1282     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsGrandfathered")) {
1283         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1284         
1285         WKStringRef hostName = static_cast<WKStringRef>(messageBody);
1286         bool isGrandfathered = TestController::singleton().isStatisticsGrandfathered(hostName);
1287         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(isGrandfathered));
1288         return result;
1289     }
1290
1291     if (WKStringIsEqualToUTF8CString(messageName, "SetUseITPDatabase")) {
1292         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1293
1294         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1295         TestController::singleton().setUseITPDatabase(WKBooleanGetValue(value));
1296         return nullptr;
1297     }
1298     
1299     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsSubframeUnderTopFrameOrigin")) {
1300         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1301         
1302         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1303         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1304         WKRetainPtr<WKStringRef> topFrameHostNameKey = adoptWK(WKStringCreateWithUTF8CString("TopFrameHostName"));
1305         
1306         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1307         WKStringRef topFrameHostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, topFrameHostNameKey.get()));
1308
1309         TestController::singleton().setStatisticsSubframeUnderTopFrameOrigin(hostName, topFrameHostName);
1310         return nullptr;
1311     }
1312     
1313     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsSubresourceUnderTopFrameOrigin")) {
1314         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1315         
1316         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1317         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1318         WKRetainPtr<WKStringRef> topFrameHostNameKey = adoptWK(WKStringCreateWithUTF8CString("TopFrameHostName"));
1319         
1320         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1321         WKStringRef topFrameHostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, topFrameHostNameKey.get()));
1322         
1323         TestController::singleton().setStatisticsSubresourceUnderTopFrameOrigin(hostName, topFrameHostName);
1324         return nullptr;
1325     }
1326     
1327     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsSubresourceUniqueRedirectTo")) {
1328         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1329         
1330         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1331         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1332         WKRetainPtr<WKStringRef> hostNameRedirectedToKey = adoptWK(WKStringCreateWithUTF8CString("HostNameRedirectedTo"));
1333         
1334         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1335         WKStringRef hostNameRedirectedTo = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameRedirectedToKey.get()));
1336         
1337         TestController::singleton().setStatisticsSubresourceUniqueRedirectTo(hostName, hostNameRedirectedTo);
1338         return nullptr;
1339     }
1340
1341     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsSubresourceUniqueRedirectFrom")) {
1342         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1343         
1344         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1345         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1346         WKRetainPtr<WKStringRef> hostNameRedirectedFromKey = adoptWK(WKStringCreateWithUTF8CString("HostNameRedirectedFrom"));
1347         
1348         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1349         WKStringRef hostNameRedirectedFrom = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameRedirectedFromKey.get()));
1350         
1351         TestController::singleton().setStatisticsSubresourceUniqueRedirectFrom(hostName, hostNameRedirectedFrom);
1352         return nullptr;
1353     }
1354     
1355     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsTopFrameUniqueRedirectTo")) {
1356         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1357         
1358         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1359         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1360         WKRetainPtr<WKStringRef> hostNameRedirectedToKey = adoptWK(WKStringCreateWithUTF8CString("HostNameRedirectedTo"));
1361         
1362         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1363         WKStringRef hostNameRedirectedTo = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameRedirectedToKey.get()));
1364         
1365         TestController::singleton().setStatisticsTopFrameUniqueRedirectTo(hostName, hostNameRedirectedTo);
1366         return nullptr;
1367     }
1368     
1369     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsTopFrameUniqueRedirectFrom")) {
1370         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1371         
1372         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1373         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1374         WKRetainPtr<WKStringRef> hostNameRedirectedFromKey = adoptWK(WKStringCreateWithUTF8CString("HostNameRedirectedFrom"));
1375         
1376         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1377         WKStringRef hostNameRedirectedFrom = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameRedirectedFromKey.get()));
1378         
1379         TestController::singleton().setStatisticsTopFrameUniqueRedirectFrom(hostName, hostNameRedirectedFrom);
1380         return nullptr;
1381     }
1382     
1383     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsCrossSiteLoadWithLinkDecoration")) {
1384         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1385         
1386         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1387         auto fromHostKey = adoptWK(WKStringCreateWithUTF8CString("FromHost"));
1388         auto toHostKey = adoptWK(WKStringCreateWithUTF8CString("ToHost"));
1389
1390         WKStringRef fromHost = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, fromHostKey.get()));
1391         WKStringRef toHost = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, toHostKey.get()));
1392         
1393         TestController::singleton().setStatisticsCrossSiteLoadWithLinkDecoration(fromHost, toHost);
1394         return nullptr;
1395     }
1396
1397     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsTimeToLiveUserInteraction")) {
1398         ASSERT(WKGetTypeID(messageBody) == WKDoubleGetTypeID());
1399         WKDoubleRef seconds = static_cast<WKDoubleRef>(messageBody);
1400         TestController::singleton().setStatisticsTimeToLiveUserInteraction(WKDoubleGetValue(seconds));
1401         return nullptr;
1402     }
1403     
1404     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsProcessStatisticsAndDataRecords")) {
1405         TestController::singleton().statisticsProcessStatisticsAndDataRecords();
1406         return nullptr;
1407     }
1408     
1409     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsUpdateCookieBlocking")) {
1410         TestController::singleton().statisticsUpdateCookieBlocking();
1411         return nullptr;
1412     }
1413
1414     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsSubmitTelemetry")) {
1415         TestController::singleton().statisticsSubmitTelemetry();
1416         return nullptr;
1417     }
1418     
1419     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsNotifyPagesWhenDataRecordsWereScanned")) {
1420         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1421         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1422         TestController::singleton().setStatisticsNotifyPagesWhenDataRecordsWereScanned(WKBooleanGetValue(value));
1423         return nullptr;
1424     }
1425
1426     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsSetIsRunningTest")) {
1427         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1428         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1429         TestController::singleton().setStatisticsIsRunningTest(WKBooleanGetValue(value));
1430         return nullptr;
1431     }
1432     
1433     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsNotifyPagesWhenTelemetryWasCaptured")) {
1434         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1435         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1436         TestController::singleton().setStatisticsNotifyPagesWhenTelemetryWasCaptured(WKBooleanGetValue(value));
1437         return nullptr;
1438     }
1439     
1440     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsShouldClassifyResourcesBeforeDataRecordsRemoval")) {
1441         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1442         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1443         TestController::singleton().setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(WKBooleanGetValue(value));
1444         return nullptr;
1445     }
1446
1447     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsMinimumTimeBetweenDataRecordsRemoval")) {
1448         ASSERT(WKGetTypeID(messageBody) == WKDoubleGetTypeID());
1449         WKDoubleRef seconds = static_cast<WKDoubleRef>(messageBody);
1450         TestController::singleton().setStatisticsMinimumTimeBetweenDataRecordsRemoval(WKDoubleGetValue(seconds));
1451         return nullptr;
1452     }
1453     
1454     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsGrandfatheringTime")) {
1455         ASSERT(WKGetTypeID(messageBody) == WKDoubleGetTypeID());
1456         WKDoubleRef seconds = static_cast<WKDoubleRef>(messageBody);
1457         TestController::singleton().setStatisticsGrandfatheringTime(WKDoubleGetValue(seconds));
1458         return nullptr;
1459     }
1460     
1461     if (WKStringIsEqualToUTF8CString(messageName, "SetMaxStatisticsEntries")) {
1462         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
1463         WKUInt64Ref entries = static_cast<WKUInt64Ref>(messageBody);
1464         TestController::singleton().setStatisticsMaxStatisticsEntries(WKUInt64GetValue(entries));
1465         return nullptr;
1466     }
1467     
1468     if (WKStringIsEqualToUTF8CString(messageName, "SetPruneEntriesDownTo")) {
1469         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
1470         WKUInt64Ref entries = static_cast<WKUInt64Ref>(messageBody);
1471         TestController::singleton().setStatisticsPruneEntriesDownTo(WKUInt64GetValue(entries));
1472         return nullptr;
1473     }
1474     
1475     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsClearInMemoryAndPersistentStore")) {
1476         TestController::singleton().statisticsClearInMemoryAndPersistentStore();
1477         return nullptr;
1478     }
1479     
1480     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsClearInMemoryAndPersistentStoreModifiedSinceHours")) {
1481         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
1482         WKUInt64Ref hours = static_cast<WKUInt64Ref>(messageBody);
1483         TestController::singleton().statisticsClearInMemoryAndPersistentStoreModifiedSinceHours(WKUInt64GetValue(hours));
1484         return nullptr;
1485     }
1486     
1487     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsClearThroughWebsiteDataRemoval")) {
1488         TestController::singleton().statisticsClearThroughWebsiteDataRemoval();
1489         return nullptr;
1490     }
1491
1492     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsDeleteCookiesForHost")) {
1493         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1494         
1495         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1496         WKRetainPtr<WKStringRef> hostNameKey = adoptWK(WKStringCreateWithUTF8CString("HostName"));
1497         WKRetainPtr<WKStringRef> valueKey = adoptWK(WKStringCreateWithUTF8CString("IncludeHttpOnlyCookies"));
1498         
1499         WKStringRef hostName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, hostNameKey.get()));
1500         WKBooleanRef includeHttpOnlyCookies = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, valueKey.get()));
1501         
1502         TestController::singleton().statisticsDeleteCookiesForHost(hostName, WKBooleanGetValue(includeHttpOnlyCookies));
1503         return nullptr;
1504     }
1505
1506     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsHasLocalStorage")) {
1507         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1508         
1509         WKStringRef hostName = static_cast<WKStringRef>(messageBody);
1510         bool hasLocalStorage = TestController::singleton().isStatisticsHasLocalStorage(hostName);
1511         auto result = adoptWK(WKBooleanCreate(hasLocalStorage));
1512         return result;
1513     }
1514
1515     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsCacheMaxAgeCap")) {
1516         ASSERT(WKGetTypeID(messageBody) == WKDoubleGetTypeID());
1517         WKDoubleRef seconds = static_cast<WKDoubleRef>(messageBody);
1518         TestController::singleton().setStatisticsCacheMaxAgeCap(WKDoubleGetValue(seconds));
1519         return nullptr;
1520     }
1521
1522     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsResetToConsistentState")) {
1523         if (m_shouldDumpResourceLoadStatistics)
1524             m_savedResourceLoadStatistics = TestController::singleton().dumpResourceLoadStatistics();
1525         TestController::singleton().statisticsResetToConsistentState();
1526         return nullptr;
1527     }
1528
1529     if (WKStringIsEqualToUTF8CString(messageName, "HasStatisticsIsolatedSession")) {
1530         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1531         
1532         WKStringRef hostName = static_cast<WKStringRef>(messageBody);
1533         bool hasIsolatedSession = TestController::singleton().hasStatisticsIsolatedSession(hostName);
1534         auto result = adoptWK(WKBooleanCreate(hasIsolatedSession));
1535         return result;
1536     }
1537     
1538     if (WKStringIsEqualToUTF8CString(messageName, "SetStatisticsShouldDowngradeReferrer")) {
1539         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1540         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1541         TestController::singleton().setStatisticsShouldDowngradeReferrer(WKBooleanGetValue(value));
1542         return nullptr;
1543     }
1544     
1545     if (WKStringIsEqualToUTF8CString(messageName, "RemoveAllSessionCredentials")) {
1546         TestController::singleton().removeAllSessionCredentials();
1547         return nullptr;
1548     }
1549
1550     if (WKStringIsEqualToUTF8CString(messageName, "ClearDOMCache")) {
1551         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1552         WKStringRef origin = static_cast<WKStringRef>(messageBody);
1553
1554         TestController::singleton().clearDOMCache(origin);
1555         return nullptr;
1556     }
1557
1558     if (WKStringIsEqualToUTF8CString(messageName, "ClearDOMCaches")) {
1559         TestController::singleton().clearDOMCaches();
1560         return nullptr;
1561     }
1562
1563     if (WKStringIsEqualToUTF8CString(messageName, "HasDOMCache")) {
1564         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1565         WKStringRef origin = static_cast<WKStringRef>(messageBody);
1566
1567         bool hasDOMCache = TestController::singleton().hasDOMCache(origin);
1568         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(hasDOMCache));
1569         return result;
1570     }
1571
1572     if (WKStringIsEqualToUTF8CString(messageName, "DOMCacheSize")) {
1573         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1574         WKStringRef origin = static_cast<WKStringRef>(messageBody);
1575
1576         auto domCacheSize = TestController::singleton().domCacheSize(origin);
1577         WKRetainPtr<WKTypeRef> result = adoptWK(WKUInt64Create(domCacheSize));
1578         return result;
1579     }
1580
1581     if (WKStringIsEqualToUTF8CString(messageName, "SetAllowStorageQuotaIncrease")) {
1582         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1583         auto canIncrease = WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody));
1584         TestController::singleton().setAllowStorageQuotaIncrease(canIncrease);
1585         return nullptr;
1586     }
1587
1588     if (WKStringIsEqualToUTF8CString(messageName, "InjectUserScript")) {
1589         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
1590         WKStringRef script = static_cast<WKStringRef>(messageBody);
1591
1592         TestController::singleton().injectUserScript(script);
1593         return nullptr;
1594     }
1595
1596     if (WKStringIsEqualToUTF8CString(messageName, "GetApplicationManifest")) {
1597 #ifdef __BLOCKS__
1598         WKPageGetApplicationManifest_b(TestController::singleton().mainWebView()->page(), ^{
1599             WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("DidGetApplicationManifest"));
1600             WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1601         });
1602 #else
1603         // FIXME: Add API for loading the manifest on non-__BLOCKS__ ports.
1604         ASSERT_NOT_REACHED();
1605 #endif
1606         return nullptr;
1607     }
1608
1609     if (WKStringIsEqualToUTF8CString(messageName, "SendDisplayConfigurationChangedMessageForTesting")) {
1610         TestController::singleton().sendDisplayConfigurationChangedMessageForTesting();
1611         return nullptr;
1612     }
1613
1614     if (WKStringIsEqualToUTF8CString(messageName, "TerminateNetworkProcess")) {
1615         ASSERT(!messageBody);
1616         TestController::singleton().terminateNetworkProcess();
1617         return nullptr;
1618     }
1619
1620     if (WKStringIsEqualToUTF8CString(messageName, "TerminateServiceWorkerProcess")) {
1621         ASSERT(!messageBody);
1622         TestController::singleton().terminateServiceWorkerProcess();
1623         return nullptr;
1624     }
1625
1626     if (WKStringIsEqualToUTF8CString(messageName, "SetWebAuthenticationMockConfiguration")) {
1627         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1628         TestController::singleton().setWebAuthenticationMockConfiguration(static_cast<WKDictionaryRef>(messageBody));
1629         return nullptr;
1630     }
1631
1632     if (WKStringIsEqualToUTF8CString(messageName, "AddTestKeyToKeychain")) {
1633         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1634         WKDictionaryRef testKeyDictionary = static_cast<WKDictionaryRef>(messageBody);
1635
1636         WKRetainPtr<WKStringRef> privateKeyKey = adoptWK(WKStringCreateWithUTF8CString("PrivateKey"));
1637         WKStringRef privateKeyWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(testKeyDictionary, privateKeyKey.get()));
1638
1639         WKRetainPtr<WKStringRef> attrLabelKey = adoptWK(WKStringCreateWithUTF8CString("AttrLabel"));
1640         WKStringRef attrLabelWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(testKeyDictionary, attrLabelKey.get()));
1641
1642         WKRetainPtr<WKStringRef> applicationTagKey = adoptWK(WKStringCreateWithUTF8CString("ApplicationTag"));
1643         WKStringRef applicationTagWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(testKeyDictionary, applicationTagKey.get()));
1644
1645         TestController::singleton().addTestKeyToKeychain(toWTFString(privateKeyWK), toWTFString(attrLabelWK), toWTFString(applicationTagWK));
1646         return nullptr;
1647     }
1648
1649     if (WKStringIsEqualToUTF8CString(messageName, "CleanUpKeychain")) {
1650         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1651         WKDictionaryRef testDictionary = static_cast<WKDictionaryRef>(messageBody);
1652
1653         WKRetainPtr<WKStringRef> attrLabelKey = adoptWK(WKStringCreateWithUTF8CString("AttrLabel"));
1654         WKStringRef attrLabelWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(testDictionary, attrLabelKey.get()));
1655
1656         WKRetainPtr<WKStringRef> applicationTagKey = adoptWK(WKStringCreateWithUTF8CString("ApplicationTag"));
1657         WKStringRef applicationTagWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(testDictionary, applicationTagKey.get()));
1658
1659         TestController::singleton().cleanUpKeychain(toWTFString(attrLabelWK), applicationTagWK ? toWTFString(applicationTagWK) : String());
1660         return nullptr;
1661     }
1662
1663     if (WKStringIsEqualToUTF8CString(messageName, "KeyExistsInKeychain")) {
1664         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1665         WKDictionaryRef testDictionary = static_cast<WKDictionaryRef>(messageBody);
1666
1667         WKRetainPtr<WKStringRef> attrLabelKey = adoptWK(WKStringCreateWithUTF8CString("AttrLabel"));
1668         WKStringRef attrLabelWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(testDictionary, attrLabelKey.get()));
1669
1670         WKRetainPtr<WKStringRef> applicationTagKey = adoptWK(WKStringCreateWithUTF8CString("ApplicationTag"));
1671         WKStringRef applicationTagWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(testDictionary, applicationTagKey.get()));
1672
1673         bool keyExistsInKeychain = TestController::singleton().keyExistsInKeychain(toWTFString(attrLabelWK), toWTFString(applicationTagWK));
1674         WKRetainPtr<WKTypeRef> result = adoptWK(WKBooleanCreate(keyExistsInKeychain));
1675         return result;
1676     }
1677
1678     if (WKStringIsEqualToUTF8CString(messageName, "ServerTrustEvaluationCallbackCallsCount")) {
1679         WKRetainPtr<WKTypeRef> result = adoptWK(WKUInt64Create(TestController::singleton().serverTrustEvaluationCallbackCallsCount()));
1680         return result;
1681     }
1682
1683     if (WKStringIsEqualToUTF8CString(messageName, "ShouldDismissJavaScriptAlertsAsynchronously")) {
1684         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1685         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1686         TestController::singleton().setShouldDismissJavaScriptAlertsAsynchronously(WKBooleanGetValue(value));
1687         return nullptr;
1688     }
1689
1690     if (WKStringIsEqualToUTF8CString(messageName, "AbortModal")) {
1691         TestController::singleton().abortModal();
1692         return nullptr;
1693     }
1694
1695     if (WKStringIsEqualToUTF8CString(messageName, "DumpAdClickAttribution")) {
1696         dumpAdClickAttribution();
1697         return nullptr;
1698     }
1699     
1700     if (WKStringIsEqualToUTF8CString(messageName, "ClearAdClickAttribution")) {
1701         TestController::singleton().clearAdClickAttribution();
1702         return nullptr;
1703     }
1704
1705     if (WKStringIsEqualToUTF8CString(messageName, "ClearAdClickAttributionsThroughWebsiteDataRemoval")) {
1706         TestController::singleton().clearAdClickAttributionsThroughWebsiteDataRemoval();
1707         return nullptr;
1708     }
1709
1710     if (WKStringIsEqualToUTF8CString(messageName, "SetAdClickAttributionOverrideTimerForTesting")) {
1711         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
1712         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
1713         TestController::singleton().setAdClickAttributionOverrideTimerForTesting(WKBooleanGetValue(value));
1714         return nullptr;
1715     }
1716     
1717     if (WKStringIsEqualToUTF8CString(messageName, "SetAdClickAttributionConversionURLForTesting")) {
1718         ASSERT(WKGetTypeID(messageBody) == WKURLGetTypeID());
1719         WKURLRef url = static_cast<WKURLRef>(messageBody);
1720         TestController::singleton().setAdClickAttributionConversionURLForTesting(url);
1721         return nullptr;
1722     }
1723
1724     if (WKStringIsEqualToUTF8CString(messageName, "MarkAdClickAttributionsAsExpiredForTesting")) {
1725         TestController::singleton().markAdClickAttributionsAsExpiredForTesting();
1726         return nullptr;
1727     }
1728
1729     if (WKStringIsEqualToUTF8CString(messageName, "SyncLocalStorage")) {
1730         TestController::singleton().syncLocalStorage();
1731         return nullptr;
1732     }
1733
1734     ASSERT_NOT_REACHED();
1735     return nullptr;
1736 }
1737
1738 void TestInvocation::runUISideScriptAfterUpdateCallback(WKErrorRef, void* context)
1739 {
1740     UIScriptInvocationData* data = static_cast<UIScriptInvocationData*>(context);
1741     if (TestInvocation* invocation = data->testInvocation) {
1742         RELEASE_ASSERT(TestController::singleton().isCurrentInvocation(invocation));
1743         invocation->runUISideScript(data->scriptString.get(), data->callbackID);
1744     }
1745     delete data;
1746 }
1747
1748 void TestInvocation::runUISideScript(WKStringRef script, unsigned scriptCallbackID)
1749 {
1750     m_pendingUIScriptInvocationData = nullptr;
1751
1752     if (!m_UIScriptContext)
1753         m_UIScriptContext = makeUnique<UIScriptContext>(*this);
1754     
1755     m_UIScriptContext->runUIScript(toWTFString(script), scriptCallbackID);
1756 }
1757
1758 void TestInvocation::uiScriptDidComplete(const String& result, unsigned scriptCallbackID)
1759 {
1760     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallUISideScriptCallback"));
1761
1762     WKRetainPtr<WKMutableDictionaryRef> messageBody = adoptWK(WKMutableDictionaryCreate());
1763     WKRetainPtr<WKStringRef> resultKey = adoptWK(WKStringCreateWithUTF8CString("Result"));
1764     WKRetainPtr<WKStringRef> callbackIDKey = adoptWK(WKStringCreateWithUTF8CString("CallbackID"));
1765     WKRetainPtr<WKUInt64Ref> callbackIDValue = adoptWK(WKUInt64Create(scriptCallbackID));
1766
1767     WKDictionarySetItem(messageBody.get(), resultKey.get(), toWK(result).get());
1768     WKDictionarySetItem(messageBody.get(), callbackIDKey.get(), callbackIDValue.get());
1769
1770     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), messageBody.get());
1771 }
1772
1773 void TestInvocation::outputText(const WTF::String& text)
1774 {
1775     m_textOutput.append(text);
1776 }
1777
1778 void TestInvocation::didBeginSwipe()
1779 {
1780     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidBeginSwipeCallback"));
1781     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1782 }
1783
1784 void TestInvocation::willEndSwipe()
1785 {
1786     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallWillEndSwipeCallback"));
1787     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1788 }
1789
1790 void TestInvocation::didEndSwipe()
1791 {
1792     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidEndSwipeCallback"));
1793     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1794 }
1795
1796 void TestInvocation::didRemoveSwipeSnapshot()
1797 {
1798     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidRemoveSwipeSnapshotCallback"));
1799     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1800 }
1801
1802 void TestInvocation::notifyDownloadDone()
1803 {
1804     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("NotifyDownloadDone"));
1805     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1806 }
1807
1808 void TestInvocation::didClearStatisticsThroughWebsiteDataRemoval()
1809 {
1810     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidClearStatisticsThroughWebsiteDataRemoval"));
1811     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1812 }
1813
1814 void TestInvocation::didSetShouldDowngradeReferrer()
1815 {
1816     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidSetShouldDowngradeReferrer"));
1817     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1818 }
1819
1820 void TestInvocation::didResetStatisticsToConsistentState()
1821 {
1822     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidResetStatisticsToConsistentState"));
1823     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1824 }
1825
1826 void TestInvocation::didSetBlockCookiesForHost()
1827 {
1828     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidSetBlockCookiesForHost"));
1829     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1830 }
1831
1832 void TestInvocation::didSetStatisticsDebugMode()
1833 {
1834     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidSetStatisticsDebugMode"));
1835     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1836 }
1837
1838 void TestInvocation::didSetPrevalentResourceForDebugMode()
1839 {
1840     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidSetPrevalentResourceForDebugMode"));
1841     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1842 }
1843
1844 void TestInvocation::didSetLastSeen()
1845 {
1846     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidSetLastSeen"));
1847     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1848 }
1849
1850 void TestInvocation::didMergeStatistic()
1851 {
1852     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidMergeStatistic"));
1853     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1854 }
1855
1856 void TestInvocation::didSetPrevalentResource()
1857 {
1858     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidSetPrevalentResource"));
1859     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1860 }
1861
1862 void TestInvocation::didSetVeryPrevalentResource()
1863 {
1864     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidSetVeryPrevalentResource"));
1865     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1866 }
1867
1868 void TestInvocation::didSetHasHadUserInteraction()
1869 {
1870     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidSetHasHadUserInteraction"));
1871     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1872 }
1873
1874 void TestInvocation::didReceiveAllStorageAccessEntries(Vector<String>& domains)
1875 {
1876     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidReceiveAllStorageAccessEntries"));
1877     
1878     WKRetainPtr<WKMutableArrayRef> messageBody = adoptWK(WKMutableArrayCreate());
1879     for (auto& domain : domains)
1880         WKArrayAppendItem(messageBody.get(), adoptWK(WKStringCreateWithUTF8CString(domain.utf8().data())).get());
1881     
1882     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), messageBody.get());
1883 }
1884
1885 void TestInvocation::didRemoveAllSessionCredentials()
1886 {
1887     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidRemoveAllSessionCredentialsCallback"));
1888     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1889 }
1890
1891 void TestInvocation::dumpResourceLoadStatistics()
1892 {
1893     m_shouldDumpResourceLoadStatistics = true;
1894 }
1895
1896 void TestInvocation::dumpAdClickAttribution()
1897 {
1898     m_shouldDumpAdClickAttribution = true;
1899 }
1900
1901 void TestInvocation::performCustomMenuAction()
1902 {
1903     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("PerformCustomMenuAction"));
1904     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
1905 }
1906
1907 void TestInvocation::initializeWaitToDumpWatchdogTimerIfNeeded()
1908 {
1909     if (m_waitToDumpWatchdogTimer.isActive())
1910         return;
1911
1912     m_waitToDumpWatchdogTimer.startOneShot(m_timeout);
1913 }
1914
1915 void TestInvocation::invalidateWaitToDumpWatchdogTimer()
1916 {
1917     m_waitToDumpWatchdogTimer.stop();
1918 }
1919
1920 void TestInvocation::waitToDumpWatchdogTimerFired()
1921 {
1922     invalidateWaitToDumpWatchdogTimer();
1923
1924 #if PLATFORM(COCOA)
1925     char buffer[1024];
1926     snprintf(buffer, sizeof(buffer), "#PID UNRESPONSIVE - %s (pid %d)\n", getprogname(), getpid());
1927     outputText(buffer);
1928 #endif
1929     outputText("FAIL: Timed out waiting for notifyDone to be called\n\n");
1930     done();
1931 }
1932
1933 void TestInvocation::setWaitUntilDone(bool waitUntilDone)
1934 {
1935     m_waitUntilDone = waitUntilDone;
1936     if (waitUntilDone && TestController::singleton().useWaitToDumpWatchdogTimer())
1937         initializeWaitToDumpWatchdogTimerIfNeeded();
1938 }
1939
1940 void TestInvocation::done()
1941 {
1942     m_gotFinalMessage = true;
1943     invalidateWaitToDumpWatchdogTimer();
1944     RunLoop::main().dispatch([] {
1945         TestController::singleton().notifyDone();
1946     });
1947 }
1948
1949 void TestInvocation::willCreateNewPage()
1950 {
1951     if (m_UIScriptContext && m_UIScriptContext->callbackWithID(CallbackTypeWillCreateNewPage))
1952         m_UIScriptContext->fireCallback(CallbackTypeWillCreateNewPage);
1953 }
1954
1955 } // namespace WTR