Skip to content

Commit

Permalink
fix(core): use postMessage IPC for remote URLs on macOS closes #7662
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Sep 4, 2023
1 parent e98393e commit 6f3347d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-ipc-remote-url-macos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:bug
---

Fixes IPC failing to communicate for remote URLs on macOS.
42 changes: 37 additions & 5 deletions core/tauri/scripts/ipc-protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,35 @@

Object.defineProperty(window, '__TAURI_POST_MESSAGE__', {
value: (message) => {
const { cmd, callback, error, payload, options } = message
const {
cmd,
callback,
error,
payload,
options
} = message

// 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
if (useCustomProtocol || cmd === fetchChannelDataCommand || (osName !== 'linux' && osName !== 'android')) {
const { contentType, data } = processIpcMessage(payload)
console.log(osName, location.protocol)

// use custom protocol for IPC if:
// - the flag is set to true or
// - the command is the fetch data command or
// - when not on Linux/Android
// AND
// - when not on macOS with an https URL
if (
(
useCustomProtocol ||
cmd === fetchChannelDataCommand ||
!(osName === 'linux' || osName === 'android')
) &&
!(osName === 'macos' && location.protocol === 'https:')
) {
console.log('process')
const {
contentType,
data
} = processIpcMessage(payload)
fetch(window.__TAURI__.convertFileSrc(cmd, 'ipc'), {
method: 'POST',
body: data,
Expand Down Expand Up @@ -44,7 +68,15 @@
})
} else {
// otherwise use the postMessage interface
const { data } = processIpcMessage({ cmd, callback, error, options, payload })
const {
data
} = processIpcMessage({
cmd,
callback,
error,
options,
payload
})
window.ipc.postMessage(data)
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/ipc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
};

pub(crate) mod channel;
#[cfg(not(ipc_custom_protocol))]
#[cfg(any(target_os = "macos", not(ipc_custom_protocol)))]
pub(crate) mod format_callback;
pub(crate) mod protocol;

Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/ipc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::{CallbackFn, InvokeBody, InvokeResponse};
const TAURI_CALLBACK_HEADER_NAME: &str = "Tauri-Callback";
const TAURI_ERROR_HEADER_NAME: &str = "Tauri-Error";

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

#[cfg(not(ipc_custom_protocol))]
#[cfg(any(target_os = "macos", not(ipc_custom_protocol)))]
fn handle_ipc_message<R: Runtime>(message: String, manager: &WindowManager<R>, label: &str) {
if let Some(window) = manager.get_window(label) {
use serde::{Deserialize, Deserializer};
Expand Down
3 changes: 2 additions & 1 deletion core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,8 @@ impl<R: Runtime> WindowManager<R> {
#[allow(clippy::redundant_clone)]
app_handle.clone(),
)?;
#[cfg(not(ipc_custom_protocol))]

#[cfg(any(target_os = "macos", not(ipc_custom_protocol)))]
{
pending.ipc_handler = Some(crate::ipc::protocol::message_handler(self.clone()));
}
Expand Down
3 changes: 2 additions & 1 deletion core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,8 @@ impl<R: Runtime> Window<R> {
Arc::new(
#[allow(unused_variables)]
move |window: Window<R>, cmd, response, callback, error| {
#[cfg(not(ipc_custom_protocol))]
if (cfg!(target_os = "macos") && window.url().scheme() == "https")
|| !cfg!(ipc_custom_protocol)
{
use crate::ipc::{
format_callback::{

Check failure on line 2106 in core/tauri/src/window.rs

View workflow job for this annotation

GitHub Actions / test (x86_64-pc-windows-msvc, windows-latest, 1.65.0, false, test, --features compression, all)

unresolved import `crate::ipc::format_callback`

Check failure on line 2106 in core/tauri/src/window.rs

View workflow job for this annotation

GitHub Actions / test (aarch64-apple-ios, macos-latest, 1.65.0, false, build, --features compression, all)

unresolved import `crate::ipc::format_callback`

Check failure on line 2106 in core/tauri/src/window.rs

View workflow job for this annotation

GitHub Actions / test (x86_64-pc-windows-msvc, windows-latest, 1.65.0, false, test, --no-default-features, no-defa...

unresolved import `crate::ipc::format_callback`

Check failure on line 2106 in core/tauri/src/window.rs

View workflow job for this annotation

GitHub Actions / test (aarch64-apple-ios, macos-latest, 1.65.0, false, build, --no-default-features, no-default)

unresolved import `crate::ipc::format_callback`
Expand Down

0 comments on commit 6f3347d

Please sign in to comment.