Skip to content

Commit c3889e8

Browse files
committed
Cleanup + Scheudule cef message loop work
1 parent cbfcd96 commit c3889e8

File tree

10 files changed

+96
-66
lines changed

10 files changed

+96
-66
lines changed

desktop/src/app.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) struct WinitApp {
1919
pub(crate) window_state: WindowStateHandle,
2020
pub(crate) cef_context: cef::Context<cef::Initialized>,
2121
pub(crate) window: Option<Arc<Window>>,
22+
cef_schedule: Instant,
2223
}
2324

2425
impl WinitApp {
@@ -27,18 +28,22 @@ impl WinitApp {
2728
window_state,
2829
cef_context,
2930
window: None,
31+
cef_schedule: Instant::now(),
3032
}
3133
}
3234
}
3335

3436
impl ApplicationHandler<CustomEvent> for WinitApp {
3537
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
36-
event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(2)));
37-
self.cef_context.work();
38+
let timeout = Instant::now() + Duration::from_millis(10);
39+
let wait_until = timeout.min(self.cef_schedule);
40+
event_loop.set_control_flow(ControlFlow::WaitUntil(wait_until));
3841
}
3942

4043
fn new_events(&mut self, _event_loop: &ActiveEventLoop, _cause: StartCause) {
41-
self.cef_context.work();
44+
if Instant::now() > self.cef_schedule {
45+
self.cef_context.work();
46+
}
4247
}
4348

4449
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
@@ -67,11 +72,14 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
6772

