<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[214006] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/214006">214006</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2017-03-15 14:31:16 -0700 (Wed, 15 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>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
&lt;rdar://problem/31046729&gt;

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:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageWebPagecpp">trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageWebPageh">trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestWebKitAPIxcodeprojprojectpbxproj">trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKit2simpleformhtml">trunk/Tools/TestWebKitAPI/Tests/WebKit2/simple-form.html</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkToolsTestWebKitAPITestsWebKit2macRestoreStateAfterTerminationmm">trunk/Tools/TestWebKitAPI/Tests/WebKit2/mac/RestoreStateAfterTermination.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (214005 => 214006)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,3 +1,20 @@
</span><ins>+2017-03-15  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        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
+        &lt;rdar://problem/31046729&gt;
+
+        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:
+
</ins><span class="cx"> 2017-03-15  Wenson Hsieh  &lt;wenson_hsieh@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         WebContent crash due to bad variant access in WebKit: WebKit::WebPage::expandedRangeFromHandle
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageWebPagecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp (214005 => 214006)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -2612,6 +2612,17 @@
</span><span class="cx">         layoutIfNeeded();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebPage::visibilityDidChange()
+{
+    bool isVisible = m_activityState &amp; 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-&gt;coreFrame())
+            frame-&gt;loader().history().saveDocumentAndScrollState();
+    }
+}
+
</ins><span class="cx"> void WebPage::setActivityState(ActivityState::Flags activityState, bool wantsDidUpdateActivityState, const Vector&lt;uint64_t&gt;&amp; callbackIDs)
</span><span class="cx"> {
</span><span class="cx">     ActivityState::Flags changed = m_activityState ^ activityState;
</span><span class="lines">@@ -2629,6 +2640,9 @@
</span><span class="cx"> 
</span><span class="cx">     if (changed &amp; ActivityState::IsInWindow)
</span><span class="cx">         updateIsInWindow();
</span><ins>+
+    if (changed &amp; ActivityState::IsVisible)
+        visibilityDidChange();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebPage::setLayerHostingMode(LayerHostingMode layerHostingMode)
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageWebPageh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h (214005 => 214006)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1054,6 +1054,7 @@
</span><span class="cx">     void tryRestoreScrollPosition();
</span><span class="cx">     void setInitialFocus(bool forward, bool isKeyboardEventValid, const WebKeyboardEvent&amp;, uint64_t callbackID);
</span><span class="cx">     void updateIsInWindow(bool isInitialState = false);
</span><ins>+    void visibilityDidChange();
</ins><span class="cx">     void setActivityState(WebCore::ActivityState::Flags, bool wantsDidUpdateActivityState, const Vector&lt;uint64_t&gt;&amp; callbackIDs);
</span><span class="cx">     void validateCommand(const String&amp;, uint64_t);
</span><span class="cx">     void executeEditCommand(const String&amp;, const String&amp;);
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (214005 => 214006)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog        2017-03-15 20:19:35 UTC (rev 214005)
+++ trunk/Tools/ChangeLog        2017-03-15 21:31:16 UTC (rev 214006)
</span><span class="lines">@@ -1,3 +1,21 @@
</span><ins>+2017-03-15  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        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
+        &lt;rdar://problem/31046729&gt;
+
+        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:
+
</ins><span class="cx"> 2017-03-15  Alex Christensen  &lt;achristensen@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Compiled content extensions should include the JSON source
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestWebKitAPIxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (214005 => 214006)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -123,6 +123,7 @@
</span><span class="cx">                 46C519E61D3563FD00DAA51A /* LocalStorageNullEntries.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 46C519E21D35629600DAA51A /* LocalStorageNullEntries.html */; };
</span><span class="cx">                 46C519E71D3563FD00DAA51A /* LocalStorageNullEntries.localstorage in Copy Resources */ = {isa = PBXBuildFile; fileRef = 46C519E31D35629600DAA51A /* LocalStorageNullEntries.localstorage */; };
</span><span class="cx">                 46C519E81D3563FD00DAA51A /* LocalStorageNullEntries.localstorage-shm in Copy Resources */ = {isa = PBXBuildFile; fileRef = 46C519E41D35629600DAA51A /* LocalStorageNullEntries.localstorage-shm */; };
</span><ins>+                46E816F81E79E29C00375ADC /* RestoreStateAfterTermination.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46E816F71E79E29100375ADC /* RestoreStateAfterTermination.mm */; };
</ins><span class="cx">                 4BFDFFA71314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BFDFFA61314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp */; };
</span><span class="cx">                 510477721D298DDD009747EB /* IDBDeleteRecovery.sqlite3 in Copy Resources */ = {isa = PBXBuildFile; fileRef = 5104776F1D298D85009747EB /* IDBDeleteRecovery.sqlite3 */; };
</span><span class="cx">                 510477731D298DDD009747EB /* IDBDeleteRecovery.sqlite3-shm in Copy Resources */ = {isa = PBXBuildFile; fileRef = 510477701D298D85009747EB /* IDBDeleteRecovery.sqlite3-shm */; };
</span><span class="lines">@@ -1010,6 +1011,7 @@
</span><span class="cx">                 46C519E21D35629600DAA51A /* LocalStorageNullEntries.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LocalStorageNullEntries.html; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 46C519E31D35629600DAA51A /* LocalStorageNullEntries.localstorage */ = {isa = PBXFileReference; lastKnownFileType = file; path = LocalStorageNullEntries.localstorage; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 46C519E41D35629600DAA51A /* LocalStorageNullEntries.localstorage-shm */ = {isa = PBXFileReference; lastKnownFileType = file; path = &quot;LocalStorageNullEntries.localstorage-shm&quot;; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                46E816F71E79E29100375ADC /* RestoreStateAfterTermination.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RestoreStateAfterTermination.mm; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 4A410F4B19AF7BD6002EBAB5 /* UserMedia.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UserMedia.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 4A410F4D19AF7BEF002EBAB5 /* getUserMedia.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = getUserMedia.html; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 4BB4160116815B2600824238 /* JSWrapperForNodeInWebFrame.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = JSWrapperForNodeInWebFrame.mm; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -2345,6 +2347,7 @@
</span><span class="cx">                                 C0C5D3BC14598B6F00A802A6 /* GetBackingScaleFactor.mm */,
</span><span class="cx">                                 C0C5D3BD14598B6F00A802A6 /* GetBackingScaleFactor_Bundle.mm */,
</span><span class="cx">                                 1AEF994817A09F5300998EF0 /* GetPIDAfterAbortedProcessLaunch.cpp */,
</span><ins>+                                46E816F71E79E29100375ADC /* RestoreStateAfterTermination.mm */,
</ins><span class="cx">                         );
</span><span class="cx">                         path = mac;
</span><span class="cx">                         sourceTree = &quot;&lt;group&gt;&quot;;
</span><span class="lines">@@ -2781,6 +2784,7 @@
</span><span class="cx">                                 5C0BF8941DD599C900B00328 /* MenuTypesForMouseEvents.mm in Sources */,
</span><span class="cx">                                 316BDBA11E70D71B00DE0D5A /* GPULibrary.mm in Sources */,
</span><span class="cx">                                 5C0BF8931DD599BD00B00328 /* IsNavigationActionTrusted.mm in Sources */,
</span><ins>+                                46E816F81E79E29C00375ADC /* RestoreStateAfterTermination.mm in Sources */,
</ins><span class="cx">                                 7CCE7EFF1A411AE600447C4C /* LoadCanceledNoServerRedirectCallback.cpp in Sources */,
</span><span class="cx">                                 7C83E0C01D0A652700FEBCF3 /* LoadInvalidURLRequest.mm in Sources */,
</span><span class="cx">                                 7CCE7F001A411AE600447C4C /* LoadPageOnCrash.cpp in Sources */,
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKit2macRestoreStateAfterTerminationmm"></a>
<div class="addfile"><h4>Added: trunk/Tools/TestWebKitAPI/Tests/WebKit2/mac/RestoreStateAfterTermination.mm (0 => 214006)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -0,0 +1,131 @@
</span><ins>+/*
+ * 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 &quot;config.h&quot;
+
+#import &quot;JavaScriptTest.h&quot;
+#import &quot;PlatformUtilities.h&quot;
+#import &quot;PlatformWebView.h&quot;
+#import &quot;Test.h&quot;
+#import &lt;WebKit/WKRetainPtr.h&gt;
+#import &lt;wtf/RetainPtr.h&gt;
+
+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&lt;uint32_t&gt;(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&lt;WKContextRef&gt; context(AdoptWK, WKContextCreate());
+    PlatformWebView webView(context.get());
+
+    // Add view to a Window and make it visible.
+    RetainPtr&lt;NSWindow&gt; window = adoptNS([[NSWindow alloc] initWithContentRect:webView.platformView().frame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]);
+    [window.get().contentView addSubview:webView.platformView()];
+
+    WKPageLoaderClientV0 loaderClient;
+    memset(&amp;loaderClient, 0, sizeof(loaderClient));
+    loaderClient.base.version = 0;
+    loaderClient.didFinishLoadForFrame = didFinishLoad;
+    loaderClient.processDidCrash = didCrash;
+    WKPageSetPageLoaderClient(webView.page(), &amp;loaderClient.base);
+
+    WKPageUIClientV0 uiClient;
+    memset(&amp;uiClient, 0, sizeof(uiClient));
+    uiClient.base.version = 0;
+    uiClient.runJavaScriptAlert = runJavaScriptAlert;
+    WKPageSetPageUIClient(webView.page(), &amp;uiClient.base);
+
+    // Load a test page containing a form.
+    WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource(&quot;simple-form&quot;, &quot;html&quot;)).get());
+    Util::run(&amp;loadBeforeCrash);
+
+    // Make the page visible.
+    isWaitingForPageSignalToContinue = true;
+    didGetPageSignalToContinue = false;
+    [window.get() makeKeyAndOrderFront:nil];
+    Util::run(&amp;didGetPageSignalToContinue);
+    EXPECT_JS_EQ(webView.page(), &quot;document.visibilityState&quot;, &quot;visible&quot;);
+
+    EXPECT_JS_EQ(webView.page(), &quot;getFormData()&quot;, &quot;Some unimportant data&quot;);
+
+    // Simulate user entering form data.
+    EXPECT_JS_EQ(webView.page(), &quot;fillForm('Important data')&quot;, &quot;undefined&quot;);
+
+    EXPECT_JS_EQ(webView.page(), &quot;getFormData()&quot;, &quot;Important data&quot;);
+
+    // 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(&amp;didGetPageSignalToContinue);
+
+    EXPECT_JS_EQ(webView.page(), &quot;document.visibilityState&quot;, &quot;hidden&quot;);
+
+    // Terminate page while in the background.
+    WKPageTerminate(webView.page());
+
+    // Reload should re-spawn webprocess and restore document state.
+    WKPageReload(webView.page());
+    Util::run(&amp;loadAfterCrash);
+
+    // Make sure that the form data is restored after the reload.
+    EXPECT_JS_EQ(webView.page(), &quot;getFormData()&quot;, &quot;Important data&quot;);
+}
+
+} // namespace TestWebKitAPI
</ins></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKit2simpleformhtml"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2/simple-form.html (214005 => 214006)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -4,6 +4,22 @@
</span><span class="cx"> {
</span><span class="cx">     document.forms[0].submit();
</span><span class="cx"> }
</span><ins>+
+function fillForm(data)
+{
+    document.getElementsByName(&quot;foo&quot;)[0].value = data;
+}
+
+function getFormData()
+{
+    return document.getElementsByName(&quot;foo&quot;)[0].value;
+}
+
+document.addEventListener(&quot;visibilitychange&quot;, function(event) {
+    // Send a signal to the test controller via alert.
+    alert('visibilitychange');
+});
+
</ins><span class="cx"> &lt;/script&gt;
</span><span class="cx"> &lt;form method=post&gt;
</span><span class="cx"> &lt;input name=foo value=&quot;Some unimportant data&quot;&gt;
</span></span></pre>
</div>
</div>

</body>
</html>