WKWebView can get stuck blank (Web Content process thinks the app is backgrounded)
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 3 Nov 2019 20:34:50 +0000 (20:34 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 3 Nov 2019 20:34:50 +0000 (20:34 +0000)
https://bugs.webkit.org/show_bug.cgi?id=203774
<rdar://problem/53399054>

Reviewed by Chris Dumez.

Source/WebKit:

With careful timing, if a WKWebView swaps out its content view in the
background, the incoming view can fail to inform the Web Content process
when the app comes to the foreground, leaving the layer tree frozen.

This occurs because the last-sent state is stored per-WKApplicationStateTrackingView,
and the content view is the WKApplicationStateTrackingView, so it is possible
for e.g. a WKPDFView to be in the hierarchy, keeping the correct state,
with an initialized-but-never-parented WKContentView hanging off the WKWebView.

If it is never parented, WKContentView will think that the current application
state is foreground (_lastObservedStateWasBackground is initialized to NO).

If you go into the background with a WKPDFView as the current content view,
it will inform the Web Content process that the application has backgrounded.

If, still in the background, WKWebView swaps from the WKPDFView to
the WKContentView, and then comes into the foreground, when we get the
notification that the app came to the foreground, we will not forward it
to the Web Content process, because WKContentView's _lastObservedStateWasBackground
remains NO.

To fix this, move _lastObservedStateWasBackground to WebPageProxy, so that
it always tracks the most recently sent state, regardless of which content view
is active.

* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::resetState):
* UIProcess/WebPageProxy.h:
(WebKit::WebPageProxy::lastObservedStateWasBackground const):
* UIProcess/ios/WKApplicationStateTrackingView.mm:
(-[WKApplicationStateTrackingView willMoveToWindow:]):
(-[WKApplicationStateTrackingView didMoveToWindow]):
(-[WKApplicationStateTrackingView _applicationDidEnterBackground]):
(-[WKApplicationStateTrackingView _applicationWillEnterForeground]):
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::applicationDidEnterBackground):
(WebKit::WebPageProxy::applicationWillEnterForeground):

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKitCocoa/WKPDFView.mm: Added.
(TEST):
(isBackground):
(createHostViewForExtensionIdentifier):
* TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewResizeCrash.mm: Removed.
* TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewStablePresentationUpdateCallback.mm: Removed.
* TestWebKitAPI/cocoa/TestNavigationDelegate.h:
* TestWebKitAPI/cocoa/TestNavigationDelegate.mm:
(-[WKWebView _test_waitForDidFinishNavigationWithoutPresentationUpdate]):
Merge existing WKPDFView tests into one file, and add one for this bug.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@251976 268f45cc-cd09-0410-ab3c-d52691b4dbfc

12 files changed:
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/WebPageProxy.cpp
Source/WebKit/UIProcess/WebPageProxy.h
Source/WebKit/UIProcess/ios/WKApplicationStateTrackingView.mm
Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFView.mm [new file with mode: 0644]
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewResizeCrash.mm [deleted file]
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewStablePresentationUpdateCallback.mm [deleted file]
Tools/TestWebKitAPI/cocoa/TestNavigationDelegate.h
Tools/TestWebKitAPI/cocoa/TestNavigationDelegate.mm

