Skip to content

Commit 1f0455d

Browse files
committed
fix: remove the library retain logic
1 parent 062bb03 commit 1f0455d

1 file changed

Lines changed: 48 additions & 14 deletions

File tree

crates/gdcef/src/cef_init.rs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ struct CefState {
1616
initialized: bool,
1717
}
1818

19+
impl CefState {
20+
fn needs_initialize(&self) -> bool {
21+
!self.initialized
22+
}
23+
24+
fn mark_initialized(&mut self) {
25+
self.initialized = true;
26+
}
27+
28+
fn retain(&mut self) {
29+
self.ref_count += 1;
30+
}
31+
32+
fn release(&mut self) {
33+
if self.ref_count == 0 {
34+
return;
35+
}
36+
37+
self.ref_count -= 1;
38+
// CEF does not support a reliable initialize -> shutdown -> initialize cycle
39+
// within the same process. Keep the process-wide CEF runtime alive after the
40+
// last browser instance is released so a later CefTexture can be created.
41+
}
42+
}
43+
1944
static CEF_STATE: Mutex<CefState> = Mutex::new(CefState {
2045
ref_count: 0,
2146
initialized: false,
@@ -36,33 +61,23 @@ fn lock_cef_state() -> MutexGuard<'static, CefState> {
3661
pub fn cef_retain() -> CefResult<()> {
3762
let mut state = lock_cef_state();
3863

39-
if state.ref_count == 0 {
64+
if state.needs_initialize() {
4065
load_cef_framework()?;
4166
cef::api_hash(cef::sys::CEF_API_VERSION_LAST, 0);
4267
initialize_cef()?;
43-
state.initialized = true;
68+
state.mark_initialized();
4469

4570
settings::warn_if_insecure_settings();
4671
settings::log_production_security_baseline();
4772
}
4873

49-
state.ref_count += 1;
74+
state.retain();
5075
Ok(())
5176
}
5277

5378
pub fn cef_release() {
5479
let mut state = lock_cef_state();
55-
56-
if state.ref_count == 0 {
57-
return;
58-
}
59-
60-
state.ref_count -= 1;
61-
62-
if state.ref_count == 0 && state.initialized {
63-
cef::shutdown();
64-
state.initialized = false;
65-
}
80+
state.release();
6681
}
6782

6883
/// Loads the CEF framework library (macOS-specific)
@@ -256,3 +271,22 @@ fn initialize_cef() -> CefResult<()> {
256271

257272
Ok(())
258273
}
274+
275+
#[cfg(test)]
276+
mod tests {
277+
use super::*;
278+
279+
#[test]
280+
fn releasing_last_reference_keeps_cef_initialized_for_recreate() {
281+
let mut state = CefState {
282+
ref_count: 1,
283+
initialized: true,
284+
};
285+
286+
state.release();
287+
288+
assert_eq!(state.ref_count, 0);
289+
assert!(state.initialized);
290+
assert!(!state.needs_initialize());
291+
}
292+
}

0 commit comments

Comments
 (0)