From 93dcf9bb863fcabc7d8c3457fd3e01d8864212a6 Mon Sep 17 00:00:00 2001 From: TG Date: Mon, 19 May 2025 10:46:31 +0100 Subject: [PATCH] add is_online member to ScriptThread and GlobalScope, and make it an argument to GlobalScope::new_inherited, Window::new and DedicatedWorkerGlobalScope::new and anywhere else that transitively calls GlobalScope::new_inherited Signed-off-by: TG --- components/script/dom/dissimilaroriginwindow.rs | 1 + components/script/dom/globalscope.rs | 11 +++++++++++ components/script/dom/window.rs | 10 ++++++++++ components/script/dom/workerglobalscope.rs | 3 ++- components/script/dom/workletglobalscope.rs | 3 ++- components/script/script_thread.rs | 12 +++++++++--- 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/components/script/dom/dissimilaroriginwindow.rs b/components/script/dom/dissimilaroriginwindow.rs index 70c384db822..b5af443a1a4 100644 --- a/components/script/dom/dissimilaroriginwindow.rs +++ b/components/script/dom/dissimilaroriginwindow.rs @@ -68,6 +68,7 @@ impl DissimilarOriginWindow { global_to_clone_from.wgpu_id_hub(), Some(global_to_clone_from.is_secure_context()), false, + global_to_clone_from.is_online().clone(), ), window_proxy: Dom::from_ref(window_proxy), location: Default::default(), diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index 55db2e4d248..fcf708c3339 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -374,6 +374,11 @@ pub(crate) struct GlobalScope { #[ignore_malloc_size_of = "Rc is hard"] notification_permission_request_callback_map: DomRefCell>>, + + /// Switch offline and online events + #[no_trace] + #[ignore_malloc_size_of = "Arc> does not implement MallocSizeOf"] + is_online: Arc>, } /// A wrapper for glue-code between the ipc router and the event-loop. @@ -735,6 +740,7 @@ impl GlobalScope { #[cfg(feature = "webgpu")] gpu_id_hub: Arc, inherited_secure_context: Option, unminify_js: bool, + is_online: Arc>, ) -> Self { Self { task_manager: Default::default(), @@ -779,6 +785,7 @@ impl GlobalScope { byte_length_queuing_strategy_size_function: OnceCell::new(), count_queuing_strategy_size_function: OnceCell::new(), notification_permission_request_callback_map: Default::default(), + is_online, } } @@ -3446,6 +3453,10 @@ impl GlobalScope { unreachable!(); } + pub(crate) fn is_online(&self) -> Arc { + Arc::clone(&self.is_online) + } + /// #[allow(unsafe_code)] pub(crate) fn report_csp_violations( diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 24e694b4f06..4b7ae8bf3f2 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -398,6 +398,12 @@ pub(crate) struct Window { /// current_event: DomRefCell>>, + + + /// Switch offline and online events + #[no_trace] + #[ignore_malloc_size_of = "Arc> does not implement MallocSizeOf"] + is_online: Arc>, } impl Window { @@ -3033,6 +3039,7 @@ impl Window { player_context: WindowGLContext, #[cfg(feature = "webgpu")] gpu_id_hub: Arc, inherited_secure_context: Option, + is_online: Arc>, ) -> DomRoot { let error_reporter = CSSErrorReporter { pipelineid: pipeline_id, @@ -3060,6 +3067,8 @@ impl Window { gpu_id_hub, inherited_secure_context, unminify_js, + is_online, + ), script_chan, layout: RefCell::new(layout), @@ -3120,6 +3129,7 @@ impl Window { current_event: DomRefCell::new(None), theme: Cell::new(PrefersColorScheme::Light), trusted_types: Default::default(), + is_online: Arc::new(Mutex::new(true)), }); unsafe { diff --git a/components/script/dom/workerglobalscope.rs b/components/script/dom/workerglobalscope.rs index fa94dcc1d04..c522997c7fa 100644 --- a/components/script/dom/workerglobalscope.rs +++ b/components/script/dom/workerglobalscope.rs @@ -5,7 +5,7 @@ use std::cell::{RefCell, RefMut}; use std::default::Default; use std::rc::Rc; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; @@ -174,6 +174,7 @@ impl WorkerGlobalScope { gpu_id_hub, init.inherited_secure_context, false, + Arc::new(Mutex::new(true)), ), worker_id: init.worker_id, worker_name, diff --git a/components/script/dom/workletglobalscope.rs b/components/script/dom/workletglobalscope.rs index c478830ac0c..27337aa338c 100644 --- a/components/script/dom/workletglobalscope.rs +++ b/components/script/dom/workletglobalscope.rs @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use base::id::PipelineId; use constellation_traits::{ScriptToConstellationChan, ScriptToConstellationMessage}; @@ -110,6 +110,7 @@ impl WorkletGlobalScope { init.gpu_id_hub.clone(), init.inherited_secure_context, false, + Arc::new(Mutex::new(true)), ), base_url, to_script_thread_sender: init.to_script_thread_sender.clone(), diff --git a/components/script/script_thread.rs b/components/script/script_thread.rs index 3ee5bfbd662..a4c9c077da3 100644 --- a/components/script/script_thread.rs +++ b/components/script/script_thread.rs @@ -1,5 +1,5 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this + +/* License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! The script thread is the thread that owns the DOM in memory, runs JavaScript, and triggers @@ -23,7 +23,7 @@ use std::default::Default; use std::option::Option; use std::rc::Rc; use std::result::Result; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, Ordering}; use std::thread; use std::time::{Duration, Instant, SystemTime}; @@ -336,6 +336,10 @@ pub struct ScriptThread { /// The screen coordinates where the primary mouse button was pressed. #[no_trace] relative_mouse_down_point: Cell>, + + /// Switch offline and online events + #[no_trace] + is_online: Arc>, } struct BHMExitSignal { @@ -957,6 +961,7 @@ impl ScriptThread { inherited_secure_context: state.inherited_secure_context, layout_factory, relative_mouse_down_point: Cell::new(Point2D::zero()), + is_online: Arc::new(Mutex::new(true)), } } @@ -3224,6 +3229,7 @@ impl ScriptThread { #[cfg(feature = "webgpu")] self.gpu_id_hub.clone(), incomplete.load_data.inherited_secure_context, + self.is_online.clone(), ); let _realm = enter_realm(&*window);