index 96212c7..feba3b5 100644 (file)
@@ -1,3 +1,49 @@
+2019-11-03  Tim Horton  <timothy_horton@apple.com>
+
+        WKWebView can get stuck blank (Web Content process thinks the app is backgrounded)
+        https://bugs.webkit.org/show_bug.cgi?id=203774
+        <rdar://problem/53399054>
+
+        Reviewed by Chris Dumez.
+
+        With careful timing, if a WKWebView swaps out its content view in the
+        background, the incoming view can fail to inform the Web Content process
+        when the app comes to the foreground, leaving the layer tree frozen.
+
+        This occurs because the last-sent state is stored per-WKApplicationStateTrackingView,
+        and the content view is the WKApplicationStateTrackingView, so it is possible
+        for e.g. a WKPDFView to be in the hierarchy, keeping the correct state,
+        with an initialized-but-never-parented WKContentView hanging off the WKWebView.
+
+        If it is never parented, WKContentView will think that the current application
+        state is foreground (_lastObservedStateWasBackground is initialized to NO).
+
+        If you go into the background with a WKPDFView as the current content view,
+        it will inform the Web Content process that the application has backgrounded.
+
+        If, still in the background, WKWebView swaps from the WKPDFView to
+        the WKContentView, and then comes into the foreground, when we get the
+        notification that the app came to the foreground, we will not forward it
+        to the Web Content process, because WKContentView's _lastObservedStateWasBackground
+        remains NO.
+
+        To fix this, move _lastObservedStateWasBackground to WebPageProxy, so that
+        it always tracks the most recently sent state, regardless of which content view
+        is active.
+
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::resetState):
+        * UIProcess/WebPageProxy.h:
+        (WebKit::WebPageProxy::lastObservedStateWasBackground const):
+        * UIProcess/ios/WKApplicationStateTrackingView.mm:
+        (-[WKApplicationStateTrackingView willMoveToWindow:]):
+        (-[WKApplicationStateTrackingView didMoveToWindow]):
+        (-[WKApplicationStateTrackingView _applicationDidEnterBackground]):
+        (-[WKApplicationStateTrackingView _applicationWillEnterForeground]):
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::applicationDidEnterBackground):
+        (WebKit::WebPageProxy::applicationWillEnterForeground):
+
 2019-11-02  Devin Rousso  <drousso@apple.com>
 
         Web Inspector: Add diagnostic logging for frontend feature usage
index f395062..c6b6674 100644 (file)
@@ -7159,6 +7159,7 @@ void WebPageProxy::resetState(ResetStateReason resetStateReason)
     m_hasNetworkRequestsOnSuspended = false;
     m_isKeyboardAnimatingIn = false;
     m_isScrollingOrZooming = false;
+    m_lastObservedStateWasBackground = false;
 #endif
 
 #if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS_FAMILY)
index 38653e2..b84fb9e 100644 (file)
@@ -736,6 +736,7 @@ public:
     void applicationDidEnterBackground();
     void applicationDidFinishSnapshottingAfterEnteringBackground();
     void applicationWillEnterForeground();
+    bool lastObservedStateWasBackground() const { return m_lastObservedStateWasBackground; }
     void applicationWillResignActive();
     void applicationDidBecomeActive();
     void commitPotentialTapFailed();
@@ -2542,6 +2543,7 @@ private:
     double m_viewportConfigurationLayoutSizeScaleFactor { 1 };
     double m_viewportConfigurationMinimumEffectiveDeviceWidth { 0 };
     WebCore::FloatSize m_maximumUnobscuredSize;
+    bool m_lastObservedStateWasBackground { false };
 #endif
 
     Optional<WebCore::FontAttributes> m_cachedFontAttributesAtSelectionStart;
index 6c8b43f..b7f7026 100644 (file)
@@ -38,7 +38,6 @@
 @implementation WKApplicationStateTrackingView {
     WeakObjCPtr<WKWebView> _webViewToTrack;
     std::unique_ptr<WebKit::ApplicationStateTracker> _applicationStateTracker;
-    BOOL _lastObservedStateWasBackground;
 }
 
 - (instancetype)initWithFrame:(CGRect)frame webView:(WKWebView *)webView
     if (!self._contentView.window || newWindow)
         return;
 
-    _lastObservedStateWasBackground = [self isBackground];
-
     auto page = [_webViewToTrack _page];
