<!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>[283728] trunk</title>
</head>
<body>

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

<h3>Log Message</h3>
<pre>Web Inspector: WebInspectorExtensionTabContentView should not reload its iframe when detached
https://bugs.webkit.org/show_bug.cgi?id=230758
<rdar://74714861>

Reviewed by Timothy Hatcher.

Source/WebInspectorUI:

When an <iframe> element detaches from the DOM, the script context is destroyed, which we don't
want to happen for _WKInspectorExtension tabs. Fix this by teaching ContentViewContainer to
'hide' such content views by setting `display:none` rather than detaching from the DOM.

* UserInterface/Views/ContentViewContainer.js:
(WI.ContentViewContainer.prototype._showEntry):
(WI.ContentViewContainer.prototype._hideEntry):
Set and unset 'display:none' instead of calling addSubview() / removeSubview().

* UserInterface/Views/ContentViewContainer.css:
(.content-view-container > .content-view.hidden-for-detach): Added.

(WI.ContentViewContainer.prototype._disassociateFromContentView):
Clean up any remaining content views that were not detached due to overriding shouldNotRemoveFromDOMWhenHidden.

* UserInterface/Views/ContentView.js:
(WI.ContentView.prototype.get shouldNotRemoveFromDOMWhenHidden): Added.

* UserInterface/Views/WebInspectorExtensionTabContentView.js:
(WI.WebInspectorExtensionTabContentView):
(WI.WebInspectorExtensionTabContentView.prototype.get shouldNotRemoveFromDOMWhenHidden):
Override this to opt into the alternate behavior that does not detach from the DOM. It is still
necessary to call attached() and detached() so that WebInpectorExtensionTabContentView can generate
didShowExtensionTab/didHideExtensionTab event callbacks.

* UserInterface/Controllers/WebInspectorExtensionController.js:
(WI.WebInspectorExtensionController.prototype.get registeredExtensionIDs):
* UserInterface/Debug/Bootstrap.js:
(updateMockWebExtensionTab):
(WI.runBootstrapOperations):
This is a drive-by fix to address a console assertion seen while developing the API test.
Don't unregister the mock extension if it is not registered in the first place.

Tools:

Add a new test to verify that re-selecting an extension tab does not cause it to reload.