6873
fn user_event(&mut self, _: &ActiveEventLoop, event: CustomEvent) {
6974
match event {
70-
CustomEvent::UiUpdate | CustomEvent::Resized => {
75+
CustomEvent::UiUpdate => {
7176
if let Some(window) = &self.window {
7277
window.request_redraw();
7378
}
7479
}
80+
CustomEvent::ScheduleBrowserWork(instant) => {
81+
self.cef_schedule = instant;
82+
}
7583
}
7684
}
7785

@@ -90,9 +98,6 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
9098
let height = physical_size.height as usize;
9199
s.width = Some(width);
92100
s.height = Some(height);
93-
if let Some(elp) = &s.event_loop_proxy {
94-
let _ = elp.send_event(CustomEvent::Resized);
95-
}
96101
if let Some(graphics_state) = &mut s.graphics_state {
97102
graphics_state.resize(width, height);
98103
}

desktop/src/cef/context.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use cef::sys::CEF_API_VERSION_LAST;
2+
use cef::{App, BrowserSettings, Client, DictionaryValue, ImplBrowser, ImplBrowserHost, ImplCommandLine, RenderHandler, RequestContext, WindowInfo, browser_host_create_browser_sync, initialize};
23
use cef::{Browser, CefString, Settings, api_hash, args::Args, execute_process};
3-
use cef::{BrowserSettings, DictionaryValue, ImplBrowser, ImplBrowserHost, ImplCommandLine, RenderHandler, RequestContext, WindowInfo, browser_host_create_browser_sync, initialize};
44
use thiserror::Error;
55
use winit::event::WindowEvent;
66

7-
use super::CefEventHandler;
8-
use super::input::{InputState, handle_window_event};
7+
use super::input::InputState;
8+
use super::scheme_handler::{FRONTEND_DOMAIN, GRAPHITE_SCHEME};
9+
use super::{CefEventHandler, input};
910

1011
use super::internal::{AppImpl, ClientImpl, NonBrowserAppImpl, RenderHandlerImpl};
1112

@@ -21,13 +22,6 @@ pub(crate) struct Context<S: ContextState> {
2122
pub(crate) input_state: InputState,
2223
marker: std::marker::PhantomData<S>,
2324
}
24-
impl<S: ContextState> Drop for Context<S> {
25-
fn drop(&mut self) {
26-
if self.browser.is_some() {
27-
cef::shutdown();
28-
}
29-
}
30-
}
3125

3226
impl Context<Setup> {
3327
pub(crate) fn new() -> Result<Context<Setup>, SetupError> {
@@ -38,22 +32,23 @@ impl Context<Setup> {
3832
loader
3933
};
4034
let _ = api_hash(CEF_API_VERSION_LAST, 0);
35+
4136
let args = Args::new();
4237
let cmd = args.as_cmd_line().unwrap();
4338
let switch = CefString::from("type");
44-
cmd.append_switch(Some(&CefString::from("disable-gpu")));
45-
cmd.append_switch(Some(&CefString::from("disable-gpu-compositing")));
4639
let is_browser_process = cmd.has_switch(Some(&switch)) != 1;
40+
4741
if !is_browser_process {
4842
let process_type = CefString::from(&cmd.switch_value(Some(&switch)));
49-
let mut app = NonBrowserAppImpl::new();
43+
let mut app = NonBrowserAppImpl::app();
5044
let ret = execute_process(Some(args.as_main_args()), Some(&mut app), std::ptr::null_mut());
5145
if ret >= 0 {
5246
return Err(SetupError::SubprocessFailed(process_type.to_string()));
5347
} else {
5448
return Err(SetupError::Subprocess);
5549
}
5650
}
51+
5752
Ok(Context {
5853
args,
5954
browser: None,
@@ -70,17 +65,18 @@ impl Context<Setup> {
7065
..Default::default()
7166
};
7267

73-
let mut cef_app = AppImpl::new(event_handler.clone());
68+
// Attention! Wrapping this in an extra App is necessary, otherwise the program still compiles but segfaults
69+
let mut cef_app = App::new(AppImpl::new(event_handler.clone()));
7470

75-
let res = initialize(Some(self.args.as_main_args()), Some(&settings), Some(&mut cef_app), std::ptr::null_mut());
76-
if res != 1 {
71+
let result = initialize(Some(self.args.as_main_args()), Some(&settings), Some(&mut cef_app), std::ptr::null_mut());
72+
if result != 1 {
7773
return Err(InitError::InitializationFailed);
7874
}
7975

8076
let render_handler = RenderHandlerImpl::new(event_handler.clone());
81-
let mut client = ClientImpl::new(RenderHandler::new(render_handler));
77+
let mut client = Client::new(ClientImpl::new(RenderHandler::new(render_handler)));
8278

83-
let url = CefString::from("graphite://frontend/");
79+
let url = CefString::from(format!("{GRAPHITE_SCHEME}://{FRONTEND_DOMAIN}/").as_str());
8480

8581
let window_info = WindowInfo {
8682
windowless_rendering_enabled: 1,
@@ -117,7 +113,7 @@ impl Context<Initialized> {
117113
}
118114

119115
pub(crate) fn handle_window_event(&mut self, event: WindowEvent) -> Option<WindowEvent> {
120-
handle_window_event(self, event)
116+
input::handle_window_event(self, event)
121117
}
122118

123119
pub(crate) fn notify_of_resize(&self) {
@@ -127,6 +123,14 @@ impl Context<Initialized> {
127123
}
128124
}
129125

126+
impl<S: ContextState> Drop for Context<S> {
127+
fn drop(&mut self) {
128+
if self.browser.is_some() {
129+
cef::shutdown();
130+
}
131+
}
132+
}
133+
130134
#[derive(Error, Debug)]
131135
pub(crate) enum SetupError {
132136
#[error("this is the sub process should exit immediately")]

desktop/src/cef/input/keymap.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ macro_rules! map {
2020
};
2121
}
2222

23+
// Windows Virtual keyboard binary representation
2324
pub(crate) trait ToVKBits {
2425
fn to_vk_bits(&self) -> i32;
2526
}
@@ -205,6 +206,7 @@ impl ToVKBits for char {
205206
}
206207
}
207208

209+
// Chromium dom key binary representation
208210
pub(crate) trait ToDomBits {
209211
fn to_dom_bits(&self) -> i32;
210212
}

desktop/src/cef/internal/app.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cef::rc::{Rc, RcImpl};
22
use cef::sys::{_cef_app_t, cef_base_ref_counted_t};
3-
use cef::{App, BrowserProcessHandler, ImplApp, SchemeRegistrar, WrapApp};
3+
use cef::{BrowserProcessHandler, ImplApp, SchemeRegistrar, WrapApp};
44

55
use crate::cef::CefEventHandler;
66
use crate::cef::scheme_handler::GraphiteSchemeHandlerFactory;
@@ -12,17 +12,17 @@ pub(crate) struct AppImpl<H: CefEventHandler> {
1212
event_handler: H,
1313
}
1414
impl<H: CefEventHandler> AppImpl<H> {
15-
pub(crate) fn new(event_handler: H) -> App {
16-
App::new(Self {
15+
pub(crate) fn new(event_handler: H) -> Self {
16+
Self {
1717
object: std::ptr::null_mut(),
1818
event_handler,
19-
})
19+
}
2020
}
2121
}
2222

2323
impl<H: CefEventHandler> ImplApp for AppImpl<H> {
2424
fn browser_process_handler(&self) -> Option<BrowserProcessHandler> {
25-
Some(BrowserProcessHandlerImpl::new(self.event_handler.clone()))
25+
Some(BrowserProcessHandler::new(BrowserProcessHandlerImpl::new(self.event_handler.clone())))
2626
}
2727

2828
fn on_register_custom_schemes(&self, registrar: Option<&mut SchemeRegistrar>) {

desktop/src/cef/internal/browser_process_handler.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1+
use std::time::{Duration, Instant};
2+
13
use cef::rc::{Rc, RcImpl};
24
use cef::sys::{_cef_browser_process_handler_t, cef_base_ref_counted_t, cef_browser_process_handler_t};
3-
use cef::{BrowserProcessHandler, CefString, ImplBrowserProcessHandler, SchemeHandlerFactory, WrapBrowserProcessHandler};
5+
use cef::{CefString, ImplBrowserProcessHandler, SchemeHandlerFactory, WrapBrowserProcessHandler};
46

57
use crate::cef::CefEventHandler;
6-
use crate::cef::scheme_handler::GraphiteSchemeHandlerFactory;
8+
use crate::cef::scheme_handler::{GRAPHITE_SCHEME, GraphiteSchemeHandlerFactory};
79

810
pub(crate) struct BrowserProcessHandlerImpl<H: CefEventHandler> {
911
object: *mut RcImpl<cef_browser_process_handler_t, Self>,
1012
event_handler: H,
1113
}
1214
impl<H: CefEventHandler> BrowserProcessHandlerImpl<H> {
13-
pub(crate) fn new(event_handler: H) -> BrowserProcessHandler {
14-
BrowserProcessHandler::new(Self {
15+
pub(crate) fn new(event_handler: H) -> Self {
16+
Self {
1517
object: std::ptr::null_mut(),
1618
event_handler,
17-
})
19+
}
1820
}
1921
}
2022

2123
impl<H: CefEventHandler> ImplBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
2224
fn on_context_initialized(&self) {
23-
cef::register_scheme_handler_factory(Some(&CefString::from("graphite")), None, Some(&mut SchemeHandlerFactory::new(GraphiteSchemeHandlerFactory::new())));
25+
cef::register_scheme_handler_factory(Some(&CefString::from(GRAPHITE_SCHEME)), None, Some(&mut SchemeHandlerFactory::new(GraphiteSchemeHandlerFactory::new())));
2426
}
2527

2628
fn get_raw(&self) -> *mut _cef_browser_process_handler_t {
2729
self.object.cast()
2830
}
31+
32+
fn on_schedule_message_pump_work(&self, delay_ms: i64) {
33+
self.event_handler.schedule_cef_message_loop_work(Instant::now() + Duration::from_millis(delay_ms as u64));
34+
}
2935
}
3036

3137
impl<H: CefEventHandler> Clone for BrowserProcessHandlerImpl<H> {

desktop/src/cef/internal/client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use cef::rc::{Rc, RcImpl};
22
use cef::sys::{_cef_client_t, cef_base_ref_counted_t};
3-
use cef::{Client, ImplClient, RenderHandler, WrapClient};
3+
use cef::{ImplClient, RenderHandler, WrapClient};
44

55
pub(crate) struct ClientImpl {
66
object: *mut RcImpl<_cef_client_t, Self>,
77
render_handler: RenderHandler,
88
}
99
impl ClientImpl {
10-
pub(crate) fn new(render_handler: RenderHandler) -> Client {
11-
Client::new(Self {
10+
pub(crate) fn new(render_handler: RenderHandler) -> Self {
11+
Self {
1212
object: std::ptr::null_mut(),
1313
render_handler,
14-
})
14+
}
1515
}
1616
}
1717

desktop/src/cef/internal/non_browser_app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) struct NonBrowserAppImpl {
88
object: *mut RcImpl<_cef_app_t, Self>,
99
}
1010
impl NonBrowserAppImpl {
11-
pub(crate) fn new() -> App {
11+
pub(crate) fn app() -> App {
1212
App::new(Self { object: std::ptr::null_mut() })
1313
}
1414
}

desktop/src/cef/mod.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
mod input;
2-
3-
mod scheme_handler;
1+
use crate::FrameBuffer;
2+
use std::time::Instant;
43

54
mod context;
6-
5+
mod input;
76
mod internal;
7+
mod scheme_handler;
8+
9+
pub(crate) use context::{Context, InitError, Initialized, Setup, SetupError};
810

911
pub(crate) trait CefEventHandler: Clone {
1012
fn window_size(&self) -> WindowSize;
1113
fn draw(&self, frame_buffer: FrameBuffer) -> bool;
14+
/// Scheudule the main event loop to run the cef event loop after the timeout
15+
/// [`_cef_browser_process_handler_t::on_schedule_message_pump_work`] for more documentation.
16+
fn schedule_cef_message_loop_work(&self, scheduled_time: Instant);
1217
}
1318

1419
#[derive(Clone)]
@@ -22,7 +27,3 @@ impl WindowSize {
2227
Self { width, height }
2328
}
2429
}
25-
26-
pub(crate) use context::{Context, InitError, Initialized, Setup, SetupError};
27-
28-
use crate::FrameBuffer;

0 commit comments

Comments
 (0)