-    RELEASE_LOG(ViewState, "%p - WKApplicationStateTrackingView: View with page [%p, pageProxyID: %" PRIu64 "] was removed from a window, _lastObservedStateWasBackground: %d", self, page, page ? page->identifier().toUInt64() : 0, _lastObservedStateWasBackground);
+    RELEASE_LOG(ViewState, "%p - WKApplicationStateTrackingView: View with page [%p, pageProxyID: %" PRIu64 "] was removed from a window, _lastObservedStateWasBackground: %d", self, page, page ? page->identifier().toUInt64() : 0, page ? page->lastObservedStateWasBackground() : false);
     ASSERT(_applicationStateTracker);
     _applicationStateTracker = nullptr;
 }
         return;
 
     auto page = [_webViewToTrack _page];
+    bool lastObservedStateWasBackground = page ? page->lastObservedStateWasBackground() : false;
+
     _applicationStateTracker = makeUnique<WebKit::ApplicationStateTracker>(self, @selector(_applicationDidEnterBackground), @selector(_applicationDidFinishSnapshottingAfterEnteringBackground), @selector(_applicationWillEnterForeground));
-    RELEASE_LOG(ViewState, "%p - WKApplicationStateTrackingView: View with page [%p, pageProxyID: %" PRIu64 "] was added to a window, _lastObservedStateWasBackground: %d, isNowBackground: %d", self, page, page ? page->identifier().toUInt64() : 0, _lastObservedStateWasBackground, [self isBackground]);
+    RELEASE_LOG(ViewState, "%p - WKApplicationStateTrackingView: View with page [%p, pageProxyID: %" PRIu64 "] was added to a window, _lastObservedStateWasBackground: %d, isNowBackground: %d", self, page, page ? page->identifier().toUInt64() : 0, lastObservedStateWasBackground, [self isBackground]);
 
-    if (_lastObservedStateWasBackground && ![self isBackground])
+    if (lastObservedStateWasBackground && ![self isBackground])
         [self _applicationWillEnterForeground];
-    else if (!_lastObservedStateWasBackground && [self isBackground])
+    else if (!lastObservedStateWasBackground && [self isBackground])
         [self _applicationDidEnterBackground];
 }
 
@@ -84,7 +83,6 @@
     if (!page)
         return;
 
-    _lastObservedStateWasBackground = YES;
     page->applicationDidEnterBackground();
     page->activityStateDidChange(WebCore::ActivityState::allFlags() - WebCore::ActivityState::IsInWindow);
 }
     if (!page)
         return;
 
-    _lastObservedStateWasBackground = NO;
     page->applicationWillEnterForeground();
     page->activityStateDidChange(WebCore::ActivityState::allFlags() - WebCore::ActivityState::IsInWindow, true, WebKit::WebPageProxy::ActivityStateChangeDispatchMode::Immediate);
 }
