<!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>[149971] 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/149971">149971</a></dd>
<dt>Author</dt> <dd>akling@apple.com</dd>
<dt>Date</dt> <dd>2013-05-12 16:16:30 -0700 (Sun, 12 May 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Unload event listeners should prevent Safari from insta-killing the web process on last tab close.
&lt;http://webkit.org/b/115988&gt;
&lt;rdar://problem/13870943&gt;

Reviewed by Anders Carlsson.

Source/WebCore:

Add two methods to Chrome (and ChromeClient):

    - enableSuddenTermination()
    - disableSuddenTermination()

..and call these from DOMWindow instead of the free global functions.

For WebKit1, it just calls the NSProcessInfo methods to keep behavior the same.
For WebKit2, the new methods plumb through the information to the UI process.

Also updated the DOMWindow logic to think in terms of per-DOMWindow sudden termination counters
instead of a process-global one, since that gets confusing in a WK2 world.
When a DOMWindow transitions between having/not having unload/beforeunload event listeners,
we send a notification to the Chrome.

* WebCore.exp.in:
* WebCore.xcodeproj/project.pbxproj:
* page/Chrome.h:
(WebCore::Chrome::enableSuddenTermination):
(WebCore::Chrome::disableSuddenTermination):
* page/ChromeClient.h:
(WebCore::ChromeClient::enableSuddenTermination):
(WebCore::ChromeClient::disableSuddenTermination):
* page/DOMWindow.cpp:
(WebCore::addUnloadEventListener):
(WebCore::removeUnloadEventListener):
(WebCore::removeAllUnloadEventListeners):
(WebCore::addBeforeUnloadEventListener):
(WebCore::removeBeforeUnloadEventListener):
(WebCore::removeAllBeforeUnloadEventListeners):
(WebCore::DOMWindow::dispatchAllPendingBeforeUnloadEvents):
(WebCore::DOMWindow::dispatchAllPendingUnloadEvents):
(WebCore::DOMWindow::enableSuddenTermination):
(WebCore::DOMWindow::disableSuddenTermination):
* page/DOMWindow.h:
(DOMWindow):

Source/WebKit/mac:

* WebCoreSupport/WebChromeClient.h:
* WebCoreSupport/WebChromeClient.mm:
(WebChromeClient::enableSuddenTermination):
(WebChromeClient::disableSuddenTermination):

Source/WebKit2:

Let WebCore control the UI process's suppression of the sudden termination mechanism.
This is implemented by having WebChromeClient push Enable/DisableSuddenTermination messages
over to the UI process where the appropriate NSProcessInfo calls are made.

We also use this information when deciding whether to insta-kill a web process when its last
page is closed. This mechanism is re-usable in WebCore to protect against sudden termination
by the UI process.

* UIProcess/WebProcessProxy.cpp:
(WebKit::WebProcessProxy::WebProcessProxy):
(WebKit::WebProcessProxy::removeWebPage):
(WebKit::WebProcessProxy::enableSuddenTermination):
(WebKit::WebProcessProxy::disableSuddenTermination):
* UIProcess/WebProcessProxy.h:
(WebProcessProxy):
* UIProcess/WebProcessProxy.messages.in:
* WebProcess/WebCoreSupport/WebChromeClient.cpp:
(WebKit::WebChromeClient::enableSuddenTermination):
(WebKit::WebChromeClient::disableSuddenTermination):
* WebProcess/WebCoreSupport/WebChromeClient.h:
(WebChromeClient):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreWebCoreexpin">trunk/Source/WebCore/WebCore.exp.in</a></li>
<li><a href="#trunkSourceWebCoreWebCorexcodeprojprojectpbxproj">trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkSourceWebCorepageChromecpp">trunk/Source/WebCore/page/Chrome.cpp</a></li>
<li><a href="#trunkSourceWebCorepageChromeh">trunk/Source/WebCore/page/Chrome.h</a></li>
<li><a href="#trunkSourceWebCorepageChromeClienth">trunk/Source/WebCore/page/ChromeClient.h</a></li>
<li><a href="#trunkSourceWebCorepageDOMWindowcpp">trunk/Source/WebCore/page/DOMWindow.cpp</a></li>
<li><a href="#trunkSourceWebCorepageDOMWindowh">trunk/Source/WebCore/page/DOMWindow.h</a></li>
<li><a href="#trunkSourceWebKitmacChangeLog">trunk/Source/WebKit/mac/ChangeLog</a></li>
<li><a href="#trunkSourceWebKitmacWebCoreSupportWebChromeClienth">trunk/Source/WebKit/mac/WebCoreSupport/WebChromeClient.h</a></li>
<li><a href="#trunkSourceWebKitmacWebCoreSupportWebChromeClientmm">trunk/Source/WebKit/mac/WebCoreSupport/WebChromeClient.mm</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessWebProcessProxycpp">trunk/Source/WebKit2/UIProcess/WebProcessProxy.cpp</a></li>
<li><a href="#trunkSourceWebKit2UIProcessWebProcessProxyh">trunk/Source/WebKit2/UIProcess/WebProcessProxy.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessWebProcessProxymessagesin">trunk/Source/WebKit2/UIProcess/WebProcessProxy.messages.in</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebCoreSupportWebChromeClientcpp">trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebChromeClient.cpp</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebCoreSupportWebChromeClienth">trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebChromeClient.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebCore/ChangeLog        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -1,3 +1,48 @@
</span><ins>+2013-05-12  Andreas Kling  &lt;akling@apple.com&gt;
+
+        Unload event listeners should prevent Safari from insta-killing the web process on last tab close.
+        &lt;http://webkit.org/b/115988&gt;
+        &lt;rdar://problem/13870943&gt;
+
+        Reviewed by Anders Carlsson.
+
+        Add two methods to Chrome (and ChromeClient):
+
+            - enableSuddenTermination()
+            - disableSuddenTermination()
+
+        ..and call these from DOMWindow instead of the free global functions.
+
+        For WebKit1, it just calls the NSProcessInfo methods to keep behavior the same.
+        For WebKit2, the new methods plumb through the information to the UI process.
+
+        Also updated the DOMWindow logic to think in terms of per-DOMWindow sudden termination counters
+        instead of a process-global one, since that gets confusing in a WK2 world.
+        When a DOMWindow transitions between having/not having unload/beforeunload event listeners,
+        we send a notification to the Chrome.
+
+        * WebCore.exp.in:
+        * WebCore.xcodeproj/project.pbxproj:
+        * page/Chrome.h:
+        (WebCore::Chrome::enableSuddenTermination):
+        (WebCore::Chrome::disableSuddenTermination):
+        * page/ChromeClient.h:
+        (WebCore::ChromeClient::enableSuddenTermination):
+        (WebCore::ChromeClient::disableSuddenTermination):
+        * page/DOMWindow.cpp:
+        (WebCore::addUnloadEventListener):
+        (WebCore::removeUnloadEventListener):
+        (WebCore::removeAllUnloadEventListeners):
+        (WebCore::addBeforeUnloadEventListener):
+        (WebCore::removeBeforeUnloadEventListener):
+        (WebCore::removeAllBeforeUnloadEventListeners):
+        (WebCore::DOMWindow::dispatchAllPendingBeforeUnloadEvents):
+        (WebCore::DOMWindow::dispatchAllPendingUnloadEvents):
+        (WebCore::DOMWindow::enableSuddenTermination):
+        (WebCore::DOMWindow::disableSuddenTermination):
+        * page/DOMWindow.h:
+        (DOMWindow):
+
</ins><span class="cx"> 2013-05-12  Anders Carlsson  &lt;andersca@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Remove Complex.h from WTF
</span></span></pre></div>
<a id="trunkSourceWebCoreWebCoreexpin"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/WebCore.exp.in (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/WebCore.exp.in        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebCore/WebCore.exp.in        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -751,6 +751,7 @@
</span><span class="cx"> __ZN7WebCore23createFragmentFromNodesEPNS_8DocumentERKN3WTF6VectorIPNS_4NodeELm0ENS2_15CrashOnOverflowEEE
</span><span class="cx"> __ZN7WebCore23dataForURLComponentTypeEP5NSURLl
</span><span class="cx"> __ZN7WebCore23decodeHostNameWithRangeEP8NSString8_NSRange
</span><ins>+__ZN7WebCore23enableSuddenTerminationEv
</ins><span class="cx"> __ZN7WebCore23encodeHostNameWithRangeEP8NSString8_NSRange
</span><span class="cx"> __ZN7WebCore23getHostnamesWithCookiesERKNS_21NetworkStorageSessionERN3WTF7HashSetINS3_6StringENS3_10StringHashENS3_10HashTraitsIS5_EEEE
</span><span class="cx"> __ZN7WebCore24BinaryPropertyListWriter17writePropertyListEv
</span><span class="lines">@@ -774,6 +775,7 @@
</span><span class="cx"> __ZN7WebCore24createFragmentFromMarkupEPNS_8DocumentERKN3WTF6StringES5_NS_19ParserContentPolicyE
</span><span class="cx"> __ZN7WebCore24decodeURLEscapeSequencesERKN3WTF6StringE
</span><span class="cx"> __ZN7WebCore24deleteCookiesForHostnameERKNS_21NetworkStorageSessionERKN3WTF6StringE
</span><ins>+__ZN7WebCore24disableSuddenTerminationEv
</ins><span class="cx"> __ZN7WebCore24fileSystemRepresentationERKN3WTF6StringE
</span><span class="cx"> __ZN7WebCore24notifyHistoryItemChangedE
</span><span class="cx"> __ZN7WebCore24pathByAppendingComponentERKN3WTF6StringES3_
</span></span></pre></div>
<a id="trunkSourceWebCoreWebCorexcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -2882,7 +2882,7 @@
</span><span class="cx">                 939B02EE0EA2DBC400C54570 /* WidthIterator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 939B02EC0EA2DBC400C54570 /* WidthIterator.cpp */; };
</span><span class="cx">                 939B02EF0EA2DBC400C54570 /* WidthIterator.h in Headers */ = {isa = PBXBuildFile; fileRef = 939B02ED0EA2DBC400C54570 /* WidthIterator.h */; };
</span><span class="cx">                 93A38B4B0D0E5808006872C2 /* EditorCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 93A38B4A0D0E5808006872C2 /* EditorCommand.cpp */; };
</span><del>-                93B2D8160F9920D2006AE6B2 /* SuddenTermination.h in Headers */ = {isa = PBXBuildFile; fileRef = 93B2D8150F9920D2006AE6B2 /* SuddenTermination.h */; };
</del><ins>+                93B2D8160F9920D2006AE6B2 /* SuddenTermination.h in Headers */ = {isa = PBXBuildFile; fileRef = 93B2D8150F9920D2006AE6B2 /* SuddenTermination.h */; settings = {ATTRIBUTES = (Private, ); }; };
</ins><span class="cx">                 93B2D8180F9920EE006AE6B2 /* SuddenTermination.mm in Sources */ = {isa = PBXBuildFile; fileRef = 93B2D8170F9920EE006AE6B2 /* SuddenTermination.mm */; };
</span><span class="cx">                 93B6A0E60B0BCA5C00F5027A /* ContextMenu.h in Headers */ = {isa = PBXBuildFile; fileRef = 93B6A0E50B0BCA5C00F5027A /* ContextMenu.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="cx">                 93B6A0EA0B0BCA8400F5027A /* ContextMenuMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 93B6A0E90B0BCA8400F5027A /* ContextMenuMac.mm */; };
</span><span class="lines">@@ -21656,6 +21656,7 @@
</span><span class="cx">                                 BC64640A11D7F304006455B0 /* DOMStringMap.h in Headers */,
</span><span class="cx">                                 0FF5025B102BA9010066F39A /* DOMStyleMedia.h in Headers */,
</span><span class="cx">                                 0FF50263102BA92C0066F39A /* DOMStyleMediaInternal.h in Headers */,
</span><ins>+                                93B2D8160F9920D2006AE6B2 /* SuddenTermination.h in Headers */,
</ins><span class="cx">                                 850657000AAB4763002D15C0 /* DOMStyleSheet.h in Headers */,
</span><span class="cx">                                 85B498FF0ADB348100925CBB /* DOMStyleSheetInternal.h in Headers */,
</span><span class="cx">                                 850657020AAB4763002D15C0 /* DOMStyleSheetList.h in Headers */,
</span><span class="lines">@@ -23518,7 +23519,6 @@
</span><span class="cx">                                 656D37480ADBA5DE00A4554D /* SubresourceLoader.h in Headers */,
</span><span class="cx">                                 659A7D130B6DB4D9001155B3 /* SubstituteData.h in Headers */,
</span><span class="cx">                                 1A8F6B020DB53006001DB794 /* SubstituteResource.h in Headers */,
</span><del>-                                93B2D8160F9920D2006AE6B2 /* SuddenTermination.h in Headers */,
</del><span class="cx">                                 97C078501165D5BE003A32EF /* SuffixTree.h in Headers */,
</span><span class="cx">                                 97627B9814FB5424002CDCA1 /* Supplementable.h in Headers */,
</span><span class="cx">                                 087558C613B4A57D00F49307 /* SurrogatePairAwareTextIterator.h in Headers */,
</span></span></pre></div>
<a id="trunkSourceWebCorepageChromecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/Chrome.cpp (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/Chrome.cpp        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebCore/page/Chrome.cpp        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -461,6 +461,16 @@
</span><span class="cx">     m_client-&gt;print(frame);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void Chrome::enableSuddenTermination()
+{
+    m_client-&gt;enableSuddenTermination();
+}
+
+void Chrome::disableSuddenTermination()
+{
+    m_client-&gt;disableSuddenTermination();
+}
+
</ins><span class="cx"> #if ENABLE(DIRECTORY_UPLOAD)
</span><span class="cx"> void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkSourceWebCorepageChromeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/Chrome.h (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/Chrome.h        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebCore/page/Chrome.h        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -147,6 +147,9 @@
</span><span class="cx"> 
</span><span class="cx">     void print(Frame*);
</span><span class="cx"> 
</span><ins>+    void enableSuddenTermination();
+    void disableSuddenTermination();
+
</ins><span class="cx"> #if ENABLE(INPUT_TYPE_COLOR)
</span><span class="cx">     PassOwnPtr&lt;ColorChooser&gt; createColorChooser(ColorChooserClient*, const Color&amp; initialColor);
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebCorepageChromeClienth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/ChromeClient.h (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/ChromeClient.h        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebCore/page/ChromeClient.h        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -317,6 +317,9 @@
</span><span class="cx">     virtual void willPopUpMenu(NSMenu *) { }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+    virtual void enableSuddenTermination() { }
+    virtual void disableSuddenTermination() { }
+
</ins><span class="cx"> #if PLATFORM(WIN)
</span><span class="cx">     virtual void setLastSetCursorToCurrentCursor() = 0;
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebCorepageDOMWindowcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/DOMWindow.cpp (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/DOMWindow.cpp        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebCore/page/DOMWindow.cpp        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -175,20 +175,16 @@
</span><span class="cx"> static void addUnloadEventListener(DOMWindow* domWindow)
</span><span class="cx"> {
</span><span class="cx">     DOMWindowSet&amp; set = windowsWithUnloadEventListeners();
</span><del>-    if (set.isEmpty())
-        disableSuddenTermination();
-    set.add(domWindow);
</del><ins>+    if (set.add(domWindow).isNewEntry)
+        domWindow-&gt;disableSuddenTermination();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static void removeUnloadEventListener(DOMWindow* domWindow)
</span><span class="cx"> {
</span><span class="cx">     DOMWindowSet&amp; set = windowsWithUnloadEventListeners();
</span><span class="cx">     DOMWindowSet::iterator it = set.find(domWindow);
</span><del>-    if (it == set.end())
-        return;
-    set.remove(it);
-    if (set.isEmpty())
-        enableSuddenTermination();
</del><ins>+    if (set.remove(it))
+        domWindow-&gt;enableSuddenTermination();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static void removeAllUnloadEventListeners(DOMWindow* domWindow)
</span><span class="lines">@@ -198,27 +194,22 @@
</span><span class="cx">     if (it == set.end())
</span><span class="cx">         return;
</span><span class="cx">     set.removeAll(it);
</span><del>-    if (set.isEmpty())
-        enableSuddenTermination();
</del><ins>+    domWindow-&gt;enableSuddenTermination();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static void addBeforeUnloadEventListener(DOMWindow* domWindow)
</span><span class="cx"> {
</span><span class="cx">     DOMWindowSet&amp; set = windowsWithBeforeUnloadEventListeners();
</span><del>-    if (set.isEmpty())
-        disableSuddenTermination();
-    set.add(domWindow);
</del><ins>+    if (set.add(domWindow).isNewEntry)
+        domWindow-&gt;disableSuddenTermination();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static void removeBeforeUnloadEventListener(DOMWindow* domWindow)
</span><span class="cx"> {
</span><span class="cx">     DOMWindowSet&amp; set = windowsWithBeforeUnloadEventListeners();
</span><span class="cx">     DOMWindowSet::iterator it = set.find(domWindow);
</span><del>-    if (it == set.end())
-        return;
-    set.remove(it);
-    if (set.isEmpty())
-        enableSuddenTermination();
</del><ins>+    if (set.remove(it))
+        domWindow-&gt;enableSuddenTermination();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static void removeAllBeforeUnloadEventListeners(DOMWindow* domWindow)
</span><span class="lines">@@ -228,8 +219,7 @@
</span><span class="cx">     if (it == set.end())
</span><span class="cx">         return;
</span><span class="cx">     set.removeAll(it);
</span><del>-    if (set.isEmpty())
-        enableSuddenTermination();
</del><ins>+    domWindow-&gt;enableSuddenTermination();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static bool allowsBeforeUnloadListeners(DOMWindow* window)
</span><span class="lines">@@ -272,12 +262,11 @@
</span><span class="cx"> 
</span><span class="cx">         if (!frame-&gt;loader()-&gt;shouldClose())
</span><span class="cx">             return false;
</span><ins>+
+        window-&gt;enableSuddenTermination();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    enableSuddenTermination();
-
</del><span class="cx">     alreadyDispatched = true;
</span><del>-
</del><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -310,10 +299,10 @@
</span><span class="cx"> 
</span><span class="cx">         window-&gt;dispatchEvent(PageTransitionEvent::create(eventNames().pagehideEvent, false), window-&gt;document());
</span><span class="cx">         window-&gt;dispatchEvent(Event::create(eventNames().unloadEvent, false, false), window-&gt;document());
</span><ins>+
+        window-&gt;enableSuddenTermination();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    enableSuddenTermination();
-
</del><span class="cx">     alreadyDispatched = true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -2017,4 +2006,16 @@
</span><span class="cx">     dialogFrame-&gt;page()-&gt;chrome()-&gt;runModal();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void DOMWindow::enableSuddenTermination()
+{
+    if (Page* page = this-&gt;page())
+        page-&gt;chrome()-&gt;enableSuddenTermination();
+}
+
+void DOMWindow::disableSuddenTermination()
+{
+    if (Page* page = this-&gt;page())
+        page-&gt;chrome()-&gt;disableSuddenTermination();
+}
+
</ins><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCorepageDOMWindowh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/DOMWindow.h (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/DOMWindow.h        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebCore/page/DOMWindow.h        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -412,6 +412,9 @@
</span><span class="cx">         void willDetachDocumentFromFrame();
</span><span class="cx">         void willDestroyCachedFrame();
</span><span class="cx"> 
</span><ins>+        void enableSuddenTermination();
+        void disableSuddenTermination();
+
</ins><span class="cx">     private:
</span><span class="cx">         explicit DOMWindow(Document*);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKitmacChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/mac/ChangeLog (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/mac/ChangeLog        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit/mac/ChangeLog        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -1,3 +1,16 @@
</span><ins>+2013-05-12  Andreas Kling  &lt;akling@apple.com&gt;
+
+        Unload event listeners should prevent Safari from insta-killing the web process on last tab close.
+        &lt;http://webkit.org/b/115988&gt;
+        &lt;rdar://problem/13870943&gt;
+
+        Reviewed by Anders Carlsson.
+
+        * WebCoreSupport/WebChromeClient.h:
+        * WebCoreSupport/WebChromeClient.mm:
+        (WebChromeClient::enableSuddenTermination):
+        (WebChromeClient::disableSuddenTermination):
+
</ins><span class="cx"> 2013-05-10  Brian J. Burg  &lt;burg@cs.washington.edu&gt;
</span><span class="cx"> 
</span><span class="cx">         Web Inspector: Implement WK2 version of WebInspectorFrontendClient::save
</span></span></pre></div>
<a id="trunkSourceWebKitmacWebCoreSupportWebChromeClienth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/mac/WebCoreSupport/WebChromeClient.h (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/mac/WebCoreSupport/WebChromeClient.h        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit/mac/WebCoreSupport/WebChromeClient.h        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -136,6 +136,9 @@
</span><span class="cx">     virtual void makeFirstResponder(NSResponder *) OVERRIDE;
</span><span class="cx"> 
</span><span class="cx">     virtual void willPopUpMenu(NSMenu *) OVERRIDE;
</span><ins>+
+    virtual void enableSuddenTermination() OVERRIDE;
+    virtual void disableSuddenTermination() OVERRIDE;
</ins><span class="cx">     
</span><span class="cx">     virtual bool shouldReplaceWithGeneratedFileForUpload(const WTF::String&amp; path, WTF::String &amp;generatedFilename) OVERRIDE;
</span><span class="cx">     virtual WTF::String generateReplacementFile(const WTF::String&amp; path) OVERRIDE;
</span></span></pre></div>
<a id="trunkSourceWebKitmacWebCoreSupportWebChromeClientmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/mac/WebCoreSupport/WebChromeClient.mm (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/mac/WebCoreSupport/WebChromeClient.mm        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit/mac/WebCoreSupport/WebChromeClient.mm        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -814,6 +814,16 @@
</span><span class="cx">     END_BLOCK_OBJC_EXCEPTIONS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebChromeClient::enableSuddenTermination()
+{
+    [[NSProcessInfo processInfo] enableSuddenTermination];
+}
+
+void WebChromeClient::disableSuddenTermination()
+{
+    [[NSProcessInfo processInfo] disableSuddenTermination];
+}
+
</ins><span class="cx"> bool WebChromeClient::shouldReplaceWithGeneratedFileForUpload(const String&amp; path, String&amp; generatedFilename)
</span><span class="cx"> {
</span><span class="cx">     NSString* filename;
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit2/ChangeLog        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -1,3 +1,33 @@
</span><ins>+2013-05-12  Andreas Kling  &lt;akling@apple.com&gt;
+
+        Unload event listeners should prevent Safari from insta-killing the web process on last tab close.
+        &lt;http://webkit.org/b/115988&gt;
+        &lt;rdar://problem/13870943&gt;
+
+        Reviewed by Anders Carlsson.
+
+        Let WebCore control the UI process's suppression of the sudden termination mechanism.
+        This is implemented by having WebChromeClient push Enable/DisableSuddenTermination messages
+        over to the UI process where the appropriate NSProcessInfo calls are made.
+
+        We also use this information when deciding whether to insta-kill a web process when its last
+        page is closed. This mechanism is re-usable in WebCore to protect against sudden termination
+        by the UI process.
+
+        * UIProcess/WebProcessProxy.cpp:
+        (WebKit::WebProcessProxy::WebProcessProxy):
+        (WebKit::WebProcessProxy::removeWebPage):
+        (WebKit::WebProcessProxy::enableSuddenTermination):
+        (WebKit::WebProcessProxy::disableSuddenTermination):
+        * UIProcess/WebProcessProxy.h:
+        (WebProcessProxy):
+        * UIProcess/WebProcessProxy.messages.in:
+        * WebProcess/WebCoreSupport/WebChromeClient.cpp:
+        (WebKit::WebChromeClient::enableSuddenTermination):
+        (WebKit::WebChromeClient::disableSuddenTermination):
+        * WebProcess/WebCoreSupport/WebChromeClient.h:
+        (WebChromeClient):
+
</ins><span class="cx"> 2013-05-12  Alexey Proskuryakov  &lt;ap@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         &lt;rdar://problem/13402976&gt; Sandbox violations in com.nvidia.OpenGL on Lion.
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessWebProcessProxycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/WebProcessProxy.cpp (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/WebProcessProxy.cpp        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit2/UIProcess/WebProcessProxy.cpp        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -41,6 +41,7 @@
</span><span class="cx"> #include &quot;WebProcessMessages.h&quot;
</span><span class="cx"> #include &quot;WebProcessProxyMessages.h&quot;
</span><span class="cx"> #include &lt;WebCore/KURL.h&gt;
</span><ins>+#include &lt;WebCore/SuddenTermination.h&gt;
</ins><span class="cx"> #include &lt;stdio.h&gt;
</span><span class="cx"> #include &lt;wtf/MainThread.h&gt;
</span><span class="cx"> #include &lt;wtf/text/CString.h&gt;
</span><span class="lines">@@ -90,6 +91,7 @@
</span><span class="cx">     : m_responsivenessTimer(this)
</span><span class="cx">     , m_context(context)
</span><span class="cx">     , m_mayHaveUniversalFileReadSandboxExtension(false)
</span><ins>+    , m_suddenTerminationCounter(0)
</ins><span class="cx"> #if ENABLE(CUSTOM_PROTOCOLS)
</span><span class="cx">     , m_customProtocolManagerProxy(this)
</span><span class="cx"> #endif
</span><span class="lines">@@ -202,7 +204,7 @@
</span><span class="cx"> #if ENABLE(NETWORK_PROCESS)
</span><span class="cx">     // Terminate the web process immediately if we have enough information to confidently do so.
</span><span class="cx">     // This only works if we're using a network process. Otherwise we have to wait for the web process to clean up.
</span><del>-    if (canTerminateChildProcess() &amp;&amp; m_context-&gt;usesNetworkProcess())
</del><ins>+    if (!m_suddenTerminationCounter &amp;&amp; canTerminateChildProcess() &amp;&amp; m_context-&gt;usesNetworkProcess())
</ins><span class="cx">         requestTermination();
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="lines">@@ -652,4 +654,23 @@
</span><span class="cx">     disconnect();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+
+void WebProcessProxy::enableSuddenTermination()
+{
+    if (!isValid())
+        return;
+
+    WebCore::enableSuddenTermination();
+    m_suddenTerminationCounter--;
+}
+
+void WebProcessProxy::disableSuddenTermination()
+{
+    if (!isValid())
+        return;
+
+    WebCore::disableSuddenTermination();
+    m_suddenTerminationCounter++;
+}
+
</ins><span class="cx"> } // namespace WebKit
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessWebProcessProxyh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/WebProcessProxy.h (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/WebProcessProxy.h        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit2/UIProcess/WebProcessProxy.h        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -115,6 +115,9 @@
</span><span class="cx">     void updateProcessSuppressionState();
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+    void enableSuddenTermination();
+    void disableSuddenTermination();
+
</ins><span class="cx">     void requestTermination();
</span><span class="cx"> 
</span><span class="cx"> private:
</span><span class="lines">@@ -188,6 +191,8 @@
</span><span class="cx">     bool m_mayHaveUniversalFileReadSandboxExtension; // True if a read extension for &quot;/&quot; was ever granted - we don't track whether WebProcess still has it.
</span><span class="cx">     HashSet&lt;String&gt; m_localPathsWithAssumedReadAccess;
</span><span class="cx"> 
</span><ins>+    unsigned m_suddenTerminationCounter;
+
</ins><span class="cx">     WebPageProxyMap m_pageMap;
</span><span class="cx">     WebFrameProxyMap m_frameMap;
</span><span class="cx">     WebBackForwardListItemMap m_backForwardListItemMap;
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessWebProcessProxymessagesin"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/WebProcessProxy.messages.in (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/WebProcessProxy.messages.in        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit2/UIProcess/WebProcessProxy.messages.in        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -33,6 +33,9 @@
</span><span class="cx"> 
</span><span class="cx">     ShouldTerminate() -&gt; (bool shouldTerminate)
</span><span class="cx"> 
</span><ins>+    EnableSuddenTermination()
+    DisableSuddenTermination()
+
</ins><span class="cx">     # Plugin messages.
</span><span class="cx"> #if ENABLE(NETSCAPE_PLUGIN_API)
</span><span class="cx">     GetPlugins(bool refresh) -&gt; (Vector&lt;WebCore::PluginInfo&gt; plugins)
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebCoreSupportWebChromeClientcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebChromeClient.cpp (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebChromeClient.cpp        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebChromeClient.cpp        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -45,6 +45,7 @@
</span><span class="cx"> #include &quot;WebPopupMenu.h&quot;
</span><span class="cx"> #include &quot;WebPreferencesStore.h&quot;
</span><span class="cx"> #include &quot;WebProcess.h&quot;
</span><ins>+#include &quot;WebProcessProxyMessages.h&quot;
</ins><span class="cx"> #include &quot;WebSearchPopupMenu.h&quot;
</span><span class="cx"> #include &quot;WebSecurityOrigin.h&quot;
</span><span class="cx"> #include &lt;WebCore/AXObjectCache.h&gt;
</span><span class="lines">@@ -844,4 +845,14 @@
</span><span class="cx">     return m_page-&gt;injectedBundleUIClient().plugInExtraScript();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebChromeClient::enableSuddenTermination()
+{
+    m_page-&gt;send(Messages::WebProcessProxy::EnableSuddenTermination());
+}
+
+void WebChromeClient::disableSuddenTermination()
+{
+    m_page-&gt;send(Messages::WebProcessProxy::DisableSuddenTermination());
+}
+
</ins><span class="cx"> } // namespace WebKit
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebCoreSupportWebChromeClienth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebChromeClient.h (149970 => 149971)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebChromeClient.h        2013-05-12 21:27:43 UTC (rev 149970)
+++ trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebChromeClient.h        2013-05-12 23:16:30 UTC (rev 149971)
</span><span class="lines">@@ -206,6 +206,9 @@
</span><span class="cx"> #if PLATFORM(MAC)
</span><span class="cx">     virtual void makeFirstResponder() OVERRIDE;
</span><span class="cx"> #endif
</span><ins>+
+    virtual void enableSuddenTermination() OVERRIDE;
+    virtual void disableSuddenTermination() OVERRIDE;
</ins><span class="cx">     
</span><span class="cx">     virtual void dispatchViewportPropertiesDidChange(const WebCore::ViewportArguments&amp;) const OVERRIDE;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>