Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions crates/zephium-engine/src/host/webext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ impl WebextHost {
pending_pages: RefCell::new(HashMap::new()),
next_request: Cell::new(1),
popup: RefCell::new(None),
popup_click: Cell::new(0),
self_weak: RefCell::new(std::rc::Weak::new()),
repeats: RefCell::new(HashMap::new()),
access: RefCell::new(HashMap::new()),
Expand Down Expand Up @@ -274,6 +275,7 @@ impl WebextHost {
external_native_messaging: load.external_native_messaging,
native_messaging_grantable: load.native_messaging_grantable,
inspectable: true,
verify_background_start: true,
};
let install = load.install;
let start_background = load.start_background || reloaded;
Expand Down Expand Up @@ -379,6 +381,7 @@ impl WebextHost {
external_native_messaging: false,
native_messaging_grantable: false,
inspectable: false,
verify_background_start: true,
};
runtime.load(spec, move |_| {
super::dispatch::best_effort_with(move |host| {
Expand Down Expand Up @@ -685,11 +688,11 @@ impl WebextHost {
};
let extension_id = install.extension_id.clone();
let tab = entry.bridge.ids.borrow_mut().tab(request.tab());
if runtime.context(&extension_id).is_none() {
let Some(clicked) = runtime.context(&extension_id) else {
return ExtensionActionSettlement::Rejected(
ExtensionActionRejection::RuntimeUnavailable,
);
}
};
if let Some(rows) = request.menu_rows() {
let tab_object = runtime.tab_object(tab);
// SAFETY: the context and tab object are live on the WebKit main thread.
Expand All @@ -715,16 +718,34 @@ impl WebextHost {
}
return ExtensionActionSettlement::Dispatched;
}
let click = entry.bridge.popup_click.get().wrapping_add(1);
entry.bridge.popup_click.set(click);
*entry.bridge.popup.borrow_mut() = parent.map(|parent| (parent, request.anchor().rect()));
// WebKit drops what a popup sends while the worker sleeps, and the
// popup then waits forever; wake it first (immediate when running).
let waking = extension_id.clone();
let clicked = objc2::rc::Weak::from_retained(&clicked);
runtime.wake_then(&waking, move || {
super::dispatch::best_effort_with(move |host| {
if let Some(entry) = host.webext.profiles.get(&profile) {
let Some(runtime) = &entry.runtime else {
return;
};
// A newer click owns the anchor once it has stored its own.
let release = || {
if entry.bridge.popup_click.get() == click {
entry.bridge.popup.borrow_mut().take();
}
};
// An unload or reload while the worker was waking leaves
// this click without the context it was made on.
let same = match (clicked.load(), runtime.context(&extension_id)) {
(Some(clicked), Some(current)) => std::ptr::eq(&*clicked, &*current),
_ => false,
};
if !same {
return release();
}
// Only a popup consumes the anchor; left behind, it would
// retain the button and place a later action.openPopup
// on it, perhaps in a window since closed.
Expand All @@ -734,7 +755,7 @@ impl WebextHost {
.and_then(|context| unsafe { context.actionForTab(tab_object.as_deref()) })
.is_some_and(|action| unsafe { action.presentsPopup() });
if !popup {
entry.bridge.popup.borrow_mut().take();
release();
}
runtime.perform_action(&extension_id, Some(tab));
}
Expand Down Expand Up @@ -1199,6 +1220,8 @@ struct Bridge {
pending_pages: RefCell<HashMap<ExtensionBrowserRequestId, PendingPage>>,
next_request: Cell<u64>,
popup: RefCell<Option<(Retained<NSView>, Rect)>>,
/// The toolbar click that stored `popup`.
popup_click: Cell<u64>,
self_weak: RefCell<std::rc::Weak<Bridge>>,
repeats: RefCell<HashMap<u64, (std::time::Instant, u32)>>,
access: RefCell<HashMap<u64, AccessAnswer>>,
Expand Down
1 change: 1 addition & 0 deletions crates/zephium-engine/src/platform/macos/private_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const IMPLEMENTED: &[(&CStr, &[&CStr])] = &[
],
),
(c"WKBackForwardList", &[c"_clear"]),
(c"WKWebExtensionContext", &[c"_backgroundWebView"]),
(
c"WKUserContentController",
&[c"_removeAllUserScriptsAssociatedWithContentWorld:"],
Expand Down
37 changes: 37 additions & 0 deletions crates/zephium-webext-macos/fixtures/popup-restart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Popup worker restart qualification

Run from the repository root on macOS with the pinned toolchain:

```sh
node scripts/qualification/macos-popup-restart.mjs
node --test scripts/qualification/macos-popup-recovery.test.mjs
```

The native script builds `webext-lab` with `--locked` and exercises both classic
and module workers in separate disposable WebKit profiles. Each variant first
closes its popup and unloads the extension, then restarts and exits with them
still live, then restarts again. Five-second pauses prevent an immediate
relaunch from masking the failure.

Each process invokes the native toolbar action and checks that its popup
receives a response from a newly started worker. Markers in `chrome.storage.local`,
localStorage and IndexedDB must survive every restart. The module variant
imports its worker from another module, checking recovery preserves module type.
The cold startup has a 2.5-second settling period; this is not a latency benchmark.

A second profile uses the raw fixture without the compatibility startup signal,
while explicitly enabling verification. This forces the pending state even when
WebKit happens to restart promptly. It checks concurrent recovery (64 accepted
waiters and one capacity refusal), unload cancellation (eight callers each settle
once), and successful reload after cancellation. The probe observes callbacks
beyond both readiness deadlines to catch duplicate settlement.

The command exits nonzero on a failed phase and preserves scenarios, logs
and `results.json` under Cargo's target directory in `popup-restart/<uuid>`.
WebKit manages the disposable profile's platform storage separately. The Node
tests exercise the exact recovery script's origin, registration, unregister
and activation refusal paths; they do not replace native qualification.

These probes need no marketplace download, account, native messaging host,
personal profile or network page. They do not establish signed-in password
manager behavior or Windows acceptance.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"manifest_version": 3,
"name": "Popup worker restart fixture",
"description": "Checks a popup's worker response across native runtime launches.",
"version": "1.0",
"permissions": ["storage"],
"background": { "service_worker": "worker.js" },
"action": { "default_popup": "popup.html" }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./worker.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html lang="en">
<meta charset="utf-8" />
<title>Popup worker restart</title>
<body style="width: 280px; min-height: 100px">
<p id="status">Waiting for worker</p>
<script src="popup.js"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions crates/zephium-webext-macos/fixtures/popup-restart/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
globalThis.popupRestart = { status: "waiting" };

const settle = (result) => {
globalThis.popupRestart = result;
document.querySelector("#status").textContent = result.status;
};

chrome.runtime.sendMessage("popup-restart-probe").then(
(reply) =>
settle(
reply?.status === "ready" && typeof reply.boot === "string"
? reply
: { status: "missing-worker-response" },
),
(error) => settle({ status: "rejected", error: String(error) }),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const boot = crypto.randomUUID();

chrome.runtime.onMessage.addListener((message, _sender, reply) => {
if (message !== "popup-restart-probe") return false;
reply({ status: "ready", boot });
return false;
});
Loading
Loading