RELEASE_ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values))...
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 6 Jun 2017 23:31:25 +0000 (23:31 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 6 Jun 2017 23:31:25 +0000 (23:31 +0000)
https://bugs.webkit.org/show_bug.cgi?id=173033
<rdar://problem/32591099>

Reviewed by Simon Fraser.

Source/WebCore:

History::scrollRestoration() was returning a bad value because HistoryItem::m_shouldRestoreScrollPosition
was initialized from FrameState::shouldRestoreScrollPosition member, which was not initialized when
restored from the legacy session restore API.

Tests: WebKit2.RestoreSessionStateContainingScrollRestoration* API tests.

* bindings/scripts/CodeGeneratorJS.pm:
(GenerateEnumerationImplementationContent):
Drop the RELEASE_ASSERT that was added to debug this and use the regular debug ASSERT.

* history/HistoryItem.cpp:
(WebCore::HistoryItem::setShouldRestoreScrollPosition):
Make sure we notify that the HistoryItem has changed when setShouldRestoreScrollPosition()
is called.

Source/WebKit2:

* Shared/SessionState.h:
Make sure FrameState::shouldRestoreScrollPosition and other struct members are always
intialized.

* UIProcess/mac/LegacySessionStateCoding.cpp:
(WebKit::encodeFrameStateNode):
(WebKit::decodeBackForwardTreeNode):
Make sure FrameState::shouldRestoreScrollPosition is properly encoded / decoded.

Tools:

Add API test coverage to make sure History's scrollRestoration is properly saved / restored via the
WKPageCopySessionState() / WKPageRestoreFromSessionState() C API.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKit2/RestoreSessionState.cpp: Added.
(TestWebKitAPI::didFinishLoadForFrame):
(TestWebKitAPI::setPageLoaderClient):
(TestWebKitAPI::createSessionStateDataContainingScrollRestoration):
(TestWebKitAPI::TEST):

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

Source/WebCore/ChangeLog
Source/WebCore/bindings/scripts/CodeGeneratorJS.pm
Source/WebCore/history/HistoryItem.cpp
Source/WebKit2/ChangeLog
Source/WebKit2/Shared/SessionState.h
Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.cpp
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebKit2/RestoreSessionState.cpp [new file with mode: 0644]

index 6cc0cf6..ed62d26 100644 (file)
@@ -1,3 +1,26 @@
+2017-06-06  Chris Dumez  <cdumez@apple.com>
+
+        RELEASE_ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values)) hit in convertEnumerationToJS<WebCore::History::ScrollRestoration>()
+        https://bugs.webkit.org/show_bug.cgi?id=173033
+        <rdar://problem/32591099>
+
+        Reviewed by Simon Fraser.
+
+        History::scrollRestoration() was returning a bad value because HistoryItem::m_shouldRestoreScrollPosition
+        was initialized from FrameState::shouldRestoreScrollPosition member, which was not initialized when
+        restored from the legacy session restore API.
+
+        Tests: WebKit2.RestoreSessionStateContainingScrollRestoration* API tests.
+
+        * bindings/scripts/CodeGeneratorJS.pm:
+        (GenerateEnumerationImplementationContent):
+        Drop the RELEASE_ASSERT that was added to debug this and use the regular debug ASSERT.
+
+        * history/HistoryItem.cpp:
+        (WebCore::HistoryItem::setShouldRestoreScrollPosition):
+        Make sure we notify that the HistoryItem has changed when setShouldRestoreScrollPosition()
+        is called.
+
 2017-06-06  Darin Adler  <darin@apple.com>
 
         Update to slightly stricter rules for custom element names from more recent standard draft
index 51b4d2f..ef09056 100644 (file)
@@ -1727,12 +1727,7 @@ sub GenerateEnumerationImplementationContent
         $result .= "    static_assert(static_cast<size_t>(${className}::$enumerationValueName) == $index, \"${className}::$enumerationValueName is not $index as expected\");\n";
         $index++;
     }
-    # FIXME: This is a debugging aid for <rdar://problem/31193201>. Please revert when no longer needed.
-    if ($className eq "History::ScrollRestoration") {
-        $result .= "    RELEASE_ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values));\n";
-    } else {
-        $result .= "    ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values));\n";
-    }
+    $result .= "    ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values));\n";
     $result .= "    return jsStringWithCache(&state, values[static_cast<size_t>(enumerationValue)]);\n";
     $result .= "}\n\n";
 
