feat: Migrate deep-link plugin to workspace repo

pull/564/head
FabianLars 2 years ago
parent 83148da959
commit bbce4a29e0
No known key found for this signature in database
GPG Key ID: 838F329885A9C43D

102
Cargo.lock generated

@ -328,9 +328,9 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5904a4d734f0235edf29aab320a14899f3e090446e594ff96508a6215f76f89c"
dependencies = [
"dirs",
"dirs 4.0.0",
"thiserror",
"winreg",
"winreg 0.10.1",
]
[[package]]
@ -1048,7 +1048,16 @@ version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
"dirs-sys 0.3.7",
]
[[package]]
name = "dirs"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [
"dirs-sys 0.4.1",
]
[[package]]
@ -1072,6 +1081,18 @@ dependencies = [
"winapi",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.48.0",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
@ -2113,6 +2134,19 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "interprocess"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81f2533f3be42fffe3b5e63b71aeca416c1c3bc33e4e27be018521e76b1f38fb"
dependencies = [
"cfg-if",
"libc",
"rustc_version",
"to_method",
"winapi",
]
[[package]]
name = "io-lifetimes"
version = "1.0.11"
@ -2835,6 +2869,28 @@ dependencies = [
"objc_exception",
]
[[package]]
name = "objc-sys"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99e1d07c6eab1ce8b6382b8e3c7246fe117ff3f8b34be065f5ebace6749fe845"
[[package]]
name = "objc2"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "559c5a40fdd30eb5e344fbceacf7595a81e242529fb4e21cf5f43fb4f11ff98d"
dependencies = [
"objc-sys",
"objc2-encode",
]
[[package]]
name = "objc2-encode"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d079845b37af429bfe5dfa76e6d087d788031045b25cfc6fd898486fd9847666"
[[package]]
name = "objc_exception"
version = "0.1.2"
@ -2918,6 +2974,12 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "ordered-stream"
version = "0.2.0"
@ -3533,7 +3595,7 @@ dependencies = [
"wasm-bindgen-futures",
"wasm-streams",
"web-sys",
"winreg",
"winreg 0.10.1",
]
[[package]]
@ -4370,7 +4432,7 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d93abb10fbd11335d31c33a70b2523c0caab348215caa2ce6da04a268c30afcb"
dependencies = [
"dirs",
"dirs 4.0.0",
"iota-crypto 0.15.3",
"libc",
"libsodium-sys",
@ -4663,6 +4725,20 @@ dependencies = [
"thiserror",
]
[[package]]
name = "tauri-plugin-deep-link"
version = "0.1.2"
dependencies = [
"dirs 5.0.1",
"interprocess",
"log",
"objc2",
"once_cell",
"tauri-utils",
"windows-sys 0.48.0",
"winreg 0.50.0",
]
[[package]]
name = "tauri-plugin-fs-extra"
version = "0.0.0"
@ -5039,6 +5115,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "to_method"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7c4ceeeca15c8384bbc3e011dbd8fccb7f068a440b752b7d9b32ceb0ca0e2e8"
[[package]]
name = "tokio"
version = "1.29.1"
@ -5953,6 +6035,16 @@ dependencies = [
"winapi",
]
[[package]]
name = "winreg"
version = "0.50.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1"
dependencies = [
"cfg-if",
"windows-sys 0.48.0",
]
[[package]]
name = "wry"
version = "0.24.3"

@ -0,0 +1,28 @@
[package]
name = "tauri-plugin-deep-link"
version = "0.1.2"
description = "Set your Tauri application as the default handler for an URL."
authors = [ "FabianLars <fabianlars@fabianlars.de>", "Tauri Programme within The Commons Conservancy" ]
license.workspace = true
edition.workspace = true
rust-version.workspace = true
readme = "README.md"
include = ["src/**", "Cargo.toml", "LICENSE_*"]
[dependencies]
log.workspace = true
dirs = "5"
once_cell = "1"
tauri-utils = "1"
[target.'cfg(windows)'.dependencies]
interprocess = { version = "1.2", default-features = false }
windows-sys = { version = "0.48.0", features = [
"Win32_Foundation",
"Win32_UI_Input_KeyboardAndMouse",
"Win32_UI_WindowsAndMessaging",
] }
winreg = "0.50.0"
[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.4.1"

@ -0,0 +1,20 @@
SPDXVersion: SPDX-2.1
DataLicense: CC0-1.0
PackageName: tauri
DataFormat: SPDXRef-1
PackageSupplier: Organization: The Tauri Programme in the Commons Conservancy
PackageHomePage: https://tauri.app
PackageLicenseDeclared: Apache-2.0
PackageLicenseDeclared: MIT
PackageCopyrightText: 2019-2023, The Tauri Programme in the Commons Conservancy
PackageSummary: <text>Tauri is a rust project that enables developers to make secure
and small desktop applications using a web frontend.
</text>
PackageComment: <text>The package includes the following libraries; see
Relationship information.
</text>
Created: 2019-05-20T09:00:00Z
PackageDownloadLocation: git://github.com/tauri-apps/tauri
PackageDownloadLocation: git+https://github.com/tauri-apps/tauri.git
PackageDownloadLocation: git+ssh://github.com/tauri-apps/tauri.git
Creator: Person: Daniel Thompson-Yvetot

@ -0,0 +1,177 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

@ -0,0 +1,22 @@
MIT License
Copyright (c) 2023 - Present Tauri Apps Contributors
Copyright (c) 2022 - 2023 FabianLars
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -0,0 +1,51 @@
![plugin-deep-link](banner.png)
Set your Tauri application as the default handler for an URL.
> Note: This plugins brings considerable security risks and you should only use it if you know what your are doing. If in doubt, use the default custom protocol implementation.
## Install
_This plugin requires a Rust version of at least **1.64**_
There are three general methods of installation that we can recommend.
1. Use crates.io and npm (easiest, and requires you to trust that our publishing pipeline worked)
2. Pull sources directly from Github using git tags / revision hashes (most secure)
3. Git submodule install this repo in your tauri project and then use file protocol to ingest the source (most secure, but inconvenient to use)
Install the Core plugin by adding the following to your `Cargo.toml` file:
`src-tauri/Cargo.toml`
```toml
[dependencies]
tauri-plugin-deep-link = "0.1.2"
# alternatively with Git:
tauri-plugin-deep-link = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
```
## Usage
Depending on your use case, for example a `Login with Google` button, you may want to take a look at https://github.com/FabianLars/tauri-plugin-oauth instead. It uses a minimalistic localhost server for the OAuth process instead of custom uri schemes because some oauth providers, like the aforementioned Google, require this setup. Personally, I think it's easier to use too.
Check out the [`example/`](https://github.com/tauri-apps/plugins-workspace/tree/v1/plugins/deep-link/example) directory for a minimal example. You must copy it into an actual tauri app first!
## macOS
In case you're one of the very few people that didn't know this already: macOS hates developers! Not only is that why the macOS implementation took me so long, it also means _you_ have to be a bit more careful if your app targets macOS:
- Read through the methods' platform-specific notes.
- On macOS you need to register the schemes in a `Info.plist` file at build time, the plugin can't change the schemes at runtime.
- macOS apps are in single-instance by default so this plugin will not manually shut down secondary instances in release mode.
- To make development via `tauri dev` a little bit more pleasant, the plugin will work similar-ish to Linux and Windows _in debug mode_ but you will see secondary instances show on the screen for a split second and the event will trigger twice in the primary instance (one of these events will be an empty string). You still have to install a `.app` bundle you got from `tauri build --debug` for this to work!
## Contributing
PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
## License
Code: (c) 2015 - Present - The Tauri Programme within The Commons Conservancy.
MIT or MIT/Apache 2.0 where applicable.

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

@ -0,0 +1,21 @@
<!-- Add this file next to your tauri.conf.json file -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<!-- Obviously needs to be replaced with your app's bundle identifier -->
<string>de.fabianlars.deep-link-test</string>
<key>CFBundleURLSchemes</key>
<array>
<!-- register the myapp:// and myscheme:// schemes -->
<string>myapp</string>
<string>myscheme</string>
</array>
</dict>
</array>
</dict>
</plist>

@ -0,0 +1,39 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::Manager;
fn main() {
// prepare() checks if it's a single instance and tries to send the args otherwise.
// It should always be the first line in your main function (with the exception of loggers or similar)
tauri_plugin_deep_link::prepare("de.fabianlars.deep-link-test");
// It's expected to use the identifier from tauri.conf.json
// Unfortuenetly getting it is pretty ugly without access to sth that implements `Manager`.
tauri::Builder::default()
.setup(|app| {
// If you need macOS support this must be called in .setup() !
// Otherwise this could be called right after prepare() but then you don't have access to tauri APIs
let handle = app.handle();
tauri_plugin_deep_link::register(
"my-scheme",
move |request| {
dbg!(&request);
handle.emit_all("scheme-request-received", request).unwrap();
},
)
.unwrap(/* If listening to the scheme is optional for your app, you don't want to unwrap here. */);
// If you also need the url when the primary instance was started by the custom scheme, you currently have to read it yourself
/*
#[cfg(not(target_os = "macos"))] // on macos the plugin handles this (macos doesn't use cli args for the url)
if let Some(url) = std::env::args().nth(1) {
app.emit_all("scheme-request-received", url).unwrap();
}
*/
Ok(())
})
// .plugin(tauri_plugin_deep_link::init()) // consider adding a js api later
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

@ -0,0 +1,63 @@
use std::io::{ErrorKind, Result};
use once_cell::sync::OnceCell;
#[cfg(target_os = "windows")]
#[path = "windows.rs"]
mod platform_impl;
#[cfg(target_os = "linux")]
#[path = "linux.rs"]
mod platform_impl;
#[cfg(target_os = "macos")]
#[path = "macos.rs"]
mod platform_impl;
static ID: OnceCell<String> = OnceCell::new();
/// This function is meant for use-cases where the default [`prepare()`] function can't be used.
///
/// # Errors
/// If ID was already set this functions returns an AlreadyExists error.
pub fn set_identifier(identifier: &str) -> Result<()> {
ID.set(identifier.to_string())
.map_err(|_| ErrorKind::AlreadyExists.into())
}
// Consider adding a function to register without starting the listener.
/// Registers a handler for the given scheme.
///
/// ## Platform-specific:
///
/// - **macOS**: On macOS schemes must be defined in an Info.plist file, therefore this function only calls [`listen()`] without registering the scheme. This function can only be called once on macOS.
pub fn register<F: FnMut(String) + Send + 'static>(scheme: &str, handler: F) -> Result<()> {
platform_impl::register(scheme, handler)
}
/// Starts the event listener without registering any schemes.
///
/// ## Platform-specific:
///
/// - **macOS**: This function can only be called once on macOS.
pub fn listen<F: FnMut(String) + Send + 'static>(handler: F) -> Result<()> {
platform_impl::listen(handler)
}
/// Unregister a previously registered scheme.
///
/// ## Platform-specific:
///
/// - **macOS**: This function has no effect on macOS.
pub fn unregister(scheme: &str) -> Result<()> {
platform_impl::unregister(scheme)
}
/// Checks if current instance is the primary instance.
/// Also sends the URL event data to the primary instance and stops the process afterwards.
///
/// ## Platform-specific:
///
/// - **macOS**: Only registers the identifier (only relevant in debug mode). It does not interact with the primary instance and does not exit the app.
pub fn prepare(identifier: &str) {
platform_impl::prepare(identifier)
}

@ -0,0 +1,141 @@
use std::{
fs::{create_dir_all, remove_file, File},
io::{Error, ErrorKind, Read, Result, Write},
os::unix::net::{UnixListener, UnixStream},
process::Command,
};
use dirs::data_dir;
use crate::ID;
pub fn register<F: FnMut(String) + Send + 'static>(scheme: &str, handler: F) -> Result<()> {
listen(handler)?;
let mut target = data_dir()
.ok_or_else(|| Error::new(ErrorKind::NotFound, "data directory not found."))?
.join("applications");
create_dir_all(&target)?;
let exe = tauri_utils::platform::current_exe()?;
let file_name = format!(
"{}-handler.desktop",
exe.file_name()
.ok_or_else(|| Error::new(
ErrorKind::NotFound,
"Couldn't get file name of curent executable.",
))?
.to_string_lossy()
);
target.push(&file_name);
let mime_types = format!("x-scheme-handler/{};", scheme);
let mut file = File::create(&target)?;
file.write_all(
format!(
include_str!("template.desktop"),
name = ID
.get()
.expect("Called register() before prepare()")
.split('.')
.last()
.unwrap(),
exec = std::env::var("APPIMAGE").unwrap_or_else(|_| exe.display().to_string()),
mime_types = mime_types
)
.as_bytes(),
)?;
target.pop();
Command::new("update-desktop-database")
.arg(target)
.status()?;
Command::new("xdg-mime")
.args(["default", &file_name, scheme])
.status()?;
Ok(())
}
pub fn unregister(_scheme: &str) -> Result<()> {
let mut target =
data_dir().ok_or_else(|| Error::new(ErrorKind::NotFound, "data directory not found."))?;
target.push("applications");
target.push(format!(
"{}-handler.desktop",
tauri_utils::platform::current_exe()?
.file_name()
.ok_or_else(|| Error::new(
ErrorKind::NotFound,
"Couldn't get file name of curent executable.",
))?
.to_string_lossy()
));
remove_file(&target)?;
Ok(())
}
pub fn listen<F: FnMut(String) + Send + 'static>(mut handler: F) -> Result<()> {
std::thread::spawn(move || {
let addr = format!(
"/tmp/{}-deep-link.sock",
ID.get().expect("listen() called before prepare()")
);
let listener = UnixListener::bind(addr).expect("Can't create listener");
for stream in listener.incoming() {
match stream {
Ok(mut stream) => {
let mut buffer = String::new();
if let Err(io_err) = stream.read_to_string(&mut buffer) {
log::error!("Error reading incoming connection: {}", io_err.to_string());
};
handler(dbg!(buffer));
}
Err(err) => {
log::error!("Incoming connection failed: {}", err);
continue;
}
}
}
});
Ok(())
}
pub fn prepare(identifier: &str) {
let addr = format!("/tmp/{}-deep-link.sock", identifier);
match UnixStream::connect(&addr) {
Ok(mut stream) => {
if let Err(io_err) =
stream.write_all(std::env::args().nth(1).unwrap_or_default().as_bytes())
{
log::error!(
"Error sending message to primary instance: {}",
io_err.to_string()
);
};
std::process::exit(0);
}
Err(err) => {
log::error!("Error creating socket listener: {}", err.to_string());
if err.kind() == ErrorKind::ConnectionRefused {
let _ = remove_file(&addr);
}
}
};
ID.set(identifier.to_string())
.expect("prepare() called more than once with different identifiers.");
}

@ -0,0 +1,184 @@
use std::{
fs::remove_file,
io::{ErrorKind, Read, Result, Write},
os::unix::net::{UnixListener, UnixStream},
sync::Mutex,
};
use objc2::{
class, declare_class, msg_send, msg_send_id,
mutability::Immutable,
rc::Id,
runtime::{AnyObject, NSObject},
sel, ClassType,
};
use once_cell::sync::OnceCell;
use crate::ID;
type THandler = OnceCell<Mutex<Box<dyn FnMut(String) + Send + 'static>>>;
// If the Mutex turns out to be a problem, or FnMut turns out to be useless, we can remove the Mutex and turn FnMut into Fn
static HANDLER: THandler = OnceCell::new();
pub fn register<F: FnMut(String) + Send + 'static>(_scheme: &str, handler: F) -> Result<()> {
listen(handler)?;
Ok(())
}
pub fn unregister(_scheme: &str) -> Result<()> {
Ok(())
}
// kInternetEventClass
const EVENT_CLASS: u32 = 0x4755524c;
// kAEGetURL
const EVENT_GET_URL: u32 = 0x4755524c;
// Adapted from https://github.com/mrmekon/fruitbasket/blob/aad14e400d710d1d46317c0d8c55ff742bfeaadd/src/osx.rs#L848
fn parse_url_event(event: *mut AnyObject) -> Option<String> {
if event as u64 == 0u64 {
return None;
}
unsafe {
let class: u32 = msg_send![event, eventClass];
let id: u32 = msg_send![event, eventID];
if class != EVENT_CLASS || id != EVENT_GET_URL {
return None;
}
let subevent: *mut AnyObject = msg_send![event, paramDescriptorForKeyword: 0x2d2d2d2d_u32];
let nsstring: *mut AnyObject = msg_send![subevent, stringValue];
let cstr: *const i8 = msg_send![nsstring, UTF8String];
if !cstr.is_null() {
Some(std::ffi::CStr::from_ptr(cstr).to_string_lossy().to_string())
} else {
None
}
}
}
declare_class!(
struct Handler;
unsafe impl ClassType for Handler {
type Super = NSObject;
type Mutability = Immutable;
const NAME: &'static str = "TauriPluginDeepLinkHandler";
}
unsafe impl Handler {
#[method(handleEvent:withReplyEvent:)]
fn handle_event(&self, event: *mut AnyObject, _replace: *const AnyObject) {
let s = parse_url_event(event).unwrap_or_default();
let mut cb = HANDLER.get().unwrap().lock().unwrap();
cb(s);
}
}
);
impl Handler {
pub fn new() -> Id<Self> {
let cls = Self::class();
unsafe { msg_send_id![msg_send_id![cls, alloc], init] }
}
}
#[cfg(debug_assertions)]
fn secondary_handler(s: String) {
let addr = format!(
"/tmp/{}-deep-link.sock",
ID.get()
.expect("URL event received before prepare() was called")
);
if let Ok(mut stream) = UnixStream::connect(addr) {
if let Err(io_err) = stream.write_all(s.as_bytes()) {
log::error!(
"Error sending message to primary instance: {}",
io_err.to_string()
);
};
}
std::process::exit(0);
}
pub fn listen<F: FnMut(String) + Send + 'static>(handler: F) -> Result<()> {
#[cfg(debug_assertions)]
let addr = format!(
"/tmp/{}-deep-link.sock",
ID.get().expect("listen() called before prepare()")
);
#[cfg(debug_assertions)]
if HANDLER
.set(match UnixStream::connect(&addr) {
Ok(_) => Mutex::new(Box::new(secondary_handler)),
Err(err) => {
log::error!("Error creating socket listener: {}", err.to_string());
if err.kind() == ErrorKind::ConnectionRefused {
let _ = remove_file(&addr);
}
Mutex::new(Box::new(handler))
}
})
.is_err()
{
return Err(std::io::Error::new(
ErrorKind::AlreadyExists,
"Handler was already set",
));
}
#[cfg(not(debug_assertions))]
if HANDLER.set(Mutex::new(Box::new(handler))).is_err() {
return Err(std::io::Error::new(
ErrorKind::AlreadyExists,
"Handler was already set",
));
}
unsafe {
let event_manager: Id<AnyObject> =
msg_send_id![class!(NSAppleEventManager), sharedAppleEventManager];
let handler = Handler::new();
let handler_boxed = Box::into_raw(Box::new(handler));
let _: () = msg_send![&event_manager,
setEventHandler: &**handler_boxed
andSelector: sel!(handleEvent:withReplyEvent:)
forEventClass:EVENT_CLASS
andEventID:EVENT_GET_URL];
}
#[cfg(debug_assertions)]
std::thread::spawn(move || {
let listener = UnixListener::bind(addr).expect("Can't create listener");
for stream in listener.incoming() {
match stream {
Ok(mut stream) => {
let mut buffer = String::new();
if let Err(io_err) = stream.read_to_string(&mut buffer) {
log::error!("Error reading incoming connection: {}", io_err.to_string());
};
let mut cb = HANDLER.get().unwrap().lock().unwrap();
cb(buffer);
}
Err(err) => {
log::error!("Incoming connection failed: {}", err);
continue;
}
}
}
});
Ok(())
}
pub fn prepare(identifier: &str) {
ID.set(identifier.to_string())
.expect("prepare() called more than once with different identifiers.");
}

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Application
Name={name}
Exec={exec} %u
Terminal=false
MimeType={mime_types}
NoDisplay=true

