/* * Copyright (C) 2016 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. */ #include "config.h" #import "PlatformUtilities.h" #import "TestWKWebView.h" #import #import #import #import #import #if PLATFORM(IOS) #import #endif #if WK_API_ENABLED @interface WKWebView () - (WKPageRef)_pageForTesting; @end static bool doneCompiling; static bool receivedAlert; static size_t alertCount; @interface ContentBlockingWebsitePoliciesDelegate : NSObject @end @implementation ContentBlockingWebsitePoliciesDelegate - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { // _webView:decidePolicyForNavigationAction:decisionHandler: should be called instead if it is implemented. EXPECT_TRUE(false); decisionHandler(WKNavigationActionPolicyAllow); } - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler { switch (alertCount++) { case 0: // FIXME: The first content blocker should be enabled here. // ContentExtensionsBackend::addContentExtension isn't being called in the WebProcess // until after the first main resource starts loading, so we need to send a message to the // WebProcess before loading if we have installed content blockers. // See rdar://problem/27788755 EXPECT_STREQ("content blockers disabled", message.UTF8String); break; case 1: // Default behavior. EXPECT_STREQ("content blockers enabled", message.UTF8String); break; case 2: // After having set websitePolicies.contentBlockersEnabled to false. EXPECT_STREQ("content blockers disabled", message.UTF8String); break; case 3: // After having reloaded without content blockers. EXPECT_STREQ("content blockers disabled", message.UTF8String); break; default: EXPECT_TRUE(false); } receivedAlert = true; completionHandler(); } - (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler { _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease]; switch (alertCount) { case 0: // Verify an existing bug the first time a page is loaded in a new WebProcess. break; case 1: // Verify the content blockers behave correctly with the default behavior. break; case 2: // Verify disabling content blockers works correctly. websitePolicies.contentBlockersEnabled = false; break; case 3: // Verify enabling content blockers has no effect when reloading without content blockers. websitePolicies.contentBlockersEnabled = true; break; default: EXPECT_TRUE(false); } decisionHandler(WKNavigationActionPolicyAllow, websitePolicies); } @end TEST(WebKit2, WebsitePoliciesContentBlockersEnabled) { [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions]; auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]); doneCompiling = false; NSString* contentBlocker = @"[{\"action\":{\"type\":\"css-display-none\",\"selector\":\".hidden\"},\"trigger\":{\"url-filter\":\".*\"}}]"; [[_WKUserContentExtensionStore defaultStore] compileContentExtensionForIdentifier:@"WebsitePoliciesTest" encodedContentExtension:contentBlocker completionHandler:^(_WKUserContentFilter *filter, NSError *error) { EXPECT_TRUE(error == nil); [[configuration userContentController] _addUserContentFilter:filter]; doneCompiling = true; }]; TestWebKitAPI::Util::run(&doneCompiling); auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); auto delegate = adoptNS([[ContentBlockingWebsitePoliciesDelegate alloc] init]); [webView setNavigationDelegate:delegate.get()]; [webView setUIDelegate:delegate.get()]; NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"contentBlockerCheck" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]; alertCount = 0; receivedAlert = false; [webView loadRequest:request]; TestWebKitAPI::Util::run(&receivedAlert); receivedAlert = false; [webView reload]; TestWebKitAPI::Util::run(&receivedAlert); receivedAlert = false; [webView reload]; TestWebKitAPI::Util::run(&receivedAlert); receivedAlert = false; [webView _reloadWithoutContentBlockers]; TestWebKitAPI::Util::run(&receivedAlert); [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions]; } @interface AutoplayPoliciesDelegate : NSObject @property (nonatomic, copy) _WKWebsiteAutoplayPolicy(^autoplayPolicyForURL)(NSURL *); @end @implementation AutoplayPoliciesDelegate - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { // _webView:decidePolicyForNavigationAction:decisionHandler: should be called instead if it is implemented. EXPECT_TRUE(false); decisionHandler(WKNavigationActionPolicyAllow); } - (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler { _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease]; if (_autoplayPolicyForURL) websitePolicies.autoplayPolicy = _autoplayPolicyForURL(navigationAction.request.URL); decisionHandler(WKNavigationActionPolicyAllow, websitePolicies); } @end TEST(WebKit2, WebsitePoliciesAutoplayEnabled) { auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]); #if PLATFORM(IOS) [configuration setAllowsInlineMediaPlayback:YES]; #endif auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]); [webView setNavigationDelegate:delegate.get()]; NSURLRequest *requestWithAudio = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]; NSURLRequest *requestWithoutAudio = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-no-audio-check" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]; [delegate setAutoplayPolicyForURL:^(NSURL *) { return _WKWebsiteAutoplayPolicyAllowWithoutSound; }]; [webView loadRequest:requestWithAudio]; [webView waitForMessage:@"did-not-play"]; [webView loadRequest:requestWithoutAudio]; [webView waitForMessage:@"autoplayed"]; [delegate setAutoplayPolicyForURL:^(NSURL *) { return _WKWebsiteAutoplayPolicyDeny; }]; [webView loadRequest:requestWithAudio]; [webView waitForMessage:@"did-not-play"]; [webView loadRequest:requestWithoutAudio]; [webView waitForMessage:@"did-not-play"]; [delegate setAutoplayPolicyForURL:^(NSURL *) { return _WKWebsiteAutoplayPolicyAllow; }]; [webView loadRequest:requestWithAudio]; [webView waitForMessage:@"autoplayed"]; [webView loadRequest:requestWithoutAudio]; [webView waitForMessage:@"autoplayed"]; NSURLRequest *requestWithAudioInIFrame = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check-in-iframe" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]; // If the top-level document allows autoplay, any iframes should also autoplay. [delegate setAutoplayPolicyForURL:^(NSURL *url) { if ([url.lastPathComponent isEqualToString:@"autoplay-check-in-iframe.html"]) return _WKWebsiteAutoplayPolicyAllow; return _WKWebsiteAutoplayPolicyDeny; }]; [webView loadRequest:requestWithAudioInIFrame]; [webView waitForMessage:@"autoplayed"]; // If the top-level document disallows autoplay, any iframes should also not autoplay. [delegate setAutoplayPolicyForURL:^(NSURL *url) { if ([url.lastPathComponent isEqualToString:@"autoplay-check-in-iframe.html"]) return _WKWebsiteAutoplayPolicyDeny; return _WKWebsiteAutoplayPolicyAllow; }]; [webView loadRequest:requestWithAudioInIFrame]; [webView waitForMessage:@"did-not-play"]; } #if PLATFORM(MAC) static void didPlayMediaPreventedFromPlayingWithoutUserGesture(WKPageRef page, const void* clientInfo) { receivedAlert = true; } // FIXME: webkit.org/b/167466 Re-enable this test once the cause of timeouts on the bots can be fixed. TEST(WebKit2, DISABLED_WebsitePoliciesPlayAfterPreventedAutoplay) { auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]); auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 336, 276) configuration:configuration.get()]); auto delegate = adoptNS([[AutoplayPoliciesDelegate alloc] init]); [delegate setAutoplayPolicyForURL:^(NSURL *) { return _WKWebsiteAutoplayPolicyDeny; }]; [webView setNavigationDelegate:delegate.get()]; WKPageUIClientV9 uiClient; memset(&uiClient, 0, sizeof(uiClient)); uiClient.base.version = 8; uiClient.didPlayMediaPreventedFromPlayingWithoutUserGesture = didPlayMediaPreventedFromPlayingWithoutUserGesture; WKPageSetPageUIClient([webView _pageForTesting], &uiClient.base); NSPoint playButtonClickPoint = NSMakePoint(20, 256); receivedAlert = false; NSURLRequest *jsPlayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"js-play-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]; [webView loadRequest:jsPlayRequest]; [webView waitForMessage:@"loaded"]; [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO]; [webView mouseUpAtPoint:playButtonClickPoint]; TestWebKitAPI::Util::run(&receivedAlert); receivedAlert = false; [webView loadHTMLString:@"" baseURL:nil]; NSURLRequest *autoplayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]; [webView loadRequest:autoplayRequest]; [webView waitForMessage:@"loaded"]; [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO]; [webView mouseUpAtPoint:playButtonClickPoint]; TestWebKitAPI::Util::run(&receivedAlert); receivedAlert = false; [webView loadHTMLString:@"" baseURL:nil]; NSURLRequest *noAutoplayRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"no-autoplay-with-controls" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]; [webView loadRequest:noAutoplayRequest]; [webView waitForMessage:@"loaded"]; [webView mouseDownAtPoint:playButtonClickPoint simulatePressure:NO]; [webView mouseUpAtPoint:playButtonClickPoint]; [webView waitForMessage:@"played"]; ASSERT_FALSE(receivedAlert); } #endif #endif