Skip to content

Commit 0a54cb8

Browse files
committed
fix IPC on windows
1 parent f79e9d9 commit 0a54cb8

File tree

6 files changed

+40
-51
lines changed

6 files changed

+40
-51
lines changed

crates/tauri/scripts/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
const osName = __TEMPLATE_os_name__
1111
const protocolScheme = __TEMPLATE_protocol_scheme__
12+
const cef = __TEMPLATE_cef__
1213

1314
Object.defineProperty(window.__TAURI_INTERNALS__, 'convertFileSrc', {
1415
value: function (filePath, protocol = 'asset') {
1516
const path = encodeURIComponent(filePath)
16-
return osName === 'windows' || osName === 'android'
17+
return (osName === 'windows' || osName === 'android') && !cef
1718
? `${protocolScheme}://${protocol}.localhost/${path}`
1819
: `${protocol}://localhost/${path}`
1920
}

crates/tauri/src/manager/mod.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,10 @@ use tauri_utils::{
1919
};
2020

2121
use crate::{
22-
app::{
22+
Assets, Context, DebugAppIcon, EventName, Pattern, Runtime, StateManager, Webview, Window, app::{
2323
AppHandle, ChannelInterceptor, GlobalWebviewEventListener, GlobalWindowEventListener,
2424
OnPageLoad,
25-
},
26-
event::{EmitArgs, Event, EventId, EventTarget, Listeners},
27-
ipc::{Invoke, InvokeHandler, RuntimeAuthority},
28-
plugin::PluginStore,
29-
resources::ResourceTable,
30-
utils::{config::Config, PackageInfo},
31-
Assets, Context, DebugAppIcon, EventName, Pattern, Runtime, StateManager, Webview, Window,
25+
}, event::{EmitArgs, Event, EventId, EventTarget, Listeners}, ipc::{Invoke, InvokeHandler, RuntimeAuthority}, plugin::PluginStore, resources::ResourceTable, utils::{PackageInfo, config::Config}, webview::custom_scheme_url
3226
};
3327

3428
#[cfg(desktop)]
@@ -351,22 +345,13 @@ impl<R: Runtime> AppManager<R> {
351345
self.config.build.dev_url.as_ref()
352346
}
353347

354-
pub(crate) fn protocol_url(&self, https: bool) -> Cow<'_, Url> {
355-
if cfg!(windows) || cfg!(target_os = "android") {
356-
let scheme = if https { "https" } else { "http" };
357-
Cow::Owned(Url::parse(&format!("{scheme}://tauri.localhost")).unwrap())
358-
} else {
359-
Cow::Owned(Url::parse("tauri://localhost").unwrap())
360-
}
361-
}
362-
363348
/// Get the base URL to use for webview requests.
364349
///
365350
/// In dev mode, this will be based on the `devUrl` configuration value.
366351
pub(crate) fn get_url(&self, https: bool) -> Cow<'_, Url> {
367352
match self.base_path() {
368353
Some(url) => Cow::Borrowed(url),
369-
_ => self.protocol_url(https),
354+
_ => Cow::Owned(Url::parse(&custom_scheme_url("tauri", https)).unwrap()),
370355
}
371356
}
372357