@ -0,0 +1,145 @@
use std::{
io::{BufRead, BufReader, Result, Write},
path::Path,
};
use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
use windows_sys::Win32::UI::{
Input::KeyboardAndMouse::{SendInput, INPUT, INPUT_0, INPUT_KEYBOARD, KEYBDINPUT},
WindowsAndMessaging::{AllowSetForegroundWindow, ASFW_ANY},
};
use winreg::{enums::HKEY_CURRENT_USER, RegKey};
use crate::ID;
pub fn register<F: FnMut(String) + Send + 'static>(scheme: &str, handler: F) -> Result<()> {
listen(handler)?;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let base = Path::new("Software").join("Classes").join(scheme);
let exe = tauri_utils::platform::current_exe()?
.display()
.to_string()
.replace("\\\\?\\", "");
let (key, _) = hkcu.create_subkey(&base)?;
key.set_value(
"",
&format!(
"URL:{}",
ID.get().expect("register() called before prepare()")
),
)?;
key.set_value("URL Protocol", &"")?;
let (icon, _) = hkcu.create_subkey(base.join("DefaultIcon"))?;
icon.set_value("", &format!("{},0", &exe))?;
let (cmd, _) = hkcu.create_subkey(base.join("shell").join("open").join("command"))?;
cmd.set_value("", &format!("{} \"%1\"", &exe))?;
Ok(())
}
pub fn unregister(scheme: &str) -> Result<()> {
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let base = Path::new("Software").join("Classes").join(scheme);
hkcu.delete_subkey_all(base)?;
Ok(())
}
pub fn listen<F: FnMut(String) + Send + 'static>(mut handler: F) -> Result<()> {
std::thread::spawn(move || {
let listener =
LocalSocketListener::bind(ID.get().expect("listen() called before prepare()").as_str())
.expect("Can't create listener");
for conn in listener.incoming().filter_map(|c| {
c.map_err(|error| log::error!("Incoming connection failed: {}", error))
.ok()
}) {
// Listen for the launch arguments
let mut conn = BufReader::new(conn);
let mut buffer = String::new();
if let Err(io_err) = conn.read_line(&mut buffer) {
log::error!("Error reading incoming connection: {}", io_err.to_string());
};
buffer.pop();
handler(buffer);
}
});
Ok(())
}
pub fn prepare(identifier: &str) {
if let Ok(mut conn) = LocalSocketStream::connect(identifier) {
// We are the secondary instance.
// Prep to activate primary instance by allowing another process to take focus.
// A workaround to allow AllowSetForegroundWindow to succeed - press a key.
// This was originally used by Chromium: https://bugs.chromium.org/p/chromium/issues/detail?id=837796
dummy_keypress();
let primary_instance_pid = conn.peer_pid().unwrap_or(ASFW_ANY);
unsafe {
let success = AllowSetForegroundWindow(primary_instance_pid) != 0;
if !success {
log::warn!("AllowSetForegroundWindow failed.");
}
}
if let Err(io_err) = conn.write_all(std::env::args().nth(1).unwrap_or_default().as_bytes())
{
log::error!(
"Error sending message to primary instance: {}",
io_err.to_string()
);
};
let _ = conn.write_all(b"\n");
std::process::exit(0);
};
ID.set(identifier.to_string())
.expect("prepare() called more than once with different identifiers.");
}
/// Send a dummy keypress event so AllowSetForegroundWindow can succeed
fn dummy_keypress() {
let keyboard_input_down = KEYBDINPUT {
wVk: 0, // This doesn't correspond to any actual keyboard key, but should still function for the workaround.
dwExtraInfo: 0,
wScan: 0,
time: 0,
dwFlags: 0,
};
let mut keyboard_input_up = keyboard_input_down;
keyboard_input_up.dwFlags = 0x0002; // KEYUP flag
let input_down_u = INPUT_0 {
ki: keyboard_input_down,
};
let input_up_u = INPUT_0 {
ki: keyboard_input_up,
};
let input_down = INPUT {
r#type: INPUT_KEYBOARD,
Anonymous: input_down_u,
};
let input_up = INPUT {
r#type: INPUT_KEYBOARD,
Anonymous: input_up_u,
};
let ipsize = std::mem::size_of::<INPUT>() as i32;
unsafe {
SendInput(2, [input_down, input_up].as_ptr(), ipsize);
};
}
Loading…
Cancel
Save