/* * Copyright (C) 2017 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #import "config.h" #import "PlatformUtilities.h" #import "Test.h" #import #import #import #import #import #import #import #import #import #import #import #import #import #import #if WK_API_ENABLED struct ResourceInfo { RetainPtr mimeType; const char* data; }; static bool done; static String expectedMessage; @interface SWMessageHandler : NSObject @end @implementation SWMessageHandler - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { EXPECT_TRUE([[message body] isEqualToString:@"Message from worker: ServiceWorker received: Hello from the web page"]); done = true; } @end @interface SWMessageHandlerForFetchTest : NSObject @end @implementation SWMessageHandlerForFetchTest - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { EXPECT_TRUE([[message body] isEqualToString:@"Intercepted by worker"]); done = true; } @end @interface SWMessageHandlerForRestoreFromDiskTest : NSObject { NSString *_expectedMessage; } - (instancetype)initWithExpectedMessage:(NSString *)expectedMessage; @end @interface SWMessageHandlerWithExpectedMessage : NSObject @end @implementation SWMessageHandlerWithExpectedMessage - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { EXPECT_TRUE([[message body] isEqualToString:expectedMessage]); done = true; } @end @implementation SWMessageHandlerForRestoreFromDiskTest - (instancetype)initWithExpectedMessage:(NSString *)expectedMessage { if (!(self = [super init])) return nil; _expectedMessage = expectedMessage; return self; } - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { EXPECT_TRUE([[message body] isEqualToString:_expectedMessage]); done = true; } @end @interface SWSchemes : NSObject { @public HashMap resources; } -(size_t)handledRequests; @end @implementation SWSchemes { size_t _handledRequests; } -(size_t)handledRequests { return _handledRequests; } - (void)webView:(WKWebView *)webView startURLSchemeTask:(id )task { auto entry = resources.find([task.request.URL absoluteString]); if (entry == resources.end()) { NSLog(@"Did not find resource entry for URL %@", task.request.URL); return; } ++_handledRequests; RetainPtr response = adoptNS([[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:entry->value.mimeType.get() expectedContentLength:1 textEncodingName:nil]); [task didReceiveResponse:response.get()]; [task didReceiveData:[NSData dataWithBytesNoCopy:(void*)entry->value.data length:strlen(entry->value.data) freeWhenDone:NO]]; [task didFinish]; } - (void)webView:(WKWebView *)webView stopURLSchemeTask:(id )task { } @end static const char* mainBytes = R"SWRESOURCE( )SWRESOURCE"; static const char* scriptBytes = R"SWRESOURCE( self.addEventListener("message", (event) => { event.source.postMessage("ServiceWorker received: " + event.data); }); )SWRESOURCE"; static const char* mainForFetchTestBytes = R"SWRESOURCE( )SWRESOURCE"; static const char* scriptHandlingFetchBytes = R"SWRESOURCE( self.addEventListener("fetch", (event) => { if (event.request.url.indexOf("test.html") !== -1) { event.respondWith(new Response(new Blob(['Intercepted by worker'], {type: 'text/html'}))); } }); )SWRESOURCE"; static const char* scriptInterceptingFirstLoadBytes = R"SWRESOURCE( self.addEventListener("fetch", (event) => { if (event.request.url.indexOf("main.html") !== -1) { event.respondWith(new Response(new Blob(['Intercepted by worker '], {type: 'text/html'}))); } }); )SWRESOURCE"; static const char* mainForFirstLoadInterceptTestBytes = R"SWRESOURCE( )SWRESOURCE"; static const char* testBytes = R"SWRESOURCE( NOT intercepted by worker )SWRESOURCE"; static const char* mainRegisteringWorkerBytes = R"SWRESOURCE( )SWRESOURCE"; static const char* mainRegisteringAlreadyExistingWorkerBytes = R"SWRESOURCE( )SWRESOURCE"; TEST(ServiceWorkers, Basic) { ASSERT(mainBytes); ASSERT(scriptBytes); [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins]; // Start with a clean slate data store [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; RetainPtr configuration = adoptNS([[WKWebViewConfiguration alloc] init]); RetainPtr messageHandler = adoptNS([[SWMessageHandler alloc] init]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; RetainPtr handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; RetainPtr webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; webView = nullptr; [[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:[NSSet setWithObject:WKWebsiteDataTypeServiceWorkerRegistrations] completionHandler:^(NSArray *websiteDataRecords) { EXPECT_EQ(1u, [websiteDataRecords count]); EXPECT_TRUE([websiteDataRecords[0].displayName isEqualToString:@"sw host"]); done = true; }]; TestWebKitAPI::Util::run(&done); done = false; } TEST(ServiceWorkers, RestoreFromDisk) { ASSERT(mainRegisteringWorkerBytes); ASSERT(scriptBytes); ASSERT(mainRegisteringAlreadyExistingWorkerBytes); [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins]; // Start with a clean slate data store [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; RetainPtr configuration = adoptNS([[WKWebViewConfiguration alloc] init]); RetainPtr messageHandler = adoptNS([[SWMessageHandlerForRestoreFromDiskTest alloc] initWithExpectedMessage:@"PASS: Registration was successful and service worker was activated"]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; RetainPtr handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainRegisteringWorkerBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; RetainPtr webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); webView = nullptr; configuration = nullptr; messageHandler = nullptr; handler = nullptr; done = false; configuration = adoptNS([[WKWebViewConfiguration alloc] init]); messageHandler = adoptNS([[SWMessageHandlerForRestoreFromDiskTest alloc] initWithExpectedMessage:@"PASS: Registration already has an active worker"]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainRegisteringAlreadyExistingWorkerBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; } TEST(ServiceWorkers, FetchAfterRestoreFromDisk) { ASSERT(mainForFetchTestBytes); ASSERT(scriptHandlingFetchBytes); [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins]; // Start with a clean slate data store [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; RetainPtr configuration = adoptNS([[WKWebViewConfiguration alloc] init]); RetainPtr messageHandler = adoptNS([[SWMessageHandlerForFetchTest alloc] init]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; RetainPtr handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainForFetchTestBytes }); handler->resources.set("sw://host/test.html", ResourceInfo { @"text/html", testBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptHandlingFetchBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; RetainPtr webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); webView = nullptr; configuration = nullptr; messageHandler = nullptr; handler = nullptr; done = false; configuration = adoptNS([[WKWebViewConfiguration alloc] init]); messageHandler = adoptNS([[SWMessageHandlerForFetchTest alloc] init]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainForFetchTestBytes }); handler->resources.set("sw://host/test.html", ResourceInfo { @"text/html", testBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptHandlingFetchBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; } TEST(ServiceWorkers, InterceptFirstLoadAfterRestoreFromDisk) { ASSERT(mainForFirstLoadInterceptTestBytes); ASSERT(scriptHandlingFetchBytes); [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins]; // Start with a clean slate data store [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; RetainPtr configuration = adoptNS([[WKWebViewConfiguration alloc] init]); RetainPtr messageHandler = adoptNS([[SWMessageHandlerWithExpectedMessage alloc] init]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; RetainPtr handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainForFirstLoadInterceptTestBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptInterceptingFirstLoadBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; RetainPtr webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; expectedMessage = "Service Worker activated"; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); webView = nullptr; configuration = nullptr; messageHandler = nullptr; handler = nullptr; done = false; configuration = adoptNS([[WKWebViewConfiguration alloc] init]); messageHandler = adoptNS([[SWMessageHandlerWithExpectedMessage alloc] init]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainForFirstLoadInterceptTestBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptInterceptingFirstLoadBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; expectedMessage = "Intercepted by worker"; request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; } #if WK_HAVE_C_SPI void setConfigurationInjectedBundlePath(WKWebViewConfiguration* configuration) { WKRetainPtr context(AdoptWK, TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest")); configuration.processPool = (WKProcessPool *)context.get(); auto pool = configuration.processPool; [pool _registerURLSchemeServiceWorkersCanHandle:@"sw"]; [pool _setMaximumNumberOfProcesses:5]; configuration.websiteDataStore = (WKWebsiteDataStore *)WKContextGetWebsiteDataStore(context.get()); } @interface RegularPageMessageHandler : NSObject @end @implementation RegularPageMessageHandler - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { EXPECT_TRUE([[message body] isEqualToString:@"PASS"]); done = true; } @end static const char* regularPageWithoutConnectionBytes = R"SWRESOURCE( )SWRESOURCE"; static const char* regularPageWithConnectionBytes = R"SWRESOURCE( )SWRESOURCE"; TEST(ServiceWorkers, SWProcessConnectionCreation) { ASSERT(mainBytes); ASSERT(scriptBytes); [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins]; RetainPtr configuration = adoptNS([[WKWebViewConfiguration alloc] init]); setConfigurationInjectedBundlePath(configuration.get()); done = false; [[configuration websiteDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; [[configuration websiteDataStore] fetchDataRecordsOfTypes:[NSSet setWithObject:WKWebsiteDataTypeServiceWorkerRegistrations] completionHandler:^(NSArray *websiteDataRecords) { EXPECT_EQ(0u, [websiteDataRecords count]); done = true; }]; TestWebKitAPI::Util::run(&done); done = false; RetainPtr messageHandler = adoptNS([[SWMessageHandler alloc] init]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; RetainPtr regularPageMessageHandler = adoptNS([[RegularPageMessageHandler alloc] init]); [[configuration userContentController] addScriptMessageHandler:regularPageMessageHandler.get() name:@"regularPage"]; RetainPtr handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/regularPageWithoutConnection.html", ResourceInfo { @"text/html", regularPageWithoutConnectionBytes }); handler->resources.set("sw://host/regularPageWithConnection.html", ResourceInfo { @"text/html", regularPageWithConnectionBytes }); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; RetainPtr regularPageWebView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); RetainPtr webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); RetainPtr newRegularPageWebView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); // Test that a regular page does not trigger a service worker connection to storage process if there is no registered service worker. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/regularPageWithoutConnection.html"]]; [regularPageWebView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; // Test that a sw scheme page can register a service worker. request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; webView = nullptr; // Now that a service worker is registered, the regular page should have a service worker connection. request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/regularPageWithConnection.html"]]; [regularPageWebView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; regularPageWebView = nullptr; [newRegularPageWebView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; newRegularPageWebView = nullptr; [[configuration websiteDataStore] fetchDataRecordsOfTypes:[NSSet setWithObject:WKWebsiteDataTypeServiceWorkerRegistrations] completionHandler:^(NSArray *websiteDataRecords) { EXPECT_EQ(1u, [websiteDataRecords count]); EXPECT_TRUE([websiteDataRecords[0].displayName isEqualToString:@"sw host"]); done = true; }]; TestWebKitAPI::Util::run(&done); done = false; [[configuration websiteDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); } TEST(ServiceWorkers, StorageProcessConnectionCreation) { ASSERT(mainBytes); ASSERT(scriptBytes); [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins]; RetainPtr configuration = adoptNS([[WKWebViewConfiguration alloc] init]); done = false; [[configuration websiteDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; configuration = adoptNS([[WKWebViewConfiguration alloc] init]); setConfigurationInjectedBundlePath(configuration.get()); RetainPtr regularPageMessageHandler = adoptNS([[RegularPageMessageHandler alloc] init]); [[configuration userContentController] addScriptMessageHandler:regularPageMessageHandler.get() name:@"regularPage"]; RetainPtr handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/regularPageWithoutConnection.html", ResourceInfo { @"text/html", regularPageWithoutConnectionBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; RetainPtr webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); EXPECT_EQ(0, webView.get().configuration.processPool._storageProcessIdentifier); // Test that a regular page does not trigger a storage process if there is no registered service worker. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/regularPageWithoutConnection.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; // Make sure than loading the simple page did not start a storage process. EXPECT_EQ(0, webView.get().configuration.processPool._storageProcessIdentifier); } static const char* mainBytesWithScope = R"SWRESOURCE( )SWRESOURCE"; TEST(ServiceWorkers, ServiceWorkerProcessCreation) { [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins]; RetainPtr configuration = adoptNS([[WKWebViewConfiguration alloc] init]); setConfigurationInjectedBundlePath(configuration.get()); done = false; [[configuration websiteDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; [[configuration websiteDataStore] fetchDataRecordsOfTypes:[NSSet setWithObject:WKWebsiteDataTypeServiceWorkerRegistrations] completionHandler:^(NSArray *websiteDataRecords) { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; RetainPtr messageHandler = adoptNS([[SWMessageHandler alloc] init]); [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; RetainPtr regularPageMessageHandler = adoptNS([[RegularPageMessageHandler alloc] init]); [[configuration userContentController] addScriptMessageHandler:regularPageMessageHandler.get() name:@"regularPage"]; RetainPtr handler = adoptNS([[SWSchemes alloc] init]); handler->resources.set("sw://host/regularPageWithoutConnection.html", ResourceInfo { @"text/html", regularPageWithoutConnectionBytes }); handler->resources.set("sw://host/regularPageWithConnection.html", ResourceInfo { @"text/html", regularPageWithConnectionBytes }); handler->resources.set("sw://host/mainWithScope.html", ResourceInfo { @"text/html", mainBytesWithScope }); handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainBytes }); handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes }); [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; RetainPtr webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); // Load a page that registers a service worker. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/mainWithScope.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; webView = nullptr; // Now that a sw is registered, let's create a new configuration and try loading a regular page, there should be no service worker process created. RetainPtr newConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]); setConfigurationInjectedBundlePath(newConfiguration.get()); newConfiguration.get().websiteDataStore = [configuration websiteDataStore]; [[newConfiguration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"]; [[newConfiguration userContentController] addScriptMessageHandler:regularPageMessageHandler.get() name:@"regularPage"]; [newConfiguration setURLSchemeHandler:handler.get() forURLScheme:@"SW"]; webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:newConfiguration.get()]); EXPECT_EQ(1u, webView.get().configuration.processPool._webProcessCount); request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/regularPageWithConnection.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; // Make sure that loading the simple page did not start the service worker process. EXPECT_EQ(1u, webView.get().configuration.processPool._webProcessCount); webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:newConfiguration.get()]); EXPECT_EQ(2u, webView.get().configuration.processPool._webProcessCount); request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]]; [webView loadRequest:request]; TestWebKitAPI::Util::run(&done); done = false; // Make sure that loading this page did start the service worker process. EXPECT_EQ(3u, webView.get().configuration.processPool._webProcessCount); [[configuration websiteDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() { done = true; }]; TestWebKitAPI::Util::run(&done); done = false; } #endif // WK_HAVE_C_SPI #endif // WK_API_ENABLED