Desktop: Cleaner CEF bidirectional message implementation (#2950)

* Rename CEF implementations to match the process they are called in

* Rename CEF implementations to match the process they are called in

* Implement ipc abstraction

* Call js function `receiveNativeMessage` for all SendToJS ipc messages

* Allow js to call `sendNativeMessage` for sending messages to the browser process

Co-authored-by: Adam <adamgerhant@gmail.com>
Co-authored-by: Dennis Kobert <dennis@kobert.dev>

* Fix missing safety consideration

---------

Co-authored-by: Adam <adamgerhant@gmail.com>
Co-authored-by: Dennis Kobert <dennis@kobert.dev>
This commit is contained in:
Timon
2025-07-28 13:04:15 +00:00
committed by GitHub
co-authored by Adam Dennis Kobert
parent 7bdf1670b5
commit 83d39fb320
12 changed files with 469 additions and 80 deletions
+10 -5
View File
@@ -7,10 +7,11 @@ use winit::event::WindowEvent;
use crate::cef::dirs::{cef_cache_dir, cef_data_dir};
use super::input::InputState;
use super::ipc::{MessageType, SendMessage};
use super::scheme_handler::{FRONTEND_DOMAIN, GRAPHITE_SCHEME};
use super::{CefEventHandler, input};
use super::internal::{AppImpl, ClientImpl, NonBrowserAppImpl, RenderHandlerImpl};
use super::internal::{BrowserProcessAppImpl, BrowserProcessClientImpl, RenderHandlerImpl, RenderProcessAppImpl};
pub(crate) struct Setup {}
pub(crate) struct Initialized {}
@@ -42,7 +43,7 @@ impl Context<Setup> {
if !is_browser_process {
let process_type = CefString::from(&cmd.switch_value(Some(&switch)));
let mut app = NonBrowserAppImpl::app();
let mut app = RenderProcessAppImpl::app();
let ret = execute_process(Some(args.as_main_args()), Some(&mut app), std::ptr::null_mut());
if ret >= 0 {
return Err(SetupError::SubprocessFailed(process_type.to_string()));
@@ -70,7 +71,7 @@ impl Context<Setup> {
};
// Attention! Wrapping this in an extra App is necessary, otherwise the program still compiles but segfaults
let mut cef_app = App::new(AppImpl::new(event_handler.clone()));
let mut cef_app = App::new(BrowserProcessAppImpl::new(event_handler.clone()));
let result = initialize(Some(self.args.as_main_args()), Some(&settings), Some(&mut cef_app), std::ptr::null_mut());
if result != 1 {
@@ -81,8 +82,8 @@ impl Context<Setup> {
return Err(InitError::InitializationFailed(cef_exit_code));
}
let render_handler = RenderHandlerImpl::new(event_handler.clone());
let mut client = Client::new(ClientImpl::new(RenderHandler::new(render_handler)));
let render_handler = RenderHandler::new(RenderHandlerImpl::new(event_handler.clone()));
let mut client = Client::new(BrowserProcessClientImpl::new(render_handler, event_handler.clone()));
let url = CefString::from(format!("{GRAPHITE_SCHEME}://{FRONTEND_DOMAIN}/").as_str());
@@ -129,6 +130,10 @@ impl Context<Initialized> {
browser.host().unwrap().was_resized();
}
}
pub(crate) fn send_web_message(&self, message: &[u8]) {
self.send_message(MessageType::SendToJS, message);
}
}
impl<S: ContextState> Drop for Context<S> {