index 29040a4..c041be1 100644 (file)
@@ -649,6 +649,8 @@ void WebPageProxy::saveImageToLibrary(const SharedMemory::Handle& imageHandle, u
 
 void WebPageProxy::applicationDidEnterBackground()
 {
+    m_lastObservedStateWasBackground = true;
+
     bool isSuspendedUnderLock = [UIApp isSuspendedUnderLock];
     
     RELEASE_LOG_IF_ALLOWED(ViewState, "applicationDidEnterBackground: isSuspendedUnderLock? %d", isSuspendedUnderLock);
@@ -678,6 +680,8 @@ bool WebPageProxy::isInHardwareKeyboardMode()
 
 void WebPageProxy::applicationWillEnterForeground()
 {
+    m_lastObservedStateWasBackground = false;
+
     bool isSuspendedUnderLock = [UIApp isSuspendedUnderLock];
     RELEASE_LOG_IF_ALLOWED(ViewState, "applicationWillEnterForeground: isSuspendedUnderLock? %d", isSuspendedUnderLock);
 
index 16a6020..0199bf1 100644 (file)
@@ -1,3 +1,23 @@
+2019-11-03  Tim Horton  <timothy_horton@apple.com>
+
+        WKWebView can get stuck blank (Web Content process thinks the app is backgrounded)
+        https://bugs.webkit.org/show_bug.cgi?id=203774
+        <rdar://problem/53399054>
+
+        Reviewed by Chris Dumez.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKitCocoa/WKPDFView.mm: Added.
+        (TEST):
+        (isBackground):
+        (createHostViewForExtensionIdentifier):
+        * TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewResizeCrash.mm: Removed.
+        * TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewStablePresentationUpdateCallback.mm: Removed.
+        * TestWebKitAPI/cocoa/TestNavigationDelegate.h:
+        * TestWebKitAPI/cocoa/TestNavigationDelegate.mm:
+        (-[WKWebView _test_waitForDidFinishNavigationWithoutPresentationUpdate]):
+        Merge existing WKPDFView tests into one file, and add one for this bug.
+
 2019-11-02  Devin Rousso  <drousso@apple.com>
 
         Web Inspector: Add diagnostic logging for frontend feature usage
index 7c09189..8e49b5f 100644 (file)
                290A9BB91735F63800D71BBC /* OpenNewWindow.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 290A9BB81735F42300D71BBC /* OpenNewWindow.html */; };
                290F4275172A221C00939FF0 /* custom-protocol-sync-xhr.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 290F4274172A1FDE00939FF0 /* custom-protocol-sync-xhr.html */; };
                297234B7173AFAC700983601 /* CustomProtocolsInvalidScheme_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 297234B5173AFAC700983601 /* CustomProtocolsInvalidScheme_Bundle.cpp */; };
-               2D00065F1C1F589A0088E6A7 /* WKPDFViewResizeCrash.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D00065D1C1F58940088E6A7 /* WKPDFViewResizeCrash.mm */; };
+               2D00065F1C1F589A0088E6A7 /* WKPDFView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D00065D1C1F58940088E6A7 /* WKPDFView.mm */; };
                2D01D06E23218FEE0039AA3A /* WKWebViewPrintFormatter.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D01D06D23218FEE0039AA3A /* WKWebViewPrintFormatter.mm */; };
                2D08E9372267D0F4002518DA /* ReparentWebViewTimeout.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D08E9362267D0F3002518DA /* ReparentWebViewTimeout.mm */; };
                2D1646E21D1862CD00015A1A /* DeferredViewInWindowStateChange.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D1646E11D1862CD00015A1A /* DeferredViewInWindowStateChange.mm */; };
-               2D21FE591F04642900B58E7D /* WKPDFViewStablePresentationUpdateCallback.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D21FE581F04642800B58E7D /* WKPDFViewStablePresentationUpdateCallback.mm */; };
                2D2BEB2D22324E5F005544CA /* RequestTextInputContext.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D2BEB2C22324E5F005544CA /* RequestTextInputContext.mm */; };
                2D2D13B3229F408B005068AF /* DeviceManagementRestrictions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D2D13B2229F408B005068AF /* DeviceManagementRestrictions.mm */; };
                2D2FE8D82315F06E00B2E9C9 /* ReloadWithDifferingInitialScale.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D2FE8D72315F06D00B2E9C9 /* ReloadWithDifferingInitialScale.mm */; };
                                577454D02359B378008E1ED7 /* web-authentication-get-assertion-hid-no-credentials.html in Copy Resources */,
                                57663DEC234F1F9300E85E09 /* web-authentication-get-assertion-hid.html in Copy Resources */,
                                579833922368FA37008E5547 /* web-authentication-get-assertion-nfc-multiple-tags.html in Copy Resources */,