index 64844a7..d99c006 100644 (file)
@@ -271,6 +271,7 @@ bool HistoryItem::shouldRestoreScrollPosition() const
 void HistoryItem::setShouldRestoreScrollPosition(bool shouldRestore)
 {
     m_shouldRestoreScrollPosition = shouldRestore;
+    notifyHistoryItemChanged(this);
 }
 
 float HistoryItem::pageScaleFactor() const
index 99cdaab..a403e6f 100644 (file)
@@ -1,3 +1,20 @@
+2017-06-06  Chris Dumez  <cdumez@apple.com>
+
+        RELEASE_ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values)) hit in convertEnumerationToJS<WebCore::History::ScrollRestoration>()
+        https://bugs.webkit.org/show_bug.cgi?id=173033
+        <rdar://problem/32591099>
+
+        Reviewed by Simon Fraser.
+
+        * Shared/SessionState.h:
+        Make sure FrameState::shouldRestoreScrollPosition and other struct members are always
+        intialized.
+
+        * UIProcess/mac/LegacySessionStateCoding.cpp:
+        (WebKit::encodeFrameStateNode):
+        (WebKit::decodeBackForwardTreeNode):
+        Make sure FrameState::shouldRestoreScrollPosition is properly encoded / decoded.
+
 2017-06-06  Andy Estes  <aestes@apple.com>
 
         REGRESSION (r199558): WKWebView upload file which name contains Chinese character results in garbled code
index a56a8db..61f4a6c 100644 (file)
@@ -92,12 +92,12 @@ struct FrameState {
     Vector<String> documentState;
     std::optional<Vector<uint8_t>> stateObjectData;
 
-    int64_t documentSequenceNumber;
-    int64_t itemSequenceNumber;
+    int64_t documentSequenceNumber { 0 };
+    int64_t itemSequenceNumber { 0 };
 
     WebCore::IntPoint scrollPosition;
-    bool shouldRestoreScrollPosition;
-    float pageScaleFactor;
+    bool shouldRestoreScrollPosition { true };
+    float pageScaleFactor { 0 };
 
     std::optional<HTTPBody> httpBody;
 
@@ -107,7 +107,7 @@ struct FrameState {
     WebCore::IntRect unobscuredContentRect;
     WebCore::FloatSize minimumLayoutSizeInScrollViewCoordinates;
     WebCore::IntSize contentSize;
-    bool scaleIsInitial = false;
+    bool scaleIsInitial { false };
 #endif
 
     Vector<FrameState> children;
index 17f66b7..54b2812 100644 (file)
@@ -346,6 +346,7 @@ static void encodeFrameStateNode(HistoryEntryDataEncoder& encoder, const FrameSt
     encoder << frameState.scrollPosition.x();
     encoder << frameState.scrollPosition.y();
 
+    encoder << frameState.shouldRestoreScrollPosition;
     encoder << frameState.pageScaleFactor;
 
     encoder << !!frameState.stateObjectData;
@@ -928,6 +929,7 @@ static void decodeBackForwardTreeNode(HistoryEntryDataDecoder& decoder, FrameSta
 
     frameState.scrollPosition = WebCore::IntPoint(scrollPositionX, scrollPositionY);
 
+    decoder >> frameState.shouldRestoreScrollPosition;
     decoder >> frameState.pageScaleFactor;
 
     bool hasStateObject;
index 5b408d2..4cebbc6 100644 (file)
@@ -1,3 +1,21 @@
+2017-06-06  Chris Dumez  <cdumez@apple.com>
+
+        RELEASE_ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values)) hit in convertEnumerationToJS<WebCore::History::ScrollRestoration>()
+        https://bugs.webkit.org/show_bug.cgi?id=173033
+        <rdar://problem/32591099>
+
+        Reviewed by Simon Fraser.
+
+        Add API test coverage to make sure History's scrollRestoration is properly saved / restored via the
+        WKPageCopySessionState() / WKPageRestoreFromSessionState() C API.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKit2/RestoreSessionState.cpp: Added.
+        (TestWebKitAPI::didFinishLoadForFrame):
+        (TestWebKitAPI::setPageLoaderClient):
+        (TestWebKitAPI::createSessionStateDataContainingScrollRestoration):
+        (TestWebKitAPI::TEST):
+
 2017-06-06  Andy Estes  <aestes@apple.com>
 
         REGRESSION (r199558): WKWebView upload file which name contains Chinese character results in garbled code
index cda9ec0..32e3ad8 100644 (file)
                835CF9671D25FCD6001A65D4 /* RestoreSessionStateWithoutNavigation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 835CF9661D25FCD6001A65D4 /* RestoreSessionStateWithoutNavigation.cpp */; };
                8361F1781E610B4E00759B25 /* link-with-download-attribute-with-slashes.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 8361F1771E610B2100759B25 /* link-with-download-attribute-with-slashes.html */; };
                837A35F11D9A1E7D00663C57 /* DownloadRequestBlobURL.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 837A35F01D9A1E6400663C57 /* DownloadRequestBlobURL.html */; };
