toplevel_info: Fix racy data creation

This commit is contained in:
Victoria Brekenfeld 2024-10-24 19:56:20 +02:00 • committed by Victoria Brekenfeld
parent 5207453157
commit 743e5d4360

View file

@ -90,7 +90,7 @@ pub struct ToplevelHandleStateInner<W: Window> {
title: String, title: String,
app_id: String, app_id: String,
states: Vec<States>, states: Vec<States>,
pub(super) window: W, pub(super) window: Option<W>,
} }
pub type ToplevelHandleState<W> = Mutex<ToplevelHandleStateInner<W>>; pub type ToplevelHandleState<W> = Mutex<ToplevelHandleStateInner<W>>;
@ -104,7 +104,20 @@ impl<W: Window> ToplevelHandleStateInner<W> {
title: String::new(), title: String::new(),
app_id: String::new(), app_id: String::new(),
states: Vec::new(), states: Vec::new(),
window: window.clone(), window: Some(window.clone()),
})
}
fn empty() -> ToplevelHandleState<W> {
ToplevelHandleState::new(ToplevelHandleStateInner {
outputs: Vec::new(),
geometry: None,
wl_outputs: HashSet::new(),
workspaces: Vec::new(),
title: String::new(),
app_id: String::new(),
states: Vec::new(),
window: None,
}) })
} }
} }
@ -187,6 +200,10 @@ where
.instances .instances
.push(instance); .push(instance);
} else { } else {
let _ = data_init.init(
cosmic_toplevel,
ToplevelHandleStateInner::empty(),
);
error!(?foreign_toplevel, "Toplevel for foreign-toplevel-list not registered for cosmic-toplevel-info."); error!(?foreign_toplevel, "Toplevel for foreign-toplevel-list not registered for cosmic-toplevel-info.");
} }
} }
@ -600,7 +617,7 @@ where
pub fn window_from_handle<W: Window + 'static>(handle: ZcosmicToplevelHandleV1) -> Option<W> { pub fn window_from_handle<W: Window + 'static>(handle: ZcosmicToplevelHandleV1) -> Option<W> {
handle handle
.data::<ToplevelHandleState<W>>() .data::<ToplevelHandleState<W>>()
.map(|state| state.lock().unwrap().window.clone()) .and_then(|state| state.lock().unwrap().window.clone())
} }
macro_rules! delegate_toplevel_info { macro_rules! delegate_toplevel_info {