Skip to content

Commit a868cb7

Browse files
authored
refactor(core): clear uri_scheme_protocol registration function names (#1617)
* refactor(core): clear `custom_protocol` registration function names * fix * fix clippy * improve docs * clippy * renames
1 parent d623d95 commit a868cb7

File tree

10 files changed

+123
-116
lines changed

10 files changed

+123
-116
lines changed

.changes/multi-custom-protocols.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"tauri": patch
33
---
44

5-
Adds APIs to determine global and window-specific custom protocol handlers.
5+
Adds APIs to determine global and webview-specific URI scheme handlers.

core/tauri/src/api/dir.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,22 @@ mod test {
120120
if first.path.extension() == Some(OsStr::new("txt")) {
121121
// check the fields for the first DiskEntry
122122
assert_eq!(first.path, file_one);
123-
assert_eq!(first.children.is_some(), false);
123+
assert!(first.children.is_none());
124124
assert_eq!(first.name, name_from_path(file_one));
125125

126126
// check the fields for the third DiskEntry
127127
assert_eq!(second.path, file_two);
128-
assert_eq!(second.children.is_some(), false);
128+
assert!(second.children.is_none());
129129
assert_eq!(second.name, name_from_path(file_two));
130130
} else {
131131
// check the fields for the second DiskEntry
132132
assert_eq!(first.path, file_two);
133-
assert_eq!(first.children.is_some(), false);
133+
assert!(first.children.is_none());
134134
assert_eq!(first.name, name_from_path(file_two));
135135

136136
// check the fields for the third DiskEntry
137137
assert_eq!(second.path, file_one);
138-
assert_eq!(second.children.is_some(), false);
138+
assert!(second.children.is_none());
139139
assert_eq!(second.name, name_from_path(file_one));
140140
}
141141
}
@@ -165,22 +165,22 @@ mod test {
165165
if first.path.extension() == Some(OsStr::new("txt")) {
166166
// check the fields for the first DiskEntry
167167
assert_eq!(first.path, PathBuf::from("test/api/test.txt"));
168-
assert_eq!(first.children.is_some(), false);
168+
assert!(first.children.is_none());
169169
assert_eq!(first.name, Some("test.txt".to_string()));
170170

171171
// check the fields for the second DiskEntry
172172
assert_eq!(second.path, PathBuf::from("test/api/test_binary"));
173-
assert_eq!(second.children.is_some(), false);
173+
assert!(second.children.is_none());
174174
assert_eq!(second.name, Some("test_binary".to_string()));
175175
} else {
176176
// check the fields for the first DiskEntry
177177
assert_eq!(second.path, PathBuf::from("test/api/test.txt"));
178-
assert_eq!(second.children.is_some(), false);
178+
assert!(second.children.is_none());
179179
assert_eq!(second.name, Some("test.txt".to_string()));
180180

181181
// check the fields for the second DiskEntry
182182
assert_eq!(first.path, PathBuf::from("test/api/test_binary"));
183-
assert_eq!(first.children.is_some(), false);
183+
assert!(first.children.is_none());
184184
assert_eq!(first.name, Some("test_binary".to_string()));
185185
}
186186
}

core/tauri/src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ mod test {
242242
let l = listeners.inner.handlers.lock().unwrap();
243243

244244
// check if the generated key is in the map
245-
assert_eq!(l.contains_key(&key), true);
245+
assert!(l.contains_key(&key));
246246
}
247247

248248
// check to see if listen inputs a handler function properly into the LISTENERS map.

core/tauri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use std::path::PathBuf;
4949
// Export types likely to be used by the application.
5050
pub use {
5151
api::config::WindowUrl,
52-
hooks::{InvokeMessage, PageLoadPayload},
52+
hooks::{InvokeHandler, InvokeMessage, OnPageLoad, PageLoadPayload, SetupHook},
5353
runtime::app::{App, Builder},
5454
runtime::webview::Attributes,
5555
runtime::window::export::Window,

core/tauri/src/runtime/app.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ where
124124
/// All passed plugins
125125
plugins: PluginStore<Args<E, L, A, R>>,
126126

127-
/// The custom protocols available to all windows.
128-
custom_protocols: HashMap<String, Arc<CustomProtocol>>,
127+
/// The webview protocols available to all windows.
128+
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>,
129129
}
130130

131131
impl<E, L, A, R> Builder<E, L, A, R>
@@ -143,7 +143,7 @@ where
143143
on_page_load: Box::new(|_, _| ()),
144144
pending_windows: Default::default(),
145145
plugins: PluginStore::default(),
146-
custom_protocols: Default::default(),
146+
uri_scheme_protocols: Default::default(),
147147
}
148148
}
149149

@@ -192,19 +192,27 @@ where
192192
self
193193
}
194194