+               83B6DE6F1EE75221001E792F /* RestoreSessionState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 83B6DE6E1EE7520F001E792F /* RestoreSessionState.cpp */; };
                8E4A85371E1D1AB200F53B0F /* GridPosition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8E4A85361E1D1AA100F53B0F /* GridPosition.cpp */; };
                930AD402150698D00067970F /* lots-of-text.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 930AD401150698B30067970F /* lots-of-text.html */; };
                9329AA291DE3F81E003ABD07 /* TextBreakIterator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9329AA281DE3F81E003ABD07 /* TextBreakIterator.cpp */; };
                835CF9661D25FCD6001A65D4 /* RestoreSessionStateWithoutNavigation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RestoreSessionStateWithoutNavigation.cpp; sourceTree = "<group>"; };
                8361F1771E610B2100759B25 /* link-with-download-attribute-with-slashes.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "link-with-download-attribute-with-slashes.html"; sourceTree = "<group>"; };
                837A35F01D9A1E6400663C57 /* DownloadRequestBlobURL.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = DownloadRequestBlobURL.html; sourceTree = "<group>"; };
+               83B6DE6E1EE7520F001E792F /* RestoreSessionState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RestoreSessionState.cpp; sourceTree = "<group>"; };
                83B88A331C80056D00BB2418 /* HTMLParserIdioms.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HTMLParserIdioms.cpp; sourceTree = "<group>"; };
                86BD19971A2DB05B006DCF0A /* RefCounter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RefCounter.cpp; sourceTree = "<group>"; };
                8A2C750D16CED9550024F352 /* ResizeWindowAfterCrash.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ResizeWindowAfterCrash.cpp; sourceTree = "<group>"; };
                                8A2C750D16CED9550024F352 /* ResizeWindowAfterCrash.cpp */,
                                C0BD669C131D3CF700E18F2A /* ResponsivenessTimerDoesntFireEarly.cpp */,
                                C0BD669E131D3CFF00E18F2A /* ResponsivenessTimerDoesntFireEarly_Bundle.cpp */,
+                               83B6DE6E1EE7520F001E792F /* RestoreSessionState.cpp */,
                                C0ADBE8212FCA6AA00D2C129 /* RestoreSessionStateContainingFormData.cpp */,
                                835CF9661D25FCD6001A65D4 /* RestoreSessionStateWithoutNavigation.cpp */,
                                2D640B5417875DFF00BFAF99 /* ScrollPinningBehaviors.cpp */,
                                F4FA91811E61849B007B8C1D /* WKWebViewSelectionTests.mm in Sources */,
                                0799C3491EBA2D7B003B7532 /* UserMediaDisabled.mm in Sources */,
                                93F56DA91E5F919D003EDE84 /* WKWebViewSnapshot.mm in Sources */,
