Skip to content

Commit

Permalink
refactor(core): allow custom protocol handler to resolve async (#7754)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Sep 6, 2023
1 parent b75a121 commit 0d63732
Show file tree
Hide file tree
Showing 36 changed files with 948 additions and 1,169 deletions.
5 changes: 5 additions & 0 deletions .changes/custom-protocol-response-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:breaking
---

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.
5 changes: 5 additions & 0 deletions .changes/fix-channel-data-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:bug
---

Fixes invalid header value type when requesting IPC body through a channel.
7 changes: 7 additions & 0 deletions .changes/http-types-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri": patch:breaking
"tauri-runtime": patch:breaking
"tauri-runtime-wry": patch:breaking
---

`tauri-runtime` no longer implements its own HTTP types and relies on the `http` crate instead.
5 changes: 5 additions & 0 deletions .changes/invoke-system-args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:breaking
---

Changed `Builder::invoke_system` to take references instead of owned values.
5 changes: 5 additions & 0 deletions .changes/register_asynchronous_uri_scheme_protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Added `Builder::register_asynchronous_uri_scheme_protocol` to allow resolving a custom URI scheme protocol request asynchronously to prevent blocking the main thread.
5 changes: 5 additions & 0 deletions .changes/runtime-custom-protocol-async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-runtime": patch:enhance
---

Changed custom protocol closure type to enable asynchronous usage.
5 changes: 5 additions & 0 deletions .changes/window-on-message-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:breaking
---

Changed `Window::on_message` signature to take a responder closure instead of returning the response object in order to asynchronously process the request.
5 changes: 5 additions & 0 deletions .changes/wry-0.32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-runtime-wry": patch:enhance
---

Update wry to 0.32 to include asynchronous custom protocol support.
3 changes: 2 additions & 1 deletion core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ rust-version = { workspace = true }
features = [ "dox" ]

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

[target."cfg(windows)".dependencies]
webview2-com = "0.25"
Expand Down
48 changes: 7 additions & 41 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle};
use tauri_runtime::{
http::{header::CONTENT_TYPE, Request as HttpRequest, RequestParts, Response as HttpResponse},
monitor::Monitor,
webview::{WebviewIpcHandler, WindowBuilder, WindowBuilderBase},
window::{
Expand Down Expand Up @@ -61,7 +60,6 @@ use wry::{
UserAttentionType as WryUserAttentionType,
},
},
http::{Request as WryRequest, Response as WryResponse},
webview::{FileDropEvent as WryFileDropEvent, Url, WebContext, WebView, WebViewBuilder},
};

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

use std::{
borrow::Cow,
cell::RefCell,
collections::{
hash_map::Entry::{Occupied, Vacant},
Expand Down Expand Up @@ -259,39 +256,6 @@ impl<T: UserEvent> fmt::Debug for Context<T> {
}
}

struct HttpRequestWrapper(HttpRequest);

impl From<&WryRequest<Vec<u8>>> for HttpRequestWrapper {
fn from(req: &WryRequest<Vec<u8>>) -> Self {
let parts = RequestParts {
uri: req.uri().to_string(),
method: req.method().clone(),
headers: req.headers().clone(),
};
Self(HttpRequest::new_internal(parts, req.body().clone()))
}
}

// response
struct HttpResponseWrapper(WryResponse<Cow<'static, [u8]>>);
impl From<HttpResponse> for HttpResponseWrapper {
fn from(response: HttpResponse) -> Self {
let (parts, body) = response.into_parts();
let mut res_builder = WryResponse::builder()
.status(parts.status)
.version(parts.version);
if let Some(mime) = parts.mimetype {
res_builder = res_builder.header(CONTENT_TYPE, mime);
}
for (name, val) in parts.headers.iter() {
res_builder = res_builder.header(name, val);
}

let res = res_builder.body(body).unwrap();
Self(res)
}
}

pub struct DeviceEventFilterWrapper(pub WryDeviceEventFilter);

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

for (scheme, protocol) in uri_scheme_protocols {
webview_builder = webview_builder.with_custom_protocol(scheme, move |wry_request| {
protocol(&HttpRequestWrapper::from(wry_request).0)
.map(|tauri_response| HttpResponseWrapper::from(tauri_response).0)
.map_err(|_| wry::Error::InitScriptError)
});
webview_builder =
webview_builder.with_asynchronous_custom_protocol(scheme, move |request, responder| {
protocol(
request,
Box::new(move |response| responder.respond(response)),
)
});
}

for script in webview_attributes.initialization_scripts {
Expand Down
1 change: 0 additions & 1 deletion core/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ thiserror = "1.0"
tauri-utils = { version = "2.0.0-alpha.7", path = "../tauri-utils" }
uuid = { version = "1", features = [ "v4" ] }
http = "0.2.4"
http-range = "0.1.4"
raw-window-handle = "0.5"
rand = "0.8"
url = { version = "2" }
Expand Down
20 changes: 0 additions & 20 deletions core/tauri-runtime/src/http/mod.rs

This file was deleted.

132 changes: 0 additions & 132 deletions core/tauri-runtime/src/http/request.rs

This file was deleted.

Loading

0 comments on commit 0d63732

Please sign in to comment.