frontend_utils: Replace confirm_website_navigation with navigate_to_website of NavigatorInterface

This makes it possible to implement "Open URL" dialogs asynchronously.
This commit is contained in:
Kamil Jarosz 2024-08-28 17:27:25 +02:00
parent c9839a36f5
commit 263ef4696e
5 changed files with 32 additions and 32 deletions

1
Cargo.lock generated
View File

@ -4343,7 +4343,6 @@ dependencies = [
"tracing", "tracing",
"url", "url",
"urlencoding", "urlencoding",
"webbrowser",
"zip", "zip",
] ]

View File

@ -10,16 +10,22 @@ use url::Url;
pub struct DesktopNavigatorInterface; pub struct DesktopNavigatorInterface;
impl NavigatorInterface for DesktopNavigatorInterface { impl NavigatorInterface for DesktopNavigatorInterface {
fn confirm_website_navigation(&self, url: &Url) -> bool { fn navigate_to_website(&self, url: Url, ask: bool) {
if ask {
let message = format!("The SWF file wants to open the website {}", url); let message = format!("The SWF file wants to open the website {}", url);
// TODO: Add a checkbox with a GUI toolkit // TODO: Add a checkbox with a GUI toolkit
MessageDialog::new() let result = MessageDialog::new()
.set_title("Open website?") .set_title("Open website?")
.set_level(MessageLevel::Info) .set_level(MessageLevel::Info)
.set_description(message) .set_description(message)
.set_buttons(MessageButtons::OkCancel) .set_buttons(MessageButtons::OkCancel)
.show() .show();
== MessageDialogResult::Ok if result != MessageDialogResult::Ok {
return;
}
}
crate::util::open_url(&url);
} }
fn open_file(&self, path: &Path) -> io::Result<File> { fn open_file(&self, path: &Path) -> io::Result<File> {

View File

@ -283,3 +283,14 @@ pub fn plot_stats_in_tracy(instance: &wgpu::Instance) {
tracy.frame_mark(); tracy.frame_mark();
} }
pub fn open_url(url: &Url) {
// TODO: This opens local files in the browser while flash opens them
// in the default program for the respective filetype.
// This especially includes mailto links. Ruffle opens the browser which opens
// the preferred program while flash opens the preferred program directly.
match webbrowser::open(url.as_str()) {
Ok(_output) => {}
Err(e) => tracing::error!("Could not open URL {}: {}", url.as_str(), e),
};
}

View File

@ -23,7 +23,6 @@ async-channel = { workspace = true }
slotmap = { workspace = true } slotmap = { workspace = true }
async-io = "2.3.4" async-io = "2.3.4"
futures-lite = "2.3.0" futures-lite = "2.3.0"
webbrowser = "1.0.1"
reqwest = { version = "0.12.7", default-features = false, features = ["rustls-tls", "cookies", "charset", "http2", "macos-system-configuration"] } reqwest = { version = "0.12.7", default-features = false, features = ["rustls-tls", "cookies", "charset", "http2", "macos-system-configuration"] }
tokio = { workspace = true, features = ["net"] } tokio = { workspace = true, features = ["net"] }

View File

@ -28,7 +28,7 @@ use tracing::warn;
use url::{ParseError, Url}; use url::{ParseError, Url};
pub trait NavigatorInterface: Clone + Send + 'static { pub trait NavigatorInterface: Clone + Send + 'static {
fn confirm_website_navigation(&self, url: &Url) -> bool; fn navigate_to_website(&self, url: Url, ask: bool);
fn open_file(&self, path: &Path) -> io::Result<File>; fn open_file(&self, path: &Path) -> io::Result<File>;
@ -182,26 +182,13 @@ impl<F: FutureSpawner + 'static, I: NavigatorInterface> NavigatorBackend
return; return;
} }
if self.open_url_mode == OpenURLMode::Confirm { if self.open_url_mode == OpenURLMode::Deny {
if !self.interface.confirm_website_navigation(&modified_url) {
tracing::info!("SWF tried to open a website, but the user declined the request");
return;
}
} else if self.open_url_mode == OpenURLMode::Deny {
tracing::warn!("SWF tried to open a website, but opening a website is not allowed"); tracing::warn!("SWF tried to open a website, but opening a website is not allowed");
return; return;
} }
// If the user confirmed or if in Allow mode, open the website let ask = self.open_url_mode == OpenURLMode::Confirm;
self.interface.navigate_to_website(modified_url, ask);
// TODO: This opens local files in the browser while flash opens them
// in the default program for the respective filetype.
// This especially includes mailto links. Ruffle opens the browser which opens
// the preferred program while flash opens the preferred program directly.
match webbrowser::open(modified_url.as_ref()) {
Ok(_output) => {}
Err(e) => tracing::error!("Could not open URL {}: {}", modified_url.as_str(), e),
};
} }
fn fetch(&self, request: Request) -> OwnedFuture<Box<dyn SuccessResponse>, ErrorResponse> { fn fetch(&self, request: Request) -> OwnedFuture<Box<dyn SuccessResponse>, ErrorResponse> {
@ -499,9 +486,7 @@ mod tests {
use super::*; use super::*;
impl NavigatorInterface for () { impl NavigatorInterface for () {
fn confirm_website_navigation(&self, _url: &Url) -> bool { fn navigate_to_website(&self, _url: Url, _ask: bool) {}
true
}
fn open_file(&self, path: &Path) -> io::Result<File> { fn open_file(&self, path: &Path) -> io::Result<File> {
File::open(path) File::open(path)