+                               83B6DE6F1EE75221001E792F /* RestoreSessionState.cpp in Sources */,
                                9984FACC1CFFAF60008D198C /* WKWebViewTextInput.mm in Sources */,
                                764322D71B61CCC30024F801 /* WordBoundaryTypingAttributes.mm in Sources */,
                                9C64DC321D76198A004B598E /* YouTubePluginReplacement.cpp in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WebKit2/RestoreSessionState.cpp b/Tools/TestWebKitAPI/Tests/WebKit2/RestoreSessionState.cpp
new file mode 100644 (file)
index 0000000..3ca9891
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * 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"
+
+#if WK_HAVE_C_SPI
+
+#include "JavaScriptTest.h"
+#include "PlatformUtilities.h"
+#include "PlatformWebView.h"
+#include "Test.h"
+#include <WebKit/WKSessionStateRef.h>
+
+namespace TestWebKitAPI {
+
+static bool didFinishLoad;
+
+static void didFinishLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef, const void*)
+{
+    didFinishLoad = true;
+}
+
+static void setPageLoaderClient(WKPageRef page)
+{
+    WKPageLoaderClientV0 loaderClient;
+    memset(&loaderClient, 0, sizeof(loaderClient));
+
+    loaderClient.base.version = 0;
+    loaderClient.didFinishLoadForFrame = didFinishLoadForFrame;
+
+    WKPageSetPageLoaderClient(page, &loaderClient.base);
+}
+
+static WKRetainPtr<WKDataRef> createSessionStateDataContainingScrollRestoration(WKContextRef context, std::function<void(WKPageRef)> apply)
+{
+    PlatformWebView webView(context);
+    setPageLoaderClient(webView.page());
+
+    WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple-form", "html")).get());
+    Util::run(&didFinishLoad);
+    didFinishLoad = false;
+
+    apply(webView.page());
+
+    auto sessionState = adoptWK(static_cast<WKSessionStateRef>(WKPageCopySessionState(webView.page(), reinterpret_cast<void*>(1), nullptr)));
+    return adoptWK(WKSessionStateCopyData(sessionState.get()));
+}
+
+TEST(WebKit2, RestoreSessionStateContainingScrollRestorationManual)
+{
+    WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate());
+
+    PlatformWebView webView(context.get());
+    setPageLoaderClient(webView.page());
+
+    WKRetainPtr<WKDataRef> data = createSessionStateDataContainingScrollRestoration(context.get(), [](WKPageRef page) {
+        EXPECT_JS_EQ(page, "history.scrollRestoration = 'manual'", "manual");
+    });
+    EXPECT_NOT_NULL(data);
+
+    auto sessionState = adoptWK(WKSessionStateCreateFromData(data.get()));
+    WKPageRestoreFromSessionState(webView.page(), sessionState.get());
+
+    Util::run(&didFinishLoad);
+
+    EXPECT_JS_EQ(webView.page(), "history.scrollRestoration", "manual");
+}
+
+TEST(WebKit2, RestoreSessionStateContainingScrollRestorationAuto)
+{
+    WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate());
+
+    PlatformWebView webView(context.get());
+    setPageLoaderClient(webView.page());
+
+    WKRetainPtr<WKDataRef> data = createSessionStateDataContainingScrollRestoration(context.get(), [](WKPageRef page) {
+        EXPECT_JS_EQ(page, "history.scrollRestoration = 'auto'", "auto");
+    });
+    EXPECT_NOT_NULL(data);
+
+    auto sessionState = adoptWK(WKSessionStateCreateFromData(data.get()));
+    WKPageRestoreFromSessionState(webView.page(), sessionState.get());
+
+    Util::run(&didFinishLoad);
+
+    EXPECT_JS_EQ(webView.page(), "history.scrollRestoration", "auto");
+}
+
+TEST(WebKit2, RestoreSessionStateContainingScrollRestorationDefault)
+{
+    WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate());
+
+    PlatformWebView webView(context.get());
+    setPageLoaderClient(webView.page());
+
+    WKRetainPtr<WKDataRef> data = createSessionStateDataContainingScrollRestoration(context.get(), [](WKPageRef) {
+    });
+    EXPECT_NOT_NULL(data);
+
+    auto sessionState = adoptWK(WKSessionStateCreateFromData(data.get()));
+    WKPageRestoreFromSessionState(webView.page(), sessionState.get());
+
+    Util::run(&didFinishLoad);
+
+    EXPECT_JS_EQ(webView.page(), "history.scrollRestoration", "auto");
+}
+
+} // namespace TestWebKitAPI
+
+#endif