<!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>[214014] trunk/Source/WebCore</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/214014">214014</a></dd>
<dt>Author</dt> <dd>dbates@webkit.org</dd>
<dt>Date</dt> <dd>2017-03-15 15:44:59 -0700 (Wed, 15 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>Iteratively dispatch DOM events after restoring a cached page
https://bugs.webkit.org/show_bug.cgi?id=169703
&lt;rdar://problem/31075903&gt;

Reviewed by Brady Eidson.

Make dispatching of DOM events when restoring a page from the page cache symmetric with
dispatching of events when saving a page to the page cache.

* history/CachedFrame.cpp:
(WebCore::CachedFrameBase::restore): Move code to dispatch events from here to FrameLoader::didRestoreFromCachedPage().
* loader/FrameLoader.cpp:
(WebCore::FrameLoader::commitProvisionalLoad): Ensure that no DOM events are dispatched during
restoration of a cached page. Call didRestoreFromCachedPage() after restoring the page to
dispatch DOM events on the restored frames.
(WebCore::FrameLoader::willRestoreFromCachedPage): Renamed; formerly named prepareForCachedPageRestore().
(WebCore::FrameLoader::didRestoreFromCachedPage): Added.
(WebCore::FrameLoader::prepareForCachedPageRestore): Renamed to willRestoreFromCachedPage().
* loader/FrameLoader.h:
* page/FrameTree.cpp:
(WebCore::FrameTree::traverseNextInPostOrderWithWrap): Returns the next Frame* in a post-order
traversal of the frame tree optionally wrapping around to the deepest first child in the tree.
(WebCore::FrameTree::deepFirstChild): Added.
* page/FrameTree.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorehistoryCachedFramecpp">trunk/Source/WebCore/history/CachedFrame.cpp</a></li>
<li><a href="#trunkSourceWebCoreloaderFrameLoadercpp">trunk/Source/WebCore/loader/FrameLoader.cpp</a></li>
<li><a href="#trunkSourceWebCoreloaderFrameLoaderh">trunk/Source/WebCore/loader/FrameLoader.h</a></li>
<li><a href="#trunkSourceWebCorepageFrameTreecpp">trunk/Source/WebCore/page/FrameTree.cpp</a></li>
<li><a href="#trunkSourceWebCorepageFrameTreeh">trunk/Source/WebCore/page/FrameTree.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (214013 => 214014)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-03-15 22:43:26 UTC (rev 214013)
+++ trunk/Source/WebCore/ChangeLog        2017-03-15 22:44:59 UTC (rev 214014)
</span><span class="lines">@@ -1,3 +1,30 @@
</span><ins>+2017-03-15  Daniel Bates  &lt;dabates@apple.com&gt;
+
+        Iteratively dispatch DOM events after restoring a cached page
+        https://bugs.webkit.org/show_bug.cgi?id=169703
+        &lt;rdar://problem/31075903&gt;
+
+        Reviewed by Brady Eidson.
+
+        Make dispatching of DOM events when restoring a page from the page cache symmetric with
+        dispatching of events when saving a page to the page cache.
+
+        * history/CachedFrame.cpp:
+        (WebCore::CachedFrameBase::restore): Move code to dispatch events from here to FrameLoader::didRestoreFromCachedPage().
+        * loader/FrameLoader.cpp:
+        (WebCore::FrameLoader::commitProvisionalLoad): Ensure that no DOM events are dispatched during
+        restoration of a cached page. Call didRestoreFromCachedPage() after restoring the page to
+        dispatch DOM events on the restored frames.
+        (WebCore::FrameLoader::willRestoreFromCachedPage): Renamed; formerly named prepareForCachedPageRestore().
+        (WebCore::FrameLoader::didRestoreFromCachedPage): Added.
+        (WebCore::FrameLoader::prepareForCachedPageRestore): Renamed to willRestoreFromCachedPage().
+        * loader/FrameLoader.h:
+        * page/FrameTree.cpp:
+        (WebCore::FrameTree::traverseNextInPostOrderWithWrap): Returns the next Frame* in a post-order
+        traversal of the frame tree optionally wrapping around to the deepest first child in the tree.
+        (WebCore::FrameTree::deepFirstChild): Added.
+        * page/FrameTree.h:
+
</ins><span class="cx"> 2017-03-15  Dave Hyatt  &lt;hyatt@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Positioned SVG not sized correctly
</span></span></pre></div>
<a id="trunkSourceWebCorehistoryCachedFramecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/history/CachedFrame.cpp (214013 => 214014)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/history/CachedFrame.cpp        2017-03-15 22:43:26 UTC (rev 214013)
+++ trunk/Source/WebCore/history/CachedFrame.cpp        2017-03-15 22:44:59 UTC (rev 214014)
</span><span class="lines">@@ -35,13 +35,10 @@
</span><span class="cx"> #include &quot;FrameLoader.h&quot;
</span><span class="cx"> #include &quot;FrameLoaderClient.h&quot;
</span><span class="cx"> #include &quot;FrameView.h&quot;
</span><del>-#include &quot;HistoryController.h&quot;
-#include &quot;HistoryItem.h&quot;
</del><span class="cx"> #include &quot;Logging.h&quot;
</span><span class="cx"> #include &quot;MainFrame.h&quot;
</span><span class="cx"> #include &quot;Page.h&quot;
</span><span class="cx"> #include &quot;PageCache.h&quot;
</span><del>-#include &quot;PageTransitionEvent.h&quot;
</del><span class="cx"> #include &quot;SVGDocumentExtensions.h&quot;
</span><span class="cx"> #include &quot;ScriptController.h&quot;
</span><span class="cx"> #include &quot;SerializedScriptValue.h&quot;
</span><span class="lines">@@ -116,6 +113,7 @@
</span><span class="cx">         ASSERT(childFrame-&gt;view()-&gt;frame().page());
</span><span class="cx">         frame.tree().appendChild(childFrame-&gt;view()-&gt;frame());
</span><span class="cx">         childFrame-&gt;open();
</span><ins>+        ASSERT_WITH_SECURITY_IMPLICATION(m_document == frame.document());
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="lines">@@ -131,14 +129,6 @@
</span><span class="cx">     }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><del>-    // FIXME: update Page Visibility state here.
-    // https://bugs.webkit.org/show_bug.cgi?id=116770
-    m_document-&gt;enqueuePageshowEvent(PageshowEventPersisted);
-
-    HistoryItem* historyItem = frame.loader().history().currentItem();
-    if (historyItem &amp;&amp; historyItem-&gt;stateObject())
-        m_document-&gt;enqueuePopstateEvent(historyItem-&gt;stateObject());
-
</del><span class="cx">     frame.view()-&gt;didRestoreFromPageCache();
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreloaderFrameLoadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/FrameLoader.cpp (214013 => 214014)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/FrameLoader.cpp        2017-03-15 22:43:26 UTC (rev 214013)
+++ trunk/Source/WebCore/loader/FrameLoader.cpp        2017-03-15 22:44:59 UTC (rev 214014)
</span><span class="lines">@@ -86,6 +86,7 @@
</span><span class="cx"> #include &quot;MainFrame.h&quot;
</span><span class="cx"> #include &quot;MemoryCache.h&quot;
</span><span class="cx"> #include &quot;MemoryRelease.h&quot;
</span><ins>+#include &quot;NoEventDispatchAssertion.h&quot;
</ins><span class="cx"> #include &quot;Page.h&quot;
</span><span class="cx"> #include &quot;PageCache.h&quot;
</span><span class="cx"> #include &quot;PageTransitionEvent.h&quot;
</span><span class="lines">@@ -1813,7 +1814,7 @@
</span><span class="cx">         // commit to happen before any changes to viewport arguments and dealing with this there is difficult.
</span><span class="cx">         m_frame.page()-&gt;chrome().setDispatchViewportDataDidChangeSuppressed(true);
</span><span class="cx"> #endif
</span><del>-        prepareForCachedPageRestore();
</del><ins>+        willRestoreFromCachedPage();
</ins><span class="cx"> 
</span><span class="cx">         // Start request for the main resource and dispatch didReceiveResponse before the load is committed for
</span><span class="cx">         // consistency with all other loads. See https://bugs.webkit.org/show_bug.cgi?id=150927.
</span><span class="lines">@@ -1825,10 +1826,19 @@
</span><span class="cx"> 
</span><span class="cx">         std::optional&lt;HasInsecureContent&gt; hasInsecureContent = cachedPage-&gt;cachedMainFrame()-&gt;hasInsecureContent();
</span><span class="cx"> 
</span><del>-        // FIXME: This API should be turned around so that we ground CachedPage into the Page.
-        cachedPage-&gt;restore(*m_frame.page());
</del><ins>+        {
+            // Do not dispatch DOM events as their JavaScript listeners could cause the page to be put
+            // into the page cache before we have finished restoring it from the page cache.
+            NoEventDispatchAssertion assertNoEventDispatch;
</ins><span class="cx"> 
</span><ins>+            // FIXME: This API should be turned around so that we ground CachedPage into the Page.
+            cachedPage-&gt;restore(*m_frame.page());
+        }
+
</ins><span class="cx">         dispatchDidCommitLoad(hasInsecureContent);
</span><ins>+
+        didRestoreFromCachedPage();
+
</ins><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx">         m_frame.page()-&gt;chrome().setDispatchViewportDataDidChangeSuppressed(false);
</span><span class="cx">         m_frame.page()-&gt;chrome().dispatchViewportPropertiesDidChange(m_frame.page()-&gt;viewportArguments());
</span><span class="lines">@@ -2047,7 +2057,7 @@
</span><span class="cx">     m_client.setMainFrameDocumentReady(false); // stop giving out the actual DOMDocument to observers
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void FrameLoader::prepareForCachedPageRestore()
</del><ins>+void FrameLoader::willRestoreFromCachedPage()
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(!m_frame.tree().parent());
</span><span class="cx">     ASSERT(m_frame.page());
</span><span class="lines">@@ -2066,6 +2076,31 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void FrameLoader::didRestoreFromCachedPage()
+{
+    // Dispatching JavaScript events can cause frame destruction.
+    auto&amp; mainFrame = m_frame.page()-&gt;mainFrame();
+    Vector&lt;Ref&lt;Frame&gt;&gt; childFrames;
+    for (auto* child = mainFrame.tree().traverseNextInPostOrderWithWrap(true); child; child = child-&gt;tree().traverseNextInPostOrderWithWrap(false))
+        childFrames.append(*child);
+
+    for (auto&amp; child : childFrames) {
+        if (!child-&gt;tree().isDescendantOf(&amp;mainFrame))
+            continue;
+        auto* document = child-&gt;document();
+        if (!document)
+            continue;
+
+        // FIXME: Update Page Visibility state here.
+        // https://bugs.webkit.org/show_bug.cgi?id=116770
+        document-&gt;enqueuePageshowEvent(PageshowEventPersisted);
+
+        auto* historyItem = child-&gt;loader().history().currentItem();
+        if (historyItem &amp;&amp; historyItem-&gt;stateObject())
+            document-&gt;enqueuePopstateEvent(historyItem-&gt;stateObject());
+    }
+}
+
</ins><span class="cx"> void FrameLoader::open(CachedFrameBase&amp; cachedFrame)
</span><span class="cx"> {
</span><span class="cx">     m_isComplete = false;
</span></span></pre></div>
<a id="trunkSourceWebCoreloaderFrameLoaderh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/FrameLoader.h (214013 => 214014)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/FrameLoader.h        2017-03-15 22:43:26 UTC (rev 214013)
+++ trunk/Source/WebCore/loader/FrameLoader.h        2017-03-15 22:44:59 UTC (rev 214014)
</span><span class="lines">@@ -345,7 +345,8 @@
</span><span class="cx">     void setState(FrameState);
</span><span class="cx"> 
</span><span class="cx">     void closeOldDataSources();
</span><del>-    void prepareForCachedPageRestore();
</del><ins>+    void willRestoreFromCachedPage();
+    void didRestoreFromCachedPage();
</ins><span class="cx"> 
</span><span class="cx">     bool shouldReloadToHandleUnreachableURL(DocumentLoader*);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorepageFrameTreecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/FrameTree.cpp (214013 => 214014)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/FrameTree.cpp        2017-03-15 22:43:26 UTC (rev 214013)
+++ trunk/Source/WebCore/page/FrameTree.cpp        2017-03-15 22:44:59 UTC (rev 214014)
</span><span class="lines">@@ -431,6 +431,25 @@
</span><span class="cx">     return nullptr;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+Frame* FrameTree::traverseNextInPostOrderWithWrap(bool wrap) const
+{
+    if (m_nextSibling)
+        return m_nextSibling-&gt;tree().deepFirstChild();
+    if (m_parent)
+        return m_parent;
+    if (wrap)
+        return deepFirstChild();
+    return nullptr;
+}
+
+Frame* FrameTree::deepFirstChild() const
+{
+    Frame* result = &amp;m_thisFrame;
+    while (auto* next = result-&gt;tree().firstChild())
+        result = next;
+    return result;
+}
+
</ins><span class="cx"> Frame* FrameTree::deepLastChild() const
</span><span class="cx"> {
</span><span class="cx">     Frame* result = &amp;m_thisFrame;
</span></span></pre></div>
<a id="trunkSourceWebCorepageFrameTreeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/FrameTree.h (214013 => 214014)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/FrameTree.h        2017-03-15 22:43:26 UTC (rev 214013)
+++ trunk/Source/WebCore/page/FrameTree.h        2017-03-15 22:44:59 UTC (rev 214014)
</span><span class="lines">@@ -64,7 +64,9 @@
</span><span class="cx">     WEBCORE_EXPORT Frame* traverseNextRendered(const Frame* stayWithin = nullptr) const;
</span><span class="cx">     WEBCORE_EXPORT Frame* traverseNextWithWrap(bool) const;
</span><span class="cx">     WEBCORE_EXPORT Frame* traversePreviousWithWrap(bool) const;
</span><del>-    
</del><ins>+
+    Frame* traverseNextInPostOrderWithWrap(bool) const;
+
</ins><span class="cx">     WEBCORE_EXPORT void appendChild(Frame&amp;);
</span><span class="cx">     void detachFromParent() { m_parent = nullptr; }
</span><span class="cx">     void removeChild(Frame&amp;);
</span><span class="lines">@@ -85,6 +87,7 @@
</span><span class="cx">     unsigned indexInParent() const;
</span><span class="cx"> 
</span><span class="cx"> private:
</span><ins>+    Frame* deepFirstChild() const;
</ins><span class="cx">     Frame* deepLastChild() const;
</span><span class="cx"> 
</span><span class="cx">     bool scopedBy(TreeScope*) const;
</span></span></pre>
</div>
</div>

</body>
</html>