Skip to content

Commit 0d63732

Browse files
authored
refactor(core): allow custom protocol handler to resolve async (#7754)
1 parent b75a121 commit 0d63732

36 files changed

Lines changed: 948 additions & 1169 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:breaking
3+
---
4+
5+
Changed `Builder::register_uri_scheme_protocol` to return a `http::Response` instead of `Result<http::Response>`. To return an error response, manually create a response with status code >= 400.
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 invalid header value type when requesting IPC body through a channel.

.changes/http-types-refactor.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch:breaking
3+
"tauri-runtime": patch:breaking
4+
"tauri-runtime-wry": patch:breaking
5+
---
6+
7+
`tauri-runtime` no longer implements its own HTTP types and relies on the `http` crate instead.

.changes/invoke-system-args.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:breaking
3+
---
4+
5+
Changed `Builder::invoke_system` to take references instead of owned values.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:enhance
3+
---
4+
5+
Added `Builder::register_asynchronous_uri_scheme_protocol` to allow resolving a custom URI scheme protocol request asynchronously to prevent blocking the main thread.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime": patch:enhance
3+
---
4+
5+
Changed custom protocol closure type to enable asynchronous usage.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:breaking
3+
---
4+
5+
Changed `Window::on_message` signature to take a responder closure instead of returning the response object in order to asynchronously process the request.

.changes/wry-0.32.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime-wry": patch:enhance
3+
---
4+
5+
Update wry to 0.32 to include asynchronous custom protocol support.

core/tauri-runtime-wry/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ rust-version = { workspace = true }
1616
features = [ "dox" ]
1717

1818
[dependencies]
19-
wry = { version = "0.31", default-features = false, features = [ "file-drop", "protocol" ] }
19+
wry = { version = "0.32", default-features = false, features = [ "tao", "file-drop", "protocol" ] }
2020
tauri-runtime = { version = "1.0.0-alpha.0", path = "../tauri-runtime" }
2121
tauri-utils = { version = "2.0.0-alpha.7", path = "../tauri-utils" }
2222
uuid = { version = "1", features = [ "v4" ] }
2323
rand = "0.8"
2424
raw-window-handle = "0.5"
25+
http = "0.2"
2526

2627
[target."cfg(windows)".dependencies]
2728
webview2-com = "0.25"

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

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle};
1515
use tauri_runtime::{
16-
http::{header::CONTENT_TYPE, Request as HttpRequest, RequestParts, Response as HttpResponse},
1716
monitor::Monitor,
1817
webview::{WebviewIpcHandler, WindowBuilder, WindowBuilderBase},
1918
window::{
@@ -61,7 +60,6 @@ use wry::{
6160
UserAttentionType as WryUserAttentionType,
6261
},
6362
},
64-
http::{Request as WryRequest, Response as WryResponse},
6563
webview::{FileDropEvent as WryFileDropEvent, Url, WebContext, WebView, WebViewBuilder},
6664
};
6765

@@ -85,7 +83,6 @@ pub use wry::application::platform::macos::{
8583
};
8684

8785
use std::{
88-
borrow::Cow,
8986
cell::RefCell,
9087
collections::{
9188
hash_map::Entry::{Occupied, Vacant},
@@ -259,39 +256,6 @@ impl<T: UserEvent> fmt::Debug for Context<T> {
259256
}
260257
}
261258

262-
struct HttpRequestWrapper(HttpRequest);
263-
264-
impl From<&WryRequest<Vec<u8>>> for HttpRequestWrapper {
265-
fn from(req: &WryRequest<Vec<u8>>) -> Self {
266-
let parts = RequestParts {
267-
uri: req.uri().to_string(),
268-
method: req.method().clone(),
269-
headers: req.headers().clone(),
270-
};
271-
Self(HttpRequest::new_internal(parts, req.body().clone()))
272-
}
273-
}
274-
275-
// response
276-
struct HttpResponseWrapper(WryResponse<Cow<'static, [u8]>>);
277-
impl From<HttpResponse> for HttpResponseWrapper {
278-
fn from(response: HttpResponse) -> Self {
279-
let (parts, body) = response.into_parts();
280-
let mut res_builder = WryResponse::builder()
281-
.status(parts.status)
282-
.version(parts.version);
283-
if let Some(mime) = parts.mimetype {
284-
res_builder = res_builder.header(CONTENT_TYPE, mime);
285-
}
286-
for (name, val) in parts.headers.iter() {
287-
res_builder = res_builder.header(name, val);
288-
}
289-
290-
let res = res_builder.body(body).unwrap();
291-
Self(res)
292-
}
293-
}
294-
295259
pub struct DeviceEventFilterWrapper(pub WryDeviceEventFilter);
296260

297261
impl From<DeviceEventFilter> for DeviceEventFilterWrapper {
@@ -2701,11 +2665,13 @@ fn create_webview<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
27012665
}
27022666

27032667
for (scheme, protocol) in uri_scheme_protocols {
2704-
webview_builder = webview_builder.with_custom_protocol(scheme, move |wry_request| {
2705-
protocol(&HttpRequestWrapper::from(wry_request).0)
2706-
.map(|tauri_response| HttpResponseWrapper::from(tauri_response).0)
2707-
.map_err(|_| wry::Error::InitScriptError)
2708-
});
2668+
webview_builder =
2669+
webview_builder.with_asynchronous_custom_protocol(scheme, move |request, responder| {
2670+
protocol(
2671+
request,
2672+
Box::new(move |response| responder.respond(response)),
2673+
)
2674+
});
27092675
}
27102676

27112677
for script in webview_attributes.initialization_scripts {

0 commit comments

Comments
 (0)