Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

A tab closed with DevTools open closes its page, even one that asks "Leave site?"

Victor44adb80263da
diff --git a/Sources/CraCEF/CraCEF.mm b/Sources/CraCEF/CraCEF.mm
index 6ca4f82..47398bd 100644
--- a/Sources/CraCEF/CraCEF.mm
+++ b/Sources/CraCEF/CraCEF.mm
@@ -1155,9 +1155,13 @@ class CraClient : public CefClient,
public CefDownloadHandler,
public CefFocusHandler,
public CefPermissionHandler,
- public CefFindHandler {
+ public CefFindHandler,
+ public CefJSDialogHandler {
public:
CraBrowser* owner() { return owner_; }
+ /// Cra closed this page (-[CraBrowser close]). Kept here rather than read from the owner:
+ /// the CraBrowser can be gone before Chromium asks.
+ void SetCloseRequested() { close_requested_ = true; }
explicit CraClient(CraBrowser* owner) : owner_(owner) { CraClientRegistry().insert(static_cast<const void*>(static_cast<CefClient*>(this))); }
~CraClient() override { CraClientRegistry().erase(static_cast<const void*>(static_cast<CefClient*>(this))); }
@@ -1171,6 +1175,21 @@ public:
CefRefPtr<CefFocusHandler> GetFocusHandler() override { return this; }
CefRefPtr<CefPermissionHandler> GetPermissionHandler() override { return this; }
CefRefPtr<CefFindHandler> GetFindHandler() override { return this; }
+ CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() override { return this; }
+
+ // JS dialogs ----------------------------------------------------------
+
+ // Cra closes pages with CloseBrowser(true), which skips beforeunload. With DevTools docked
+ // Chromium still asked "Leave site?" (a shop with something in its cart): DevTools close
+ // first and the page's beforeunload ran anyway. The tab was already gone, so its page window
+ // stayed over the next tab, and after Cancel it never closed. A page Cra closes leaves.
+ // Everything else (reloads, navigations, other dialogs) keeps Chrome's own prompts.
+ bool OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser, const CefString& message_text, bool is_reload,
+ CefRefPtr<CefJSDialogCallback> callback) override {
+ if (!close_requested_) return false;
+ callback->Continue(true, CefString());
+ return true;
+ }
// Life span -----------------------------------------------------------
@@ -1773,6 +1792,7 @@ private:
// True while a prompt's answer runs (UI thread only).
static inline bool answering_prompt_ = false;
__weak CraBrowser* owner_;
+ bool close_requested_ = false;
CefRefPtr<FaviconState> favicon_ = new FaviconState();
CefRefPtr<CefResourceRequestHandler> blocker_ = new CraContentBlockingHandler();
IMPLEMENT_REFCOUNTING(CraClient);
@@ -2398,6 +2418,7 @@ static NSView* FindRenderWidgetHostView(NSView* root) {
- (void)close {
if (_isClosed || _parked) return;
self.closeRequested = YES;
+ if (CraClient* c = _browser ? AsCraClient(_browser->GetHost()->GetClient().get()) : nullptr) c->SetCloseRequested();
if (_browser && _activeDownloads.count && !g_quitting) {
[self park];
} else if (_browser) {
diff --git a/Tests/Fixtures/Pages/page.html b/Tests/Fixtures/Pages/page.html
index 3a46203..1035c5c 100644
--- a/Tests/Fixtures/Pages/page.html
+++ b/Tests/Fixtures/Pages/page.html
@@ -7,8 +7,11 @@
<input placeholder="Type here">
<div id="rows"></div>
<script>
- const n = new URLSearchParams(location.search).get('n') || '0';
+ const query = new URLSearchParams(location.search);
+ const n = query.get('n') || '0';
document.title = 'Fixture ' + n;
+ // ?beforeunload=1: asks "Leave site?" like a shop with something in its cart (once the page had a gesture).
+ if (query.get('beforeunload') === '1') addEventListener('beforeunload', e => { e.preventDefault(); e.returnValue = ''; });
const rows = document.getElementById('rows');
for (let i = 0; i < 200; i++) { const d = document.createElement('div'); d.className = 'row'; d.textContent = 'Row ' + i; rows.appendChild(d); }
</script>
diff --git a/Tests/Scenarios/devtools-close-tab.scenario b/Tests/Scenarios/devtools-close-tab.scenario
new file mode 100644
index 0000000..1785be2
--- /dev/null
+++ b/Tests/Scenarios/devtools-close-tab.scenario
@@ -0,0 +1,17 @@
+# A tab closed while its DevTools are open, on a page that asks "Leave site?" (a shop
+# with something in its cart). Chromium still ran the page's beforeunload despite the forced
+# close, so the closed page and its window stayed over the next tab. The page must close: its
+# renderer (file://, a process of its own) and DevTools' go, back to the baseline.
+budget rss=600 stalls50=5
+cra://open?url=${PAGE}?n=first
+wait 3
+helpers baseline
+cra://open?url=file://${FIXTURES}/Pages/page.html?beforeunload=1
+wait 3
+cra://debug?cdp=Runtime.evaluate&params=%7B%22expression%22%3A%20%22document.body.click%28%29%22%2C%20%22userGesture%22%3A%20true%7D
+wait 1
+cra://action/devTools
+wait 4
+cra://action/closeTab
+wait 3
+helpers settle 0