Skip to content

Commit

Permalink
core: Move OpenURLMode from core to web
Browse files Browse the repository at this point in the history
After changes in desktop, this is the only place where it is used.
This additionally renames it to OpenUrlMode.
  • Loading branch information
kjarosh committed Oct 10, 2024
1 parent 308c91c commit 7a60229
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 41 deletions.
18 changes: 0 additions & 18 deletions core/src/backend/navigator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,6 @@ pub enum SocketMode {
Ask,
}

/// The handling mode of links opening a new website.
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum OpenURLMode {
/// Allow all links to open a new website.
#[cfg_attr(feature = "serde", serde(rename = "allow"))]
Allow,

/// A confirmation dialog opens with every link trying to open a new website.
#[cfg_attr(feature = "serde", serde(rename = "confirm"))]
Confirm,

/// Deny all links to open a new website.
#[cfg_attr(feature = "serde", serde(rename = "deny"))]
Deny,
}

impl NavigationMethod {
/// Convert an SWF method enum into a NavigationMethod.
pub fn from_send_vars_method(s: SendVarsMethod) -> Option<Self> {
Expand Down
12 changes: 1 addition & 11 deletions desktop/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::preferences::storage::StorageBackend;
use crate::RUFFLE_VERSION;
use anyhow::{anyhow, Error};
use clap::{Parser, ValueEnum};
use ruffle_core::backend::navigator::{OpenURLMode, SocketMode};
use ruffle_core::backend::navigator::SocketMode;
use ruffle_core::config::Letterbox;
use ruffle_core::events::{GamepadButton, KeyCode};
use ruffle_core::{LoadBehavior, PlayerRuntime, StageAlign, StageScaleMode};
Expand Down Expand Up @@ -372,16 +372,6 @@ impl FromStr for OpenUrlMode {
}
}

impl From<OpenUrlMode> for OpenURLMode {
fn from(value: OpenUrlMode) -> Self {
match value {
OpenUrlMode::Confirm => OpenURLMode::Confirm,
OpenUrlMode::Allow => OpenURLMode::Allow,
OpenUrlMode::Deny => OpenURLMode::Deny,
}
}
}

// TODO The following enum exists in order to preserve
// the behavior of mapping gamepad buttons,
// We should probably do something smarter here.
Expand Down
13 changes: 6 additions & 7 deletions web/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::external_interface::JavascriptInterface;
use crate::navigator::WebNavigatorBackend;
use crate::navigator::{OpenUrlMode, WebNavigatorBackend};
use crate::{
audio, log_adapter, storage, ui, JavascriptPlayer, RuffleHandle, SocketProxy,
RUFFLE_GLOBAL_PANIC,
};
use js_sys::Promise;
use ruffle_core::backend::audio::{AudioBackend, NullAudioBackend};
use ruffle_core::backend::navigator::OpenURLMode;
use ruffle_core::backend::storage::{MemoryStorageBackend, StorageBackend};
use ruffle_core::backend::ui::FontDefinition;
use ruffle_core::compatibility_rules::CompatibilityRules;
Expand Down Expand Up @@ -54,7 +53,7 @@ pub struct RuffleInstanceBuilder {
pub(crate) max_execution_duration: Duration,
pub(crate) player_version: Option<u8>,
pub(crate) preferred_renderer: Option<String>, // TODO: Enumify?
pub(crate) open_url_mode: OpenURLMode,
pub(crate) open_url_mode: OpenUrlMode,
pub(crate) allow_networking: NetworkingAccessMode,
pub(crate) socket_proxy: Vec<SocketProxy>,
pub(crate) credential_allow_list: Vec<String>,
Expand Down Expand Up @@ -90,7 +89,7 @@ impl Default for RuffleInstanceBuilder {
max_execution_duration: Duration::from_secs_f64(15.0),
player_version: None,
preferred_renderer: None,
open_url_mode: OpenURLMode::Allow,
open_url_mode: OpenUrlMode::Allow,
allow_networking: NetworkingAccessMode::All,
socket_proxy: vec![],
credential_allow_list: vec![],
Expand Down Expand Up @@ -248,9 +247,9 @@ impl RuffleInstanceBuilder {
#[wasm_bindgen(js_name = "setOpenUrlMode")]
pub fn set_open_url_mode(&mut self, value: &str) {
self.open_url_mode = match value {
"allow" => OpenURLMode::Allow,
"confirm" => OpenURLMode::Confirm,
"deny" => OpenURLMode::Deny,
"allow" => OpenUrlMode::Allow,
"confirm" => OpenUrlMode::Confirm,
"deny" => OpenUrlMode::Deny,
_ => return,
};
}
Expand Down
26 changes: 21 additions & 5 deletions web/src/navigator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use gloo_net::websocket::{futures::WebSocket, Message};
use js_sys::{Array, Promise, Uint8Array};
use ruffle_core::backend::navigator::{
async_return, create_fetch_error, create_specific_fetch_error, get_encoding, ErrorResponse,
NavigationMethod, NavigatorBackend, OpenURLMode, OwnedFuture, Request, SuccessResponse,
NavigationMethod, NavigatorBackend, OwnedFuture, Request, SuccessResponse,
};
use ruffle_core::config::NetworkingAccessMode;
use ruffle_core::indexmap::IndexMap;
Expand All @@ -32,13 +32,29 @@ use web_sys::{
RequestCredentials, RequestInit, Response as WebResponse,
};

/// The handling mode of links opening a new website.
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum OpenUrlMode {
/// Allow all links to open a new website.
#[serde(rename = "allow")]
Allow,

/// A confirmation dialog opens with every link trying to open a new website.
#[serde(rename = "confirm")]
Confirm,

/// Deny all links to open a new website.
#[serde(rename = "deny")]
Deny,
}

pub struct WebNavigatorBackend {
log_subscriber: Arc<Layered<WASMLayer, Registry>>,
allow_script_access: bool,
allow_networking: NetworkingAccessMode,
upgrade_to_https: bool,
base_url: Option<Url>,
open_url_mode: OpenURLMode,
open_url_mode: OpenUrlMode,
socket_proxies: Vec<SocketProxy>,
credential_allow_list: Vec<String>,
player: Weak<Mutex<Player>>,
Expand All @@ -52,7 +68,7 @@ impl WebNavigatorBackend {
upgrade_to_https: bool,
base_url: Option<String>,
log_subscriber: Arc<Layered<WASMLayer, Registry>>,
open_url_mode: OpenURLMode,
open_url_mode: OpenUrlMode,
socket_proxies: Vec<SocketProxy>,
credential_allow_list: Vec<String>,
) -> Self {
Expand Down Expand Up @@ -168,7 +184,7 @@ impl NavigatorBackend for WebNavigatorBackend {
let window = window().expect("window()");

if url.scheme() != "javascript" {
if self.open_url_mode == OpenURLMode::Confirm {
if self.open_url_mode == OpenUrlMode::Confirm {
let message = format!("The SWF file wants to open the website {}", &url);
// TODO: Add a checkbox with a GUI toolkit
let confirm = window
Expand All @@ -180,7 +196,7 @@ impl NavigatorBackend for WebNavigatorBackend {
);
return;
}
} else if self.open_url_mode == OpenURLMode::Deny {
} else if self.open_url_mode == OpenUrlMode::Deny {
tracing::warn!("SWF tried to open a website, but opening a website is not allowed");
return;
}
Expand Down

0 comments on commit 7a60229

Please sign in to comment.