2019-09-01 19:24:04 +00:00
|
|
|
//! Navigator backend for web
|
|
|
|
|
2019-11-07 19:34:38 +00:00
|
|
|
use ruffle_core::backend::navigator::{Error, NavigationMethod, NavigatorBackend};
|
2019-09-01 19:24:04 +00:00
|
|
|
use std::collections::HashMap;
|
2019-11-07 19:34:38 +00:00
|
|
|
use std::future::Future;
|
2019-11-10 02:19:49 +00:00
|
|
|
use std::pin::Pin;
|
2019-09-04 02:15:03 +00:00
|
|
|
use wasm_bindgen::JsCast;
|
2019-11-07 19:34:38 +00:00
|
|
|
use wasm_bindgen_futures::spawn_local;
|
2019-09-17 03:37:11 +00:00
|
|
|
use web_sys::window;
|
2019-09-01 19:24:04 +00:00
|
|
|
|
2019-09-17 03:37:11 +00:00
|
|
|
pub struct WebNavigatorBackend {}
|
2019-09-01 19:24:04 +00:00
|
|
|
|
|
|
|
impl WebNavigatorBackend {
|
|
|
|
pub fn new() -> Self {
|
2019-09-17 03:37:11 +00:00
|
|
|
WebNavigatorBackend {}
|
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>,
|
|
|
|
vars_method: Option<(NavigationMethod, HashMap<String, String>)>,
|
|
|
|
) {
|
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
|
|
|
|
2019-11-10 02:19:49 +00:00
|
|
|
fn fetch(&self, _url: String) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>>>> {
|
|
|
|
Box::pin(async { Ok(Vec::new()) })
|
2019-11-07 19:34:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-10 02:19:49 +00:00
|
|
|
fn spawn_future(&mut self, future: Pin<Box<dyn Future<Output = ()> + 'static>>) {
|
2019-11-07 19:34:38 +00:00
|
|
|
spawn_local(future)
|
|
|
|
}
|
2019-09-17 03:37:11 +00:00
|
|
|
}
|