Skip to content

Commit 354be36

Browse files
authored
refactor(core)!: pass webview label to uri scheme protocol handlers (#11163)
* refactor!(core): pass webview label to uri scheme protocol handlers close #10691 * Add `UriSchemeContext` * doctests
1 parent 12de4fa commit 354be36

File tree

6 files changed

+63
-25
lines changed

6 files changed

+63
-25
lines changed

.changes/custom-protocol-label.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"tauri": "patch:breaking"
3+
---
4+
5+
Changed uri scheme protocol handler to take `UriSchemeContext` as first argument instead of `AppHandle`. `UriSchemeContext` can be used to access an app handle or the webview label that made the request. The following methods are affected:
6+
- `tauri::Builder::register_uri_scheme_protocol`
7+
- `tauri::Builder::register_asynchronous_uri_scheme_protocol`
8+
- `tauri::plugin::Builder::register_uri_scheme_protocol`
9+
- `tauri::plugin::Builder::register_asynchronous_uri_scheme_protocol`

crates/tauri/src/app.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,7 @@ tauri::Builder::default()
16651665
}
16661666

16671667
/// Registers a URI scheme protocol available to all webviews.
1668+
///
16681669
/// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
16691670
/// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
16701671
/// and [webkit-web-context-register-uri-scheme](https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebContext.html#webkit-web-context-register-uri-scheme) on Linux.
@@ -1677,7 +1678,7 @@ tauri::Builder::default()
16771678
/// # Examples
16781679
/// ```
16791680
/// tauri::Builder::default()
1680-
/// .register_uri_scheme_protocol("app-files", |_app, request| {
1681+
/// .register_uri_scheme_protocol("app-files", |_ctx, request| {
16811682
/// // skip leading `/`
16821683
/// if let Ok(data) = std::fs::read(&request.uri().path()[1..]) {
16831684
/// http::Response::builder()
@@ -1696,7 +1697,10 @@ tauri::Builder::default()
16961697
pub fn register_uri_scheme_protocol<
16971698
N: Into<String>,
16981699
T: Into<Cow<'static, [u8]>>,
1699-
H: Fn(&AppHandle<R>, http::Request<Vec<u8>>) -> http::Response<T> + Send + Sync + 'static,
1700+
H: Fn(UriSchemeContext<'_, R>, http::Request<Vec<u8>>) -> http::Response<T>
1701+
+ Send
1702+
+ Sync
1703+
+ 'static,
17001704
>(
17011705
mut self,
17021706
uri_scheme: N,
@@ -1705,8 +1709,8 @@ tauri::Builder::default()
17051709
self.uri_scheme_protocols.insert(
17061710
uri_scheme.into(),
17071711
Arc::new(UriSchemeProtocol {
1708-
protocol: Box::new(move |app, request, responder| {
1709-
responder.respond(protocol(app, request))
1712+
protocol: Box::new(move |ctx, request, responder| {
1713+
responder.respond(protocol(ctx, request))
17101714
}),
17111715
}),
17121716
);
@@ -1724,7 +1728,7 @@ tauri::Builder::default()
17241728
/// # Examples
17251729
/// ```
17261730
/// tauri::Builder::default()
1727-
/// .register_asynchronous_uri_scheme_protocol("app-files", |_app, request, responder| {
1731+
/// .register_asynchronous_uri_scheme_protocol("app-files", |_ctx, request, responder| {
17281732
/// // skip leading `/`
17291733
/// let path = request.uri().path()[1..].to_string();
17301734
/// std::thread::spawn(move || {
@@ -1749,7 +1753,7 @@ tauri::Builder::default()
17491753
#[must_use]
17501754
pub fn register_asynchronous_uri_scheme_protocol<
17511755
N: Into<String>,
1752-
H: Fn(&AppHandle<R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync + 'static,
1756+
H: Fn(UriSchemeContext<'_, R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync + 'static,
17531757
>(
17541758
mut self,
17551759
uri_scheme: N,
@@ -2001,6 +2005,24 @@ impl UriSchemeResponder {
20012005
}
20022006
}
20032007

2008+
/// Uri scheme protocol context
2009+
pub struct UriSchemeContext<'a, R: Runtime> {
2010+
pub(crate) app_handle: &'a AppHandle<R>,
2011+
pub(crate) webview_label: &'a str,
2012+
}
2013+
2014+
impl<'a, R: Runtime> UriSchemeContext<'a, R> {
2015+
/// Get a reference to an [`AppHandle`].
2016+
pub fn app_handle(&self) -> &'a AppHandle<R> {
2017+
self.app_handle
2018+
}
2019+
2020+
/// Get the webview label that made the uri scheme request.
2021+
pub fn webview_label(&self) -> &'a str {
2022+
self.webview_label
2023+
}
2024+
}
2025+
20042026
#[cfg(target_os = "macos")]
20052027
fn init_app_menu<R: Runtime>(menu: &Menu<R>) -> crate::Result<()> {
20062028
menu.inner().init_for_nsapp();

crates/tauri/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ pub use self::utils::TitleBarStyle;
213213
pub use self::event::{Event, EventId, EventTarget};
214214
pub use {
215215
self::app::{
216-
App, AppHandle, AssetResolver, Builder, CloseRequestApi, RunEvent, UriSchemeResponder,
217-
WebviewEvent, WindowEvent,
216+
App, AppHandle, AssetResolver, Builder, CloseRequestApi, RunEvent, UriSchemeContext,
217+
UriSchemeResponder, WebviewEvent, WindowEvent,
218218
},
219219
self::manager::Asset,
220220
self::runtime::{

crates/tauri/src/manager/webview.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use crate::{
2525
pattern::PatternJavascript,
2626
sealed::ManagerBase,
2727
webview::PageLoadPayload,
28-
AppHandle, Emitter, EventLoopMessage, EventTarget, Manager, Runtime, Scopes, Webview, Window,
28+
Emitter, EventLoopMessage, EventTarget, Manager, Runtime, Scopes, UriSchemeContext, Webview,
29+
Window,
2930
};
3031

3132
use super::{
@@ -61,7 +62,7 @@ pub struct UriSchemeProtocol<R: Runtime> {
6162
/// Handler for protocol
6263
#[allow(clippy::type_complexity)]
6364
pub protocol:
64-
Box<dyn Fn(&AppHandle<R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync>,
65+
Box<dyn Fn(UriSchemeContext<'_, R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync>,
6566
}
6667

6768
pub struct WebviewManager<R: Runtime> {
@@ -210,13 +211,15 @@ impl<R: Runtime> WebviewManager<R> {
210211
for (uri_scheme, protocol) in &*self.uri_scheme_protocols.lock().unwrap() {
211212
registered_scheme_protocols.push(uri_scheme.clone());
212213
let protocol = protocol.clone();
213-
let app_handle = Mutex::new(manager.app_handle().clone());
214-
pending.register_uri_scheme_protocol(uri_scheme.clone(), move |p, responder| {
215-
(protocol.protocol)(
216-
&app_handle.lock().unwrap(),
217-
p,
218-
UriSchemeResponder(responder),
219-
)
214+
let app_handle = manager.app_handle().clone();
215+
let webview_label = label.to_string();
216+
217+
pending.register_uri_scheme_protocol(uri_scheme.clone(), move |request, responder| {
218+
let context = UriSchemeContext {
219+
app_handle: &app_handle,
220+
webview_label: webview_label.as_str(),
221+
};
222+
(protocol.protocol)(context, request, UriSchemeResponder(responder))
220223
});
221224
}
222225

crates/tauri/src/plugin.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
manager::webview::UriSchemeProtocol,
1111
utils::config::PluginConfig,
1212
webview::PageLoadPayload,
13-
AppHandle, Error, RunEvent, Runtime, Webview, Window,
13+
AppHandle, Error, RunEvent, Runtime, UriSchemeContext, Webview, Window,
1414
};
1515
use serde::{
1616
de::{Deserialize, DeserializeOwned, Deserializer, Error as DeError},
@@ -527,6 +527,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
527527
}
528528

529529
/// Registers a URI scheme protocol available to all webviews.
530+
///
530531
/// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
531532
/// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
532533
/// and [webkit-web-context-register-uri-scheme](https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebContext.html#webkit-web-context-register-uri-scheme) on Linux.
@@ -547,7 +548,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
547548
///
548549
/// fn init<R: Runtime>() -> TauriPlugin<R> {
549550
/// Builder::new("myplugin")
550-
/// .register_uri_scheme_protocol("myscheme", |app, req| {
551+
/// .register_uri_scheme_protocol("myscheme", |_ctx, req| {
551552
/// http::Response::builder().body(Vec::new()).unwrap()
552553
/// })
553554
/// .build()
@@ -557,7 +558,10 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
557558
pub fn register_uri_scheme_protocol<
558559
N: Into<String>,
559560
T: Into<Cow<'static, [u8]>>,
560-
H: Fn(&AppHandle<R>, http::Request<Vec<u8>>) -> http::Response<T> + Send + Sync + 'static,
561+
H: Fn(UriSchemeContext<'_, R>, http::Request<Vec<u8>>) -> http::Response<T>
562+
+ Send
563+
+ Sync
564+
+ 'static,
561565
>(
562566
mut self,
563567
uri_scheme: N,
@@ -566,8 +570,8 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
566570
self.uri_scheme_protocols.insert(
567571
uri_scheme.into(),
568572
Arc::new(UriSchemeProtocol {
569-
protocol: Box::new(move |app, request, responder| {
570-
responder.respond(protocol(app, request))
573+
protocol: Box::new(move |ctx, request, responder| {
574+
responder.respond(protocol(ctx, request))
571575
}),
572576
}),
573577
);
@@ -589,7 +593,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
589593
///
590594
/// fn init<R: Runtime>() -> TauriPlugin<R> {
591595
/// Builder::new("myplugin")
592-
/// .register_asynchronous_uri_scheme_protocol("app-files", |_app, request, responder| {
596+
/// .register_asynchronous_uri_scheme_protocol("app-files", |_ctx, request, responder| {
593597
/// // skip leading `/`
594598
/// let path = request.uri().path()[1..].to_string();
595599
/// std::thread::spawn(move || {
@@ -616,7 +620,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
616620
#[must_use]
617621
pub fn register_asynchronous_uri_scheme_protocol<
618622
N: Into<String>,
619-
H: Fn(&AppHandle<R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync + 'static,
623+
H: Fn(UriSchemeContext<'_, R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync + 'static,
620624
>(
621625
mut self,
622626
uri_scheme: N,

examples/streaming/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn main() {
190190
download_video();
191191

192192
tauri::Builder::default()
193-
.register_asynchronous_uri_scheme_protocol("stream", move |_app, request, responder| {
193+
.register_asynchronous_uri_scheme_protocol("stream", move |_ctx, request, responder| {
194194
match get_stream_response(request) {
195195
Ok(http_response) => responder.respond(http_response),
196196
Err(e) => responder.respond(

0 commit comments

Comments
 (0)