mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-09-11 07:55:34 +03:00
This commit is contained in:
parent
c68218b362
commit
a77be97474
5
.changes/ipc-post-message-fallback.md
Normal file
5
.changes/ipc-post-message-fallback.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"tauri": patch:enhance
|
||||||
|
---
|
||||||
|
|
||||||
|
Fallback to the postMessage IPC interface if we cannot reach the IPC custom protocol.
|
@ -217,11 +217,6 @@ fn main() {
|
|||||||
alias("desktop", !mobile);
|
alias("desktop", !mobile);
|
||||||
alias("mobile", mobile);
|
alias("mobile", mobile);
|
||||||
|
|
||||||
alias(
|
|
||||||
"ipc_custom_protocol",
|
|
||||||
target_os != "android" && (target_os != "linux" || has_feature("linux-ipc-protocol")),
|
|
||||||
);
|
|
||||||
|
|
||||||
let out_dir = PathBuf::from(var("OUT_DIR").unwrap());
|
let out_dir = PathBuf::from(var("OUT_DIR").unwrap());
|
||||||
|
|
||||||
let checked_features_out_path = out_dir.join("checked_features");
|
let checked_features_out_path = out_dir.join("checked_features");
|
||||||
|
@ -6,26 +6,20 @@
|
|||||||
const processIpcMessage = __RAW_process_ipc_message_fn__
|
const processIpcMessage = __RAW_process_ipc_message_fn__
|
||||||
const osName = __TEMPLATE_os_name__
|
const osName = __TEMPLATE_os_name__
|
||||||
const fetchChannelDataCommand = __TEMPLATE_fetch_channel_data_command__
|
const fetchChannelDataCommand = __TEMPLATE_fetch_channel_data_command__
|
||||||
const useCustomProtocol = __TEMPLATE_use_custom_protocol__
|
const linuxIpcProtocolEnabled = __TEMPLATE_linux_ipc_protocol_enabled__
|
||||||
|
let customProtocolIpcFailed = false
|
||||||
|
|
||||||
Object.defineProperty(window.__TAURI_INTERNALS__, 'postMessage', {
|
// on Linux we only use the custom-protocol-based IPC if the the linux-ipc-protocol Cargo feature is enabled
|
||||||
value: (message) => {
|
// on Android we never use it because Android does not have support to reading the request body
|
||||||
|
const canUseCustomProtocol =
|
||||||
|
osName === 'linux' ? linuxIpcProtocolEnabled : osName !== 'android'
|
||||||
|
|
||||||
|
function sendIpcMessage(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 or
|
|
||||||
// - the command is the fetch data command or
|
|
||||||
// - when not on Linux/Android
|
|
||||||
// AND
|
|
||||||
// - when not on macOS with an https URL
|
|
||||||
if (
|
if (
|
||||||
(useCustomProtocol ||
|
!customProtocolIpcFailed &&
|
||||||
cmd === fetchChannelDataCommand ||
|
(canUseCustomProtocol || cmd === fetchChannelDataCommand)
|
||||||
!(osName === 'linux' || osName === 'android')) &&
|
|
||||||
!(
|
|
||||||
(osName === 'macos' || osName === 'ios') &&
|
|
||||||
location.protocol === 'https:'
|
|
||||||
)
|
|
||||||
) {
|
) {
|
||||||
const { contentType, data } = processIpcMessage(payload)
|
const { contentType, data } = processIpcMessage(payload)
|
||||||
fetch(window.__TAURI_INTERNALS__.convertFileSrc(cmd, 'ipc'), {
|
fetch(window.__TAURI_INTERNALS__.convertFileSrc(cmd, 'ipc'), {
|
||||||
@ -41,9 +35,7 @@
|
|||||||
.then((response) => {
|
.then((response) => {
|
||||||
const cb = response.ok ? callback : error
|
const cb = response.ok ? callback : error
|
||||||
// we need to split here because on Android the content-type gets duplicated
|
// we need to split here because on Android the content-type gets duplicated
|
||||||
switch (
|
switch ((response.headers.get('content-type') || '').split(',')[0]) {
|
||||||
(response.headers.get('content-type') || '').split(',')[0]
|
|
||||||
) {
|
|
||||||
case 'application/json':
|
case 'application/json':
|
||||||
return response.json().then((r) => [cb, r])
|
return response.json().then((r) => [cb, r])
|
||||||
case 'text/plain':
|
case 'text/plain':
|
||||||
@ -61,6 +53,12 @@
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.catch(() => {
|
||||||
|
// failed to use the custom protocol IPC (either the webview blocked a custom protocol or it was a CSP error)
|
||||||
|
// so we need to fallback to the postMessage interface
|
||||||
|
customProtocolIpcFailed = true
|
||||||
|
sendIpcMessage(message)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
// otherwise use the postMessage interface
|
// otherwise use the postMessage interface
|
||||||
const { data } = processIpcMessage({
|
const { data } = processIpcMessage({
|
||||||
@ -73,5 +71,8 @@
|
|||||||
window.ipc.postMessage(data)
|
window.ipc.postMessage(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(window.__TAURI_INTERNALS__, 'postMessage', {
|
||||||
|
value: sendIpcMessage
|
||||||
})
|
})
|
||||||
})()
|
})()
|
||||||
|
@ -1084,7 +1084,7 @@ struct InvokeInitializationScript<'a> {
|
|||||||
process_ipc_message_fn: &'a str,
|
process_ipc_message_fn: &'a str,
|
||||||
os_name: &'a str,
|
os_name: &'a str,
|
||||||
fetch_channel_data_command: &'a str,
|
fetch_channel_data_command: &'a str,
|
||||||
use_custom_protocol: bool,
|
linux_ipc_protocol_enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make `Wry` the default `Runtime` for `Builder`
|
/// Make `Wry` the default `Runtime` for `Builder`
|
||||||
@ -1117,7 +1117,7 @@ impl<R: Runtime> Builder<R> {
|
|||||||
process_ipc_message_fn: crate::manager::webview::PROCESS_IPC_MESSAGE_FN,
|
process_ipc_message_fn: crate::manager::webview::PROCESS_IPC_MESSAGE_FN,
|
||||||
os_name: std::env::consts::OS,
|
os_name: std::env::consts::OS,
|
||||||
fetch_channel_data_command: crate::ipc::channel::FETCH_CHANNEL_DATA_COMMAND,
|
fetch_channel_data_command: crate::ipc::channel::FETCH_CHANNEL_DATA_COMMAND,
|
||||||
use_custom_protocol: cfg!(ipc_custom_protocol),
|
linux_ipc_protocol_enabled: cfg!(feature = "linux-ipc-protocol"),
|
||||||
}
|
}
|
||||||
.render_default(&Default::default())
|
.render_default(&Default::default())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -21,7 +21,6 @@ use crate::{webview::Webview, Runtime, StateManager};
|
|||||||
mod authority;
|
mod authority;
|
||||||
pub(crate) mod channel;
|
pub(crate) mod channel;
|
||||||
mod command;
|
mod command;
|
||||||
#[cfg(any(target_os = "macos", target_os = "ios", not(ipc_custom_protocol)))]
|
|
||||||
pub(crate) mod format_callback;
|
pub(crate) mod format_callback;
|
||||||
pub(crate) mod protocol;
|
pub(crate) mod protocol;
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@ use super::{CallbackFn, InvokeBody, InvokeResponse};
|
|||||||
const TAURI_CALLBACK_HEADER_NAME: &str = "Tauri-Callback";
|
const TAURI_CALLBACK_HEADER_NAME: &str = "Tauri-Callback";
|
||||||
const TAURI_ERROR_HEADER_NAME: &str = "Tauri-Error";
|
const TAURI_ERROR_HEADER_NAME: &str = "Tauri-Error";
|
||||||
|
|
||||||
#[cfg(any(target_os = "macos", target_os = "ios", not(ipc_custom_protocol)))]
|
|
||||||
pub fn message_handler<R: Runtime>(
|
pub fn message_handler<R: Runtime>(
|
||||||
manager: Arc<AppManager<R>>,
|
manager: Arc<AppManager<R>>,
|
||||||
) -> crate::runtime::webview::WebviewIpcHandler<crate::EventLoopMessage, R> {
|
) -> crate::runtime::webview::WebviewIpcHandler<crate::EventLoopMessage, R> {
|
||||||
@ -162,7 +161,6 @@ pub fn get<R: Runtime>(manager: Arc<AppManager<R>>, label: String) -> UriSchemeP
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "macos", target_os = "ios", not(ipc_custom_protocol)))]
|
|
||||||
fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, label: &str) {
|
fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, label: &str) {
|
||||||
if let Some(webview) = manager.get_webview(label) {
|
if let Some(webview) = manager.get_webview(label) {
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
@ -374,9 +372,14 @@ fn parse_invoke_request<R: Runtime>(
|
|||||||
.decode_utf8_lossy()
|
.decode_utf8_lossy()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
// the body is not set if ipc_custom_protocol is not enabled so we'll just ignore it
|
// on Android and on Linux (without the linux-ipc-protocol Cargo feature) we cannot read the request body
|
||||||
#[cfg(all(feature = "isolation", ipc_custom_protocol))]
|
// so we must ignore it because some commands use the IPC for faster response
|
||||||
|
let has_payload = !body.is_empty();
|
||||||
|
|
||||||
|
#[cfg(feature = "isolation")]
|
||||||
if let crate::Pattern::Isolation { crypto_keys, .. } = &*manager.pattern {
|
if let crate::Pattern::Isolation { crypto_keys, .. } = &*manager.pattern {
|
||||||
|
// if the platform does not support request body, we ignore it
|
||||||
|
if has_payload {
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
let _span = tracing::trace_span!("ipc::request::decrypt_isolation_payload").entered();
|
let _span = tracing::trace_span!("ipc::request::decrypt_isolation_payload").entered();
|
||||||
|
|
||||||
@ -384,6 +387,7 @@ fn parse_invoke_request<R: Runtime>(
|
|||||||
.and_then(|raw| crypto_keys.decrypt(raw))
|
.and_then(|raw| crypto_keys.decrypt(raw))
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let callback = CallbackFn(
|
let callback = CallbackFn(
|
||||||
parts
|
parts
|
||||||
@ -420,12 +424,12 @@ fn parse_invoke_request<R: Runtime>(
|
|||||||
let body = if content_type == mime::APPLICATION_OCTET_STREAM {
|
let body = if content_type == mime::APPLICATION_OCTET_STREAM {
|
||||||
body.into()
|
body.into()
|
||||||
} else if content_type == mime::APPLICATION_JSON {
|
} else if content_type == mime::APPLICATION_JSON {
|
||||||
if cfg!(ipc_custom_protocol) {
|
// if the platform does not support request body, we ignore it
|
||||||
|
if has_payload {
|
||||||
serde_json::from_slice::<serde_json::Value>(&body)
|
serde_json::from_slice::<serde_json::Value>(&body)
|
||||||
.map_err(|e| e.to_string())?
|
.map_err(|e| e.to_string())?
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
// the body is not set if ipc_custom_protocol is not enabled so we'll just ignore it
|
|
||||||
serde_json::Value::Object(Default::default()).into()
|
serde_json::Value::Object(Default::default()).into()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -496,12 +496,9 @@ impl<R: Runtime> WebviewManager<R> {
|
|||||||
manager,
|
manager,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
#[cfg(any(target_os = "macos", target_os = "ios", not(ipc_custom_protocol)))]
|
|
||||||
{
|
|
||||||
pending.ipc_handler = Some(crate::ipc::protocol::message_handler(
|
pending.ipc_handler = Some(crate::ipc::protocol::message_handler(
|
||||||
manager.manager_owned(),
|
manager.manager_owned(),
|
||||||
));
|
));
|
||||||
}
|
|
||||||
|
|
||||||
// in `windows`, we need to force a data_directory
|
// in `windows`, we need to force a data_directory
|
||||||
// but we do respect user-specification
|
// but we do respect user-specification
|
||||||
|
Loading…
Reference in New Issue
Block a user