Skip to content

Commit

Permalink
implement sync_ipc_handler for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Arc-blroth committed Aug 27, 2022
1 parent ecc7d97 commit c58142a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tempfile = "3.3.0"
http-range = "0.1.5"

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
webkit2gtk = { version = "0.18", features = [ "v2_22" ] }
webkit2gtk = { version = "0.18", features = [ "v2_24" ] }
webkit2gtk-sys = "0.18"
gio = "0.15"
glib = "0.15"
Expand Down
29 changes: 25 additions & 4 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use gio::Cancellable;
use glib::signal::Inhibit;
use gtk::prelude::*;
use webkit2gtk::{
traits::*, NavigationPolicyDecision, PolicyDecisionType, UserContentInjectedFrames, UserScript,
UserScriptInjectionTime, WebView, WebViewBuilder,
traits::*, NavigationPolicyDecision, PolicyDecisionType, ScriptDialogType,
UserContentInjectedFrames, UserScript, UserScriptInjectionTime, WebView, WebViewBuilder,
};
use webkit2gtk_sys::{
webkit_get_major_version, webkit_get_micro_version, webkit_get_minor_version,
Expand Down Expand Up @@ -99,6 +99,24 @@ impl InnerWebView {
// Register the handler we just connected
manager.register_script_message_handler(&window_hash);

// Sync message handler
let sync_ipc_w = window_rc.clone();
let sync_ipc_marker = format!("__WRY_SYNC_IPC_{window_hash}__");
let sync_ipc_marker_clone = sync_ipc_marker.clone();
let sync_ipc_handler = attributes.sync_ipc_handler.take();
webview.connect_script_dialog(move |_webview, dialog| {
if dialog.dialog_type() == ScriptDialogType::Prompt {
if let Some(message) = dialog.message() && message.starts_with(&sync_ipc_marker) {
if let Some(sync_ipc_handler) = &sync_ipc_handler {
let payload = message[(&sync_ipc_marker).len()..].to_owned();
dialog.prompt_set_text(&sync_ipc_handler(&sync_ipc_w, payload));
return true;
}
}
}
return false;
});

// Allow the webview to close it's own window
let close_window = window_rc.clone();
webview.connect_close(move |_| {
Expand Down Expand Up @@ -306,11 +324,14 @@ impl InnerWebView {
is_inspector_open,
};

// Initialize message handler
// Initialize message handlers
let mut init = String::with_capacity(115 + 20 + 22);
init.push_str("(function(){const PROMPT=window.prompt;");
init.push_str("Object.defineProperty(window, 'ipc', {value: Object.freeze({postMessage:function(x){window.webkit.messageHandlers[\"");
init.push_str(&window_hash);
init.push_str("\"].postMessage(x)}})})");
init.push_str("\"].postMessage(x)},postSyncMessage:function(x){PROMPT(\"");
init.push_str(&sync_ipc_marker_clone);
init.push_str("\"+x)}})})})()");
w.init(&init)?;

// Initialize scripts
Expand Down

0 comments on commit c58142a

Please sign in to comment.