-                               5798337C235EB689008E5547 /* web-authentication-get-assertion-nfc-multiple-tags.html in Copy Resources */,
                                57663DEA234EA66D00E85E09 /* web-authentication-get-assertion-nfc.html in Copy Resources */,
                                577454D22359BB01008E1ED7 /* web-authentication-get-assertion-u2f-no-credentials.html in Copy Resources */,
                                57C624502346C21E00383FE7 /* web-authentication-get-assertion.html in Copy Resources */,
                29AB8A9F164C735800D49BEC /* CustomProtocolsTest.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = CustomProtocolsTest.mm; path = WebKitObjC/CustomProtocolsTest.mm; sourceTree = "<group>"; };
                29AB8AA2164C7A9300D49BEC /* TestBrowsingContextLoadDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TestBrowsingContextLoadDelegate.mm; sourceTree = "<group>"; };
                29AB8AA3164C7A9300D49BEC /* TestBrowsingContextLoadDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestBrowsingContextLoadDelegate.h; sourceTree = "<group>"; };
-               2D00065D1C1F58940088E6A7 /* WKPDFViewResizeCrash.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKPDFViewResizeCrash.mm; sourceTree = "<group>"; };
+               2D00065D1C1F58940088E6A7 /* WKPDFView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKPDFView.mm; sourceTree = "<group>"; };
                2D01D06D23218FEE0039AA3A /* WKWebViewPrintFormatter.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKWebViewPrintFormatter.mm; sourceTree = "<group>"; };
                2D08E9362267D0F3002518DA /* ReparentWebViewTimeout.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ReparentWebViewTimeout.mm; sourceTree = "<group>"; };
                2D1646E11D1862CD00015A1A /* DeferredViewInWindowStateChange.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = DeferredViewInWindowStateChange.mm; path = WebKit/DeferredViewInWindowStateChange.mm; sourceTree = "<group>"; };
                2D1C04A51D76298B000A6816 /* TestNavigationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestNavigationDelegate.h; path = cocoa/TestNavigationDelegate.h; sourceTree = "<group>"; };
                2D1C04A61D76298B000A6816 /* TestNavigationDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = TestNavigationDelegate.mm; path = cocoa/TestNavigationDelegate.mm; sourceTree = "<group>"; };
                2D1FE0AF1AD465C1006CD9E6 /* FixedLayoutSize.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FixedLayoutSize.mm; sourceTree = "<group>"; };
-               2D21FE581F04642800B58E7D /* WKPDFViewStablePresentationUpdateCallback.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKPDFViewStablePresentationUpdateCallback.mm; sourceTree = "<group>"; };
                2D2BEB2C22324E5F005544CA /* RequestTextInputContext.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RequestTextInputContext.mm; sourceTree = "<group>"; };
                2D2D13B2229F408B005068AF /* DeviceManagementRestrictions.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DeviceManagementRestrictions.mm; sourceTree = "<group>"; };
                2D2FE8D72315F06D00B2E9C9 /* ReloadWithDifferingInitialScale.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ReloadWithDifferingInitialScale.mm; sourceTree = "<group>"; };
                                DF4B273821A47727009BD1CA /* WKNSDictionaryEmptyDictionaryCrash.mm */,
                                375E0E151D66674400EFEC2C /* WKNSNumber.mm */,
                                37B47E2E1D64E7CA005F4EFF /* WKObject.mm */,
-                               2D00065D1C1F58940088E6A7 /* WKPDFViewResizeCrash.mm */,
-                               2D21FE581F04642800B58E7D /* WKPDFViewStablePresentationUpdateCallback.mm */,
+                               2D00065D1C1F58940088E6A7 /* WKPDFView.mm */,
                                3781746C2198AE2400062C26 /* WKProcessPoolConfiguration.mm */,
                                44817A2E1F0486BF00003810 /* WKRequestActivatedElementInfo.mm */,
                                5E4B1D2C1D404C6100053621 /* WKScrollViewDelegate.mm */,
                                52D673EE1AFB127300FA19FE /* WKPageCopySessionStateWithFiltering.cpp in Sources */,
                                7CCE7F1F1A411AE600447C4C /* WKPageGetScaleFactorNotZero.cpp in Sources */,
                                7CCE7F201A411AE600447C4C /* WKPageIsPlayingAudio.cpp in Sources */,