@@ -447,7 +432,7 @@ impl<R: Runtime> AppManager<R> {
447432
let default_src = csp_map
448433
.entry("default-src".into())
449434
.or_insert_with(Default::default);
450-
default_src.push(crate::pattern::format_real_schema(
435+
default_src.push(crate::webview::custom_scheme_url(
451436
schema,
452437
_use_https_schema,
453438
));

crates/tauri/src/manager/webview.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl<R: Runtime> WebviewManager<R> {
141141
isolation_origin: &match &*app_manager.pattern {
142142
#[cfg(feature = "isolation")]
143143
crate::Pattern::Isolation { schema, .. } => {
144-
crate::pattern::format_real_schema(schema, use_https_scheme)
144+
crate::webview::custom_scheme_url(schema, use_https_scheme)
145145
}
146146
_ => "".to_owned(),
147147
},
@@ -199,7 +199,7 @@ impl<R: Runtime> WebviewManager<R> {
199199
if let crate::Pattern::Isolation { schema, .. } = &*app_manager.pattern {
200200
all_initialization_scripts.push(main_frame_script(
201201
IsolationJavascript {
202-
isolation_src: &crate::pattern::format_real_schema(schema, use_https_scheme),
202+
isolation_src: crate::webview::custom_scheme_url(schema, use_https_scheme).as_str(),
203203
style: tauri_utils::pattern::isolation::IFRAME_STYLE,
204204
}
205205
.render_default(&Default::default())?
@@ -238,7 +238,7 @@ impl<R: Runtime> WebviewManager<R> {
238238
let window_url = Url::parse(&pending.url).unwrap();
239239
let window_origin = if window_url.scheme() == "data" {
240240
"null".into()
241-
} else if (cfg!(windows) || cfg!(target_os = "android"))
241+
} else if (!cfg!(feature = "cef") && cfg!(windows) || cfg!(target_os = "android"))
242242
&& window_url.scheme() != "http"
243243
&& window_url.scheme() != "https"
244244
{
@@ -368,7 +368,7 @@ impl<R: Runtime> WebviewManager<R> {
368368
struct CoreJavascript<'a> {
369369
os_name: &'a str,
370370
protocol_scheme: &'a str,
371-
invoke_key: &'a str,
371+
cef: bool,
372372
}
373373

374374
let freeze_prototype = if app_manager.config.app.security.freeze_prototype {
@@ -383,7 +383,7 @@ impl<R: Runtime> WebviewManager<R> {
383383
core_script: &CoreJavascript {
384384
os_name: std::env::consts::OS,
385385
protocol_scheme: if use_https_scheme { "https" } else { "http" },
386-
invoke_key: self.invoke_key(),
386+
cef: cfg!(feature = "cef"),
387387
}
388388
.render_default(&Default::default())?
389389
.into_string(),
@@ -548,12 +548,20 @@ impl<R: Runtime> WebviewManager<R> {
548548
let navigation_handler = pending.navigation_handler.take();
549549
let app_manager = manager.manager_owned();
550550
let label = pending.label.clone();
551+
552+
#[cfg(feature = "isolation")]
553+
let isolation_frame_url = if let crate::Pattern::Isolation { schema, .. } = &*pattern {
554+
Some(Url::parse(&crate::webview::custom_scheme_url(schema, pending.webview_attributes.use_https_scheme)).unwrap())
555+
} else {
556+
None
557+
};
558+
551559
pending.navigation_handler = Some(Box::new(move |url| {
552560
// always allow navigation events for the isolation iframe and do not emit them for consumers
553561
#[cfg(feature = "isolation")]
554-
if let crate::Pattern::Isolation { schema, .. } = &*pattern {
555-
if url.scheme() == schema
556-
&& url.domain() == Some(crate::pattern::ISOLATION_IFRAME_SRC_DOMAIN)
562+
if let Some(isolation_frame_url) = &isolation_frame_url {
563+
if url.scheme() == isolation_frame_url.scheme()
564+
&& url.domain() == isolation_frame_url.domain()
557565
{
558566
return true;
559567
}

crates/tauri/src/pattern.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ use std::sync::Arc;
88
use serde::Serialize;
99
use serialize_to_javascript::{default_template, Template};
1010

11-
/// The domain of the isolation iframe source.
12-
#[cfg(feature = "isolation")]
13-
pub const ISOLATION_IFRAME_SRC_DOMAIN: &str = "localhost";
14-
1511
/// An application pattern.
1612
#[derive(Debug)]
1713
pub enum Pattern {
@@ -80,13 +76,3 @@ pub(crate) enum IsolationSide {
8076
pub(crate) struct PatternJavascript {
8177
pub(crate) pattern: PatternObject,
8278
}
83-
84-
#[cfg(feature = "isolation")]
85-
pub(crate) fn format_real_schema(schema: &str, https: bool) -> String {
86-
if cfg!(windows) || cfg!(target_os = "android") {
87-
let scheme = if https { "https" } else { "http" };
88-
format!("{scheme}://{schema}.{ISOLATION_IFRAME_SRC_DOMAIN}/")
89-
} else {
90-
format!("{schema}://{ISOLATION_IFRAME_SRC_DOMAIN}/")
91-
}
92-
}

crates/tauri/src/protocol/isolation.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::sync::Arc;
1414

1515
use crate::{
1616
manager::{set_csp, webview::PROCESS_IPC_MESSAGE_FN, AppManager},
17-
webview::UriSchemeProtocolHandler,
17+
webview::{UriSchemeProtocolHandler, custom_scheme_url},
1818
Runtime,
1919
};
2020

@@ -26,12 +26,7 @@ pub fn get<R: Runtime>(
2626
window_origin: String,
2727
use_https_scheme: bool,
2828
) -> UriSchemeProtocolHandler {
29-
let frame_src = if cfg!(any(windows, target_os = "android")) {
30-
let https = if use_https_scheme { "https" } else { "http" };
31-
format!("{https}://{schema}.localhost")
32-
} else {
33-
format!("{schema}:")
34-
};
29+
let frame_src = custom_scheme_url(schema, use_https_scheme);
3530

3631
let assets = assets as Arc<dyn Assets<R>>;
3732

crates/tauri/src/webview/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,7 @@ tauri::Builder::default()
16821682

16831683
// if from `tauri://` custom protocol
16841684
({
1685-
let protocol_url = self.manager().protocol_url(uses_https);
1685+
let protocol_url = Url::parse(&custom_scheme_url("tauri", uses_https)).unwrap();
16861686
current_url.scheme() == protocol_url.scheme()
16871687
&& current_url.domain() == protocol_url.domain()
16881688
}) ||
@@ -1706,7 +1706,7 @@ tauri::Builder::default()
17061706
// so we check using the first part of the domain
17071707
#[cfg(any(windows, target_os = "android"))]
17081708
let local = {
1709-
let protocol_url = self.manager().protocol_url(uses_https);
1709+
let protocol_url = Url::parse(&custom_scheme_url("tauri", uses_https)).unwrap();
17101710
let maybe_protocol = current_url
17111711
.domain()
17121712
.and_then(|d| d .split_once('.'))
@@ -2305,6 +2305,20 @@ impl<T: ScopeObject> ResolvedScope<T> {
23052305
}
23062306
}
23072307

2308+
#[cfg(feature = "cef")]
2309+
pub(crate) fn custom_scheme_url(scheme: &str, _https: bool) -> String {
2310+
format!("{scheme}://localhost")
2311+
}
2312+
2313+
#[cfg(not(feature = "cef"))]
2314+
pub(crate) fn custom_scheme_url(scheme: &str, https: bool) -> String {
2315+
if cfg!(any(windows, target_os = "android")) {
2316+
format!("{}://{scheme}.localhost", if https { "https" } else { "http" })
2317+
} else {
2318+
format!("{scheme}://localhost")
2319+
}
2320+
}
2321+
23082322
#[cfg(test)]
23092323
mod tests {
23102324
#[test]

0 commit comments

Comments
 (0)