Skip to content

Commit 29818de

Browse files
fix(core): use postMessage IPC for remote URLs on macOS closes #7662 (#7751)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
1 parent e98393e commit 29818de

File tree

8 files changed

+58
-20
lines changed

8 files changed

+58
-20
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:bug
3+
---
4+
5+
Fixes IPC failing to communicate for remote URLs on macOS.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ use std::{
9494
fmt,
9595
ops::Deref,
9696
path::PathBuf,
97+
rc::Rc,
9798
sync::{
9899
mpsc::{channel, Sender},
99100
Arc, Mutex, Weak,
@@ -227,7 +228,7 @@ impl<T: UserEvent> Context<T> {
227228
pub struct DispatcherMainThreadContext<T: UserEvent> {
228229
pub window_target: EventLoopWindowTarget<Message<T>>,
229230
pub web_context: WebContextStore,
230-
pub windows: Arc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
231+
pub windows: Rc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
231232
}
232233

233234
impl<T: UserEvent> std::fmt::Debug for DispatcherMainThreadContext<T> {
@@ -1544,7 +1545,7 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
15441545
#[derive(Clone)]
15451546
enum WindowHandle {
15461547
Webview {
1547-
inner: Arc<WebView>,
1548+
inner: Rc<WebView>,
15481549
context_store: WebContextStore,
15491550
// the key of the WebContext if it's not shared
15501551
context_key: Option<PathBuf>,
@@ -1560,7 +1561,7 @@ impl Drop for WindowHandle {
15601561
context_key,
15611562
} = self
15621563
{
1563-
if Arc::get_mut(inner).is_some() {
1564+
if Rc::get_mut(inner).is_some() {
15641565
context_store.lock().unwrap().remove(context_key);
15651566
}
15661567
}
@@ -1816,7 +1817,7 @@ impl<T: UserEvent> Wry<T> {
18161817
let main_thread_id = current_thread().id();
18171818
let web_context = WebContextStore::default();
18181819

1819-
let windows = Arc::new(RefCell::new(HashMap::default()));
1820+
let windows = Rc::new(RefCell::new(HashMap::default()));
18201821
let webview_id_map = WebviewIdStore::default();
18211822

18221823
let context = Context {
@@ -2056,11 +2057,11 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
20562057
pub struct EventLoopIterationContext<'a, T: UserEvent> {
20572058
pub callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
20582059
pub webview_id_map: WebviewIdStore,
2059-
pub windows: Arc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
2060+
pub windows: Rc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
20602061
}
20612062

20622063
struct UserMessageContext {
2063-
windows: Arc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
2064+
windows: Rc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
20642065
webview_id_map: WebviewIdStore,
20652066
}
20662067

@@ -2529,7 +2530,7 @@ fn handle_event_loop<T: UserEvent>(
25292530
fn on_close_requested<'a, T: UserEvent>(
25302531
callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
25312532
window_id: WebviewId,
2532-
windows: Arc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
2533+
windows: Rc<RefCell<HashMap<WebviewId, WindowWrapper>>>,
25332534
) {
25342535
let (tx, rx) = channel();
25352536
let windows_ref = windows.borrow();
@@ -2557,7 +2558,7 @@ fn on_close_requested<'a, T: UserEvent>(
25572558
}
25582559
}
25592560

2560-
fn on_window_close(window_id: WebviewId, windows: Arc<RefCell<HashMap<WebviewId, WindowWrapper>>>) {
2561+
fn on_window_close(window_id: WebviewId, windows: Rc<RefCell<HashMap<WebviewId, WindowWrapper>>>) {
25612562
if let Some(window_wrapper) = windows.borrow_mut().get_mut(&window_id) {
25622563
window_wrapper.inner = None;
25632564
}
@@ -2803,7 +2804,7 @@ fn create_webview<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
28032804
Ok(WindowWrapper {
28042805
label,
28052806
inner: Some(WindowHandle::Webview {
2806-
inner: Arc::new(webview),
2807+
inner: Rc::new(webview),
28072808
context_store: web_context_store.clone(),
28082809
context_key: if automation_enabled {
28092810
None

core/tauri/scripts/ipc-protocol.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,33 @@
1010

1111
Object.defineProperty(window, '__TAURI_POST_MESSAGE__', {
1212
value: (message) => {
13-
const { cmd, callback, error, payload, options } = message
13+
const {
14+
cmd,
15+
callback,
16+
error,
17+
payload,
18+
options
19+
} = message
1420

15-
// use custom protocol for IPC if the flag is set to true, the command is the fetch data command or when not on Linux/Android
16-
if (useCustomProtocol || cmd === fetchChannelDataCommand || (osName !== 'linux' && osName !== 'android')) {
17-
const { contentType, data } = processIpcMessage(payload)
21+
// use custom protocol for IPC if:
22+
// - the flag is set to true or
23+
// - the command is the fetch data command or
24+
// - when not on Linux/Android
25+
// AND
26+
// - when not on macOS with an https URL
27+
if (
28+
(
29+
useCustomProtocol ||
30+
cmd === fetchChannelDataCommand ||
31+
!(osName === 'linux' || osName === 'android')
32+
) &&
33+
!(osName === 'macos' && location.protocol === 'https:')
34+
) {
35+
console.log('process')
36+
const {
37+
contentType,
38+
data
39+
} = processIpcMessage(payload)
1840
fetch(window.__TAURI__.convertFileSrc(cmd, 'ipc'), {
1941
method: 'POST',
2042
body: data,
@@ -44,7 +66,15 @@
4466
})
4567
} else {
4668
// otherwise use the postMessage interface
47-
const { data } = processIpcMessage({ cmd, callback, error, options, payload })
69+
const {
70+
data
71+
} = processIpcMessage({
72+
cmd,
73+
callback,
74+
error,
75+
options,
76+
payload
77+
})
4878
window.ipc.postMessage(data)
4979
}
5080
}

core/tauri/src/ipc/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::{
2121
};
2222

2323
pub(crate) mod channel;
24-
#[cfg(not(ipc_custom_protocol))]
2524
pub(crate) mod format_callback;
2625
pub(crate) mod protocol;
2726

core/tauri/src/ipc/protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::{CallbackFn, InvokeBody, InvokeResponse};
1919
const TAURI_CALLBACK_HEADER_NAME: &str = "Tauri-Callback";
2020
const TAURI_ERROR_HEADER_NAME: &str = "Tauri-Error";
2121

22-
#[cfg(not(ipc_custom_protocol))]
22+
#[cfg(any(target_os = "macos", not(ipc_custom_protocol)))]
2323
pub fn message_handler<R: Runtime>(
2424
manager: WindowManager<R>,
2525
) -> crate::runtime::webview::WebviewIpcHandler<crate::EventLoopMessage, R> {
@@ -87,7 +87,7 @@ pub fn get<R: Runtime>(manager: WindowManager<R>, label: String) -> UriSchemePro
8787
})
8888
}
8989

90-
#[cfg(not(ipc_custom_protocol))]
90+
#[cfg(any(target_os = "macos", not(ipc_custom_protocol)))]
9191
fn handle_ipc_message<R: Runtime>(message: String, manager: &WindowManager<R>, label: &str) {
9292
if let Some(window) = manager.get_window(label) {
9393
use serde::{Deserialize, Deserializer};

core/tauri/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,8 @@ macro_rules! run_main_thread {
919919
let (tx, rx) = channel();
920920
let self_ = $self.clone();
921921
let task = move || {
922-
let _ = tx.send($ex(self_));
922+
let f = $ex;
923+
let _ = tx.send(f(self_));
923924
};
924925
$self.app_handle.run_on_main_thread(Box::new(task))?;
925926
rx.recv().map_err(|_| crate::Error::FailedToReceiveMessage)

core/tauri/src/manager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,8 @@ impl<R: Runtime> WindowManager<R> {
11881188
#[allow(clippy::redundant_clone)]
11891189
app_handle.clone(),
11901190
)?;
1191-
#[cfg(not(ipc_custom_protocol))]
1191+
1192+
#[cfg(any(target_os = "macos", not(ipc_custom_protocol)))]
11921193
{
11931194
pending.ipc_handler = Some(crate::ipc::protocol::message_handler(self.clone()));
11941195
}

core/tauri/src/window.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,8 @@ impl<R: Runtime> Window<R> {
20992099
Arc::new(
21002100
#[allow(unused_variables)]
21012101
move |window: Window<R>, cmd, response, callback, error| {
2102-
#[cfg(not(ipc_custom_protocol))]
2102+
if (cfg!(target_os = "macos") && window.url().scheme() == "https")
2103+
|| !cfg!(ipc_custom_protocol)
21032104
{
21042105
use crate::ipc::{
21052106
format_callback::{

0 commit comments

Comments
 (0)