* TestWebKitAPI/Tests/WebKitCocoa/InspectorExtension-basic-tab.html:
* TestWebKitAPI/Tests/WebKitCocoa/WKInspectorExtension.mm:
(TEST):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebInspectorUIChangeLog">trunk/Source/WebInspectorUI/ChangeLog</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceControllersWebInspectorExtensionControllerjs">trunk/Source/WebInspectorUI/UserInterface/Controllers/WebInspectorExtensionController.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceDebugBootstrapjs">trunk/Source/WebInspectorUI/UserInterface/Debug/Bootstrap.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsContentViewjs">trunk/Source/WebInspectorUI/UserInterface/Views/ContentView.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsContentViewContainercss">trunk/Source/WebInspectorUI/UserInterface/Views/ContentViewContainer.css</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsContentViewContainerjs">trunk/Source/WebInspectorUI/UserInterface/Views/ContentViewContainer.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsTabBrowserjs">trunk/Source/WebInspectorUI/UserInterface/Views/TabBrowser.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsWebInspectorExtensionTabContentViewjs">trunk/Source/WebInspectorUI/UserInterface/Views/WebInspectorExtensionTabContentView.js</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKitCocoaInspectorExtensionbasictabhtml">trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/InspectorExtension-basic-tab.html</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKitCocoaWKInspectorExtensionmm">trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKInspectorExtension.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebInspectorUIChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/ChangeLog (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/ChangeLog    2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Source/WebInspectorUI/ChangeLog       2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -1,3 +1,44 @@
</span><ins>+2021-10-07  BJ Burg  <bburg@apple.com>
+
+        Web Inspector: WebInspectorExtensionTabContentView should not reload its iframe when detached
+        https://bugs.webkit.org/show_bug.cgi?id=230758
+        <rdar://74714861>
+
+        Reviewed by Timothy Hatcher.
+
+        When an <iframe> element detaches from the DOM, the script context is destroyed, which we don't
+        want to happen for _WKInspectorExtension tabs. Fix this by teaching ContentViewContainer to
+        'hide' such content views by setting `display:none` rather than detaching from the DOM.
+
+        * UserInterface/Views/ContentViewContainer.js:
+        (WI.ContentViewContainer.prototype._showEntry):
+        (WI.ContentViewContainer.prototype._hideEntry):
+        Set and unset 'display:none' instead of calling addSubview() / removeSubview().
+
+        * UserInterface/Views/ContentViewContainer.css:
+        (.content-view-container > .content-view.hidden-for-detach): Added.
+
+        (WI.ContentViewContainer.prototype._disassociateFromContentView):
+        Clean up any remaining content views that were not detached due to overriding shouldNotRemoveFromDOMWhenHidden.
+
+        * UserInterface/Views/ContentView.js:
+        (WI.ContentView.prototype.get shouldNotRemoveFromDOMWhenHidden): Added.
+
+        * UserInterface/Views/WebInspectorExtensionTabContentView.js:
+        (WI.WebInspectorExtensionTabContentView):
+        (WI.WebInspectorExtensionTabContentView.prototype.get shouldNotRemoveFromDOMWhenHidden):
+        Override this to opt into the alternate behavior that does not detach from the DOM. It is still
+        necessary to call attached() and detached() so that WebInpectorExtensionTabContentView can generate
+        didShowExtensionTab/didHideExtensionTab event callbacks.
+
+        * UserInterface/Controllers/WebInspectorExtensionController.js:
+        (WI.WebInspectorExtensionController.prototype.get registeredExtensionIDs):
+        * UserInterface/Debug/Bootstrap.js:
+        (updateMockWebExtensionTab):
+        (WI.runBootstrapOperations):
+        This is a drive-by fix to address a console assertion seen while developing the API test.
+        Don't unregister the mock extension if it is not registered in the first place.
+
</ins><span class="cx"> 2021-10-07  Nikita Vasilyev  <nvasilyev@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Web Inspector: Styles: format style declarations after editing
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceControllersWebInspectorExtensionControllerjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Controllers/WebInspectorExtensionController.js (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Controllers/WebInspectorExtensionController.js 2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Source/WebInspectorUI/UserInterface/Controllers/WebInspectorExtensionController.js    2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -37,6 +37,11 @@
</span><span class="cx"> 
</span><span class="cx">     // Public
</span><span class="cx"> 
</span><ins>+    get registeredExtensionIDs()
+    {
+        return new Set(this._extensionForExtensionIDMap.keys());
+    }
+
</ins><span class="cx">     registerExtension(extensionID, displayName)
</span><span class="cx">     {
</span><span class="cx">         if (this._extensionForExtensionIDMap.has(extensionID)) {
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceDebugBootstrapjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Debug/Bootstrap.js (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Debug/Bootstrap.js     2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Source/WebInspectorUI/UserInterface/Debug/Bootstrap.js        2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -1,5 +1,6 @@
</span><span class="cx"> /*
</span><span class="cx">  * Copyright (C) 2015 University of Washington.
</span><ins>+ * Copyright (C) 2021 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -150,7 +151,9 @@
</span><span class="cx"> 
</span><span class="cx">         // Simulates the steps taken by WebInspectorUIExtensionController to create an extension tab in WebInspectorUI.
</span><span class="cx">         if (!WI.settings.engineeringShowMockWebExtensionTab.value) {
</span><del>-            InspectorFrontendAPI.unregisterExtension(mockData.extensionID);
</del><ins>+            if (WI.sharedApp.extensionController.registeredExtensionIDs.has(mockData.extensionID))
+                InspectorFrontendAPI.unregisterExtension(mockData.extensionID);
+
</ins><span class="cx">             return;
</span><span class="cx">         }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsContentViewjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/ContentView.js (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/ContentView.js   2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/ContentView.js      2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2013-2021 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -42,6 +42,13 @@
</span><span class="cx"> 
</span><span class="cx">     // Static
</span><span class="cx"> 
</span><ins>+    static shouldNotRemoveFromDOMWhenHidden()
+    {
+        // Implemented by subclasses.
+        // Returns true if the content view should *not* be detached from the DOM when hidden.
+        return false;
+    }
+
</ins><span class="cx">     static createFromRepresentedObject(representedObject, extraArguments)
</span><span class="cx">     {
</span><span class="cx">         console.assert(representedObject);
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsContentViewContainercss"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/ContentViewContainer.css (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/ContentViewContainer.css 2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/ContentViewContainer.css    2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2013 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2013-2021 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -37,3 +37,7 @@
</span><span class="cx"> 
</span><span class="cx">     overflow: hidden;
</span><span class="cx"> }
</span><ins>+
+.content-view-container > .content-view.hidden-simulating-dom-detached {
+    display: none;
+}
</ins></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsContentViewContainerjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/ContentViewContainer.js (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/ContentViewContainer.js  2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/ContentViewContainer.js     2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -421,6 +421,11 @@
</span><span class="cx">             return;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        // Deselected extension tabs are still attached to the DOM via `this.element`,
+        // so this is the last chance to actually remove the subview and detach from the DOM.
+        if (contentView.constructor.shouldNotRemoveFromDOMWhenHidden() && contentView.isAttached)
+            this.removeSubview(contentView);
+
</ins><span class="cx">         console.assert(!contentView.isAttached);
</span><span class="cx"> 
</span><span class="cx">         if (!contentView._parentContainer)
</span><span class="lines">@@ -457,6 +462,10 @@
</span><span class="cx"> 
</span><span class="cx">         if (!this.subviews.includes(entry.contentView))
</span><span class="cx">             this.addSubview(entry.contentView);
</span><ins>+        else if (entry.contentView.constructor.shouldNotRemoveFromDOMWhenHidden()) {
+            entry.contentView.element.classList.remove("hidden-simulating-dom-detached");
+            entry.contentView._didMoveToParent(this);
+        }
</ins><span class="cx"> 
</span><span class="cx">         entry.prepareToShow();
</span><span class="cx">     }
</span><span class="lines">@@ -471,8 +480,13 @@
</span><span class="cx">             return;
</span><span class="cx"> 
</span><span class="cx">         entry.prepareToHide();
</span><del>-        if (this.subviews.includes(entry.contentView))
-            this.removeSubview(entry.contentView);
</del><ins>+        if (this.subviews.includes(entry.contentView)) {
+            if (entry.contentView.constructor.shouldNotRemoveFromDOMWhenHidden()) {
+                entry.contentView.element.classList.add("hidden-simulating-dom-detached");
+                entry.contentView._didMoveToParent(null);
+            } else
+                this.removeSubview(entry.contentView);
+        }
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     _tombstoneContentViewContainersForContentView(contentView)
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsTabBrowserjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/TabBrowser.js (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/TabBrowser.js    2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/TabBrowser.js       2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -171,8 +171,10 @@
</span><span class="cx">             this._tabBar.addTabBarItem(tabBarItem, options);
</span><span class="cx"> 
</span><span class="cx">         console.assert(this._recentTabContentViews.length === this._tabBar.tabCount);
</span><del>-        console.assert(!this.selectedTabContentView || this.selectedTabContentView === this._recentTabContentViews[0]);
</del><span class="cx"> 
</span><ins>+        let shouldSaveTab = this.selectedTabContentView?.constructor.shouldSaveTab() || this.selectedTabContentView?.constructor.shouldPinTab();
+        console.assert(!this.selectedTabContentView || this.selectedTabContentView === this._recentTabContentViews[0] || !shouldSaveTab);
+
</ins><span class="cx">         return true;
</span><span class="cx">     }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsWebInspectorExtensionTabContentViewjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/WebInspectorExtensionTabContentView.js (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/WebInspectorExtensionTabContentView.js   2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/WebInspectorExtensionTabContentView.js      2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -40,9 +40,6 @@
</span><span class="cx">         this._tabInfo = tabInfo;
</span><span class="cx">         this._sourceURL = sourceURL;
</span><span class="cx"> 
</span><del>-        // FIXME: the <iframe>'s document is implicitly reloaded when this
-        // content view's element is detached and later re-attached to the DOM.
-        // This is a bug and will be addressed in <https://webkit.org/b/230758>.
</del><span class="cx">         this._iframeElement = this.element.appendChild(document.createElement("iframe"));
</span><span class="cx">         this._iframeElement.addEventListener("load", this._extensionFrameDidLoad.bind(this));
</span><span class="cx">         this._iframeElement.src = this._sourceURL;
</span><span class="lines">@@ -87,6 +84,11 @@
</span><span class="cx"> 
</span><span class="cx">     static shouldSaveTab() { return false; }
</span><span class="cx"> 
</span><ins>+    static shouldNotRemoveFromDOMWhenHidden() {
+        // This is necessary to avoid the <iframe> content from being reloaded when the extension tab is hidden.
+        return true;
+    }
+
</ins><span class="cx">     // Private
</span><span class="cx"> 
</span><span class="cx">     _extensionFrameDidLoad()
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Tools/ChangeLog       2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2021-10-07  BJ Burg  <bburg@apple.com>
+
+        Web Inspector: WebInspectorExtensionTabContentView should not reload its iframe when detached
+        https://bugs.webkit.org/show_bug.cgi?id=230758
+        <rdar://74714861>
+
+        Reviewed by Timothy Hatcher.
+
+        Add a new test to verify that re-selecting an extension tab does not cause it to reload.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/InspectorExtension-basic-tab.html:
+        * TestWebKitAPI/Tests/WebKitCocoa/WKInspectorExtension.mm:
+        (TEST):
+
</ins><span class="cx"> 2021-10-07  Youenn Fablet  <youenn@apple.com>
</span><span class="cx"> 
</span><span class="cx">         REGRESSION (r283613): [ macOS ] TestWebKitAPI.ResourceLoadDelegate.LoadInfo is failing
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitCocoaInspectorExtensionbasictabhtml"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/InspectorExtension-basic-tab.html (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/InspectorExtension-basic-tab.html    2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/InspectorExtension-basic-tab.html       2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -1,10 +1,24 @@
</span><span class="cx"> <html>
</span><span class="cx"> <head>
</span><span class="cx"> <script>
</span><ins>+    // Used by WKInspectorExtension.CanEvaluateScriptInExtensionTab
</ins><span class="cx">     window._secretValue = {answer:42};
</span><ins>+
+    // Used by WKInspectorExtension.ExtensionTabIsPersistent
+    window.getUniqueValue = function() {
+        if (!window._cachedUniqueValue) {
+            window._cachedUniqueValue = Math.floor(Math.random() * 10e9);
+            document.getElementById("uniqueValueField").innerText = window._cachedUniqueValue;
+        }
+
+        return window._cachedUniqueValue;
+    }
</ins><span class="cx"> </script>
</span><span class="cx"> <body>
</span><span class="cx"> <h1>This is a test extension.</h1>
</span><span class="cx"> <p>In a normal extension, this area would show the extension's user interface.</p>
</span><ins>+<p>The unique value for this iframe's execution context is:
+    <span id="uniqueValueField">TBD</span>
+</p>
</ins><span class="cx"> </body>
</span><span class="cx"> </html>
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitCocoaWKInspectorExtensionmm"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKInspectorExtension.mm (283727 => 283728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKInspectorExtension.mm      2021-10-07 18:35:01 UTC (rev 283727)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKInspectorExtension.mm 2021-10-07 18:38:12 UTC (rev 283728)
</span><span class="lines">@@ -47,6 +47,7 @@
</span><span class="cx"> static RetainPtr<TestInspectorURLSchemeHandler> sharedURLSchemeHandler;
</span><span class="cx"> static RetainPtr<_WKInspectorExtension> sharedInspectorExtension;
</span><span class="cx"> static RetainPtr<NSString> sharedExtensionTabIdentifier;
</span><ins>+static RetainPtr<NSObject> evaluationResult;
</ins><span class="cx"> 
</span><span class="cx"> static void resetGlobalState()
</span><span class="cx"> {
</span><span class="lines">@@ -188,4 +189,121 @@
</span><span class="cx">     TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+TEST(WKInspectorExtension, ExtensionTabIsPersistent)
+{
+    resetGlobalState();
+
+    auto webViewConfiguration = adoptNS([WKWebViewConfiguration new]);
+    webViewConfiguration.get().preferences._developerExtrasEnabled = YES;
+    auto webView = adoptNS([[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:webViewConfiguration.get()]);
+    auto uiDelegate = adoptNS([UIDelegateForTestingInspectorExtension new]);
+
+    [webView setUIDelegate:uiDelegate.get()];
+    [webView loadHTMLString:@"<head><title>Test page to be inspected</title></head><body><p>Filler content</p></body>" baseURL:[NSURL URLWithString:@"http://example.com/"]];
+
+    [[webView _inspector] show];
+    TestWebKitAPI::Util::run(&didAttachLocalInspectorCalled);
+
+    auto extensionID = [NSUUID UUID].UUIDString;
+    auto extensionDisplayName = @"SecondExtension";
+
+    // Register the test extension.
+    pendingCallbackWasCalled = false;
+    [[webView _inspector] registerExtensionWithID:extensionID displayName:extensionDisplayName completionHandler:^(NSError * _Nullable error, _WKInspectorExtension * _Nullable extension) {
+        EXPECT_NULL(error);
+        EXPECT_NOT_NULL(extension);
+        sharedInspectorExtension = extension;
+
+        pendingCallbackWasCalled = true;
+    }];
+    TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
+
+    auto extensionDelegate = adoptNS([InspectorExtensionDelegateForTestingInspectorExtension new]);
+    [sharedInspectorExtension setDelegate:extensionDelegate.get()];
+
+    // Create and show an extension tab.
+    auto iconURL = [NSURL URLWithString:@"test-resource://SecondExtension/InspectorExtension-TabIcon-30x30.png"];
+    auto sourceURL = [NSURL URLWithString:@"test-resource://SecondExtension/InspectorExtension-basic-tab.html"];
+
+    pendingCallbackWasCalled = false;
+    [sharedInspectorExtension createTabWithName:@"SecondExtension-Tab" tabIconURL:iconURL sourceURL:sourceURL completionHandler:^(NSError * _Nullable error, NSString * _Nullable extensionTabIdentifier) {
+        EXPECT_NULL(error);
+        EXPECT_NOT_NULL(extensionTabIdentifier);
+        sharedExtensionTabIdentifier = extensionTabIdentifier;
+
+        pendingCallbackWasCalled = true;
+    }];
+    TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
+
+    pendingCallbackWasCalled = false;
+    didShowExtensionTabWasCalled = false;
+    [[webView _inspector] showExtensionTabWithIdentifier:sharedExtensionTabIdentifier.get() completionHandler:^(NSError * _Nullable error) {
+        EXPECT_NULL(error);
+
+        pendingCallbackWasCalled = true;
+    }];
+    TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
+    TestWebKitAPI::Util::run(&didShowExtensionTabWasCalled);
+
+    auto scriptSource = @"!!window.getUniqueValue && window.getUniqueValue()";
+
+    // Read back a value that is unique to the <iframe>'s script context.
+    pendingCallbackWasCalled = false;
+    [sharedInspectorExtension _evaluateScript:scriptSource inExtensionTabWithIdentifier:sharedExtensionTabIdentifier.get() completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable result) {
+        EXPECT_NULL(error);
+        EXPECT_NOT_NULL(result);
+        evaluationResult = result;
+
+        pendingCallbackWasCalled = true;
+    }];
+    TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
+
+    // Cause the extension tab to be hidden. Its <iframe> should not detach from the DOM.
+    [[webView _inspector] showConsole];
+    TestWebKitAPI::Util::run(&didHideExtensionTabWasCalled);
+
+    // Check the unique value again, while the <iframe> is being hidden. If the <iframe> is
+    // detached from the DOM, then this evaluation will fail or hang.
+    pendingCallbackWasCalled = false;
+    [sharedInspectorExtension _evaluateScript:scriptSource inExtensionTabWithIdentifier:sharedExtensionTabIdentifier.get() completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable result) {
+        EXPECT_NULL(error);
+        EXPECT_NOT_NULL(result);
+        EXPECT_NS_EQUAL(result, evaluationResult.get());
+
+        pendingCallbackWasCalled = true;
+    }];
+    TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
+
+    // Reselect the extension tab.
+    pendingCallbackWasCalled = false;
+    didShowExtensionTabWasCalled = false;
+    [[webView _inspector] showExtensionTabWithIdentifier:sharedExtensionTabIdentifier.get() completionHandler:^(NSError * _Nullable error) {
+        EXPECT_NULL(error);
+
+        pendingCallbackWasCalled = true;
+    }];
+    TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
+    TestWebKitAPI::Util::run(&didShowExtensionTabWasCalled);
+
+    // Check the unique value again after reselecting the extension tab.
+    pendingCallbackWasCalled = false;
+    [sharedInspectorExtension _evaluateScript:scriptSource inExtensionTabWithIdentifier:sharedExtensionTabIdentifier.get() completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable result) {
+        EXPECT_NULL(error);
+        EXPECT_NOT_NULL(result);
+        EXPECT_NS_EQUAL(result, evaluationResult.get());
+
+        pendingCallbackWasCalled = true;
+    }];
+    TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
+
+    // Unregister the test extension.
+    pendingCallbackWasCalled = false;
+    [[webView _inspector] unregisterExtension:sharedInspectorExtension.get() completionHandler:^(NSError * _Nullable error) {
+        EXPECT_NULL(error);
+
+        pendingCallbackWasCalled = true;
+    }];
+    TestWebKitAPI::Util::run(&pendingCallbackWasCalled);
+}
+
</ins><span class="cx"> #endif // ENABLE(INSPECTOR_EXTENSIONS)
</span></span></pre>
</div>
</div>

</body>
</html>