195-
/// Adds a custom protocol available to all windows.
196-
pub fn custom_protocol<
195+
/// Registers a URI scheme protocol available to all webviews.
196+
/// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
197+
/// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
198+
/// and [webkit-web-context-register-uri-scheme](https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebContext.html#webkit-web-context-register-uri-scheme) on Linux.
199+
///
200+
/// # Arguments
201+
///
202+
/// * `uri_scheme` The URI scheme to register, such as `example`.
203+
/// * `protocol` the protocol associated with the given URI scheme. It's a function that takes an URL such as `example://localhost/asset.css`.
204+
pub fn register_global_uri_scheme_protocol<
197205
N: Into<String>,
198206
H: Fn(&str) -> crate::Result<Vec<u8>> + Send + Sync + 'static,
199207
>(
200208
mut self,
201-
name: N,
202-
handler: H,
209+
uri_scheme: N,
210+
protocol: H,
203211
) -> Self {
204-
self.custom_protocols.insert(
205-
name.into(),
212+
self.uri_scheme_protocols.insert(
213+
uri_scheme.into(),
206214
Arc::new(CustomProtocol {
207-
handler: Box::new(handler),
215+
protocol: Box::new(protocol),
208216
}),
209217
);
210218
self
@@ -217,7 +225,7 @@ where
217225
self.plugins,
218226
self.invoke_handler,
219227
self.on_page_load,
220-
self.custom_protocols,
228+
self.uri_scheme_protocols,
221229
);
222230

223231
// set up all the windows defined in the config

core/tauri/src/runtime/flavors/wry.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl TryFrom<Icon> for WryIcon {
4444
#[derive(Default, Clone)]
4545
pub struct WryAttributes {
4646
attributes: wry::Attributes,
47-
custom_protocols: Arc<Mutex<HashMap<String, wry::CustomProtocol>>>,
47+
uri_scheme_protocols: Arc<Mutex<HashMap<String, wry::CustomProtocol>>>,
4848
}
4949

5050
impl AttributesBase for WryAttributes {}
@@ -197,24 +197,24 @@ impl Attributes for WryAttributes {
197197
self
198198
}
199199

200-
fn has_custom_protocol(&self, name: &str) -> bool {
201-
self.custom_protocols.lock().unwrap().contains_key(name)
200+
fn has_uri_scheme_protocol(&self, name: &str) -> bool {
201+
self.uri_scheme_protocols.lock().unwrap().contains_key(name)
202202
}
203203

204-
fn custom_protocol<
204+
fn register_uri_scheme_protocol<
205205
N: Into<String>,
206206
H: Fn(&str) -> crate::Result<Vec<u8>> + Send + Sync + 'static,
207207
>(
208208
self,
209-
name: N,
210-
handler: H,
209+
uri_scheme: N,
210+
protocol: H,
211211
) -> Self {
212-
let name = name.into();
213-
self.custom_protocols.lock().unwrap().insert(
214-
name.clone(),
212+
let uri_scheme = uri_scheme.into();
213+
self.uri_scheme_protocols.lock().unwrap().insert(
214+
uri_scheme.clone(),
215215
wry::CustomProtocol {
216-
name,
217-
handler: Box::new(move |data| (handler)(data).map_err(|_| wry::Error::InitScriptError)),
216+
name: uri_scheme,
217+
handler: Box::new(move |data| (protocol)(data).map_err(|_| wry::Error::InitScriptError)),
218218
},
219219
);
220220
self
@@ -276,9 +276,9 @@ impl Dispatch for WryDispatcher {
276276
let file_drop_handler = file_drop_handler
277277
.map(|handler| create_file_drop_handler(proxy.clone(), label.clone(), handler));
278278

279-
let custom_protocols = {
279+
let uri_scheme_protocols = {
280280
let mut lock = attributes
281-
.custom_protocols
281+
.uri_scheme_protocols
282282
.lock()
283283
.expect("poisoned custom protocols");
284284
std::mem::take(&mut *lock)
@@ -289,7 +289,7 @@ impl Dispatch for WryDispatcher {
289289
.add_window_with_configs(
290290
attributes.attributes,
291291
rpc_handler,
292-
custom_protocols.into_iter().map(|(_, p)| p).collect(),
292+
uri_scheme_protocols.into_iter().map(|(_, p)| p).collect(),
293293
file_drop_handler,
294294
)
295295
.map_err(|e| crate::Error::CreateWebview(e.to_string()))?;
@@ -490,9 +490,9 @@ impl Runtime for Wry {
490490
let file_drop_handler = file_drop_handler
491491
.map(|handler| create_file_drop_handler(proxy.clone(), label.clone(), handler));
492492

493-
let custom_protocols = {
493+
let uri_scheme_protocols = {
494494
let mut lock = attributes
495-
.custom_protocols
495+
.uri_scheme_protocols
496496
.lock()
497497
.expect("poisoned custom protocols");
498498
std::mem::take(&mut *lock)
@@ -503,7 +503,7 @@ impl Runtime for Wry {
503503
.add_window_with_configs(
504504
attributes.attributes,
505505
rpc_handler,
506-
custom_protocols.into_iter().map(|(_, p)| p).collect(),
506+
uri_scheme_protocols.into_iter().map(|(_, p)| p).collect(),
507507
file_drop_handler,
508508
)
509509
.map_err(|e| crate::Error::CreateWebview(e.to_string()))?;

core/tauri/src/runtime/manager.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ pub struct InnerWindowManager<P: Params> {
5151
/// A list of salts that are valid for the current application.
5252
salts: Mutex<HashSet<Uuid>>,
5353
package_info: PackageInfo,
54-
/// The custom protocols available to all windows.
55-
custom_protocols: HashMap<String, std::sync::Arc<CustomProtocol>>,
54+
/// The webview protocols protocols available to all windows.
55+
uri_scheme_protocols: HashMap<String, std::sync::Arc<CustomProtocol>>,
5656
}
5757

5858
/// A [Zero Sized Type] marker representing a full [`Params`].
@@ -104,7 +104,7 @@ impl<P: Params> WindowManager<P> {
104104
plugins: PluginStore<P>,
105105
invoke_handler: Box<InvokeHandler<P>>,
106106
on_page_load: Box<OnPageLoad<P>>,
107-
custom_protocols: HashMap<String, std::sync::Arc<CustomProtocol>>,
107+
uri_scheme_protocols: HashMap<String, std::sync::Arc<CustomProtocol>>,
108108
) -> Self {
109109
Self {
110110
inner: Arc::new(InnerWindowManager {
@@ -118,7 +118,7 @@ impl<P: Params> WindowManager<P> {
118118
default_window_icon: context.default_window_icon,
119119
salts: Mutex::default(),
120120
package_info: context.package_info,
121-
custom_protocols,
121+
uri_scheme_protocols,
122122
}),
123123
_marker: Args::default(),
124124
}
@@ -179,15 +179,17 @@ impl<P: Params> WindowManager<P> {
179179
}
180180
}
181181

182-
for (name, protocol) in &self.inner.custom_protocols {
183-
if !attributes.has_custom_protocol(name) {
182+
for (uri_scheme, protocol) in &self.inner.uri_scheme_protocols {
183+
if !attributes.has_uri_scheme_protocol(uri_scheme) {
184184
let protocol = protocol.clone();
185-
attributes = attributes.custom_protocol(name.clone(), move |p| (protocol.handler)(p));
185+
attributes = attributes
186+
.register_uri_scheme_protocol(uri_scheme.clone(), move |p| (protocol.protocol)(p));
186187
}
187188
}
188189

189-
if !attributes.has_custom_protocol("tauri") {
190-
attributes = attributes.custom_protocol("tauri", self.prepare_custom_protocol().handler);
190+
if !attributes.has_uri_scheme_protocol("tauri") {
191+
attributes = attributes
192+
.register_uri_scheme_protocol("tauri", self.prepare_uri_scheme_protocol().protocol);
191193
}
192194

193195
let local_app_data = resolve_path(
@@ -233,10 +235,10 @@ impl<P: Params> WindowManager<P> {
233235
})
234236
}
235237

236-
fn prepare_custom_protocol(&self) -> CustomProtocol {
238+
fn prepare_uri_scheme_protocol(&self) -> CustomProtocol {
237239
let assets = self.inner.assets.clone();
238240
CustomProtocol {
239-
handler: Box::new(move |path| {
241+
protocol: Box::new(move |path| {
240242
let mut path = path
241243
.split('?')
242244
// ignore query string

core/tauri/src/runtime/webview.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,25 @@ pub trait Attributes: AttributesBase {
9292
/// Sets the webview url.
9393
fn url(self, url: String) -> Self;
9494

95-
/// Whether the custom protocol handler is defined or not.
96-
fn has_custom_protocol(&self, name: &str) -> bool;
97-
98-
/// Adds a custom protocol handler.
99-
fn custom_protocol<
95+
/// Whether the webview URI scheme protocol is defined or not.
96+
fn has_uri_scheme_protocol(&self, name: &str) -> bool;
97+
98+
/// Registers a webview protocol handler.
99+
/// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
100+
/// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
101+
/// and [webkit-web-context-register-uri-scheme](https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebContext.html#webkit-web-context-register-uri-scheme) on Linux.
102+
///
103+
/// # Arguments
104+
///
105+
/// * `uri_scheme` The URI scheme to register, such as `example`.
106+
/// * `protocol` the protocol associated with the given URI scheme. It's a function that takes an URL such as `example://localhost/asset.css`.
107+
fn register_uri_scheme_protocol<
100108
N: Into<String>,
101109
H: Fn(&str) -> crate::Result<Vec<u8>> + Send + Sync + 'static,
102110
>(
103111
self,
104-
name: N,
105-
handler: H,
112+
uri_scheme: N,
113+
protocol: H,
106114
) -> Self;
107115

108116
/// The full attributes.
@@ -117,10 +125,10 @@ pub struct RpcRequest {
117125
pub params: Option<JsonValue>,
118126
}
119127

120-
/// Uses a custom handler to resolve file requests
128+
/// Uses a custom URI scheme handler to resolve file requests
121129
pub struct CustomProtocol {
122130
/// Handler for protocol
123-
pub handler: Box<dyn Fn(&str) -> crate::Result<Vec<u8>> + Send + Sync>,
131+
pub protocol: Box<dyn Fn(&str) -> crate::Result<Vec<u8>> + Send + Sync>,
124132
}
125133

126134
/// The file drop event payload.

0 commit comments

Comments
 (0)