Title: [214006] trunk
Revision
214006
Author
cdu...@apple.com
Date
2017-03-15 14:31:16 -0700 (Wed, 15 Mar 2017)

Log Message

Document state (e.g. form data) is lost after a tab is terminated in the background for power reasons
https://bugs.webkit.org/show_bug.cgi?id=169635
<rdar://problem/31046729>

Reviewed by Andreas Kling.

Source/WebKit2:

Document state (e.g. form data) was lost after a tab was terminated in the background for power
reasons. To address the issue, we now save the document state whenever a page is backgrounded.
This document state automatically gets restored when reloading the page after termination.

* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::visibilityDidChange):
(WebKit::WebPage::setActivityState):
* WebProcess/WebPage/WebPage.h:

Tools:

Add API test coverage.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKit2/mac/RestoreStateAfterTermination.mm: Added.
(TestWebKitAPI::runJavaScriptAlert):
(TestWebKitAPI::didFinishLoad):
(TestWebKitAPI::didCrash):
(TestWebKitAPI::TEST):
* TestWebKitAPI/Tests/WebKit2/simple-form.html:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (214005 => 214006)


--- trunk/Source/WebKit2/ChangeLog	2017-03-15 20:19:35 UTC (rev 214005)
+++ trunk/Source/WebKit2/ChangeLog	2017-03-15 21:31:16 UTC (rev 214006)
@@ -1,3 +1,20 @@
+2017-03-15  Chris Dumez  <cdu...@apple.com>
+
+        Document state (e.g. form data) is lost after a tab is terminated in the background for power reasons
+        https://bugs.webkit.org/show_bug.cgi?id=169635
+        <rdar://problem/31046729>
+
+        Reviewed by Andreas Kling.
+
+        Document state (e.g. form data) was lost after a tab was terminated in the background for power
+        reasons. To address the issue, we now save the document state whenever a page is backgrounded.
+        This document state automatically gets restored when reloading the page after termination.
+
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::visibilityDidChange):
+        (WebKit::WebPage::setActivityState):
+        * WebProcess/WebPage/WebPage.h:
+
 2017-03-15  Wenson Hsieh  <wenson_hs...@apple.com>
 
         WebContent crash due to bad variant access in WebKit: WebKit::WebPage::expandedRangeFromHandle

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp (214005 => 214006)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp	2017-03-15 20:19:35 UTC (rev 214005)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp	2017-03-15 21:31:16 UTC (rev 214006)
@@ -2612,6 +2612,17 @@
         layoutIfNeeded();
 }
 
+void WebPage::visibilityDidChange()
+{
+    bool isVisible = m_activityState & ActivityState::IsVisible;
+    if (!isVisible) {
+        // We save the document / scroll state when backgrounding a tab so that we are able to restore it
+        // if it gets terminated while in the background.
+        if (auto* frame = m_mainFrame->coreFrame())
+            frame->loader().history().saveDocumentAndScrollState();
+    }
+}
+
 void WebPage::setActivityState(ActivityState::Flags activityState, bool wantsDidUpdateActivityState, const Vector<uint64_t>& callbackIDs)
 {
     ActivityState::Flags changed = m_activityState ^ activityState;
@@ -2629,6 +2640,9 @@
 
     if (changed & ActivityState::IsInWindow)
         updateIsInWindow();
+
+    if (changed & ActivityState::IsVisible)
+        visibilityDidChange();
 }
 
 void WebPage::setLayerHostingMode(LayerHostingMode layerHostingMode)

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h (214005 => 214006)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2017-03-15 20:19:35 UTC (rev 214005)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2017-03-15 21:31:16 UTC (rev 214006)
@@ -1054,6 +1054,7 @@
     void tryRestoreScrollPosition();
     void setInitialFocus(bool forward, bool isKeyboardEventValid, const WebKeyboardEvent&, uint64_t callbackID);
     void updateIsInWindow(bool isInitialState = false);
+    void visibilityDidChange();
     void setActivityState(WebCore::ActivityState::Flags, bool wantsDidUpdateActivityState, const Vector<uint64_t>& callbackIDs);
     void validateCommand(const String&, uint64_t);
     void executeEditCommand(const String&, const String&);

Modified: trunk/Tools/ChangeLog (214005 => 214006)


