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",
"url",
"urlencoding",
"webbrowser",
"zip",
]

View File

@ -10,16 +10,22 @@ use url::Url;
pub struct 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);
// TODO: Add a checkbox with a GUI toolkit
MessageDialog::new()
let result = MessageDialog::new()
.set_title("Open website?")
.set_level(MessageLevel::Info)
.set_description(message)
.set_buttons(MessageButtons::OkCancel)
.show()
== MessageDialogResult::Ok
.show();
if result != MessageDialogResult::Ok {
return;
}
}
crate::util::open_url(&url);
}
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();
}
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 }
async-io = "2.3.4"
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"] }
tokio = { workspace = true, features = ["net"] }

View File

@ -28,7 +28,7 @@ use tracing::warn;
use url::{ParseError, Url};
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>;
@ -182,26 +182,13 @@ impl<F: FutureSpawner + 'static, I: NavigatorInterface> NavigatorBackend
return;
}
if self.open_url_mode == OpenURLMode::Confirm {
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 {
if self.open_url_mode == OpenURLMode::Deny {
tracing::warn!("SWF tried to open a website, but opening a website is not allowed");
return;
}
// If the user confirmed or if in Allow mode, open the website
// 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),
};
let ask = self.open_url_mode == OpenURLMode::Confirm;
self.interface.navigate_to_website(modified_url, ask);
}
fn fetch(&self, request: Request) -> OwnedFuture<Box<dyn SuccessResponse>, ErrorResponse> {
@ -499,9 +486,7 @@ mod tests {
use super::*;
impl NavigatorInterface for () {
fn confirm_website_navigation(&self, _url: &Url) -> bool {
true
}
fn navigate_to_website(&self, _url: Url, _ask: bool) {}
fn open_file(&self, path: &Path) -> io::Result<File> {
File::open(path)