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
2 changes: 1 addition & 1 deletion moli-renderer-v8/src/document_runtime/mutation_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ impl DocumentRuntime {
owner = ?binding.owner(),
element = ?binding.element(),
load_delay_token = ?binding.load_delay_token(),
settled,
?settled,
"settled invalidated connected-style lease at mutation commit"
);
}
Expand Down
2 changes: 2 additions & 0 deletions moli-renderer-v8/src/frame_owner_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ mod script_events;
mod store;
mod task_sources;

pub(crate) use load_event_gate::DocumentLoadDelaySettlement;

pub(crate) use classic_script_tasks::{
FrameClassicDocumentScriptExecutionAction, FrameClassicDocumentScriptExecutionStart,
FrameDocumentClassicCompletionFinishAction, FrameDocumentClassicCompletionFollowup,
Expand Down
172 changes: 162 additions & 10 deletions moli-renderer-v8/src/frame_owner_model/lifecycle_blockers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::BTreeSet;

use super::load_event_gate::{DocumentLoadGate, DocumentLoadGateRelease};
use super::load_event_gate::{
DocumentLoadDelaySettlement, DocumentLoadGate, DocumentLoadGateRelease,
};
use super::records::{DocumentLoadDelayReason, DocumentLoadDelayTokenId};

/// All phase-specific blockers owned by one exact Document lifecycle.
Expand All @@ -12,6 +14,17 @@ use super::records::{DocumentLoadDelayReason, DocumentLoadDelayTokenId};
pub(super) struct DocumentLifecycleBlockers {
parser_deferred_scripts: BTreeSet<DocumentLoadDelayTokenId>,
window_load: DocumentLoadGate,
state: DocumentLoadDelayState,
}

/// Blocking authority and token identity have different lifetimes. A current
/// Document can invalidate all delays while their resource tasks still exist.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
enum DocumentLoadDelayState {
#[default]
Open,
Invalidated,
Retired,
}

impl DocumentLifecycleBlockers {
Expand All @@ -20,7 +33,7 @@ impl DocumentLifecycleBlockers {
token: DocumentLoadDelayTokenId,
reason: DocumentLoadDelayReason,
) -> bool {
if self.owns_any(token) {
if self.state != DocumentLoadDelayState::Open || self.owns_any(token) {
return false;
}
if reason == DocumentLoadDelayReason::ParserDeferredScript {
Expand All @@ -30,15 +43,30 @@ impl DocumentLifecycleBlockers {
}
}

pub(super) fn release(
pub(super) fn settle(
&mut self,
token: DocumentLoadDelayTokenId,
reason: DocumentLoadDelayReason,
) -> bool {
) -> DocumentLoadDelaySettlement {
match self.state {
DocumentLoadDelayState::Invalidated => {
return if self.owns_exact_token(token, reason) {
DocumentLoadDelaySettlement::Invalidated
} else {
DocumentLoadDelaySettlement::NotOwned
};
}
DocumentLoadDelayState::Retired => return DocumentLoadDelaySettlement::NotOwned,
DocumentLoadDelayState::Open => {}
}
if reason == DocumentLoadDelayReason::ParserDeferredScript {
self.parser_deferred_scripts.remove(&token)
if self.parser_deferred_scripts.remove(&token) {
DocumentLoadDelaySettlement::Released
} else {
DocumentLoadDelaySettlement::NotOwned
}
} else {
self.window_load.release(token, reason).released()
self.window_load.release(token, reason).settlement()
}
}

Expand All @@ -48,13 +76,32 @@ impl DocumentLifecycleBlockers {
reason: DocumentLoadDelayReason,
) -> DocumentLoadGateRelease {
debug_assert!(reason.blocks_window_load_directly());
match self.state {
DocumentLoadDelayState::Invalidated => {
return if self.owns_exact_token(token, reason) {
DocumentLoadGateRelease::Invalidated
} else {
DocumentLoadGateRelease::NotOwned
};
}
DocumentLoadDelayState::Retired => return DocumentLoadGateRelease::NotOwned,
DocumentLoadDelayState::Open => {}
}
self.window_load.release(token, reason)
}

pub(super) fn owns(
&self,
token: DocumentLoadDelayTokenId,
reason: DocumentLoadDelayReason,
) -> bool {
self.state == DocumentLoadDelayState::Open && self.owns_exact_token(token, reason)
}

fn owns_exact_token(
&self,
token: DocumentLoadDelayTokenId,
reason: DocumentLoadDelayReason,
) -> bool {
if reason == DocumentLoadDelayReason::ParserDeferredScript {
self.parser_deferred_scripts.contains(&token)
Expand All @@ -68,6 +115,9 @@ impl DocumentLifecycleBlockers {
}

pub(super) fn has_reason(&self, reason: DocumentLoadDelayReason) -> bool {
if self.state != DocumentLoadDelayState::Open {
return false;
}
if reason == DocumentLoadDelayReason::ParserDeferredScript {
!self.parser_deferred_scripts.is_empty()
} else {
Expand All @@ -76,27 +126,43 @@ impl DocumentLifecycleBlockers {
}

pub(super) fn blocks_domcontentloaded(&self) -> bool {
!self.parser_deferred_scripts.is_empty()
self.state == DocumentLoadDelayState::Open && !self.parser_deferred_scripts.is_empty()
}

pub(super) fn blocks_window_load_directly(&self) -> bool {
self.window_load.is_blocked()
self.state == DocumentLoadDelayState::Open && self.window_load.is_blocked()
}

pub(super) fn release_all_document_script_delays(&mut self) -> usize {
if self.state != DocumentLoadDelayState::Open {
return 0;
}
let parser_deferred = self.parser_deferred_scripts.len();
self.parser_deferred_scripts.clear();
parser_deferred + self.window_load.release_all_document_script_delays()
}

pub(super) fn clear_for_retirement(&mut self) {
self.state = DocumentLoadDelayState::Retired;
self.parser_deferred_scripts.clear();
self.window_load.clear();
}

/// End all outstanding delays while their Document remains current.
/// Resource tasks may still finish, but no longer own a gate transition.
pub(super) fn invalidate_all(&mut self) {
if self.state != DocumentLoadDelayState::Retired {
self.state = DocumentLoadDelayState::Invalidated;
}
}

#[cfg(test)]
pub(super) fn len(&self) -> usize {
self.parser_deferred_scripts.len() + self.window_load.len()
if self.state == DocumentLoadDelayState::Open {
self.parser_deferred_scripts.len() + self.window_load.len()
} else {
0
}
}
}

Expand All @@ -116,11 +182,97 @@ mod tests {
assert!(blockers.blocks_domcontentloaded());
assert!(blockers.blocks_window_load_directly());

assert!(blockers.release(parser, DocumentLoadDelayReason::ParserDeferredScript));
assert_eq!(
blockers.settle(parser, DocumentLoadDelayReason::ParserDeferredScript),
DocumentLoadDelaySettlement::Released
);
assert!(!blockers.blocks_domcontentloaded());
assert_eq!(
blockers.release_window_load(window, DocumentLoadDelayReason::AsyncModuleScript),
DocumentLoadGateRelease::BecameUnblocked
);
}

#[test]
fn invalidation_settles_every_resource_kind_without_a_new_gate_transition() {
let reasons = [
DocumentLoadDelayReason::AsyncClassicScript,
DocumentLoadDelayReason::BlockingStylesheet,
DocumentLoadDelayReason::StylesheetSubresource,
DocumentLoadDelayReason::ParserDeferredScript,
DocumentLoadDelayReason::AsyncModuleScript,
DocumentLoadDelayReason::Image,
DocumentLoadDelayReason::Media,
DocumentLoadDelayReason::StyleLoadEvent,
DocumentLoadDelayReason::Navigation,
];
let mut blockers = DocumentLifecycleBlockers::default();
for (index, reason) in reasons.iter().copied().enumerate() {
assert!(blockers.acquire(DocumentLoadDelayTokenId(index as u64), reason));
}
assert!(blockers.blocks_domcontentloaded());
assert!(blockers.blocks_window_load_directly());

blockers.invalidate_all();
blockers.invalidate_all();
assert!(!blockers.blocks_domcontentloaded());
assert!(!blockers.blocks_window_load_directly());
assert_eq!(blockers.len(), 0);
for (index, reason) in reasons.iter().copied().enumerate() {
let token = DocumentLoadDelayTokenId(index as u64);
assert!(!blockers.owns(token, reason));
assert!(!blockers.acquire(token, reason));
for _ in 0..2 {
assert_eq!(
blockers.settle(token, reason),
DocumentLoadDelaySettlement::Invalidated
);
}
if reason.blocks_window_load_directly() {
assert_eq!(
blockers.release_window_load(token, reason),
DocumentLoadGateRelease::Invalidated
);
}
}
assert!(!blockers.blocks_domcontentloaded());
assert!(!blockers.blocks_window_load_directly());

blockers.clear_for_retirement();
for (index, reason) in reasons.iter().copied().enumerate() {
assert!(!blockers.owns_any(DocumentLoadDelayTokenId(index as u64)));
assert_eq!(
blockers.settle(DocumentLoadDelayTokenId(index as u64), reason),
DocumentLoadDelaySettlement::NotOwned
);
}
}

#[test]
fn invalidation_does_not_authorize_unknown_wrong_reason_or_already_released_tokens() {
let mut blockers = DocumentLifecycleBlockers::default();
let released = DocumentLoadDelayTokenId(1);
let outstanding = DocumentLoadDelayTokenId(2);
assert!(blockers.acquire(released, DocumentLoadDelayReason::StyleLoadEvent));
assert!(blockers.acquire(outstanding, DocumentLoadDelayReason::Image));
assert_eq!(
blockers.settle(released, DocumentLoadDelayReason::StyleLoadEvent),
DocumentLoadDelaySettlement::Released
);
blockers.invalidate_all();
for token in [released, outstanding, DocumentLoadDelayTokenId(3)] {
assert_eq!(
blockers.settle(token, DocumentLoadDelayReason::StyleLoadEvent),
DocumentLoadDelaySettlement::NotOwned
);
}
assert_eq!(
blockers.release_window_load(outstanding, DocumentLoadDelayReason::StyleLoadEvent),
DocumentLoadGateRelease::NotOwned
);
assert_eq!(
blockers.settle(outstanding, DocumentLoadDelayReason::Image),
DocumentLoadDelaySettlement::Invalidated
);
}
}
4 changes: 3 additions & 1 deletion moli-renderer-v8/src/frame_owner_model/lifecycle_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ pub(crate) enum MainDocumentScriptLoadDelayRelease {
/// `load` had already completed when the script was admitted, so the
/// lease intentionally carried no gate token.
AlreadyUnblocked,
/// The Document invalidated this exact lease before its terminal body.
Invalidated,
/// The exact token was released, while another load blocker remains.
StillBlocked,
/// The exact token was the final entry in the Document load gate.
BecameUnblocked,
}

impl MainDocumentScriptLoadDelayRelease {
pub(crate) const fn released(self) -> bool {
pub(crate) const fn settled(self) -> bool {
!matches!(self, Self::NotOwned)
}
}
Expand Down
Loading
Loading