-                               2D00065F1C1F589A0088E6A7 /* WKPDFViewResizeCrash.mm in Sources */,
-                               2D21FE591F04642900B58E7D /* WKPDFViewStablePresentationUpdateCallback.mm in Sources */,
+                               2D00065F1C1F589A0088E6A7 /* WKPDFView.mm in Sources */,
                                7CCE7F211A411AE600447C4C /* WKPreferences.cpp in Sources */,
                                3781746D2198AE4600062C26 /* WKProcessPoolConfiguration.mm in Sources */,
                                44817A2F1F0486BF00003810 /* WKRequestActivatedElementInfo.mm in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFView.mm b/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFView.mm
new file mode 100644 (file)
index 0000000..a0f5a45
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2015-2019 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 "ClassMethodSwizzler.h"
+#import "InstanceMethodSwizzler.h"
+#import "PlatformUtilities.h"
+#import "Test.h"
+#import "TestNavigationDelegate.h"
+#import <WebKit/WKWebView.h>
+#import <WebKit/WKWebViewConfigurationPrivate.h>
+#import <wtf/RetainPtr.h>
+
+#if PLATFORM(IOS_FAMILY)
+
+@interface PDFHostViewController : UIViewController
++ (void)createHostView:(void(^)(id hostViewController))callback forExtensionIdentifier:(NSString *)extensionIdentifier;
+@end
+
+@interface WKApplicationStateTrackingView : UIView
+- (BOOL)isBackground;
+@end
+
+TEST(WebKit, WKPDFViewResizeCrash)
+{
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf" subdirectory:@"TestWebKitAPI.resources"]];
+    [webView loadRequest:request];
+    [webView _test_waitForDidFinishNavigation];
+
+    [webView setFrame:NSMakeRect(0, 0, 100, 100)];
+    webView = nil;
+
+    __block bool finishedDispatch = false;
+    dispatch_async(dispatch_get_main_queue(), ^{
+        finishedDispatch = true;
+    });
+
+    TestWebKitAPI::Util::run(&finishedDispatch);
+}
+
+TEST(WebKit, WKPDFViewStablePresentationUpdateCallback)
+{
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf" subdirectory:@"TestWebKitAPI.resources"]];
+    [webView loadRequest:request];
+    [webView _test_waitForDidFinishNavigation];
+
+    __block bool finished;
+    [webView _doAfterNextStablePresentationUpdate:^{
+        finished = true;
+    }];
+
+    TestWebKitAPI::Util::run(&finished);
+}
+
+static BOOL sIsBackground;
+static BOOL isBackground(id self)
+{
+    return sIsBackground;
+}
+
+static void createHostViewForExtensionIdentifier(void(^callback)(id hostViewController), NSString *extensionIdentifier)
+{
+}
+
+TEST(WebKit, WKPDFViewLosesApplicationForegroundNotification)
+{
+    std::unique_ptr<ClassMethodSwizzler> pdfHostViewControllerSwizzler = makeUnique<ClassMethodSwizzler>([PDFHostViewController class], @selector(createHostView:forExtensionIdentifier:), reinterpret_cast<IMP>(createHostViewForExtensionIdentifier));
+
+    std::unique_ptr<InstanceMethodSwizzler> isInBackgroundSwizzler = makeUnique<InstanceMethodSwizzler>(NSClassFromString(@"WKApplicationStateTrackingView"), @selector(isBackground), reinterpret_cast<IMP>(isBackground));
+
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [configuration _setAlwaysRunsAtForegroundPriority:YES];
+
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+
+    // Load a PDF, so we install a WKPDFView.
+    [webView loadData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf" subdirectory:@"TestWebKitAPI.resources"]] MIMEType:@"application/pdf" characterEncodingName:@"" baseURL:[NSURL URLWithString:@"https://www.apple.com/0"]];
+    [webView _test_waitForDidFinishNavigation];
+
+    // Go into the background and parent the WKWebView.
+    // This will cause WKPDFView to send a applicationDidEnterBackground
+    // to the Web Content process, freezing the layer tree.
+    sIsBackground = YES;
+    RetainPtr<UIWindow> uiWindow = adoptNS([[UIWindow alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+    [uiWindow addSubview:webView.get()];
+    [webView removeFromSuperview];
+
+    // Load a HTML document, so we switch back to WKContentView.
+    [webView loadHTMLString:@"<meta name='viewport' content='width=device-width'><h1>hello world</h1>" baseURL:[NSURL URLWithString:@"https://www.apple.com/1"]];
+    [webView _test_waitForDidFinishNavigationWithoutPresentationUpdate];
+
+    // Go into the foreground, and parent the view.
+    // This should be sufficient to unfreeze the layer tree.
+    sIsBackground = NO;
+    [uiWindow addSubview:webView.get()];
+
+    __block bool finished = false;
+    // If the bug reproduces, no stable presentation update will ever come,
+    // so the test times out here.
+    [webView _doAfterNextPresentationUpdate:^{
+        finished = true;
+    }];
+
+    TestWebKitAPI::Util::run(&finished);
+}
+
+#endif
diff --git a/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewResizeCrash.mm b/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewResizeCrash.mm
deleted file mode 100644 (file)
index 28659e1..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (C) 2015 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 "Test.h"
-#import "TestNavigationDelegate.h"
-#import <WebKit/WKWebView.h>
-#import <wtf/RetainPtr.h>
-
-static bool finishedDispatch;
-
-TEST(WebKit, WKPDFViewResizeCrash)
-{
-    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
-
-    NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf" subdirectory:@"TestWebKitAPI.resources"]];
-    [webView loadRequest:request];
-    [webView _test_waitForDidFinishNavigation];
-
-    [webView setFrame:NSMakeRect(0, 0, 100, 100)];
-    webView = nil;
-
-    dispatch_async(dispatch_get_main_queue(), ^{
-        finishedDispatch = true;
-    });
-
-    TestWebKitAPI::Util::run(&finishedDispatch);
-}
-
diff --git a/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewStablePresentationUpdateCallback.mm b/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFViewStablePresentationUpdateCallback.mm
deleted file mode 100644 (file)
index afe6dfe..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * 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.
- */
-
-#include "config.h"
-
-#import "PlatformUtilities.h"
-#import "Test.h"
-#import "TestNavigationDelegate.h"
-#import <WebKit/WKWebView.h>
-#import <wtf/RetainPtr.h>
-
-#if PLATFORM(IOS_FAMILY)
-
-TEST(WebKit, WKPDFViewStablePresentationUpdateCallback)
-{
-    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
-
-    NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf" subdirectory:@"TestWebKitAPI.resources"]];
-    [webView loadRequest:request];
-    [webView _test_waitForDidFinishNavigation];
-
-    __block bool finished;
-
-    [webView _doAfterNextStablePresentationUpdate:^ {
-        finished = true;
-    }];
-
-    TestWebKitAPI::Util::run(&finished);
-}
-
-#endif
index 425d675..223201b 100644 (file)
@@ -48,4 +48,5 @@
 @interface WKWebView (TestWebKitAPIExtras)
 - (void)_test_waitForDidStartProvisionalNavigation;
 - (void)_test_waitForDidFinishNavigation;
+- (void)_test_waitForDidFinishNavigationWithoutPresentationUpdate;
 @end
index 558ea77..9676a33 100644 (file)
     self.navigationDelegate = nil;
 }
 
+- (void)_test_waitForDidFinishNavigationWithoutPresentationUpdate
+{
+    EXPECT_FALSE(self.navigationDelegate);
+
+    auto navigationDelegate = adoptNS([[TestNavigationDelegate alloc] init]);
+    self.navigationDelegate = navigationDelegate.get();
+    [navigationDelegate waitForDidFinishNavigation];
+
+    self.navigationDelegate = nil;
+}
+
 - (void)_test_waitForDidFinishNavigation
 {
     EXPECT_FALSE(self.navigationDelegate);