--- trunk/Tools/ChangeLog	2017-03-15 20:19:35 UTC (rev 214005)
+++ trunk/Tools/ChangeLog	2017-03-15 21:31:16 UTC (rev 214006)
@@ -1,3 +1,21 @@
+2017-03-15  Chris Dumez  <cdu...@apple.com>
+
+        Document state (e.g. form data) is lost after a tab is terminated in the background for power reasons
+        https://bugs.webkit.org/show_bug.cgi?id=169635
+        <rdar://problem/31046729>
+
+        Reviewed by Andreas Kling.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKit2/mac/RestoreStateAfterTermination.mm: Added.
+        (TestWebKitAPI::runJavaScriptAlert):
+        (TestWebKitAPI::didFinishLoad):
+        (TestWebKitAPI::didCrash):
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/Tests/WebKit2/simple-form.html:
+
 2017-03-15  Alex Christensen  <achristen...@webkit.org>
 
         Compiled content extensions should include the JSON source

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (214005 => 214006)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2017-03-15 20:19:35 UTC (rev 214005)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2017-03-15 21:31:16 UTC (rev 214006)
@@ -123,6 +123,7 @@
 		46C519E61D3563FD00DAA51A /* LocalStorageNullEntries.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 46C519E21D35629600DAA51A /* LocalStorageNullEntries.html */; };
 		46C519E71D3563FD00DAA51A /* LocalStorageNullEntries.localstorage in Copy Resources */ = {isa = PBXBuildFile; fileRef = 46C519E31D35629600DAA51A /* LocalStorageNullEntries.localstorage */; };
 		46C519E81D3563FD00DAA51A /* LocalStorageNullEntries.localstorage-shm in Copy Resources */ = {isa = PBXBuildFile; fileRef = 46C519E41D35629600DAA51A /* LocalStorageNullEntries.localstorage-shm */; };
+		46E816F81E79E29C00375ADC /* RestoreStateAfterTermination.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46E816F71E79E29100375ADC /* RestoreStateAfterTermination.mm */; };
 		4BFDFFA71314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BFDFFA61314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp */; };
 		510477721D298DDD009747EB /* IDBDeleteRecovery.sqlite3 in Copy Resources */ = {isa = PBXBuildFile; fileRef = 5104776F1D298D85009747EB /* IDBDeleteRecovery.sqlite3 */; };
 		510477731D298DDD009747EB /* IDBDeleteRecovery.sqlite3-shm in Copy Resources */ = {isa = PBXBuildFile; fileRef = 510477701D298D85009747EB /* IDBDeleteRecovery.sqlite3-shm */; };
@@ -1010,6 +1011,7 @@
 		46C519E21D35629600DAA51A /* LocalStorageNullEntries.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LocalStorageNullEntries.html; sourceTree = "<group>"; };
 		46C519E31D35629600DAA51A /* LocalStorageNullEntries.localstorage */ = {isa = PBXFileReference; lastKnownFileType = file; path = LocalStorageNullEntries.localstorage; sourceTree = "<group>"; };
 		46C519E41D35629600DAA51A /* LocalStorageNullEntries.localstorage-shm */ = {isa = PBXFileReference; lastKnownFileType = file; path = "LocalStorageNullEntries.localstorage-shm"; sourceTree = "<group>"; };
+		46E816F71E79E29100375ADC /* RestoreStateAfterTermination.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RestoreStateAfterTermination.mm; sourceTree = "<group>"; };
 		4A410F4B19AF7BD6002EBAB5 /* UserMedia.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UserMedia.cpp; sourceTree = "<group>"; };
 		4A410F4D19AF7BEF002EBAB5 /* getUserMedia.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = getUserMedia.html; sourceTree = "<group>"; };
 		4BB4160116815B2600824238 /* JSWrapperForNodeInWebFrame.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = JSWrapperForNodeInWebFrame.mm; sourceTree = "<group>"; };
@@ -2345,6 +2347,7 @@
 				C0C5D3BC14598B6F00A802A6 /* GetBackingScaleFactor.mm */,
 				C0C5D3BD14598B6F00A802A6 /* GetBackingScaleFactor_Bundle.mm */,
 				1AEF994817A09F5300998EF0 /* GetPIDAfterAbortedProcessLaunch.cpp */,
+				46E816F71E79E29100375ADC /* RestoreStateAfterTermination.mm */,
 			);
 			path = mac;
 			sourceTree = "<group>";
@@ -2781,6 +2784,7 @@
 				5C0BF8941DD599C900B00328 /* MenuTypesForMouseEvents.mm in Sources */,
 				316BDBA11E70D71B00DE0D5A /* GPULibrary.mm in Sources */,
 				5C0BF8931DD599BD00B00328 /* IsNavigationActionTrusted.mm in Sources */,
