2019-09-01 19:24:04 +00:00
|
|
|
//! Navigator backend for web
|
|
|
|
|
2020-01-12 22:06:27 +00:00
|
|
|
use js_sys::{Array, ArrayBuffer, Uint8Array};
|
2020-01-18 04:11:09 +00:00
|
|
|
use ruffle_core::backend::navigator::{
|
2020-07-23 22:30:39 +00:00
|
|
|
url_from_relative_url, NavigationMethod, NavigatorBackend, OwnedFuture, RequestOptions,
|
2020-01-18 04:11:09 +00:00
|
|
|
};
|
2020-08-28 17:16:13 +00:00
|
|
|
use ruffle_core::indexmap::IndexMap;
|
2020-06-18 08:36:04 +00:00
|
|
|
use ruffle_core::loader::Error;
|
2020-07-23 04:00:41 +00:00
|
|
|
use std::borrow::Cow;
|
2020-03-29 05:28:07 +00:00
|
|
|
use std::time::Duration;
|
2019-09-04 02:15:03 +00:00
|
|
|
use wasm_bindgen::JsCast;
|
2019-11-09 02:10:21 +00:00
|
|
|
use wasm_bindgen_futures::{spawn_local, JsFuture};
|
2020-03-29 05:28:07 +00:00
|
|
|
use web_sys::{window, Blob, BlobPropertyBag, Performance, Request, RequestInit, Response};
|
2019-09-01 19:24:04 +00:00
|
|
|
|
2020-03-29 05:28:07 +00:00
|
|
|
pub struct WebNavigatorBackend {
|
|
|
|
performance: Performance,
|
|
|
|
start_time: f64,
|
|
|
|
}
|
2019-09-01 19:24:04 +00:00
|
|
|
|
|
|
|
impl WebNavigatorBackend {
|
|
|
|
pub fn new() -> Self {
|
2020-03-29 05:28:07 +00:00
|
|
|
let window = web_sys::window().expect("window()");
|
|
|
|
let performance = window.performance().expect("window.performance()");
|
|
|
|
|
|
|
|
WebNavigatorBackend {
|
|
|
|
start_time: performance.now(),
|
|
|
|
performance,
|
|
|
|
}
|
2019-09-01 19:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NavigatorBackend for WebNavigatorBackend {
|
2019-09-17 03:37:11 +00:00
|
|
|
fn navigate_to_url(
|
|
|
|
&self,
|
|
|
|
url: String,
|
|
|
|
window_spec: Option<String>,
|
2020-08-28 17:16:13 +00:00
|
|
|
vars_method: Option<(NavigationMethod, IndexMap<String, String>)>,
|
2019-09-17 03:37:11 +00:00
|
|
|
) {
|
2019-09-01 19:24:04 +00:00
|
|
|
if let Some(window) = window() {
|
2019-09-02 20:23:10 +00:00
|
|
|
//TODO: Should we return a result for failed opens? Does Flash care?
|
|
|
|
#[allow(unused_must_use)]
|
2019-09-04 02:15:03 +00:00
|
|
|
match (vars_method, window_spec) {
|
|
|
|
(Some((navmethod, formvars)), window_spec) => {
|
|
|
|
let document = match window.document() {
|
|
|
|
Some(document) => document,
|
2019-09-17 03:37:11 +00:00
|
|
|
None => return,
|
2019-09-04 02:15:03 +00:00
|
|
|
};
|
|
|
|
|
2019-09-17 03:37:11 +00:00
|
|
|
let form = document
|
|
|
|
.create_element("form")
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<web_sys::HtmlFormElement>()
|
|
|
|
.unwrap();
|
2019-09-04 02:15:03 +00:00
|
|
|
|
2019-09-17 03:37:11 +00:00
|
|
|
form.set_attribute(
|
|
|
|
"method",
|
|
|
|
match navmethod {
|
|
|
|
NavigationMethod::GET => "get",
|
|
|
|
NavigationMethod::POST => "post",
|
|
|
|
},
|
|
|
|
);
|
2019-09-04 02:15:03 +00:00
|
|
|
|
|
|
|
form.set_attribute("action", &url);
|
|
|
|
|
|
|
|
if let Some(target) = window_spec {
|
|
|
|
form.set_attribute("target", &target);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k, v) in formvars.iter() {
|
|
|
|
let hidden = document.create_element("hidden").unwrap();
|
|
|
|
|
|
|
|
hidden.set_attribute("type", "hidden");
|
|
|
|
hidden.set_attribute("name", k);
|
|
|
|
hidden.set_attribute("value", v);
|
|
|
|
|
|
|
|
form.append_child(&hidden);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body().unwrap().append_child(&form);
|
|
|
|
form.submit();
|
2019-09-17 03:37:11 +00:00
|
|
|
}
|
|
|
|
(_, Some(ref window_name)) if window_name != "" => {
|
|
|
|
window.open_with_url_and_target(&url, window_name);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
window.location().assign(&url);
|
|
|
|
}
|
2019-09-02 20:23:10 +00:00
|
|
|
};
|
2019-09-01 19:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-07 19:34:38 +00:00
|
|
|
|
2020-03-29 05:28:07 +00:00
|
|
|
fn time_since_launch(&mut self) -> Duration {
|
|
|
|
let dt = self.performance.now() - self.start_time;
|
|
|
|
Duration::from_millis(dt as u64)
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:36:04 +00:00
|
|
|
fn fetch(&self, url: &str, options: RequestOptions) -> OwnedFuture<Vec<u8>, Error> {
|
2020-06-19 16:32:22 +00:00
|
|
|
let url = url.to_string();
|
2019-11-09 02:10:21 +00:00
|
|
|
Box::pin(async move {
|
2020-01-12 22:06:27 +00:00
|
|
|
let mut init = RequestInit::new();
|
|
|
|
|
|
|
|
init.method(match options.method() {
|
|
|
|
NavigationMethod::GET => "GET",
|
|
|
|
NavigationMethod::POST => "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some((data, mime)) = options.body() {
|
|
|
|
let arraydata = ArrayBuffer::new(data.len() as u32);
|
|
|
|
let u8data = Uint8Array::new(&arraydata);
|
|
|
|
|
|
|
|
for (i, byte) in data.iter().enumerate() {
|
|
|
|
u8data.fill(*byte, i as u32, i as u32 + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let blobparts = Array::new();
|
|
|
|
blobparts.push(&arraydata);
|
|
|
|
|
|
|
|
let mut blobprops = BlobPropertyBag::new();
|
|
|
|
blobprops.type_(mime);
|
|
|
|
|
|
|
|
let datablob =
|
|
|
|
Blob::new_with_buffer_source_sequence_and_options(&blobparts, &blobprops)
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
init.body(Some(&datablob));
|
|
|
|
}
|
|
|
|
|
|
|
|
let request = Request::new_with_str_and_init(&url, &init).unwrap();
|
|
|
|
|
2019-11-09 02:10:21 +00:00
|
|
|
let window = web_sys::window().unwrap();
|
2020-01-12 22:06:27 +00:00
|
|
|
let fetchval = JsFuture::from(window.fetch_with_request(&request)).await;
|
2019-11-09 02:10:21 +00:00
|
|
|
if fetchval.is_err() {
|
2020-06-18 08:36:04 +00:00
|
|
|
return Err(Error::NetworkError(std::io::Error::new(
|
2020-06-17 22:35:36 +00:00
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Could not fetch, got JS Error",
|
|
|
|
)));
|
2019-11-09 02:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let resp: Response = fetchval.unwrap().dyn_into().unwrap();
|
|
|
|
let data: ArrayBuffer = JsFuture::from(resp.array_buffer().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into()
|
|
|
|
.unwrap();
|
|
|
|
let jsarray = Uint8Array::new(&data);
|
|
|
|
let mut rust_array = vec![0; jsarray.length() as usize];
|
|
|
|
jsarray.copy_to(&mut rust_array);
|
|
|
|
|
|
|
|
Ok(rust_array)
|
|
|
|
})
|
2019-11-07 19:34:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 08:36:04 +00:00
|
|
|
fn spawn_future(&mut self, future: OwnedFuture<(), Error>) {
|
2019-11-14 19:52:13 +00:00
|
|
|
spawn_local(async move {
|
|
|
|
if let Err(e) = future.await {
|
2020-09-19 14:27:24 +00:00
|
|
|
log::error!("Asynchronous error occurred: {}", e);
|
2019-11-14 19:52:13 +00:00
|
|
|
}
|
|
|
|
})
|
2019-11-07 19:34:38 +00:00
|
|
|
}
|
2020-07-23 04:00:41 +00:00
|
|
|
|
|
|
|
fn resolve_relative_url<'a>(&mut self, url: &'a str) -> Cow<'a, str> {
|
2020-07-23 22:30:39 +00:00
|
|
|
let window = web_sys::window().expect("window()");
|
|
|
|
let document = window.document().expect("document()");
|
|
|
|
|
|
|
|
if let Ok(Some(base_uri)) = document.base_uri() {
|
|
|
|
if let Ok(new_url) = url_from_relative_url(&base_uri, url) {
|
|
|
|
return new_url.into_string().into();
|
2020-07-23 04:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url.into()
|
|
|
|
}
|
2019-09-17 03:37:11 +00:00
|
|
|
}
|