Skip to content

Commit 26da7ae

Browse files
Cleanup
1 parent 1204656 commit 26da7ae

4 files changed

Lines changed: 104 additions & 120 deletions

File tree

desktop/src/cef/context/builder.rs

Lines changed: 82 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ unsafe impl<H: CefEventHandler> Send for CefContextBuilder<H> {}
2424

2525
impl<H: CefEventHandler> CefContextBuilder<H> {
2626
pub(crate) fn new() -> Self {
27-
Self::new_inner(false)
27+
Self::new_impl(false)
2828
}
29-
3029
pub(crate) fn new_helper() -> Self {
31-
Self::new_inner(true)
30+
Self::new_impl(true)
3231
}
3332

34-
fn new_inner(helper: bool) -> Self {
33+
fn new_impl(helper: bool) -> Self {
3534
#[cfg(target_os = "macos")]
3635
let _loader = {
3736
let loader = cef::library_loader::LibraryLoader::new(&std::env::current_exe().unwrap(), helper);
@@ -42,11 +41,8 @@ impl<H: CefEventHandler> CefContextBuilder<H> {
4241
let _ = helper;
4342

4443
let _ = api_hash(CEF_API_VERSION_LAST, 0);
45-
4644
let args = Args::new();
47-
let cmd = args.as_cmd_line().unwrap();
48-
let is_sub_process = cmd.has_switch(Some(&"type".into())) == 1;
49-
45+
let is_sub_process = args.as_cmd_line().unwrap().has_switch(Some(&"type".into())) == 1;
5046
Self {
5147
args,
5248
is_sub_process,
@@ -70,103 +66,96 @@ impl<H: CefEventHandler> CefContextBuilder<H> {
7066
}
7167
}
7268

73-
fn common_settings(instance_dir: &Path) -> Settings {
74-
let log_severity = match std::env::var("GRAPHITE_BROWSER_LOG") {
75-
Ok(level) => match level.to_lowercase().as_str() {
76-
"debug" => LogSeverity::from(cef_log_severity_t::LOGSEVERITY_VERBOSE),
77-
"info" => LogSeverity::from(cef_log_severity_t::LOGSEVERITY_INFO),
78-
"warn" => LogSeverity::from(cef_log_severity_t::LOGSEVERITY_WARNING),
79-
"error" => LogSeverity::from(cef_log_severity_t::LOGSEVERITY_ERROR),
80-
"none" => LogSeverity::from(cef_log_severity_t::LOGSEVERITY_DISABLE),
81-
_ => LogSeverity::from(cef_log_severity_t::LOGSEVERITY_FATAL),
82-
},
83-
Err(_) => LogSeverity::from(cef_log_severity_t::LOGSEVERITY_FATAL),
84-
};
85-
86-
Settings {
87-
windowless_rendering_enabled: 1,
88-
root_cache_path: instance_dir.to_str().map(CefString::from).unwrap(),
89-
cache_path: "".into(),
90-
disable_signal_handlers: 1,
91-
log_severity,
92-
..Default::default()
93-
}
94-
}
95-
9669
#[cfg(target_os = "macos")]
97-
pub(crate) fn initialize(self, event_handler: H, disable_gpu_acceleration: bool) -> Result<impl CefContext, InitError> {
70+
pub(crate) fn create(self, event_handler: H, disable_gpu_acceleration: bool) -> Result<impl CefContext, InitError> {
9871
let instance_dir = TempDir::new().expect("Failed to create temporary directory for CEF instance");
99-
100-
let exe = std::env::current_exe().expect("cannot get current exe path");
101-
let app_root = exe.parent().and_then(|p| p.parent()).expect("bad path structure").parent().expect("bad path structure");
102-
103-
let settings = Settings {
104-
main_bundle_path: app_root.to_str().map(CefString::from).unwrap(),
105-
multi_threaded_message_loop: 0,
106-
external_message_pump: 1,
107-
no_sandbox: 1, // GPU helper crashes when running with sandbox
108-
..Self::common_settings(instance_dir.as_ref())
109-
};
110-
111-
self.initialize_inner(&event_handler, settings)?;
112-
113-
create_browser(event_handler, instance_dir, disable_gpu_acceleration)
72+
let accelerated_paint = accelerated_paint(disable_gpu_acceleration);
73+
self.build_inner(&event_handler, instance_dir.as_ref(), accelerated_paint)?;
74+
create_browser(event_handler, instance_dir, accelerated_paint)
11475
}
11576

11677
#[cfg(not(target_os = "macos"))]
117-
pub(crate) fn initialize(self, event_handler: H, disable_gpu_acceleration: bool) -> Result<impl CefContext, InitError> {
78+
pub(crate) fn create(self, event_handler: H, disable_gpu_acceleration: bool) -> Result<impl CefContext, InitError> {
11879
let instance_dir = TempDir::new().expect("Failed to create temporary directory for CEF instance");
119-
120-
let settings = Settings {
121-
multi_threaded_message_loop: 1,
122-
#[cfg(target_os = "linux")]
123-
no_sandbox: 1,
124-
..Self::common_settings(instance_dir.as_ref())
125-
};
126-
127-
self.initialize_inner(&event_handler, settings)?;
128-
129-
super::multithreaded::run_on_ui_thread(move || match create_browser(event_handler, instance_dir, disable_gpu_acceleration) {
130-
Ok(context) => {
131-
super::multithreaded::CONTEXT.with(|b| {
132-
*b.borrow_mut() = Some(context);
133-
});
134-
}
135-
Err(e) => {
136-
panic!("Failed to initialize CEF context: {:?}", e);
137-
}
80+
let accelerated_paint = accelerated_paint(disable_gpu_acceleration);
81+
self.build_inner(&event_handler, instance_dir.as_ref(), accelerated_paint)?;
82+
super::multithreaded::run_on_ui_thread(move || match create_browser(event_handler, instance_dir, accelerated_paint) {
83+
Ok(context) => super::multithreaded::CONTEXT.with(|b| *b.borrow_mut() = Some(context)),
84+
Err(e) => panic!("Failed to initialize CEF context: {:?}", e),
13885
});
139-
14086
Ok(super::multithreaded::MultiThreadedCefContextProxy)
14187
}
14288

143-
fn initialize_inner(self, event_handler: &H, settings: Settings) -> Result<(), InitError> {
144-
// Attention! Wrapping this in an extra App is necessary, otherwise the program still compiles but segfaults
145-
let mut cef_app = App::new(BrowserProcessAppImpl::new(event_handler.duplicate()));
146-
147-
let result = cef::initialize(Some(self.args.as_main_args()), Some(&settings), Some(&mut cef_app), std::ptr::null_mut());
89+
fn build_inner(self, event_handler: &H, instance_dir: &Path, accelerated_paint: bool) -> Result<(), InitError> {
90+
let mut cef_app = App::new(BrowserProcessAppImpl::new(event_handler.duplicate(), accelerated_paint));
91+
let result = cef::initialize(Some(self.args.as_main_args()), Some(&platform_settings(instance_dir)), Some(&mut cef_app), std::ptr::null_mut());
14892
if result != 1 {
149-
let cef_exit_code = cef::get_exit_code() as u32;
150-
return Err(InitError::InitializationFailureCode(cef_exit_code));
93+
return Err(InitError::InitializationFailureCode(cef::get_exit_code() as u32));
15194
}
15295
Ok(())
15396
}
15497
}
15598

156-
fn create_browser<H: CefEventHandler>(event_handler: H, instance_dir: TempDir, disable_gpu_acceleration: bool) -> Result<SingleThreadedCefContext, InitError> {
157-
let mut client = Client::new(BrowserProcessClientImpl::new(&event_handler));
158-
99+
fn accelerated_paint(disable_gpu_acceleration: bool) -> bool {
159100
#[cfg(feature = "accelerated_paint")]
160-
let use_accelerated_paint = if disable_gpu_acceleration {
101+
{
102+
!disable_gpu_acceleration && crate::cef::platform::should_enable_hardware_acceleration()
103+
}
104+
#[cfg(not(feature = "accelerated_paint"))]
105+
{
106+
let _ = disable_gpu_acceleration;
161107
false
162-
} else {
163-
crate::cef::platform::should_enable_hardware_acceleration()
108+
}
109+
}
110+
111+
fn platform_settings(instance_dir: &Path) -> Settings {
112+
let log_severity = LogSeverity::from(match std::env::var("GRAPHITE_BROWSER_LOG").as_deref() {
113+
Ok("debug") => cef_log_severity_t::LOGSEVERITY_VERBOSE,
114+
Ok("info") => cef_log_severity_t::LOGSEVERITY_INFO,
115+
Ok("warn") => cef_log_severity_t::LOGSEVERITY_WARNING,
116+
Ok("error") => cef_log_severity_t::LOGSEVERITY_ERROR,
117+
Ok("none") => cef_log_severity_t::LOGSEVERITY_DISABLE,
118+
_ => cef_log_severity_t::LOGSEVERITY_FATAL,
119+
});
120+
121+
let base = Settings {
122+
windowless_rendering_enabled: 1,
123+
root_cache_path: instance_dir.to_str().map(CefString::from).unwrap(),
124+
cache_path: "".into(),
125+
disable_signal_handlers: 1,
126+
log_severity,
127+
..Default::default()
164128
};
165129

130+
#[cfg(target_os = "macos")]
131+
{
132+
let exe = std::env::current_exe().expect("cannot get current exe path");
133+
let app_root = exe.parent().and_then(|p| p.parent()).expect("bad path structure").parent().expect("bad path structure");
134+
return Settings {
135+
main_bundle_path: app_root.to_str().map(CefString::from).unwrap(),
136+
multi_threaded_message_loop: 0,
137+
external_message_pump: 1,
138+
no_sandbox: 1, // GPU helper crashes when running with sandbox
139+
..base
140+
};
141+
}
142+
143+
#[cfg(not(target_os = "macos"))]
144+
Settings {
145+
multi_threaded_message_loop: 1,
146+
#[cfg(target_os = "linux")]
147+
no_sandbox: 1,
148+
..base
149+
}
150+
}
151+
152+
fn create_browser<H: CefEventHandler>(event_handler: H, instance_dir: TempDir, accelerated_paint: bool) -> Result<SingleThreadedCefContext, InitError> {
153+
let mut client = Client::new(BrowserProcessClientImpl::new(&event_handler));
154+
166155
let window_info = WindowInfo {
167156
windowless_rendering_enabled: 1,
168157
#[cfg(feature = "accelerated_paint")]
169-
shared_texture_enabled: use_accelerated_paint as i32,
158+
shared_texture_enabled: accelerated_paint as i32,
170159
..Default::default()
171160
};
172161

@@ -192,27 +181,24 @@ fn create_browser<H: CefEventHandler>(event_handler: H, instance_dir: TempDir, d
192181
incognito_request_context.register_scheme_handler_factory(Some(&RESOURCE_SCHEME.into()), Some(&RESOURCE_DOMAIN.into()), Some(&mut scheme_handler_factory));
193182

194183
let url = format!("{RESOURCE_SCHEME}://{RESOURCE_DOMAIN}/");
195-
196-
let browser = browser_host_create_browser_sync(
184+
browser_host_create_browser_sync(
197185
Some(&window_info),
198186
Some(&mut client),
199187
Some(&url.as_str().into()),
200188
Some(&settings),
201189
Option::<&mut DictionaryValue>::None,
202190
Some(&mut incognito_request_context),
203-
);
204-
205-
if let Some(browser) = browser {
206-
Ok(SingleThreadedCefContext {
207-
event_handler: Box::new(event_handler),
208-
browser,
209-
input_state: InputState::default(),
210-
_instance_dir: instance_dir,
211-
})
212-
} else {
191+
)
192+
.map(|browser| SingleThreadedCefContext {
193+
event_handler: Box::new(event_handler),
194+
browser,
195+
input_state: InputState::default(),
196+
_instance_dir: instance_dir,
197+
})
198+
.ok_or_else(|| {
213199
tracing::error!("Failed to create browser");
214-
Err(InitError::BrowserCreationFailed)
215-
}
200+
InitError::BrowserCreationFailed
201+
})
216202
}
217203

218204
#[derive(thiserror::Error, Debug)]

desktop/src/cef/internal/browser_process_app.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ use crate::cef::CefEventHandler;
99
pub(crate) struct BrowserProcessAppImpl<H: CefEventHandler> {
1010
object: *mut RcImpl<_cef_app_t, Self>,
1111
event_handler: H,
12+
accelerated_paint: bool,
1213
}
1314
impl<H: CefEventHandler> BrowserProcessAppImpl<H> {
14-
pub(crate) fn new(event_handler: H) -> Self {
15+
pub(crate) fn new(event_handler: H, accelerated_paint: bool) -> Self {
1516
Self {
1617
object: std::ptr::null_mut(),
1718
event_handler,
19+
accelerated_paint,
1820
}
1921
}
2022
}
@@ -61,35 +63,30 @@ impl<H: CefEventHandler> ImplApp for BrowserProcessAppImpl<H> {
6163
disabled_features.extend(extra_disabled_features.into_iter().map(ToOwned::to_owned));
6264
cmd.append_switch_with_value(disabled_features_switch, Some(&disabled_features.join(",").as_str().into()));
6365

64-
#[cfg(not(feature = "accelerated_paint"))]
65-
{
66-
// Disable GPU acceleration when accelerated_paint feature is not enabled
67-
cmd.append_switch(Some(&"disable-gpu".into()));
68-
cmd.append_switch(Some(&"disable-gpu-compositing".into()));
69-
}
70-
71-
#[cfg(feature = "accelerated_paint")]
72-
{
66+
if self.accelerated_paint {
7367
cmd.append_switch(Some(&"enable-gpu".into()));
7468
cmd.append_switch(Some(&"enable-gpu-compositing".into()));
7569
cmd.append_switch(Some(&"enable-begin-frame-scheduling".into()));
7670
cmd.append_switch(Some(&"off-screen-rendering-enabled".into()));
7771
cmd.append_switch(Some(&"enable-accelerated-2d-canvas".into()));
78-
}
7972

80-
#[cfg(all(feature = "accelerated_paint", target_os = "linux"))]
81-
{
82-
cmd.append_switch_with_value(Some(&"use-angle".into()), Some(&"gl-egl".into()));
73+
#[cfg(target_os = "linux")]
74+
{
75+
cmd.append_switch_with_value(Some(&"use-angle".into()), Some(&"gl-egl".into()));
8376

84-
let use_wayland = std::env::var("WAYLAND_DISPLAY")
85-
.ok()
86-
.filter(|var| !var.is_empty())
87-
.or_else(|| std::env::var("WAYLAND_SOCKET").ok())
88-
.filter(|var| !var.is_empty())
89-
.is_some();
90-
if use_wayland {
91-
cmd.append_switch_with_value(Some(&"ozone-platform".into()), Some(&"wayland".into()));
77+
let use_wayland = std::env::var("WAYLAND_DISPLAY")
78+
.ok()
79+
.filter(|var| !var.is_empty())
80+
.or_else(|| std::env::var("WAYLAND_SOCKET").ok())
81+
.filter(|var| !var.is_empty())
82+
.is_some();
83+
if use_wayland {
84+
cmd.append_switch_with_value(Some(&"ozone-platform".into()), Some(&"wayland".into()));
85+
}
9286
}
87+
} else {
88+
cmd.append_switch(Some(&"disable-gpu".into()));
89+
cmd.append_switch(Some(&"disable-gpu-compositing".into()));
9390
}
9491

9592
#[cfg(target_os = "macos")]
@@ -122,6 +119,7 @@ impl<H: CefEventHandler> Clone for BrowserProcessAppImpl<H> {
122119
Self {
123120
object: self.object,
124121
event_handler: self.event_handler.duplicate(),
122+
accelerated_paint: self.accelerated_paint,
125123
}
126124
}
127125
}

desktop/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn start() {
8686
}
8787

8888
let cef_handler = cef::CefHandler::new(wgpu_context.clone(), app_event_scheduler.clone(), cef_view_info_receiver);
89-
let cef_context = match cef_context_builder.initialize(cef_handler, disable_ui_acceleration) {
89+
let cef_context = match cef_context_builder.create(cef_handler, disable_ui_acceleration) {
9090
Ok(context) => {
9191
tracing::info!("CEF initialized successfully");
9292
context

desktop/src/persist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(crate) fn write_state(state: PersistedState) {
3737
if let Err(e) = std::fs::write(state_file_path(), data) {
3838
tracing::error!("Failed to write persistent data to disk: {e}");
3939
}
40-
garbage_collect_document_files(&state);
40+
garbage_collect_document_files(state);
4141
}
4242

4343
pub(crate) fn write_document_content(id: DocumentId, document_content: String) {

0 commit comments

Comments
 (0)