Skip to content

Commit

Permalink
web: Adds support for base embed/object attribute (Part of #4258)
Browse files Browse the repository at this point in the history
hatal175 authored and adrian17 committed Aug 19, 2021

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 985a97d commit 8cb5cf0
Showing 5 changed files with 57 additions and 10 deletions.
8 changes: 8 additions & 0 deletions web/packages/core/src/load-options.ts
Original file line number Diff line number Diff line change
@@ -178,6 +178,14 @@ export interface BaseLoadOptions {
secs: number;
nanos: number;
};

/**
* Specifies the base directory or URL used to resolve all relative path statements in the SWF file.
* null means the current directory.
*
* @default null
*/
base?: string | null;
}

/**
7 changes: 6 additions & 1 deletion web/packages/core/src/ruffle-embed.ts
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ export class RuffleEmbed extends RufflePlayer {
),
parameters: this.attributes.getNamedItem("flashvars")?.value,
backgroundColor: this.attributes.getNamedItem("bgcolor")?.value,
base: this.attributes.getNamedItem("base")?.value,
});
}
}
@@ -105,7 +106,11 @@ export class RuffleEmbed extends RufflePlayer {
}
const src = this.attributes.getNamedItem("src");
if (src) {
this.load({ url: src.value, parameters });
this.load({
url: src.value,
parameters,
base: this.attributes.getNamedItem("base")?.value,
});
}
}
}
9 changes: 9 additions & 0 deletions web/packages/core/src/ruffle-object.ts
Original file line number Diff line number Diff line change
@@ -112,6 +112,12 @@ export class RuffleObject extends RufflePlayer {
this.getAttribute("bgcolor")
);

const base = findCaseInsensitive(
this.params,
"base",
this.getAttribute("base")
);

if (url) {
const options: URLLoadOptions = { url };
options.allowScriptAccess = isScriptAccessAllowed(
@@ -124,6 +130,9 @@ export class RuffleObject extends RufflePlayer {
if (backgroundColor) {
options.backgroundColor = backgroundColor;
}
if (base) {
options.base = base;
}

// Kick off the SWF download.
this.load(options);
5 changes: 5 additions & 0 deletions web/src/lib.rs
Original file line number Diff line number Diff line change
@@ -138,6 +138,9 @@ pub struct Config {
#[serde(rename = "upgradeToHttps")]
upgrade_to_https: bool,

#[serde(rename = "base")]
base_url: Option<String>,

#[serde(rename = "warnOnUnsupportedContent")]
warn_on_unsupported_content: bool,

@@ -155,6 +158,7 @@ impl Default for Config {
background_color: Default::default(),
letterbox: Default::default(),
upgrade_to_https: true,
base_url: None,
warn_on_unsupported_content: true,
log_level: log::Level::Error,
max_execution_duration: Duration::from_secs(15),
@@ -457,6 +461,7 @@ impl Ruffle {
let navigator = Box::new(navigator::WebNavigatorBackend::new(
allow_script_access,
config.upgrade_to_https,
config.base_url,
));
let storage = match window.local_storage() {
Ok(Some(s)) => {
38 changes: 29 additions & 9 deletions web/src/navigator.rs
Original file line number Diff line number Diff line change
@@ -10,17 +10,24 @@ use std::time::Duration;
use url::Url;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::{spawn_local, JsFuture};
use web_sys::{window, Blob, BlobPropertyBag, Performance, Request, RequestInit, Response};
use web_sys::{
window, Blob, BlobPropertyBag, Document, Performance, Request, RequestInit, Response,
};

pub struct WebNavigatorBackend {
performance: Performance,
start_time: f64,
allow_script_access: bool,
upgrade_to_https: bool,
base_url: Option<String>,
}

impl WebNavigatorBackend {
pub fn new(allow_script_access: bool, upgrade_to_https: bool) -> Self {
pub fn new(
allow_script_access: bool,
upgrade_to_https: bool,
base_url: Option<String>,
) -> Self {
let window = web_sys::window().expect("window()");
let performance = window.performance().expect("window.performance()");

@@ -33,6 +40,17 @@ impl WebNavigatorBackend {
performance,
allow_script_access,
upgrade_to_https,
base_url,
}
}

fn base_uri(&self, document: &Document) -> Option<String> {
if let Some(base_url) = self.base_url.clone() {
Some(base_url)
} else if let Ok(Some(base_uri)) = document.base_uri() {
Some(base_uri)
} else {
None
}
}
}
@@ -51,12 +69,14 @@ impl NavigatorBackend for WebNavigatorBackend {

if let Some(window) = window() {
let document = window.document().expect("Could not get document");
let url = if let Ok(Some(base_uri)) = document.base_uri() {
if let Ok(new_url) = url_from_relative_url(&base_uri, &url) {
new_url
} else {
return;
}

let base_uri = match self.base_uri(&document) {
Some(base_uri) => base_uri,
_ => return,
};

let url = if let Ok(new_url) = url_from_relative_url(&base_uri, &url) {
new_url
} else {
return;
};
@@ -209,7 +229,7 @@ impl NavigatorBackend for WebNavigatorBackend {
let window = web_sys::window().expect("window()");
let document = window.document().expect("document()");

if let Ok(Some(base_uri)) = document.base_uri() {
if let Some(base_uri) = self.base_uri(&document) {
if let Ok(new_url) = url_from_relative_url(&base_uri, url) {
return String::from(new_url).into();
}

0 comments on commit 8cb5cf0

Please sign in to comment.