Skip to content

Commit aecf146

Browse files
authored
fix(core/ipc): access url through webview native object, closes #6889 (#6976)
1 parent 2b26b2e commit aecf146

File tree

7 files changed

+37
-33
lines changed

7 files changed

+37
-33
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch'
3+
---
4+
5+
Fix IPC failing after a failed navigation to an external URL.

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ impl<T: UserEvent> Context<T> {
214214
impl<T: UserEvent> Context<T> {
215215
fn create_webview(&self, pending: PendingWindow<T, Wry<T>>) -> Result<DetachedWindow<T, Wry<T>>> {
216216
let label = pending.label.clone();
217-
let current_url = pending.current_url.clone();
218217
let menu_ids = pending.menu_ids.clone();
219218
let js_event_listeners = pending.js_event_listeners.clone();
220219
let context = self.clone();
@@ -236,7 +235,6 @@ impl<T: UserEvent> Context<T> {
236235
};
237236
Ok(DetachedWindow {
238237
label,
239-
current_url,
240238
dispatcher,
241239
menu_ids,
242240
js_event_listeners,
@@ -1987,7 +1985,6 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
19871985

19881986
fn create_window(&self, pending: PendingWindow<T, Self>) -> Result<DetachedWindow<T, Self>> {
19891987
let label = pending.label.clone();
1990-
let current_url = pending.current_url.clone();
19911988
let menu_ids = pending.menu_ids.clone();
19921989
let js_event_listeners = pending.js_event_listeners.clone();
19931990
let window_id = rand::random();
@@ -2014,7 +2011,6 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
20142011

20152012
Ok(DetachedWindow {
20162013
label,
2017-
current_url,
20182014
dispatcher,
20192015
menu_ids,
20202016
js_event_listeners,
@@ -3044,7 +3040,7 @@ fn create_webview<T: UserEvent>(
30443040
mut window_builder,
30453041
ipc_handler,
30463042
label,
3047-
current_url,
3043+
url,
30483044
menu_ids,
30493045
js_event_listeners,
30503046
..
@@ -3093,7 +3089,7 @@ fn create_webview<T: UserEvent>(
30933089
}
30943090
let mut webview_builder = WebViewBuilder::new(window)
30953091
.map_err(|e| Error::CreateWebview(Box::new(e)))?
3096-
.with_url(current_url.lock().unwrap().as_str())
3092+
.with_url(&url)
30973093
.unwrap() // safe to unwrap because we validate the URL beforehand
30983094
.with_transparent(is_window_transparent)
30993095
.with_accept_first_mouse(webview_attributes.accept_first_mouse);
@@ -3128,7 +3124,6 @@ fn create_webview<T: UserEvent>(
31283124
webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
31293125
context,
31303126
label.clone(),
3131-
current_url,
31323127
menu_ids,
31333128
js_event_listeners,
31343129
handler,
@@ -3239,7 +3234,6 @@ fn create_webview<T: UserEvent>(
32393234
fn create_ipc_handler<T: UserEvent>(
32403235
context: Context<T>,
32413236
label: String,
3242-
current_url: Arc<Mutex<Url>>,
32433237
menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
32443238
js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
32453239
handler: WebviewIpcHandler<T, Wry<T>>,
@@ -3248,7 +3242,6 @@ fn create_ipc_handler<T: UserEvent>(
32483242
let window_id = context.webview_id_map.get(&window.id()).unwrap();
32493243
handler(
32503244
DetachedWindow {
3251-
current_url: current_url.clone(),
32523245
dispatcher: WryDispatcher {
32533246
window_id,
32543247
context: context.clone(),

core/tauri-runtime/src/window.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ pub struct PendingWindow<T: UserEvent, R: Runtime<T>> {
238238

239239
pub web_resource_request_handler: Option<Box<WebResourceRequestHandler>>,
240240

241-
/// The current webview URL.
242-
pub current_url: Arc<Mutex<Url>>,
241+
/// The resolved URL to load on the webview.
242+
pub url: String,
243243
}
244244

245245
pub fn is_label_valid(label: &str) -> bool {
@@ -280,7 +280,7 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
280280
js_event_listeners: Default::default(),
281281
navigation_handler: Default::default(),
282282
web_resource_request_handler: Default::default(),
283-
current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())),
283+
url: "tauri://localhost".to_string(),
284284
})
285285
}
286286
}
@@ -311,7 +311,7 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
311311
js_event_listeners: Default::default(),
312312
navigation_handler: Default::default(),
313313
web_resource_request_handler: Default::default(),
314-
current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())),
314+
url: "tauri://localhost".to_string(),
315315
})
316316
}
317317
}
@@ -352,9 +352,6 @@ pub struct JsEventListenerKey {
352352
/// A webview window that is not yet managed by Tauri.
353353
#[derive(Debug)]
354354
pub struct DetachedWindow<T: UserEvent, R: Runtime<T>> {
355-
/// The current webview URL.
356-
pub current_url: Arc<Mutex<Url>>,
357-
358355
/// Name of the window
359356
pub label: String,
360357

@@ -371,7 +368,6 @@ pub struct DetachedWindow<T: UserEvent, R: Runtime<T>> {
371368
impl<T: UserEvent, R: Runtime<T>> Clone for DetachedWindow<T, R> {
372369
fn clone(&self) -> Self {
373370
Self {
374-
current_url: self.current_url.clone(),
375371
label: self.label.clone(),
376372
dispatcher: self.dispatcher.clone(),
377373
menu_ids: self.menu_ids.clone(),

core/tauri/src/manager.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ impl<R: Runtime> WindowManager<R> {
470470
});
471471
}
472472

473-
let window_url = pending.current_url.lock().unwrap().clone();
473+
let window_url = Url::parse(&pending.url).unwrap();
474474
let window_origin =
475475
if cfg!(windows) && window_url.scheme() != "http" && window_url.scheme() != "https" {
476476
format!("https://{}.localhost", window_url.scheme())
@@ -1165,7 +1165,7 @@ impl<R: Runtime> WindowManager<R> {
11651165
}
11661166
}
11671167

1168-
*pending.current_url.lock().unwrap() = url;
1168+
pending.url = url.to_string();
11691169

11701170
if !pending.window_builder.has_icon() {
11711171
if let Some(default_window_icon) = self.inner.default_window_icon.clone() {
@@ -1210,7 +1210,6 @@ impl<R: Runtime> WindowManager<R> {
12101210

12111211
#[cfg(feature = "isolation")]
12121212
let pattern = self.pattern().clone();
1213-
let current_url_ = pending.current_url.clone();
12141213
let navigation_handler = pending.navigation_handler.take();
12151214
pending.navigation_handler = Some(Box::new(move |url| {
12161215
// always allow navigation events for the isolation iframe and do not emit them for consumers
@@ -1222,7 +1221,6 @@ impl<R: Runtime> WindowManager<R> {
12221221
return true;
12231222
}
12241223
}
1225-
*current_url_.lock().unwrap() = url.clone();
12261224
if let Some(handler) = &navigation_handler {
12271225
handler(url)
12281226
} else {

core/tauri/src/scope/ipc.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ mod tests {
260260

261261
#[test]
262262
fn scope_not_defined() {
263-
let (_app, window) = test_context(vec![RemoteDomainAccessScope::new("app.tauri.app")
263+
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("app.tauri.app")
264264
.add_window("other")
265265
.enable_tauri_api()]);
266266

@@ -277,7 +277,7 @@ mod tests {
277277

278278
#[test]
279279
fn scope_not_defined_for_window() {
280-
let (_app, window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
280+
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
281281
.add_window("second")
282282
.enable_tauri_api()]);
283283

@@ -291,7 +291,7 @@ mod tests {
291291

292292
#[test]
293293
fn scope_not_defined_for_url() {
294-
let (_app, window) = test_context(vec![RemoteDomainAccessScope::new("github.com")
294+
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("github.com")
295295
.add_window("main")
296296
.enable_tauri_api()]);
297297

@@ -353,7 +353,7 @@ mod tests {
353353

354354
#[test]
355355
fn subpath_is_allowed() {
356-
let (app, window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
356+
let (app, mut window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
357357
.add_window("main")
358358
.enable_tauri_api()]);
359359

@@ -367,7 +367,7 @@ mod tests {
367367

368368
#[test]
369369
fn tauri_api_not_allowed() {
370-
let (_app, window) = test_context(vec![
370+
let (_app, mut window) = test_context(vec![
371371
RemoteDomainAccessScope::new("tauri.app").add_window("main")
372372
]);
373373

@@ -381,7 +381,7 @@ mod tests {
381381

382382
#[test]
383383
fn plugin_allowed() {
384-
let (_app, window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
384+
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
385385
.add_window("main")
386386
.add_plugin(PLUGIN_NAME)]);
387387

@@ -395,7 +395,7 @@ mod tests {
395395

396396
#[test]
397397
fn plugin_not_allowed() {
398-
let (_app, window) = test_context(vec![
398+
let (_app, mut window) = test_context(vec![
399399
RemoteDomainAccessScope::new("tauri.app").add_window("main")
400400
]);
401401

core/tauri/src/test/mock_runtime.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
6969
) -> Result<DetachedWindow<T, Self::Runtime>> {
7070
Ok(DetachedWindow {
7171
label: pending.label,
72-
current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())),
7372
dispatcher: MockDispatcher {
7473
context: self.context.clone(),
7574
last_evaluated_script: Default::default(),
@@ -703,7 +702,6 @@ impl<T: UserEvent> Runtime<T> for MockRuntime {
703702
fn create_window(&self, pending: PendingWindow<T, Self>) -> Result<DetachedWindow<T, Self>> {
704703
Ok(DetachedWindow {
705704
label: pending.label,
706-
current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())),
707705
dispatcher: MockDispatcher {
708706
context: self.context.clone(),
709707
last_evaluated_script: Default::default(),

core/tauri/src/window.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ pub struct Window<R: Runtime> {
681681
/// The manager to associate this webview window with.
682682
manager: WindowManager<R>,
683683
pub(crate) app_handle: AppHandle<R>,
684+
685+
#[cfg(test)]
686+
pub(crate) current_url: url::Url,
684687
}
685688

686689
unsafe impl<R: Runtime> raw_window_handle::HasRawWindowHandle for Window<R> {
@@ -695,6 +698,8 @@ impl<R: Runtime> Clone for Window<R> {
695698
window: self.window.clone(),
696699
manager: self.manager.clone(),
697700
app_handle: self.app_handle.clone(),
701+
#[cfg(test)]
702+
current_url: self.current_url.clone(),
698703
}
699704
}
700705
}
@@ -891,6 +896,8 @@ impl<R: Runtime> Window<R> {
891896
window,
892897
manager,
893898
app_handle,
899+
#[cfg(test)]
900+
current_url: "http://tauri.app".parse().unwrap(),
894901
}
895902
}
896903

@@ -1383,13 +1390,20 @@ impl<R: Runtime> Window<R> {
13831390
/// Webview APIs.
13841391
impl<R: Runtime> Window<R> {
13851392
/// Returns the current url of the webview.
1393+
// TODO: in v2, change this type to Result
1394+
#[cfg(not(test))]
1395+
pub fn url(&self) -> Url {
1396+
self.window.dispatcher.url().unwrap()
1397+
}
1398+
1399+
#[cfg(test)]
13861400
pub fn url(&self) -> Url {
1387-
self.window.current_url.lock().unwrap().clone()
1401+
self.current_url.clone()
13881402
}
13891403

13901404
#[cfg(test)]
1391-
pub(crate) fn navigate(&self, url: Url) {
1392-
*self.window.current_url.lock().unwrap() = url;
1405+
pub(crate) fn navigate(&mut self, url: Url) {
1406+
self.current_url = url;
13931407
}
13941408

13951409
/// Handles this window receiving an [`InvokeMessage`].

0 commit comments

Comments
 (0)