<!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>[196717] trunk/Source</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/196717">196717</a></dd>
<dt>Author</dt> <dd>simon.fraser@apple.com</dd>
<dt>Date</dt> <dd>2016-02-17 13:27:02 -0800 (Wed, 17 Feb 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>PDFPlugin's scrollableArea container is not properly unregistered when page is going into the PageCache
https://bugs.webkit.org/show_bug.cgi?id=148182

Reviewed by Brent Fulgham.
Source/WebCore:

When handling Command-arrow key while showing a scrollable PDF, the timing of PDFPlugin
teardown and navigation could result in PDFPlugin::destroy() getting the wrong FrameView,
so the old FrameView was left with a stale pointer in its scrollableAreaSet.

Fix this by adding an explicit willDetatchRenderer() which is called on the plugin
before the Frame gets a new FrameView.

Also narrow the scope of the RefPtr&lt;Widget&gt; in HTMLPlugInElement::defaultEventHandler()
so that the Widget is not kept alive over a possible navigation.

I was unable to make an automated test, because reproducing the bug requires handling
a Command-arrow key event in a way that the last ref to a Widget is held over the event
handling, and this wasn't possible in an iframe.

* html/HTMLPlugInElement.cpp:
(WebCore::HTMLPlugInElement::defaultEventHandler):
* html/HTMLPlugInImageElement.cpp:
(WebCore::HTMLPlugInImageElement::willDetachRenderers):
* plugins/PluginViewBase.h:
(WebCore::PluginViewBase::willDetatchRenderer):
* style/StyleTreeResolver.cpp:
(WebCore::Style::detachRenderTree): Drive-by nullptr.

Source/WebKit2:

When handling Command-arrow key while showing a scrollable PDF, the timing of PDFPlugin
teardown and navigation could result in PDFPlugin::destroy() getting the wrong FrameView,
so the old FrameView was left with a stale pointer in its scrollableAreaSet.

Fix this by adding an explicit willDetatchRenderer() which is called on the plugin
before the Frame gets a new FrameView.

Also narrow the scope of the RefPtr&lt;Widget&gt; in HTMLPlugInElement::defaultEventHandler()
so that the Widget is not kept alive over a possible navigation.

I was unable to make an automated test, because reproducing the bug requires handling
a Command-arrow key event in a way that the last ref to a Widget is held over the event
handling, and this wasn't possible in an iframe.

* WebProcess/Plugins/PDF/DeprecatedPDFPlugin.h:
* WebProcess/Plugins/PDF/DeprecatedPDFPlugin.mm:
(WebKit::PDFPlugin::willDetatchRenderer):
* WebProcess/Plugins/Plugin.h:
(WebKit::Plugin::willDetatchRenderer):
* WebProcess/Plugins/PluginView.cpp:
(WebKit::PluginView::willDetatchRenderer):
* WebProcess/Plugins/PluginView.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorehtmlHTMLPlugInElementcpp">trunk/Source/WebCore/html/HTMLPlugInElement.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlHTMLPlugInImageElementcpp">trunk/Source/WebCore/html/HTMLPlugInImageElement.cpp</a></li>
<li><a href="#trunkSourceWebCorepluginsPluginViewBaseh">trunk/Source/WebCore/plugins/PluginViewBase.h</a></li>
<li><a href="#trunkSourceWebCorestyleStyleTreeResolvercpp">trunk/Source/WebCore/style/StyleTreeResolver.cpp</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2WebProcessPluginsPDFDeprecatedPDFPluginh">trunk/Source/WebKit2/WebProcess/Plugins/PDF/DeprecatedPDFPlugin.h</a></li>
<li><a href="#trunkSourceWebKit2WebProcessPluginsPDFDeprecatedPDFPluginmm">trunk/Source/WebKit2/WebProcess/Plugins/PDF/DeprecatedPDFPlugin.mm</a></li>
<li><a href="#trunkSourceWebKit2WebProcessPluginsPluginh">trunk/Source/WebKit2/WebProcess/Plugins/Plugin.h</a></li>
<li><a href="#trunkSourceWebKit2WebProcessPluginsPluginViewcpp">trunk/Source/WebKit2/WebProcess/Plugins/PluginView.cpp</a></li>
<li><a href="#trunkSourceWebKit2WebProcessPluginsPluginViewh">trunk/Source/WebKit2/WebProcess/Plugins/PluginView.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebCore/ChangeLog        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -1,3 +1,33 @@
</span><ins>+2016-02-17  Simon Fraser  &lt;simon.fraser@apple.com&gt;
+
+        PDFPlugin's scrollableArea container is not properly unregistered when page is going into the PageCache
+        https://bugs.webkit.org/show_bug.cgi?id=148182
+
+        Reviewed by Brent Fulgham.
+
+        When handling Command-arrow key while showing a scrollable PDF, the timing of PDFPlugin
+        teardown and navigation could result in PDFPlugin::destroy() getting the wrong FrameView,
+        so the old FrameView was left with a stale pointer in its scrollableAreaSet.
+
+        Fix this by adding an explicit willDetatchRenderer() which is called on the plugin
+        before the Frame gets a new FrameView.
+
+        Also narrow the scope of the RefPtr&lt;Widget&gt; in HTMLPlugInElement::defaultEventHandler()
+        so that the Widget is not kept alive over a possible navigation.
+
+        I was unable to make an automated test, because reproducing the bug requires handling
+        a Command-arrow key event in a way that the last ref to a Widget is held over the event
+        handling, and this wasn't possible in an iframe.
+
+        * html/HTMLPlugInElement.cpp:
+        (WebCore::HTMLPlugInElement::defaultEventHandler):
+        * html/HTMLPlugInImageElement.cpp:
+        (WebCore::HTMLPlugInImageElement::willDetachRenderers):
+        * plugins/PluginViewBase.h:
+        (WebCore::PluginViewBase::willDetatchRenderer):
+        * style/StyleTreeResolver.cpp:
+        (WebCore::Style::detachRenderTree): Drive-by nullptr.
+
</ins><span class="cx"> 2016-02-17  Brady Eidson  &lt;beidson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Modern IDB: Encoder/Decoder/Messaging scaffolding for WK2 IPC.
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlHTMLPlugInElementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/HTMLPlugInElement.cpp (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/HTMLPlugInElement.cpp        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebCore/html/HTMLPlugInElement.cpp        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -225,12 +225,15 @@
</span><span class="cx">             return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    RefPtr&lt;Widget&gt; widget = downcast&lt;RenderWidget&gt;(*renderer).widget();
-    if (!widget)
-        return;
-    widget-&gt;handleEvent(event);
-    if (event-&gt;defaultHandled())
-        return;
</del><ins>+    // Don't keep the widget alive over the defaultEventHandler call, since that can do things like navigate.
+    {
+        RefPtr&lt;Widget&gt; widget = downcast&lt;RenderWidget&gt;(*renderer).widget();
+        if (!widget)
+            return;
+        widget-&gt;handleEvent(event);
+        if (event-&gt;defaultHandled())
+            return;
+    }
</ins><span class="cx">     HTMLFrameOwnerElement::defaultEventHandler(event);
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlHTMLPlugInImageElementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/HTMLPlugInImageElement.cpp (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/HTMLPlugInImageElement.cpp        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebCore/html/HTMLPlugInImageElement.cpp        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -272,6 +272,10 @@
</span><span class="cx">         setNeedsWidgetUpdate(true);
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    Widget* widget = pluginWidget(PluginLoadingPolicy::DoNotLoad);
+    if (is&lt;PluginViewBase&gt;(widget))
+        downcast&lt;PluginViewBase&gt;(*widget).willDetatchRenderer();
+
</ins><span class="cx">     HTMLPlugInElement::willDetachRenderers();
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorepluginsPluginViewBaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/plugins/PluginViewBase.h (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/plugins/PluginViewBase.h        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebCore/plugins/PluginViewBase.h        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -81,6 +81,8 @@
</span><span class="cx">     virtual void setJavaScriptPaused(bool) { }
</span><span class="cx"> 
</span><span class="cx">     virtual RefPtr&lt;JSC::Bindings::Instance&gt; bindingInstance() { return nullptr; }
</span><ins>+    
+    virtual void willDetatchRenderer() { }
</ins><span class="cx"> 
</span><span class="cx"> protected:
</span><span class="cx">     explicit PluginViewBase(PlatformWidget widget = 0) : Widget(widget) { }
</span></span></pre></div>
<a id="trunkSourceWebCorestyleStyleTreeResolvercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/StyleTreeResolver.cpp (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/StyleTreeResolver.cpp        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebCore/style/StyleTreeResolver.cpp        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -613,7 +613,7 @@
</span><span class="cx"> 
</span><span class="cx">     if (current.renderer())
</span><span class="cx">         current.renderer()-&gt;destroyAndCleanupAnonymousWrappers();
</span><del>-    current.setRenderer(0);
</del><ins>+    current.setRenderer(nullptr);
</ins><span class="cx"> 
</span><span class="cx">     if (current.hasCustomStyleResolveCallbacks())
</span><span class="cx">         current.didDetachRenderers();
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebKit2/ChangeLog        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -1,3 +1,33 @@
</span><ins>+2016-02-17  Simon Fraser  &lt;simon.fraser@apple.com&gt;
+
+        PDFPlugin's scrollableArea container is not properly unregistered when page is going into the PageCache
+        https://bugs.webkit.org/show_bug.cgi?id=148182
+
+        Reviewed by Brent Fulgham.
+        
+        When handling Command-arrow key while showing a scrollable PDF, the timing of PDFPlugin
+        teardown and navigation could result in PDFPlugin::destroy() getting the wrong FrameView,
+        so the old FrameView was left with a stale pointer in its scrollableAreaSet.
+
+        Fix this by adding an explicit willDetatchRenderer() which is called on the plugin
+        before the Frame gets a new FrameView.
+
+        Also narrow the scope of the RefPtr&lt;Widget&gt; in HTMLPlugInElement::defaultEventHandler()
+        so that the Widget is not kept alive over a possible navigation.
+
+        I was unable to make an automated test, because reproducing the bug requires handling
+        a Command-arrow key event in a way that the last ref to a Widget is held over the event
+        handling, and this wasn't possible in an iframe.
+
+        * WebProcess/Plugins/PDF/DeprecatedPDFPlugin.h:
+        * WebProcess/Plugins/PDF/DeprecatedPDFPlugin.mm:
+        (WebKit::PDFPlugin::willDetatchRenderer):
+        * WebProcess/Plugins/Plugin.h:
+        (WebKit::Plugin::willDetatchRenderer):
+        * WebProcess/Plugins/PluginView.cpp:
+        (WebKit::PluginView::willDetatchRenderer):
+        * WebProcess/Plugins/PluginView.h:
+
</ins><span class="cx"> 2016-02-17  Brady Eidson  &lt;beidson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Modern IDB: Encoder/Decoder/Messaging scaffolding for WK2 IPC.
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessPluginsPDFDeprecatedPDFPluginh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Plugins/PDF/DeprecatedPDFPlugin.h (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Plugins/PDF/DeprecatedPDFPlugin.h        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PDF/DeprecatedPDFPlugin.h        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -163,6 +163,7 @@
</span><span class="cx">     virtual bool getFormValue(String&amp; formValue) override { return false; }
</span><span class="cx">     virtual bool handleScroll(WebCore::ScrollDirection, WebCore::ScrollGranularity) override;
</span><span class="cx">     virtual RefPtr&lt;WebCore::SharedBuffer&gt; liveResourceData() const override;
</span><ins>+    virtual void willDetatchRenderer() override;
</ins><span class="cx"> 
</span><span class="cx">     virtual bool isBeingAsynchronouslyInitialized() const override { return false; }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessPluginsPDFDeprecatedPDFPluginmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Plugins/PDF/DeprecatedPDFPlugin.mm (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Plugins/PDF/DeprecatedPDFPlugin.mm        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PDF/DeprecatedPDFPlugin.mm        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -1091,6 +1091,14 @@
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void PDFPlugin::willDetatchRenderer()
+{
+    if (webFrame()) {
+        if (FrameView* frameView = webFrame()-&gt;coreFrame()-&gt;view())
+            frameView-&gt;removeScrollableArea(this);
+    }
+}
+
</ins><span class="cx"> void PDFPlugin::destroy()
</span><span class="cx"> {
</span><span class="cx">     m_pdfLayerController.get().delegate = 0;
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessPluginsPluginh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Plugins/Plugin.h (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Plugins/Plugin.h        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebKit2/WebProcess/Plugins/Plugin.h        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -301,6 +301,8 @@
</span><span class="cx"> 
</span><span class="cx">     virtual bool requiresUnifiedScaleFactor() const { return false; }
</span><span class="cx"> 
</span><ins>+    virtual void willDetatchRenderer() { }
+
</ins><span class="cx"> protected:
</span><span class="cx">     Plugin(PluginType);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessPluginsPluginViewcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Plugins/PluginView.cpp (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Plugins/PluginView.cpp        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PluginView.cpp        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -1005,6 +1005,14 @@
</span><span class="cx">     return m_pluginElement-&gt;displayState() &lt; HTMLPlugInElement::Restarting &amp;&amp; !m_plugin-&gt;supportsSnapshotting();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void PluginView::willDetatchRenderer()
+{
+    if (!m_isInitialized || !m_plugin)
+        return;
+
+    m_plugin-&gt;willDetatchRenderer();
+}
+
</ins><span class="cx"> PassRefPtr&lt;SharedBuffer&gt; PluginView::liveResourceData() const
</span><span class="cx"> {
</span><span class="cx">     if (!m_isInitialized || !m_plugin)
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessPluginsPluginViewh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Plugins/PluginView.h (196716 => 196717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Plugins/PluginView.h        2016-02-17 21:17:00 UTC (rev 196716)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PluginView.h        2016-02-17 21:27:02 UTC (rev 196717)
</span><span class="lines">@@ -167,6 +167,7 @@
</span><span class="cx">     virtual void beginSnapshottingRunningPlugin() override;
</span><span class="cx">     virtual bool shouldAllowNavigationFromDrags() const override;
</span><span class="cx">     virtual bool shouldNotAddLayer() const override;
</span><ins>+    virtual void willDetatchRenderer() override;
</ins><span class="cx"> 
</span><span class="cx">     // WebCore::Widget
</span><span class="cx">     virtual void setFrameRect(const WebCore::IntRect&amp;) override;
</span></span></pre>
</div>
</div>

</body>
</html>