/* * Copyright (C) 2021 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 "HTTPServer.h" #import "PlatformUtilities.h" #import "Test.h" #import "TestWKWebView.h" #import "Utilities.h" #import #import #import #import namespace TestWebKitAPI { enum class ShouldUseSameProcess : bool { No, Yes }; static void runSnapshotAcrossPagesTest(ShouldUseSameProcess shouldUseSameProcess) { TestWebKitAPI::HTTPServer server({ { "/"_s, { "foo"_s } } }); auto configuration1 = adoptNS([[WKWebViewConfiguration alloc] init]); for (_WKExperimentalFeature *feature in [WKPreferences _experimentalFeatures]) { if ([feature.key isEqualToString:@"WebLocksAPIEnabled"]) { [[configuration1 preferences] _setEnabled:YES forExperimentalFeature:feature]; break; } } auto webView1 = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration1.get()]); [webView1 synchronouslyLoadRequest:server.request()]; auto pid1 = [webView1 _webProcessIdentifier]; EXPECT_NE(pid1, 0); __block bool done = false; [webView1 performAfterReceivingAnyMessage:^(NSString *message) { EXPECT_WK_STREQ(message, @"ACQUIRED"); done = true; }]; [webView1 evaluateJavaScript:@"navigator.locks.request('foo', lock => { webkit.messageHandlers.testHandler.postMessage('ACQUIRED'); return new Promise(() => {}); }) && 1" completionHandler:^(id result, NSError *error) { EXPECT_NULL(error); }]; TestWebKitAPI::Util::run(&done); auto configuration2 = adoptNS([[WKWebViewConfiguration alloc] init]); configuration2.get().processPool = [configuration1 processPool]; for (_WKExperimentalFeature *feature in [WKPreferences _experimentalFeatures]) { if ([feature.key isEqualToString:@"WebLocksAPIEnabled"]) { [[configuration2 preferences] _setEnabled:YES forExperimentalFeature:feature]; break; } } if (shouldUseSameProcess == ShouldUseSameProcess::Yes) configuration2.get()._relatedWebView = webView1.get(); auto webView2 = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration2.get()]); [webView2 synchronouslyLoadRequest:server.request()]; auto pid2 = [webView2 _webProcessIdentifier]; if (shouldUseSameProcess == ShouldUseSameProcess::Yes) EXPECT_EQ(pid1, pid2); else EXPECT_NE(pid1, pid2); done = false; [webView2 performAfterReceivingAnyMessage:^(NSString *message) { EXPECT_WK_STREQ(message, @"ACQUIRED"); done = true; }]; [webView2 evaluateJavaScript:@"navigator.locks.request('bar', lock => { webkit.messageHandlers.testHandler.postMessage('ACQUIRED'); return new Promise(() => {}); }) && 1" completionHandler:^(id result, NSError *error) { EXPECT_NULL(error); }]; TestWebKitAPI::Util::run(&done); done = false; [webView1 performAfterReceivingAnyMessage:^(NSString *message) { // There should be 0 pending locks, and 2 held ones. EXPECT_WK_STREQ(message, @"0-2"); done = true; }]; [webView1 evaluateJavaScript:@"navigator.locks.query().then(snapshot => { webkit.messageHandlers.testHandler.postMessage('' + snapshot.pending.length + '-' + snapshot.held.length); }) && 1" completionHandler:^(id result, NSError *error) { EXPECT_NULL(error); }]; TestWebKitAPI::Util::run(&done); done = false; [webView2 performAfterReceivingAnyMessage:^(NSString *message) { // There should be 0 pending locks, and 2 held ones. EXPECT_WK_STREQ(message, @"0-2"); done = true; }]; [webView2 evaluateJavaScript:@"navigator.locks.query().then(snapshot => { webkit.messageHandlers.testHandler.postMessage('' + snapshot.pending.length + '-' + snapshot.held.length); }) && 1" completionHandler:^(id result, NSError *error) { EXPECT_NULL(error); }]; TestWebKitAPI::Util::run(&done); } TEST(WebLocks, SnapshotAcrossPagesInDifferentProcesses) { runSnapshotAcrossPagesTest(ShouldUseSameProcess::No); } TEST(WebLocks, SnapshotAcrossPagesInSameProcess) { runSnapshotAcrossPagesTest(ShouldUseSameProcess::Yes); } static void runLockRequestWaitingOnAnotherPage(ShouldUseSameProcess shouldUseSameProcess) { TestWebKitAPI::HTTPServer server({ { "/"_s, { "foo"_s } } }); auto configuration1 = adoptNS([[WKWebViewConfiguration alloc] init]); for (_WKExperimentalFeature *feature in [WKPreferences _experimentalFeatures]) { if ([feature.key isEqualToString:@"WebLocksAPIEnabled"]) { [[configuration1 preferences] _setEnabled:YES forExperimentalFeature:feature]; break; } } auto webView1 = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration1.get()]); [webView1 synchronouslyLoadRequest:server.request()]; auto pid1 = [webView1 _webProcessIdentifier]; EXPECT_NE(pid1, 0); __block bool done = false; [webView1 performAfterReceivingAnyMessage:^(NSString *message) { EXPECT_WK_STREQ(message, @"ACQUIRED"); done = true; }]; [webView1 evaluateJavaScript:@"navigator.locks.request('foo', lock => { webkit.messageHandlers.testHandler.postMessage('ACQUIRED'); return new Promise(() => {}); }) && 1" completionHandler:^(id result, NSError *error) { EXPECT_NULL(error); }]; TestWebKitAPI::Util::run(&done); auto configuration2 = adoptNS([[WKWebViewConfiguration alloc] init]); configuration2.get().processPool = [configuration1 processPool]; for (_WKExperimentalFeature *feature in [WKPreferences _experimentalFeatures]) { if ([feature.key isEqualToString:@"WebLocksAPIEnabled"]) { [[configuration2 preferences] _setEnabled:YES forExperimentalFeature:feature]; break; } } if (shouldUseSameProcess == ShouldUseSameProcess::Yes) configuration2.get()._relatedWebView = webView1.get(); auto webView2 = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration2.get()]); [webView2 synchronouslyLoadRequest:server.request()]; auto pid2 = [webView2 _webProcessIdentifier]; if (shouldUseSameProcess == ShouldUseSameProcess::Yes) EXPECT_EQ(pid1, pid2); else EXPECT_NE(pid1, pid2); __block bool lockAcquired = false; [webView2 performAfterReceivingAnyMessage:^(NSString *message) { EXPECT_WK_STREQ(message, @"ACQUIRED"); lockAcquired = true; }]; // Requesting the same lock name as webView1 so the request should get queued until webView1 releases it. [webView2 evaluateJavaScript:@"navigator.locks.request('foo', lock => { webkit.messageHandlers.testHandler.postMessage('ACQUIRED'); return new Promise(() => {}); }) && 1" completionHandler:^(id result, NSError *error) { EXPECT_NULL(error); }]; TestWebKitAPI::Util::sleep(0.5); EXPECT_FALSE(lockAcquired); done = false; [webView2 performAfterReceivingAnyMessage:^(NSString *message) { // There should be 1 pending lock, and 1 held one. EXPECT_WK_STREQ(message, @"1-1"); done = true; }]; [webView2 evaluateJavaScript:@"navigator.locks.query().then(snapshot => { webkit.messageHandlers.testHandler.postMessage('' + snapshot.pending.length + '-' + snapshot.held.length); }) && 1" completionHandler:^(id result, NSError *error) { EXPECT_NULL(error); }]; TestWebKitAPI::Util::run(&done); [webView2 performAfterReceivingAnyMessage:^(NSString *message) { EXPECT_WK_STREQ(message, @"ACQUIRED"); lockAcquired = true; }]; // Now close the webView1. This should release the 'foo' lock and allow webView2 to acquire it. [webView1 _close]; TestWebKitAPI::Util::run(&lockAcquired); done = false; [webView2 performAfterReceivingAnyMessage:^(NSString *message) { // There should be 0 pending locks, and 1 held one. EXPECT_WK_STREQ(message, @"0-1"); done = true; }]; [webView2 evaluateJavaScript:@"navigator.locks.query().then(snapshot => { webkit.messageHandlers.testHandler.postMessage('' + snapshot.pending.length + '-' + snapshot.held.length); }) && 1" completionHandler:^(id result, NSError *error) { EXPECT_NULL(error); }]; TestWebKitAPI::Util::run(&done); } TEST(WebLocks, LockRequestWaitingOnAnotherPageInOtherProcess) { runLockRequestWaitingOnAnotherPage(ShouldUseSameProcess::No); } TEST(WebLocks, LockRequestWaitingOnAnotherPageInSameProcess) { runLockRequestWaitingOnAnotherPage(ShouldUseSameProcess::Yes); } } // namespace TestWebKitAPI