mirror of
https://github.com/lexmount/moli.git
synced 2026-11-22 00:02:21 +00:00
fix(window): defer lightweight popup teardown
This commit is contained in:
@@ -281,6 +281,11 @@ impl CallbackInvoker {
|
||||
invocation.relevant_context,
|
||||
invocation.incumbent_context,
|
||||
|scope| {
|
||||
let relevant_dispatch_scope = invocation
|
||||
.relevant_identity
|
||||
.map(WindowExecutionContextIdentity::dispatch_scope);
|
||||
let previous_relevant_dispatch_scope =
|
||||
relevant_dispatch_scope.map(|dispatch_scope| dispatch_scope.enter(scope));
|
||||
let relevant_context = invocation.relevant_context;
|
||||
let previous_window_event =
|
||||
invocation
|
||||
@@ -353,6 +358,11 @@ impl CallbackInvoker {
|
||||
set_private_value(scope, global, WINDOW_EVENT_SLOT, previous);
|
||||
}
|
||||
}
|
||||
if let (Some(dispatch_scope), Some(previous_dispatch_scope)) =
|
||||
(relevant_dispatch_scope, previous_relevant_dispatch_scope)
|
||||
{
|
||||
dispatch_scope.restore(scope, previous_dispatch_scope);
|
||||
}
|
||||
completed
|
||||
},
|
||||
)
|
||||
|
||||
+8
-3
@@ -12,6 +12,7 @@ use crate::{
|
||||
context_bootstrap::{EventHandlerType, apply_event_handler_return_value},
|
||||
exception_reporting::CallbackExceptionLogLevel,
|
||||
host::report_event_callback_exception,
|
||||
native_bridge::lightweight_popup_id_from_window,
|
||||
util::context_host_ptr_from_global_bridge,
|
||||
};
|
||||
|
||||
@@ -294,6 +295,7 @@ fn invoke_simple_event_listener_collecting_errors<'s>(
|
||||
callback_name,
|
||||
callback_this,
|
||||
listener.relevant_context(),
|
||||
captured_relevant_identity,
|
||||
invocation,
|
||||
callback_errors,
|
||||
)
|
||||
@@ -322,13 +324,16 @@ fn invoke_simple_event_callback_with_invocation<'s>(
|
||||
callback_name: &str,
|
||||
callback_target: v8::Local<'s, v8::Value>,
|
||||
relevant_context: v8::Local<'s, v8::Context>,
|
||||
captured_relevant_identity: Option<crate::native_bridge::WindowExecutionContextIdentity>,
|
||||
mut invocation: CallbackInvocation<'s, '_>,
|
||||
callback_errors: Option<&mut Vec<crate::exception_reporting::V8ExceptionReport>>,
|
||||
) -> SimpleEventCallbackResult {
|
||||
let host_ptr = context_host_ptr_from_global_bridge(scope);
|
||||
let relevant_identity = host_ptr.and_then(|host_ptr| {
|
||||
unsafe { &*host_ptr }
|
||||
.window_execution_context_identity_for_v8_context(scope, relevant_context)
|
||||
let relevant_identity = captured_relevant_identity.or_else(|| {
|
||||
host_ptr.and_then(|host_ptr| {
|
||||
unsafe { &*host_ptr }
|
||||
.window_execution_context_identity_for_v8_context(scope, relevant_context)
|
||||
})
|
||||
});
|
||||
if let Some(host_ptr) = host_ptr {
|
||||
invocation = invocation.with_execution_context_currentness(host_ptr, relevant_identity);
|
||||
|
||||
+150
-33
@@ -4,19 +4,24 @@ use crate::callback_invocation::CallbackInvocation;
|
||||
use crate::event_listener_args::{
|
||||
AddEventListenerArgs, RemoveEventListenerArgs, parse_listener_args,
|
||||
};
|
||||
use crate::util::{get_private_value, set_private_value, v8_string};
|
||||
use crate::native_bridge::WindowExecutionContextIdentity;
|
||||
use crate::util::{
|
||||
context_host_ptr_from_global_bridge, get_private_value, set_private_value, v8_string,
|
||||
};
|
||||
use crate::webidl;
|
||||
use moli_webapi_declare::WebApiObject;
|
||||
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_RESOURCE_INITIATOR_SLOT: &str =
|
||||
"__lmSimpleListenerResourceInitiator";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_ORIGINAL_SLOT: &str = "__moliSimpleEventTargetListenerOriginal";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_CALLBACK_SLOT: &str = "__moliSimpleEventTargetListenerCallback";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_RELEVANT_CONTEXT_ANCHOR_SLOT: &str =
|
||||
"__moliSimpleEventTargetListenerRelevantContextAnchor";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_INCUMBENT_CONTEXT_ANCHOR_SLOT: &str =
|
||||
"__moliSimpleEventTargetListenerIncumbentContextAnchor";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_RELEVANT_IDENTITY_SLOT: &str =
|
||||
"__moliSimpleEventTargetListenerRelevantIdentity";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_CALLABLE_SLOT: &str = "__moliSimpleEventTargetListenerCallable";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_RESOURCE_INITIATOR_SLOT: &str =
|
||||
"__lmSimpleListenerResourceInitiator";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_CAPTURE_SLOT: &str = "__moliSimpleEventTargetListenerCapture";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_ONCE_SLOT: &str = "__moliSimpleEventTargetListenerOnce";
|
||||
const SIMPLE_EVENT_TARGET_LISTENER_PASSIVE_SLOT: &str = "__moliSimpleEventTargetListenerPassive";
|
||||
@@ -85,9 +90,10 @@ struct SimpleObjectEventHandlerEntryDeclaration<'scope> {
|
||||
pub(crate) struct SimpleObjectEventListenerSnapshot<'s> {
|
||||
entry: v8::Local<'s, v8::Object>,
|
||||
pub(crate) original: v8::Local<'s, v8::Value>,
|
||||
callback: v8::Local<'s, v8::Object>,
|
||||
callback: Option<v8::Local<'s, v8::Object>>,
|
||||
relevant_context: v8::Local<'s, v8::Context>,
|
||||
incumbent_context: v8::Local<'s, v8::Context>,
|
||||
relevant_identity: Option<WindowExecutionContextIdentity>,
|
||||
resource_initiator: crate::resource_initiator::CallbackInitiator,
|
||||
is_callable: bool,
|
||||
pub(crate) capture: bool,
|
||||
@@ -113,6 +119,7 @@ struct SimpleObjectResolvedEventListener<'s> {
|
||||
callback: v8::Local<'s, v8::Object>,
|
||||
relevant_context_anchor: v8::Local<'s, v8::Object>,
|
||||
incumbent_context_anchor: v8::Local<'s, v8::Object>,
|
||||
relevant_identity: Option<WindowExecutionContextIdentity>,
|
||||
is_callable: bool,
|
||||
}
|
||||
|
||||
@@ -123,18 +130,7 @@ impl<'s> SimpleObjectEventListenerSnapshot<'s> {
|
||||
registry: SimpleEventListenerRegistry<'s>,
|
||||
event_type: &str,
|
||||
) -> Option<Self> {
|
||||
// Removing and re-adding the same callback creates a different entry.
|
||||
// The removed entry must stay inactive in an existing dispatch snapshot.
|
||||
if !simple_object_event_listener_entry_registered(scope, registry, event_type, self.entry) {
|
||||
return None;
|
||||
}
|
||||
let listener = if self.handler_slot.is_some() {
|
||||
// An active handler keeps its registration position, but its value
|
||||
// and callback contexts can change before dispatch reaches it.
|
||||
simple_object_event_listener_snapshot_entry(scope, self.entry.into())?
|
||||
} else {
|
||||
self.clone()
|
||||
};
|
||||
let listener = self.resolve_callback(scope, registry, event_type)?;
|
||||
if listener.once {
|
||||
simple_object_event_remove_listener_from_registry(
|
||||
scope,
|
||||
@@ -147,14 +143,57 @@ impl<'s> SimpleObjectEventListenerSnapshot<'s> {
|
||||
Some(listener)
|
||||
}
|
||||
|
||||
fn resolve_callback(
|
||||
&self,
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
registry: SimpleEventListenerRegistry<'s>,
|
||||
event_type: &(impl EventTypeKey + ?Sized),
|
||||
) -> Option<Self> {
|
||||
// Removing and re-adding the same callback creates a different entry.
|
||||
// The removed entry must stay inactive in an existing dispatch snapshot.
|
||||
if !simple_object_event_listener_entry_registered(scope, registry, event_type, self.entry) {
|
||||
return None;
|
||||
}
|
||||
let listener = if self.handler_slot.is_some() {
|
||||
let target = get_private_value(scope, self.entry, HANDLER_TARGET_SLOT)
|
||||
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())?;
|
||||
if let Some(popup_id) = lightweight_popup_id_from_window(scope, target)
|
||||
&& let Some(host_ptr) = context_host_ptr_from_global_bridge(scope)
|
||||
{
|
||||
let value = unsafe { &mut *host_ptr }
|
||||
.lightweight_popup_event_handler_property_value(
|
||||
scope,
|
||||
popup_id,
|
||||
self.handler_slot.as_deref()?,
|
||||
)?;
|
||||
// A failed lazy compilation returns null for this read even
|
||||
// if its error handler has installed a replacement callback.
|
||||
if !value.is_object()
|
||||
|| !simple_object_event_listener_entry_registered(
|
||||
scope, registry, event_type, self.entry,
|
||||
)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
}
|
||||
// An active handler keeps its registration position, but its value
|
||||
// and callback contexts can change before dispatch reaches it.
|
||||
simple_object_event_listener_snapshot_entry(scope, self.entry.into())?
|
||||
} else {
|
||||
self.clone()
|
||||
};
|
||||
listener.callback?;
|
||||
Some(listener)
|
||||
}
|
||||
|
||||
pub(crate) fn invocation<'a>(
|
||||
&self,
|
||||
callback_this: v8::Local<'s, v8::Value>,
|
||||
arguments: &'a [v8::Local<'s, v8::Value>],
|
||||
current_event: Option<v8::Local<'s, v8::Object>>,
|
||||
) -> CallbackInvocation<'s, 'a> {
|
||||
) -> Option<CallbackInvocation<'s, 'a>> {
|
||||
let invocation = CallbackInvocation::new(
|
||||
self.callback,
|
||||
self.callback?,
|
||||
callback_this,
|
||||
self.relevant_context,
|
||||
self.incumbent_context,
|
||||
@@ -164,20 +203,25 @@ impl<'s> SimpleObjectEventListenerSnapshot<'s> {
|
||||
current_event,
|
||||
)
|
||||
.with_resource_initiator(self.resource_initiator.clone());
|
||||
if self.handler_slot.is_some() {
|
||||
Some(if self.handler_slot.is_some() {
|
||||
invocation.with_legacy_event_handler()
|
||||
} else {
|
||||
invocation
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn relevant_context(&self) -> v8::Local<'s, v8::Context> {
|
||||
self.relevant_context
|
||||
}
|
||||
|
||||
pub(crate) fn relevant_identity(&self) -> Option<WindowExecutionContextIdentity> {
|
||||
self.relevant_identity
|
||||
}
|
||||
|
||||
pub(crate) fn callable_function(&self) -> Option<v8::Local<'s, v8::Function>> {
|
||||
let callback = self.callback?;
|
||||
self.is_callable
|
||||
.then(|| unsafe { v8::Local::<v8::Function>::cast_unchecked(self.callback) })
|
||||
.then(|| unsafe { v8::Local::<v8::Function>::cast_unchecked(callback) })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,6 +303,7 @@ fn simple_object_event_target_register_resolved_listener<'s>(
|
||||
listener.callback,
|
||||
listener.relevant_context_anchor,
|
||||
listener.incumbent_context_anchor,
|
||||
listener.relevant_identity,
|
||||
listener.is_callable,
|
||||
options.capture,
|
||||
options.once,
|
||||
@@ -519,15 +564,21 @@ pub(crate) fn simple_event_target_inspector_listener_snapshots<'s>(
|
||||
for listener in
|
||||
simple_object_event_listeners_snapshot_in_registry(scope, registry, &event_type)
|
||||
{
|
||||
let Some(listener) = listener.resolve_callback(scope, registry, &event_type) else {
|
||||
continue;
|
||||
};
|
||||
let Some(callback) = listener.callback else {
|
||||
continue;
|
||||
};
|
||||
let original = if listener.handler_slot.is_some() {
|
||||
listener.callback.into()
|
||||
callback.into()
|
||||
} else {
|
||||
listener.original
|
||||
};
|
||||
snapshots.push(SimpleObjectEventListenerInspectorSnapshot {
|
||||
event_type: event_type.clone(),
|
||||
original,
|
||||
callback: listener.callback,
|
||||
callback,
|
||||
relevant_context: listener.relevant_context,
|
||||
is_callable: listener.is_callable,
|
||||
capture: listener.capture,
|
||||
@@ -595,7 +646,7 @@ pub(super) fn simple_object_event_set_handler_in_registry<'s>(
|
||||
return;
|
||||
};
|
||||
if let Some(callback) = callback {
|
||||
let (relevant_context_anchor, incumbent_context_anchor) =
|
||||
let (relevant_context_anchor, incumbent_context_anchor, relevant_identity) =
|
||||
simple_callback_context_anchors(scope, callback);
|
||||
for index in 0..listeners.length() {
|
||||
let Some(candidate) = listeners.get_index(scope, index) else {
|
||||
@@ -632,6 +683,11 @@ pub(super) fn simple_object_event_set_handler_in_registry<'s>(
|
||||
incumbent_context_anchor.into(),
|
||||
);
|
||||
capture_simple_listener_initiator(scope, entry);
|
||||
set_simple_object_event_listener_relevant_identity(
|
||||
scope,
|
||||
entry,
|
||||
relevant_identity,
|
||||
);
|
||||
}
|
||||
ensure_simple_object_event_type_order(scope, registry, event_type);
|
||||
return;
|
||||
@@ -643,13 +699,17 @@ pub(super) fn simple_object_event_set_handler_in_registry<'s>(
|
||||
callback,
|
||||
relevant_context_anchor,
|
||||
incumbent_context_anchor,
|
||||
relevant_identity,
|
||||
);
|
||||
set_private_value(scope, entry, HANDLER_TARGET_SLOT, target.into());
|
||||
let _ = listeners.set_index(scope, listeners.length(), entry.into());
|
||||
ensure_simple_object_event_type_order(scope, registry, event_type);
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(listeners) = simple_object_event_listener_array(scope, registry, event_type, false)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let next = v8::Array::new(scope, 0);
|
||||
for index in 0..listeners.length() {
|
||||
let Some(candidate) = listeners.get_index(scope, index) else {
|
||||
@@ -852,13 +912,14 @@ fn simple_object_event_listener_parts<'s>(
|
||||
.expect("converted EventListener callback must remain an object");
|
||||
let relevant_context = listener.relevant_context(scope);
|
||||
let incumbent_context = listener.incumbent_context(scope);
|
||||
let (relevant_context_anchor, incumbent_context_anchor) =
|
||||
let (relevant_context_anchor, incumbent_context_anchor, relevant_identity) =
|
||||
simple_callback_context_anchors_for_contexts(scope, relevant_context, incumbent_context);
|
||||
SimpleObjectResolvedEventListener {
|
||||
original: callback.into(),
|
||||
callback,
|
||||
relevant_context_anchor,
|
||||
incumbent_context_anchor,
|
||||
relevant_identity,
|
||||
is_callable: listener.callable_at_conversion(),
|
||||
}
|
||||
}
|
||||
@@ -866,7 +927,11 @@ fn simple_object_event_listener_parts<'s>(
|
||||
fn simple_callback_context_anchors<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
callback: v8::Local<'s, v8::Object>,
|
||||
) -> (v8::Local<'s, v8::Object>, v8::Local<'s, v8::Object>) {
|
||||
) -> (
|
||||
v8::Local<'s, v8::Object>,
|
||||
v8::Local<'s, v8::Object>,
|
||||
Option<WindowExecutionContextIdentity>,
|
||||
) {
|
||||
let current_context = scope.get_current_context();
|
||||
let relevant_context = callback
|
||||
.get_creation_context(scope)
|
||||
@@ -879,7 +944,15 @@ fn simple_callback_context_anchors_for_contexts<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
relevant_context: v8::Local<'s, v8::Context>,
|
||||
incumbent_context: v8::Local<'s, v8::Context>,
|
||||
) -> (v8::Local<'s, v8::Object>, v8::Local<'s, v8::Object>) {
|
||||
) -> (
|
||||
v8::Local<'s, v8::Object>,
|
||||
v8::Local<'s, v8::Object>,
|
||||
Option<WindowExecutionContextIdentity>,
|
||||
) {
|
||||
let relevant_identity = context_host_ptr_from_global_bridge(scope).and_then(|host_ptr| {
|
||||
unsafe { &*host_ptr }
|
||||
.window_execution_context_identity_for_v8_context(scope, relevant_context)
|
||||
});
|
||||
let relevant_context_anchor = {
|
||||
let scope = &mut v8::ContextScope::new(scope, relevant_context);
|
||||
v8::Object::new(scope)
|
||||
@@ -888,7 +961,11 @@ fn simple_callback_context_anchors_for_contexts<'s>(
|
||||
let scope = &mut v8::ContextScope::new(scope, incumbent_context);
|
||||
v8::Object::new(scope)
|
||||
};
|
||||
(relevant_context_anchor, incumbent_context_anchor)
|
||||
(
|
||||
relevant_context_anchor,
|
||||
incumbent_context_anchor,
|
||||
relevant_identity,
|
||||
)
|
||||
}
|
||||
|
||||
fn simple_object_event_listener_entry_object<'s>(
|
||||
@@ -897,6 +974,7 @@ fn simple_object_event_listener_entry_object<'s>(
|
||||
callback: v8::Local<'s, v8::Object>,
|
||||
relevant_context_anchor: v8::Local<'s, v8::Object>,
|
||||
incumbent_context_anchor: v8::Local<'s, v8::Object>,
|
||||
relevant_identity: Option<WindowExecutionContextIdentity>,
|
||||
is_callable: bool,
|
||||
capture: bool,
|
||||
once: bool,
|
||||
@@ -914,6 +992,7 @@ fn simple_object_event_listener_entry_object<'s>(
|
||||
)
|
||||
.bind(scope)
|
||||
.expect("SimpleObject event listener entry declaration should bind");
|
||||
set_simple_object_event_listener_relevant_identity(scope, entry, relevant_identity);
|
||||
capture_simple_listener_initiator(scope, entry);
|
||||
entry
|
||||
}
|
||||
@@ -924,8 +1003,12 @@ fn simple_object_event_handler_entry_object<'s>(
|
||||
callback: v8::Local<'s, v8::Object>,
|
||||
relevant_context_anchor: v8::Local<'s, v8::Object>,
|
||||
incumbent_context_anchor: v8::Local<'s, v8::Object>,
|
||||
relevant_identity: Option<WindowExecutionContextIdentity>,
|
||||
) -> v8::Local<'s, v8::Object> {
|
||||
let marker = format!("event-handler:{handler_slot_name}");
|
||||
let value = callback
|
||||
.map(v8::Local::<v8::Value>::from)
|
||||
.unwrap_or_else(|| v8::null(scope).into());
|
||||
let entry = SimpleObjectEventHandlerEntryDeclaration::new(
|
||||
v8_string(scope, &marker),
|
||||
v8_string(scope, handler_slot_name).expect("event handler slot"),
|
||||
@@ -936,6 +1019,7 @@ fn simple_object_event_handler_entry_object<'s>(
|
||||
)
|
||||
.bind(scope)
|
||||
.expect("SimpleObject event handler entry declaration should bind");
|
||||
set_simple_object_event_listener_relevant_identity(scope, entry, relevant_identity);
|
||||
capture_simple_listener_initiator(scope, entry);
|
||||
entry
|
||||
}
|
||||
@@ -953,6 +1037,23 @@ fn capture_simple_listener_initiator<'s>(
|
||||
);
|
||||
}
|
||||
|
||||
fn set_simple_object_event_listener_relevant_identity<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
entry: v8::Local<'s, v8::Object>,
|
||||
identity: Option<WindowExecutionContextIdentity>,
|
||||
) {
|
||||
let value = identity
|
||||
.and_then(|identity| v8_string(scope, &identity.serialize_for_internal_slot()))
|
||||
.map(v8::Local::<v8::Value>::from)
|
||||
.unwrap_or_else(|| v8::undefined(scope).into());
|
||||
set_private_value(
|
||||
scope,
|
||||
entry,
|
||||
SIMPLE_EVENT_TARGET_LISTENER_RELEVANT_IDENTITY_SLOT,
|
||||
value,
|
||||
);
|
||||
}
|
||||
|
||||
fn simple_object_event_listener_array_contains_original(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
listeners: v8::Local<'_, v8::Array>,
|
||||
@@ -1024,7 +1125,11 @@ fn simple_object_event_listener_snapshot_entry<'s>(
|
||||
let original = get_private_value(scope, entry, SIMPLE_EVENT_TARGET_LISTENER_ORIGINAL_SLOT)?;
|
||||
let callback_value =
|
||||
get_private_value(scope, entry, SIMPLE_EVENT_TARGET_LISTENER_CALLBACK_SLOT)?;
|
||||
let callback = v8::Local::<v8::Object>::try_from(callback_value).ok()?;
|
||||
let callback = v8::Local::<v8::Object>::try_from(callback_value).ok();
|
||||
let handler_slot = simple_object_event_listener_handler_slot(scope, candidate);
|
||||
if callback.is_none() && handler_slot.is_none() {
|
||||
return None;
|
||||
}
|
||||
let relevant_context = simple_object_callback_context(
|
||||
scope,
|
||||
entry,
|
||||
@@ -1035,6 +1140,16 @@ fn simple_object_event_listener_snapshot_entry<'s>(
|
||||
entry,
|
||||
SIMPLE_EVENT_TARGET_LISTENER_INCUMBENT_CONTEXT_ANCHOR_SLOT,
|
||||
)?;
|
||||
let relevant_identity = get_private_value(
|
||||
scope,
|
||||
entry,
|
||||
SIMPLE_EVENT_TARGET_LISTENER_RELEVANT_IDENTITY_SLOT,
|
||||
)
|
||||
.and_then(|value| value.to_string(scope))
|
||||
.map(|value| value.to_rust_string_lossy(scope))
|
||||
.and_then(|serialized| {
|
||||
WindowExecutionContextIdentity::deserialize_from_internal_slot(&serialized)
|
||||
});
|
||||
let is_callable =
|
||||
simple_object_private_bool_slot(scope, entry, SIMPLE_EVENT_TARGET_LISTENER_CALLABLE_SLOT)
|
||||
.unwrap_or(false);
|
||||
@@ -1047,25 +1162,27 @@ fn simple_object_event_listener_snapshot_entry<'s>(
|
||||
let passive =
|
||||
simple_object_private_bool_slot(scope, entry, SIMPLE_EVENT_TARGET_LISTENER_PASSIVE_SLOT)
|
||||
.unwrap_or(false);
|
||||
let value = get_private_value(
|
||||
let resource_initiator = get_private_value(
|
||||
scope,
|
||||
entry,
|
||||
SIMPLE_EVENT_TARGET_LISTENER_RESOURCE_INITIATOR_SLOT,
|
||||
)
|
||||
.unwrap_or_else(|| v8::undefined(scope).into());
|
||||
let resource_initiator = crate::resource_initiator::CallbackInitiator::from_value(scope, value);
|
||||
let resource_initiator =
|
||||
crate::resource_initiator::CallbackInitiator::from_value(scope, resource_initiator);
|
||||
Some(SimpleObjectEventListenerSnapshot {
|
||||
entry,
|
||||
original,
|
||||
callback,
|
||||
relevant_context,
|
||||
incumbent_context,
|
||||
relevant_identity,
|
||||
resource_initiator,
|
||||
is_callable,
|
||||
capture,
|
||||
once,
|
||||
passive,
|
||||
handler_slot: simple_object_event_listener_handler_slot(scope, candidate),
|
||||
handler_slot,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -915,6 +915,18 @@ impl PageScriptHost {
|
||||
.popup_load_event()
|
||||
}
|
||||
|
||||
pub(crate) fn page_popup_close_sender(
|
||||
&self,
|
||||
) -> crate::page_task_queue::RendererPagePopupCloseSender {
|
||||
self.page_task_capabilities
|
||||
.get()
|
||||
.expect(
|
||||
"a live Page Window must install its complete Page task capabilities before popup close admission",
|
||||
)
|
||||
.dom_manipulation()
|
||||
.popup_close()
|
||||
}
|
||||
|
||||
pub(crate) fn page_file_entry_file_callback_sender(
|
||||
&self,
|
||||
) -> crate::page_task_queue::RendererPageFileEntryFileCallbackSender {
|
||||
|
||||
@@ -434,13 +434,17 @@ pub(super) struct LightweightPopupBrowsingContextRecord {
|
||||
location_url: Url,
|
||||
opener_sandbox_policy: Option<DocumentSandboxPolicy>,
|
||||
lifecycle: LightweightPopupLifecycle,
|
||||
is_closing: bool,
|
||||
navigation_id: LightweightPopupNavigationId,
|
||||
}
|
||||
|
||||
impl LightweightPopupBrowsingContextRecord {
|
||||
fn is_live(&self) -> bool {
|
||||
matches!(self.lifecycle, LightweightPopupLifecycle::Open(_))
|
||||
}
|
||||
|
||||
fn is_open(&self) -> bool {
|
||||
!self.auxiliary_window.is_closed()
|
||||
&& matches!(self.lifecycle, LightweightPopupLifecycle::Open(_))
|
||||
self.is_live() && !self.is_closing && !self.auxiliary_window.is_closed()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -602,15 +606,30 @@ impl PageScriptHost {
|
||||
.map(|record| record.navigation_id)
|
||||
}
|
||||
|
||||
fn close_lightweight_popup_browsing_context(
|
||||
fn begin_lightweight_popup_close(&mut self, popup_id: u64) -> bool {
|
||||
let Some(record) = self.lightweight_popup_record_mut(popup_id) else {
|
||||
return false;
|
||||
};
|
||||
if !record.is_open() {
|
||||
return false;
|
||||
}
|
||||
record.is_closing = true;
|
||||
true
|
||||
}
|
||||
|
||||
fn take_lightweight_popup_close_transition(
|
||||
&mut self,
|
||||
popup_id: u64,
|
||||
) -> Option<LightweightPopupCloseTransition> {
|
||||
let record = self.lightweight_popup_record_mut(popup_id)?;
|
||||
if !record.is_live() || !record.is_closing {
|
||||
return None;
|
||||
}
|
||||
let lifecycle = std::mem::replace(&mut record.lifecycle, LightweightPopupLifecycle::Closed);
|
||||
let LightweightPopupLifecycle::Open(open) = lifecycle else {
|
||||
return None;
|
||||
};
|
||||
record.is_closing = false;
|
||||
record.navigation_id = LightweightPopupNavigationId::new(
|
||||
record
|
||||
.navigation_id
|
||||
@@ -627,6 +646,38 @@ impl PageScriptHost {
|
||||
})
|
||||
}
|
||||
|
||||
/// Definitely close a lightweight top-level browsing context after its
|
||||
/// queued DOM-manipulation close task reaches the head of the source.
|
||||
pub(crate) fn definitely_close_lightweight_popup_browsing_context<'s>(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
popup_id: u64,
|
||||
) -> bool {
|
||||
let Some(transition) = self.take_lightweight_popup_close_transition(popup_id) else {
|
||||
return false;
|
||||
};
|
||||
let window_handle = self
|
||||
.lightweight_popup_record(popup_id)
|
||||
.expect("closed popup record retained")
|
||||
.auxiliary_window
|
||||
.clone();
|
||||
self.request_auxiliary_window_close(window_handle);
|
||||
let window = self.lightweight_popup_window(scope, popup_id);
|
||||
self.unregister_service_worker_popup_client(popup_id);
|
||||
self.cancel_lightweight_popup_document_loads(popup_id);
|
||||
self.cancel_lightweight_popup_classic_script_loads(popup_id);
|
||||
if let Some(window) = window {
|
||||
clear_lightweight_popup_window_document_event_state(scope, window);
|
||||
}
|
||||
if let Some(document_handle) = transition.retired_document_handle {
|
||||
self.retire_lightweight_popup_document_handle(popup_id, document_handle);
|
||||
self.clear_custom_element_registry_associations_for_document(document_handle);
|
||||
}
|
||||
self.retire_lightweight_popup_document_owner(transition.retired_owner);
|
||||
self.retire_lightweight_popup_local_window(popup_id, transition.retired_local_window_id);
|
||||
true
|
||||
}
|
||||
|
||||
fn set_lightweight_popup_same_document_url(&mut self, popup_id: u64, url: Url) -> bool {
|
||||
let Some(record) = self.lightweight_popup_record_mut(popup_id) else {
|
||||
return false;
|
||||
@@ -958,6 +1009,7 @@ impl PageScriptHost {
|
||||
},
|
||||
session_storage_store,
|
||||
})),
|
||||
is_closing: false,
|
||||
navigation_id: LightweightPopupNavigationId::new(1),
|
||||
},
|
||||
);
|
||||
@@ -1186,7 +1238,7 @@ impl PageScriptHost {
|
||||
) -> Option<v8::Local<'s, v8::Object>> {
|
||||
let (endpoint, opener) = {
|
||||
let record = self.lightweight_popup_browsing_contexts.get(&popup_id)?;
|
||||
if !record.is_open() {
|
||||
if !record.is_live() {
|
||||
return None;
|
||||
}
|
||||
let endpoint = record.opener?;
|
||||
@@ -1581,6 +1633,11 @@ impl PageScriptHost {
|
||||
.is_some_and(LightweightPopupBrowsingContextRecord::is_open)
|
||||
}
|
||||
|
||||
pub(crate) fn lightweight_popup_is_closing(&self, popup_id: u64) -> bool {
|
||||
self.lightweight_popup_record(popup_id)
|
||||
.is_some_and(|record| record.is_live() && record.is_closing)
|
||||
}
|
||||
|
||||
pub(crate) fn open_lightweight_popup_ids(&self) -> Vec<u64> {
|
||||
let mut popup_ids = self
|
||||
.lightweight_popup_browsing_contexts
|
||||
@@ -3873,7 +3930,7 @@ impl PageScriptHost {
|
||||
document_remained_current = false;
|
||||
break;
|
||||
}
|
||||
set_lightweight_popup_script_global(scope, window, &name, value);
|
||||
set_lightweight_popup_persisted_script_global(scope, window, &name, value);
|
||||
if !self.lightweight_popup_document_owner_is_current(script_document_owner) {
|
||||
document_remained_current = false;
|
||||
break;
|
||||
@@ -3900,7 +3957,7 @@ impl PageScriptHost {
|
||||
if value.is_undefined() {
|
||||
continue;
|
||||
}
|
||||
set_lightweight_popup_script_global(scope, window, &name, value);
|
||||
set_lightweight_popup_persisted_script_global(scope, window, &name, value);
|
||||
if !self.lightweight_popup_document_owner_is_current(script_document_owner) {
|
||||
document_remained_current = false;
|
||||
break;
|
||||
@@ -4702,26 +4759,21 @@ fn lightweight_popup_close_callback<'s>(
|
||||
return;
|
||||
};
|
||||
let host = unsafe { &mut *host_ptr };
|
||||
let Some(transition) = host.close_lightweight_popup_browsing_context(popup_id) else {
|
||||
if !host.begin_lightweight_popup_close(popup_id) {
|
||||
return;
|
||||
};
|
||||
let window_handle = host
|
||||
.lightweight_popup_record(popup_id)
|
||||
.unwrap()
|
||||
.auxiliary_window
|
||||
.clone();
|
||||
host.request_auxiliary_window_close(window_handle);
|
||||
|
||||
host.unregister_service_worker_popup_client(popup_id);
|
||||
host.cancel_lightweight_popup_document_loads(popup_id);
|
||||
host.cancel_lightweight_popup_classic_script_loads(popup_id);
|
||||
clear_lightweight_popup_window_document_event_state(scope, window);
|
||||
if let Some(document_handle) = transition.retired_document_handle {
|
||||
host.retire_lightweight_popup_document_handle(popup_id, document_handle);
|
||||
host.clear_custom_element_registry_associations_for_document(document_handle);
|
||||
}
|
||||
host.retire_lightweight_popup_document_owner(transition.retired_owner);
|
||||
host.retire_lightweight_popup_local_window(popup_id, transition.retired_local_window_id);
|
||||
set_object_slot(
|
||||
scope,
|
||||
window,
|
||||
"closed",
|
||||
v8::Boolean::new(scope, true).into(),
|
||||
);
|
||||
if host.page_popup_close_sender().send(popup_id).is_err() {
|
||||
tracing::debug!(
|
||||
popup_id,
|
||||
"retired Page DOM-manipulation route rejected popup close task"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn lightweight_popup_initiator_endpoint<'s>(
|
||||
@@ -4923,19 +4975,19 @@ fn sync_lightweight_popup_window_location<'s>(
|
||||
}
|
||||
}
|
||||
|
||||
fn set_lightweight_popup_script_global(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
window: v8::Local<'_, v8::Object>,
|
||||
fn set_lightweight_popup_persisted_script_global<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
window: v8::Local<'s, v8::Object>,
|
||||
name: &str,
|
||||
value: v8::Local<'_, v8::Value>,
|
||||
value: v8::Local<'s, v8::Value>,
|
||||
) {
|
||||
// Exporting a script global must retain Window's handler accessor and its
|
||||
// registration position, rather than replacing it with a data property.
|
||||
if crate::context_bootstrap::is_window_event_handler_property(name) {
|
||||
set_object_value(scope, window, name, value);
|
||||
} else {
|
||||
set_object_slot(scope, window, name, value);
|
||||
// The wrapped popup script already assigned this value while its
|
||||
// exact popup owner scope was active. Do not invoke the setter again
|
||||
// from the surrounding top scope or replace its accessor.
|
||||
return;
|
||||
}
|
||||
set_object_slot(scope, window, name, value);
|
||||
}
|
||||
|
||||
fn lightweight_popup_effective_navigation_kind(
|
||||
|
||||
@@ -216,9 +216,157 @@ impl WindowExecutionContextIdentity {
|
||||
self.access_policy == WindowExecutionContextAccessPolicy::Universal
|
||||
}
|
||||
|
||||
/// Encodes an exact callback-realm identity for storage in an internal V8
|
||||
/// private slot. Lightweight popup realms alias their opener's V8 context,
|
||||
/// so retaining only that context would lose the LocalWindow generation
|
||||
/// and dispatch address captured when the callback was converted.
|
||||
pub(crate) fn serialize_for_internal_slot(self) -> String {
|
||||
let policy = u8::from(self.grants_universal_access());
|
||||
match (self.owner, self.dispatch_scope) {
|
||||
(WindowExecutionContextOwner::Frame(local_window_id), OwnerDispatchScope::Top) => {
|
||||
format!(
|
||||
"frame:{}:top:{}:{policy}",
|
||||
local_window_id.0,
|
||||
self.realm_token.as_u64()
|
||||
)
|
||||
}
|
||||
(
|
||||
WindowExecutionContextOwner::Frame(local_window_id),
|
||||
OwnerDispatchScope::Child(child_handle),
|
||||
) => format!(
|
||||
"frame:{}:child:{}:{}:{policy}",
|
||||
local_window_id.0,
|
||||
child_handle.index(),
|
||||
self.realm_token.as_u64()
|
||||
),
|
||||
(
|
||||
WindowExecutionContextOwner::LightweightPopup {
|
||||
popup_id,
|
||||
local_window_id,
|
||||
},
|
||||
OwnerDispatchScope::LightweightPopup(dispatch_popup_id),
|
||||
) => {
|
||||
debug_assert_eq!(popup_id, dispatch_popup_id);
|
||||
format!(
|
||||
"popup:{popup_id}:{}:{}:{policy}",
|
||||
local_window_id.as_u64(),
|
||||
self.realm_token.as_u64()
|
||||
)
|
||||
}
|
||||
_ => unreachable!("Window execution-context owner and dispatch scope diverged"),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn deserialize_from_internal_slot(serialized: &str) -> Option<Self> {
|
||||
fn access_policy(value: &str) -> Option<WindowExecutionContextAccessPolicy> {
|
||||
match value {
|
||||
"0" => Some(WindowExecutionContextAccessPolicy::EnforceWebOrigin),
|
||||
"1" => Some(WindowExecutionContextAccessPolicy::Universal),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
let parts = serialized.split(':').collect::<Vec<_>>();
|
||||
match parts.as_slice() {
|
||||
["frame", local_window_id, "top", realm_token, policy] => Some(Self::new(
|
||||
WindowExecutionContextOwner::Frame(crate::frame_owner_model::LocalWindowId(
|
||||
local_window_id.parse().ok()?,
|
||||
)),
|
||||
OwnerDispatchScope::Top,
|
||||
RuntimeObservableContextToken::from_raw(realm_token.parse().ok()?),
|
||||
access_policy(policy)?,
|
||||
)),
|
||||
[
|
||||
"frame",
|
||||
local_window_id,
|
||||
"child",
|
||||
child_handle,
|
||||
realm_token,
|
||||
policy,
|
||||
] => {
|
||||
let child_handle = child_handle.parse::<u64>().ok()?;
|
||||
let child_handle = usize::try_from(child_handle).ok()?;
|
||||
Some(Self::new(
|
||||
WindowExecutionContextOwner::Frame(crate::frame_owner_model::LocalWindowId(
|
||||
local_window_id.parse().ok()?,
|
||||
)),
|
||||
OwnerDispatchScope::Child(crate::document_runtime::DomHandle::new(
|
||||
child_handle,
|
||||
)),
|
||||
RuntimeObservableContextToken::from_raw(realm_token.parse().ok()?),
|
||||
access_policy(policy)?,
|
||||
))
|
||||
}
|
||||
["popup", popup_id, local_window_id, realm_token, policy] => {
|
||||
let popup_id = popup_id.parse().ok()?;
|
||||
Some(Self::new(
|
||||
WindowExecutionContextOwner::LightweightPopup {
|
||||
popup_id,
|
||||
local_window_id: LightweightPopupLocalWindowId::new(
|
||||
local_window_id.parse().ok()?,
|
||||
),
|
||||
},
|
||||
OwnerDispatchScope::LightweightPopup(popup_id),
|
||||
RuntimeObservableContextToken::from_raw(realm_token.parse().ok()?),
|
||||
access_policy(policy)?,
|
||||
))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::native_bridge::context_host) fn access_policy(
|
||||
self,
|
||||
) -> WindowExecutionContextAccessPolicy {
|
||||
self.access_policy
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn callback_identity_internal_slot_encoding_round_trips_every_owner_shape() {
|
||||
let identities = [
|
||||
WindowExecutionContextIdentity::new(
|
||||
WindowExecutionContextOwner::Frame(crate::frame_owner_model::LocalWindowId(7)),
|
||||
OwnerDispatchScope::Top,
|
||||
RuntimeObservableContextToken::from_raw(11),
|
||||
WindowExecutionContextAccessPolicy::EnforceWebOrigin,
|
||||
),
|
||||
WindowExecutionContextIdentity::new(
|
||||
WindowExecutionContextOwner::Frame(crate::frame_owner_model::LocalWindowId(13)),
|
||||
OwnerDispatchScope::Child(crate::document_runtime::DomHandle::new(17)),
|
||||
RuntimeObservableContextToken::from_raw(19),
|
||||
WindowExecutionContextAccessPolicy::Universal,
|
||||
),
|
||||
WindowExecutionContextIdentity::new(
|
||||
WindowExecutionContextOwner::LightweightPopup {
|
||||
popup_id: 23,
|
||||
local_window_id: LightweightPopupLocalWindowId::new(29),
|
||||
},
|
||||
OwnerDispatchScope::LightweightPopup(23),
|
||||
RuntimeObservableContextToken::from_raw(31),
|
||||
WindowExecutionContextAccessPolicy::EnforceWebOrigin,
|
||||
),
|
||||
];
|
||||
|
||||
for identity in identities {
|
||||
assert_eq!(
|
||||
WindowExecutionContextIdentity::deserialize_from_internal_slot(
|
||||
&identity.serialize_for_internal_slot()
|
||||
),
|
||||
Some(identity)
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
WindowExecutionContextIdentity::deserialize_from_internal_slot("popup:23:29:31:2"),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
WindowExecutionContextIdentity::deserialize_from_internal_slot("not-an-identity"),
|
||||
None
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ mod owner_sources;
|
||||
mod parse_time;
|
||||
mod parser_async_module_admission;
|
||||
mod parser_owned_module_continuation;
|
||||
mod popup_close;
|
||||
mod popup_load_event;
|
||||
mod post_domcontentloaded_runtime;
|
||||
mod post_parse_owner_work;
|
||||
@@ -371,6 +372,10 @@ pub(crate) use self::parser_owned_module_continuation::{
|
||||
PageParserOwnedModuleContinuationBodyActivity, PageParserOwnedModuleContinuationTargetEffect,
|
||||
PageParserOwnedModuleContinuationTurnAction,
|
||||
};
|
||||
pub(crate) use self::popup_close::{
|
||||
PagePopupCloseTargetEffect, PagePopupCloseTurnAction, PagePopupCloseTurnOutcome,
|
||||
RendererPagePopupCloseOwner, RendererPagePopupCloseSender, RendererPagePopupCloseTask,
|
||||
};
|
||||
pub(crate) use self::popup_load_event::{
|
||||
PagePopupLoadEventTargetEffect, PagePopupLoadEventTurnAction, PagePopupLoadEventTurnOutcome,
|
||||
RendererPagePopupLoadEventOwner, RendererPagePopupLoadEventSender,
|
||||
|
||||
@@ -27,6 +27,9 @@ use super::{
|
||||
RendererPageImageLoadEventOwner, RendererPageImageLoadEventSender,
|
||||
RendererPageImageLoadEventTask,
|
||||
},
|
||||
popup_close::{
|
||||
RendererPagePopupCloseOwner, RendererPagePopupCloseSender, RendererPagePopupCloseTask,
|
||||
},
|
||||
popup_load_event::{
|
||||
RendererPagePopupLoadEventOwner, RendererPagePopupLoadEventSender,
|
||||
RendererPagePopupLoadEventTask,
|
||||
@@ -66,6 +69,7 @@ pub(crate) enum RendererPageDomManipulationOwner {
|
||||
FileEntryFileCallback(RendererPageFileEntryFileCallbackOwner),
|
||||
ImageLoadEvent(RendererPageImageLoadEventOwner),
|
||||
PopupLoadEvent(RendererPagePopupLoadEventOwner),
|
||||
PopupClose(RendererPagePopupCloseOwner),
|
||||
ConnectedStyleEvent(RendererPageStylesheetTaskOwner),
|
||||
TextTrackDefaultMode(RendererPageTextTrackDefaultModeOwner),
|
||||
TextTrackLoad(RendererPageTextTrackLoadOwner),
|
||||
@@ -82,6 +86,7 @@ pub(crate) enum RendererPageDomManipulationTask {
|
||||
FileEntryFileCallback(RendererPageFileEntryFileCallbackTask),
|
||||
ImageLoadEvent(RendererPageImageLoadEventTask),
|
||||
PopupLoadEvent(RendererPagePopupLoadEventTask),
|
||||
PopupClose(RendererPagePopupCloseTask),
|
||||
ConnectedStyleEvent(RendererPageConnectedStyleEventTask),
|
||||
TextTrackDefaultMode(RendererPageTextTrackDefaultModeTask),
|
||||
TextTrackLoad(RendererPageTextTrackLoadTask),
|
||||
@@ -110,6 +115,7 @@ impl RendererPageDomManipulationTask {
|
||||
Self::PopupLoadEvent(task) => {
|
||||
RendererPageDomManipulationOwner::PopupLoadEvent(task.owner())
|
||||
}
|
||||
Self::PopupClose(task) => RendererPageDomManipulationOwner::PopupClose(task.owner()),
|
||||
Self::ConnectedStyleEvent(task) => {
|
||||
RendererPageDomManipulationOwner::ConnectedStyleEvent(task.owner())
|
||||
}
|
||||
@@ -144,6 +150,7 @@ pub(crate) enum PageDomManipulationTurnAction {
|
||||
FileEntryFileCallback(super::PageFileEntryFileCallbackTurnAction),
|
||||
ImageLoadEvent(super::PageImageLoadEventTurnAction),
|
||||
PopupLoadEvent(super::PagePopupLoadEventTurnAction),
|
||||
PopupClose(super::PagePopupCloseTurnAction),
|
||||
ConnectedStyleEvent(PageConnectedStyleEventTurnAction),
|
||||
TextTrackDefaultMode(super::PageTextTrackDefaultModeTurnAction),
|
||||
TextTrackLoad(super::PageTextTrackLoadTurnAction),
|
||||
@@ -203,6 +210,10 @@ impl RendererPageDomManipulationSender {
|
||||
RendererPagePopupLoadEventSender::new(self.route.clone(), self.root_document)
|
||||
}
|
||||
|
||||
pub(crate) fn popup_close(&self) -> RendererPagePopupCloseSender {
|
||||
RendererPagePopupCloseSender::new(self.route.clone(), self.root_document)
|
||||
}
|
||||
|
||||
pub(crate) fn text_track_default_mode(&self) -> RendererPageTextTrackDefaultModeSender {
|
||||
RendererPageTextTrackDefaultModeSender::new(self.route.clone(), self.root_document)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
use crate::runtime::{PageOwnerTurnOutcome, RendererDocumentToken};
|
||||
|
||||
use super::dom_manipulation::{RendererPageDomManipulationRoute, RendererPageDomManipulationTask};
|
||||
|
||||
/// PageVm namespace plus the top-level lightweight browsing context whose
|
||||
/// script-visible closing flag has been set.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) struct RendererPagePopupCloseOwner {
|
||||
root_document: RendererDocumentToken,
|
||||
popup_id: u64,
|
||||
}
|
||||
|
||||
impl RendererPagePopupCloseOwner {
|
||||
pub(crate) const fn new(root_document: RendererDocumentToken, popup_id: u64) -> Self {
|
||||
Self {
|
||||
root_document,
|
||||
popup_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const fn root_document(self) -> RendererDocumentToken {
|
||||
self.root_document
|
||||
}
|
||||
|
||||
pub(crate) const fn popup_id(self) -> u64 {
|
||||
self.popup_id
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct RendererPagePopupCloseTask {
|
||||
owner: RendererPagePopupCloseOwner,
|
||||
}
|
||||
|
||||
impl RendererPagePopupCloseTask {
|
||||
fn new(owner: RendererPagePopupCloseOwner) -> Self {
|
||||
Self { owner }
|
||||
}
|
||||
|
||||
pub(crate) const fn owner(&self) -> RendererPagePopupCloseOwner {
|
||||
self.owner
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) struct RendererPagePopupCloseRouteClosed;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct RendererPagePopupCloseSender {
|
||||
route: RendererPageDomManipulationRoute,
|
||||
root_document: RendererDocumentToken,
|
||||
}
|
||||
|
||||
impl RendererPagePopupCloseSender {
|
||||
pub(super) fn new(
|
||||
route: RendererPageDomManipulationRoute,
|
||||
root_document: RendererDocumentToken,
|
||||
) -> Self {
|
||||
Self {
|
||||
route,
|
||||
root_document,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn send(&self, popup_id: u64) -> Result<(), RendererPagePopupCloseRouteClosed> {
|
||||
let owner = RendererPagePopupCloseOwner::new(self.root_document, popup_id);
|
||||
self.route
|
||||
.send(RendererPageDomManipulationTask::PopupClose(
|
||||
RendererPagePopupCloseTask::new(owner),
|
||||
))
|
||||
.map_err(|_| RendererPagePopupCloseRouteClosed)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum PagePopupCloseTargetEffect {
|
||||
DefinitelyClosed,
|
||||
DiscardedStaleOwner {
|
||||
current_owner: Option<RendererPagePopupCloseOwner>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) struct PagePopupCloseTurnAction {
|
||||
pub(crate) owner: RendererPagePopupCloseOwner,
|
||||
pub(crate) target_effect: PagePopupCloseTargetEffect,
|
||||
}
|
||||
|
||||
pub(crate) type PagePopupCloseTurnOutcome = PageOwnerTurnOutcome<PagePopupCloseTurnAction>;
|
||||
@@ -148,6 +148,7 @@ pub(crate) use self::page_vm::AuthorizedCurrentPageMiscPlatformApiTask;
|
||||
pub(crate) use self::page_vm::AuthorizedCurrentPageModuleReaction;
|
||||
pub(crate) use self::page_vm::AuthorizedCurrentPageNavigationApiTask;
|
||||
pub(crate) use self::page_vm::AuthorizedCurrentPageOpfsTask;
|
||||
pub(crate) use self::page_vm::AuthorizedCurrentPagePopupClose;
|
||||
pub(crate) use self::page_vm::AuthorizedCurrentPagePopupLoadEvent;
|
||||
pub(crate) use self::page_vm::AuthorizedCurrentPageRenderingUpdate;
|
||||
pub(crate) use self::page_vm::AuthorizedCurrentPageServiceWorkerClientMessage;
|
||||
|
||||
@@ -140,6 +140,7 @@ mod page_owned_document_script_completion;
|
||||
mod page_owned_document_script_hooks;
|
||||
mod page_parser_async_module_admission;
|
||||
mod page_parser_owned_module_continuation;
|
||||
mod page_popup_close;
|
||||
mod page_popup_load_event;
|
||||
mod page_rendering_update;
|
||||
#[cfg(test)]
|
||||
@@ -236,6 +237,7 @@ pub(crate) use page_module_reaction::AuthorizedCurrentPageModuleReaction;
|
||||
pub(crate) use page_modulepreload_start::AuthorizedCurrentChildModulepreloadStartTask;
|
||||
pub(crate) use page_navigation_api_task::AuthorizedCurrentPageNavigationApiTask;
|
||||
pub(crate) use page_opfs_task::AuthorizedCurrentPageOpfsTask;
|
||||
pub(crate) use page_popup_close::AuthorizedCurrentPagePopupClose;
|
||||
pub(crate) use page_popup_load_event::AuthorizedCurrentPagePopupLoadEvent;
|
||||
pub(crate) use page_rendering_update::AuthorizedCurrentPageRenderingUpdate;
|
||||
pub(crate) use page_resource_completion::{
|
||||
|
||||
@@ -33,6 +33,9 @@ impl PageVm {
|
||||
RendererPageDomManipulationTask::PopupLoadEvent(task) => self
|
||||
.apply_selected_page_popup_load_event_turn(task)
|
||||
.map(|outcome| outcome.map_action(PageDomManipulationTurnAction::PopupLoadEvent)),
|
||||
RendererPageDomManipulationTask::PopupClose(task) => self
|
||||
.apply_selected_page_popup_close_turn(task)
|
||||
.map(|outcome| outcome.map_action(PageDomManipulationTurnAction::PopupClose)),
|
||||
RendererPageDomManipulationTask::ConnectedStyleEvent(task) => self
|
||||
.apply_selected_page_connected_style_event_turn(task)
|
||||
.map(|outcome| {
|
||||
|
||||
@@ -37,6 +37,7 @@ impl PageVm {
|
||||
PageDomManipulationTurnAction::PopupLoadEvent(action) => {
|
||||
action.into_page_task_completion()
|
||||
}
|
||||
PageDomManipulationTurnAction::PopupClose(action) => action.into_page_task_completion(),
|
||||
PageDomManipulationTurnAction::ConnectedStyleEvent(action) => {
|
||||
return self
|
||||
.finish_selected_page_connected_style_event_task(action, loader)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
use crate::page_task_queue::{
|
||||
PagePopupCloseTargetEffect, PagePopupCloseTurnAction, PagePopupCloseTurnOutcome,
|
||||
RendererPagePopupCloseOwner, RendererPagePopupCloseTask,
|
||||
};
|
||||
|
||||
use super::{IntoPageTaskCompletion, PageTaskCompletion, PageVm};
|
||||
|
||||
impl IntoPageTaskCompletion for PagePopupCloseTurnAction {
|
||||
fn into_page_task_completion(self) -> PageTaskCompletion {
|
||||
match self.target_effect {
|
||||
PagePopupCloseTargetEffect::DefinitelyClosed => PageTaskCompletion::CheckpointOnly,
|
||||
PagePopupCloseTargetEffect::DiscardedStaleOwner { .. } => {
|
||||
PageTaskCompletion::NoCompletion
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Proof that the Page arbiter matched the PageVm namespace and a live
|
||||
/// lightweight popup whose closing flag is set.
|
||||
pub(crate) struct AuthorizedCurrentPagePopupClose(RendererPagePopupCloseTask);
|
||||
|
||||
impl AuthorizedCurrentPagePopupClose {
|
||||
fn new(task: RendererPagePopupCloseTask) -> Self {
|
||||
Self(task)
|
||||
}
|
||||
|
||||
pub(crate) fn into_task(self) -> RendererPagePopupCloseTask {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl PageVm {
|
||||
fn current_page_popup_close_owner(
|
||||
&self,
|
||||
expected: RendererPagePopupCloseOwner,
|
||||
) -> Option<RendererPagePopupCloseOwner> {
|
||||
if expected.root_document() != self.document_lifecycle.identity().document {
|
||||
return None;
|
||||
}
|
||||
self.vm().current_popup_close_owner(
|
||||
expected.popup_id(),
|
||||
self.document_lifecycle.identity().document,
|
||||
)
|
||||
}
|
||||
|
||||
pub(in crate::runtime) fn apply_selected_page_popup_close_turn(
|
||||
&mut self,
|
||||
task: RendererPagePopupCloseTask,
|
||||
) -> anyhow::Result<PagePopupCloseTurnOutcome> {
|
||||
let owner = task.owner();
|
||||
let current_owner = self.current_page_popup_close_owner(owner);
|
||||
let target_effect = if current_owner == Some(owner) {
|
||||
self.vm_mut()
|
||||
.apply_current_popup_close_body(AuthorizedCurrentPagePopupClose::new(task))?;
|
||||
PagePopupCloseTargetEffect::DefinitelyClosed
|
||||
} else {
|
||||
tracing::debug!(
|
||||
?owner,
|
||||
?current_owner,
|
||||
"discarded stale exact-popup close task"
|
||||
);
|
||||
PagePopupCloseTargetEffect::DiscardedStaleOwner { current_owner }
|
||||
};
|
||||
let action = PagePopupCloseTurnAction {
|
||||
owner,
|
||||
target_effect,
|
||||
};
|
||||
Ok(PagePopupCloseTurnOutcome::new(action))
|
||||
}
|
||||
}
|
||||
@@ -690,6 +690,7 @@ mod page_task_enqueue;
|
||||
mod parser_owned_classic;
|
||||
pub(crate) use parser_owned_classic::*;
|
||||
mod parser_module_terminal;
|
||||
mod popup_close;
|
||||
mod popup_load_event;
|
||||
mod post_parse;
|
||||
mod post_parse_lifecycle;
|
||||
@@ -1070,7 +1071,6 @@ pub(super) struct ActiveDragSession {
|
||||
#[cfg(feature = "devtools")]
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) struct PendingDragGesture {
|
||||
pub handle: DomHandle,
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
pub root_to_frame: moli_layout::LayoutTransform2D,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
use anyhow::Result;
|
||||
|
||||
use super::ScriptVm;
|
||||
use crate::{
|
||||
page_task_queue::RendererPagePopupCloseOwner,
|
||||
runtime::{AuthorizedCurrentPagePopupClose, RendererDocumentToken},
|
||||
};
|
||||
|
||||
impl ScriptVm {
|
||||
pub(crate) fn current_popup_close_owner(
|
||||
&self,
|
||||
popup_id: u64,
|
||||
root_document: RendererDocumentToken,
|
||||
) -> Option<RendererPagePopupCloseOwner> {
|
||||
self._context_host
|
||||
.borrow()
|
||||
.lightweight_popup_is_closing(popup_id)
|
||||
.then(|| RendererPagePopupCloseOwner::new(root_document, popup_id))
|
||||
}
|
||||
|
||||
pub(crate) fn apply_current_popup_close_body(
|
||||
&mut self,
|
||||
authorization: AuthorizedCurrentPagePopupClose,
|
||||
) -> Result<()> {
|
||||
let popup_id = authorization.into_task().owner().popup_id();
|
||||
self.with_default_context_scope(|scope, host_ptr| {
|
||||
assert!(
|
||||
unsafe { &mut *host_ptr }
|
||||
.definitely_close_lightweight_popup_browsing_context(scope, popup_id),
|
||||
"authorized popup close task must retain its closing browsing context"
|
||||
);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
+10
-4
@@ -529,7 +529,8 @@ popup.clearTimeout(cancelledPopupTimer);
|
||||
#[tokio::test]
|
||||
async fn lightweight_popup_close_retires_its_local_window_timers() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
|
||||
let mut vm = new_storage_test_vm_with_loader("https://timer-popup-close.test/", &loader);
|
||||
let mut vm =
|
||||
new_page_task_executor_test_vm_with_loader("https://timer-popup-close.test/", &loader);
|
||||
|
||||
vm.eval(
|
||||
r#"
|
||||
@@ -541,9 +542,14 @@ popup.close();
|
||||
"#,
|
||||
)
|
||||
.expect("popup timer close setup should evaluate");
|
||||
vm.advance_timers_until_deadline_for_test(&loader)
|
||||
.await
|
||||
.expect("closed popup timer should leave the timer queue quiescent");
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(popup.opener === null)",
|
||||
"true",
|
||||
"queued popup definite close",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
vm.eval("__closedPopupTimerEvents.length")
|
||||
.expect("closed popup timer result should evaluate"),
|
||||
|
||||
@@ -3328,9 +3328,10 @@ fn window_open_existing_named_child_sets_and_can_disown_its_opener() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lightweight_popup_opener_accessor_preserves_and_disowns_the_underlying_relation() {
|
||||
let mut vm = new_storage_test_vm("https://example.com/");
|
||||
#[tokio::test]
|
||||
async fn lightweight_popup_opener_accessor_preserves_and_disowns_the_underlying_relation() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
|
||||
let mut vm = new_page_task_executor_test_vm_with_loader("https://example.com/", &loader);
|
||||
|
||||
let result = vm
|
||||
.eval(
|
||||
@@ -3353,6 +3354,8 @@ fn lightweight_popup_opener_accessor_preserves_and_disowns_the_underlying_relati
|
||||
const closedGet = Object.getOwnPropertyDescriptor(closed, "opener").get;
|
||||
closed.close();
|
||||
closed.opener = "closed replacement";
|
||||
globalThis.__closedPopupOpenerGet = closedGet;
|
||||
globalThis.__closedPopupForOpenerTest = closed;
|
||||
|
||||
return JSON.stringify({
|
||||
replaced: {
|
||||
@@ -3377,8 +3380,9 @@ fn lightweight_popup_opener_accessor_preserves_and_disowns_the_underlying_relati
|
||||
boundGetterIsNull: disownedGet() === null,
|
||||
accessorPreserved: descriptorAfterNull.get === disownedGet
|
||||
},
|
||||
closed: {
|
||||
boundGetterIsNull: closedGet() === null,
|
||||
closing: {
|
||||
closedFlag: closed.closed,
|
||||
boundGetterKeepsRelation: closedGet() === window,
|
||||
value: closed.opener
|
||||
}
|
||||
});
|
||||
@@ -3389,6 +3393,142 @@ fn lightweight_popup_opener_accessor_preserves_and_disowns_the_underlying_relati
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
r#"{"replaced":{"accessorShape":"function:function::true:true","value":"replacement","boundGetterKeepsRelation":true,"borrowedGetterKeepsRelation":true,"dataShape":"true:true:true"},"disowned":{"valueIsNull":true,"boundGetterIsNull":true,"accessorPreserved":true},"closed":{"boundGetterIsNull":true,"value":"closed replacement"}}"#
|
||||
r#"{"replaced":{"accessorShape":"function:function::true:true","value":"replacement","boundGetterKeepsRelation":true,"borrowedGetterKeepsRelation":true,"dataShape":"true:true:true"},"disowned":{"valueIsNull":true,"boundGetterIsNull":true,"accessorPreserved":true},"closing":{"closedFlag":true,"boundGetterKeepsRelation":true,"value":"closed replacement"}}"#
|
||||
);
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(__closedPopupOpenerGet() === null)",
|
||||
"true",
|
||||
"queued popup definite close clears the opener relation",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
vm.eval("String(__closedPopupForOpenerTest.opener)")
|
||||
.expect("closed popup replacement opener should evaluate"),
|
||||
"closed replacement"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn top_realm_popup_load_handler_can_queue_a_timer_after_window_close() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
|
||||
let mut vm =
|
||||
new_page_task_executor_test_vm_with_loader("https://example.com/base/page.html", &loader);
|
||||
|
||||
assert_eq!(
|
||||
vm.eval(
|
||||
r#"
|
||||
(() => {
|
||||
globalThis.__popupCloseHandlerState = "pending";
|
||||
globalThis.__popupCloseHandlerEvents = [];
|
||||
const targetUrl = URL.createObjectURL(new Blob([
|
||||
"<!doctype html><title>close target</title>"
|
||||
], { type: "text/html" }));
|
||||
const popup = open(targetUrl);
|
||||
popup.onload = () => {
|
||||
popup.close();
|
||||
const timerId = setTimeout(() => {
|
||||
__popupCloseHandlerEvents.push([
|
||||
popup.closed,
|
||||
popup.opener === null
|
||||
].join(":"));
|
||||
}, 0);
|
||||
__popupCloseHandlerState = [
|
||||
popup.closed,
|
||||
popup.opener === window,
|
||||
timerId !== 0
|
||||
].join(":");
|
||||
};
|
||||
globalThis.__popupCloseHandlerPopup = popup;
|
||||
return __popupCloseHandlerState;
|
||||
})()
|
||||
"#,
|
||||
)
|
||||
.expect("popup close handler setup should evaluate"),
|
||||
"pending"
|
||||
);
|
||||
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"__popupCloseHandlerState",
|
||||
"true:true:true",
|
||||
"popup close handler synchronous state",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
vm.eval("__popupCloseHandlerEvents.length")
|
||||
.expect("popup close handler timer count should evaluate"),
|
||||
"0",
|
||||
"the close task must remain ahead of the later timer"
|
||||
);
|
||||
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"__popupCloseHandlerEvents.join('|')",
|
||||
"true:true",
|
||||
"top-realm timer queued after popup close",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn named_popup_broadcast_precedes_its_queued_close() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
|
||||
let mut vm =
|
||||
new_page_task_executor_test_vm_with_loader("https://example.com/base/page.html", &loader);
|
||||
|
||||
assert_eq!(
|
||||
vm.eval(
|
||||
r#"
|
||||
(() => {
|
||||
const popupName = "moli-noopener-close-order";
|
||||
globalThis.__namedPopupCloseOrder = "pending";
|
||||
const channel = new BroadcastChannel(popupName);
|
||||
const popup = open("about:blank", popupName);
|
||||
channel.onmessage = event => {
|
||||
__namedPopupCloseOrder = JSON.stringify({
|
||||
payload: event.data,
|
||||
openerPreserved: popup.opener === window,
|
||||
alreadyClosing: popup.closed
|
||||
});
|
||||
};
|
||||
const targetMarkup = `<!doctype html><script>
|
||||
const channel = new BroadcastChannel(window.name);
|
||||
channel.postMessage("sent-before-close");
|
||||
window.close();
|
||||
<\/script>`;
|
||||
const link = document.createElement("a");
|
||||
link.rel = "noopener";
|
||||
link.target = popupName;
|
||||
link.href = URL.createObjectURL(new Blob([targetMarkup], { type: "text/html" }));
|
||||
document.body.appendChild(link);
|
||||
globalThis.__namedPopupCloseOrderPopup = popup;
|
||||
link.click();
|
||||
return __namedPopupCloseOrder;
|
||||
})()
|
||||
"#,
|
||||
)
|
||||
.expect("named popup close ordering setup should evaluate"),
|
||||
"pending"
|
||||
);
|
||||
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"__namedPopupCloseOrder",
|
||||
r#"{"payload":"sent-before-close","openerPreserved":true,"alreadyClosing":true}"#,
|
||||
"BroadcastChannel delivery before named popup close",
|
||||
)
|
||||
.await;
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(__namedPopupCloseOrderPopup.opener === null)",
|
||||
"true",
|
||||
"named popup definite close",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user