+				46E816F81E79E29C00375ADC /* RestoreStateAfterTermination.mm in Sources */,
 				7CCE7EFF1A411AE600447C4C /* LoadCanceledNoServerRedirectCallback.cpp in Sources */,
 				7C83E0C01D0A652700FEBCF3 /* LoadInvalidURLRequest.mm in Sources */,
 				7CCE7F001A411AE600447C4C /* LoadPageOnCrash.cpp in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/WebKit2/mac/RestoreStateAfterTermination.mm (0 => 214006)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2/mac/RestoreStateAfterTermination.mm	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2/mac/RestoreStateAfterTermination.mm	2017-03-15 21:31:16 UTC (rev 214006)
@@ -0,0 +1,131 @@
+/*
+ * 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 "_javascript_Test.h"
+#import "PlatformUtilities.h"
+#import "PlatformWebView.h"
+#import "Test.h"
+#import <WebKit/WKRetainPtr.h>
+#import <wtf/RetainPtr.h>
+
+namespace TestWebKitAPI {
+
+static bool loadBeforeCrash = false;
+static bool loadAfterCrash = false;
+static bool isWaitingForPageSignalToContinue = false;
+static bool didGetPageSignalToContinue = false;
+
+static void runJavaScriptAlert(WKPageRef page, WKStringRef alertText, WKFrameRef frame, const void* clientInfo)
+{
+    EXPECT_TRUE(isWaitingForPageSignalToContinue);
+    isWaitingForPageSignalToContinue = false;
+    didGetPageSignalToContinue = true;
+}
+
+static void didFinishLoad(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo)
+{
+    // First load before WebProcess was terminated.
+    if (!loadBeforeCrash) {
+        loadBeforeCrash = true;
+        return;
+    }
+
+    // Next load after WebProcess was terminated (hopefully
+    // it will be correctly re-spawned).
+    EXPECT_EQ(static_cast<uint32_t>(kWKFrameLoadStateFinished), WKFrameGetFrameLoadState(frame));
+    EXPECT_FALSE(loadAfterCrash);
+
+    // Set it, otherwise the loop will not end.
+    loadAfterCrash = true;
+}
+
+static void didCrash(WKPageRef page, const void*)
+{
+    // Test if first load actually worked.
+    EXPECT_TRUE(loadBeforeCrash);
+}
+
+TEST(WebKit2, RestoreStateAfterTermination)
+{
+    WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate());
+    PlatformWebView webView(context.get());
+
+    // Add view to a Window and make it visible.
+    RetainPtr<NSWindow> window = adoptNS([[NSWindow alloc] initWithContentRect:webView.platformView().frame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]);
+    [window.get().contentView addSubview:webView.platformView()];
+
+    WKPageLoaderClientV0 loaderClient;
+    memset(&loaderClient, 0, sizeof(loaderClient));
+    loaderClient.base.version = 0;
+    loaderClient.didFinishLoadForFrame = didFinishLoad;
+    loaderClient.processDidCrash = didCrash;
+    WKPageSetPageLoaderClient(webView.page(), &loaderClient.base);
+
+    WKPageUIClientV0 uiClient;
+    memset(&uiClient, 0, sizeof(uiClient));
+    uiClient.base.version = 0;
+    uiClient.runJavaScriptAlert = runJavaScriptAlert;
+    WKPageSetPageUIClient(webView.page(), &uiClient.base);
+
+    // Load a test page containing a form.
+    WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple-form", "html")).get());
+    Util::run(&loadBeforeCrash);
+
+    // Make the page visible.
+    isWaitingForPageSignalToContinue = true;
+    didGetPageSignalToContinue = false;
+    [window.get() makeKeyAndOrderFront:nil];
+    Util::run(&didGetPageSignalToContinue);
+    EXPECT_JS_EQ(webView.page(), "document.visibilityState", "visible");
+
+    EXPECT_JS_EQ(webView.page(), "getFormData()", "Some unimportant data");
+
+    // Simulate user entering form data.
+    EXPECT_JS_EQ(webView.page(), "fillForm('Important data')", "undefined");
+
+    EXPECT_JS_EQ(webView.page(), "getFormData()", "Important data");
+
+    // Unparent the view so that it is no longer visible. This should cause us to save document state.
+    isWaitingForPageSignalToContinue = true;
+    didGetPageSignalToContinue = false;
+    [webView.platformView() removeFromSuperview];
+    Util::run(&didGetPageSignalToContinue);
+
+    EXPECT_JS_EQ(webView.page(), "document.visibilityState", "hidden");
+
+    // Terminate page while in the background.
+    WKPageTerminate(webView.page());
+
+    // Reload should re-spawn webprocess and restore document state.
+    WKPageReload(webView.page());
+    Util::run(&loadAfterCrash);
+
+    // Make sure that the form data is restored after the reload.
+    EXPECT_JS_EQ(webView.page(), "getFormData()", "Important data");
+}
+
+} // namespace TestWebKitAPI

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2/simple-form.html (214005 => 214006)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2/simple-form.html	2017-03-15 20:19:35 UTC (rev 214005)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2/simple-form.html	2017-03-15 21:31:16 UTC (rev 214006)
@@ -4,6 +4,22 @@
 {
     document.forms[0].submit();
 }
+
+function fillForm(data)
+{
+    document.getElementsByName("foo")[0].value = data;
+}
+
+function getFormData()
+{
+    return document.getElementsByName("foo")[0].value;
+}
+
+document.addEventListener("visibilitychange", function(event) {
+    // Send a signal to the test controller via alert.
+    alert('visibilitychange');
+});
+
 </script>
 <form method=post>
 <input name=foo value="Some unimportant data">
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to