fix(events): separate Worker intrinsic support from public exposure

This commit is contained in:
ldm0
2026-10-23 20:55:15 +08:00
committed by Donough Liu
parent 6fa8b7dede
commit 651bb9f99d
2 changed files with 62 additions and 9 deletions
@@ -20,6 +20,8 @@ pub(in crate::context_bootstrap) const STORAGE_INTERFACE_NAMES: &[&str] = &[
pub(in crate::context_bootstrap) const WORKER_SHARED_INTERFACE_NAMES: &[&str] = &[
"NetworkInformation",
// WorkerNavigator's native Service Worker support needs the intrinsic
// prototype even when the interface object is hidden on the global.
"ServiceWorkerContainer",
"ServiceWorkerRegistration",
"ServiceWorker",
@@ -205,6 +207,7 @@ pub(crate) fn dedicated_worker_lazy_interface_names_for_test() -> Vec<&'static s
.chain(WORKER_SHARED_INTERFACE_NAMES.iter().copied())
.chain(crate::context_bootstrap::streams::stream_interface_names())
.chain(INDEXED_DB_INTERFACE_NAMES.iter().copied())
.filter(|name| exposure_for_name(name).contains(RealmKind::DedicatedWorker))
.collect()
}
@@ -297,6 +300,7 @@ impl ExposureSet {
const WINDOW_ONLY: Self = Self(Self::WINDOW);
const WINDOW_AND_DEDICATED_WORKER: Self = Self(Self::WINDOW | Self::DEDICATED_WORKER);
const WINDOW_AND_SERVICE_WORKER: Self = Self(Self::WINDOW | Self::SERVICE_WORKER);
const ALL_REALMS: Self =
Self(Self::WINDOW | Self::DEDICATED_WORKER | Self::SHARED_WORKER | Self::SERVICE_WORKER);
const WORKERS: Self = Self(Self::DEDICATED_WORKER | Self::SHARED_WORKER | Self::SERVICE_WORKER);
@@ -349,7 +353,12 @@ impl ExposedInterfaceMetadata {
}
pub(super) fn is_supported_by(self, profile: TemplateBuildProfile) -> bool {
self.exposure.contains(profile.realm_kind()) && profile.supports_name(self.name)
// Native WorkerNavigator factories need these prototypes independently
// of whether their interface objects are exposed on the worker global.
let worker_factory_intrinsic = profile != TemplateBuildProfile::Window
&& matches!(self.name, "ServiceWorkerContainer" | "ServiceWorker");
(self.exposure.contains(profile.realm_kind()) || worker_factory_intrinsic)
&& profile.supports_name(self.name)
}
}
@@ -454,6 +463,8 @@ pub(super) fn installation_for_spec(spec: ConstructorSpec) -> GlobalInstallation
fn exposure_for_name(name: &str) -> ExposureSet {
match name {
"ServiceWorkerContainer" => ExposureSet::WINDOW_ONLY,
"ServiceWorker" => ExposureSet::WINDOW_AND_SERVICE_WORKER,
"FileSystemSyncAccessHandle" => ExposureSet::DEDICATED_WORKER_ONLY,
"EncodedVideoChunk" => ExposureSet::WINDOW_AND_DEDICATED_WORKER,
_ if WINDOW_DEDICATED_AND_SHARED_WORKER_INTERFACE_NAMES.contains(&name) => {
@@ -589,7 +589,7 @@ fn failed_intrinsic_finalization_does_not_expose_registered_objects() {
#[test]
fn worker_realm_lazy_properties_follow_chromium_exposure_sets() {
fn own_properties(realm: super::RealmKind) -> Vec<bool> {
fn own_properties(realm: super::RealmKind, secure_context: bool) -> Vec<bool> {
crate::ensure_v8_for_test();
let mut isolate = v8::Isolate::new(Default::default());
let scope = pin!(v8::HandleScope::new(&mut isolate));
@@ -598,7 +598,10 @@ fn worker_realm_lazy_properties_follow_chromium_exposure_sets() {
let scope = &mut v8::ContextScope::new(scope, context);
let global = context.global(scope);
crate::context_bootstrap::install_worker_lazy_exposed_interfaces(
scope, global, realm, true,
scope,
global,
realm,
secure_context,
)
.expect("worker lazy interfaces should install");
let registry = super::template_registry::ExposedInterfaceTemplateRegistry::current(scope)
@@ -644,6 +647,24 @@ fn worker_realm_lazy_properties_follow_chromium_exposure_sets() {
);
assert_eq!(registry.build_count(url_id), 1);
if secure_context {
let event_target = super::ensure_intrinsic_interface_prototype(scope, "EventTarget")
.expect("worker EventTarget prototype");
let key = v8str(scope, "addEventListener");
let canonical_add = event_target.get(scope, key.into()).unwrap();
for name in ["ServiceWorkerContainer", "ServiceWorker"] {
let prototype = super::ensure_intrinsic_interface_prototype(scope, name)
.expect("native Service Worker objects need their prototype independently of global exposure");
let key = v8str(scope, "addEventListener");
assert!(
prototype
.get(scope, key.into())
.unwrap()
.strict_equals(canonical_add)
);
}
}
[
"Worker",
"XMLHttpRequest",
@@ -653,6 +674,11 @@ fn worker_realm_lazy_properties_follow_chromium_exposure_sets() {
"URL",
"WorkerLocation",
"webkitURL",
"BatteryManager",
"NetworkInformation",
"ServiceWorkerContainer",
"ServiceWorkerRegistration",
"ServiceWorker",
]
.iter()
.map(|name| {
@@ -664,17 +690,33 @@ fn worker_realm_lazy_properties_follow_chromium_exposure_sets() {
}
assert_eq!(
own_properties(super::RealmKind::DedicatedWorker),
vec![true, true, true, false, true, true, false, false]
own_properties(super::RealmKind::DedicatedWorker, true),
vec![
true, true, true, false, true, true, false, false, false, true, false, true, false
]
);
assert_eq!(
own_properties(super::RealmKind::SharedWorker),
vec![true, true, true, false, false, true, false, false]
own_properties(super::RealmKind::SharedWorker, true),
vec![
true, true, true, false, false, true, false, false, false, true, false, true, false
]
);
assert_eq!(
own_properties(super::RealmKind::ServiceWorker),
vec![false, false, false, false, false, true, false, false]
own_properties(super::RealmKind::ServiceWorker, true),
vec![
false, false, false, false, false, true, false, false, false, true, false, true, true
]
);
for realm in [
super::RealmKind::DedicatedWorker,
super::RealmKind::SharedWorker,
super::RealmKind::ServiceWorker,
] {
assert_eq!(
&own_properties(realm, false)[8..],
&[false, true, false, false, false]
);
}
}
#[test]