use std::default::Default;
use std::rc::Rc;
use dom_struct::dom_struct;
use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
use script_traits::{Job, JobError, JobResult, JobResultValue, JobType, ScriptMsg};
use crate::dom::bindings::codegen::Bindings::ServiceWorkerContainerBinding::{
RegistrationOptions, ServiceWorkerContainerMethods,
};
use crate::dom::bindings::error::Error;
use crate::dom::bindings::refcounted::TrustedPromise;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::bindings::str::USVString;
use crate::dom::client::Client;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::dom::serviceworker::ServiceWorker;
use crate::dom::serviceworkerregistration::ServiceWorkerRegistration;
use crate::realms::{enter_realm, InRealm};
use crate::task::TaskCanceller;
use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
use crate::task_source::{TaskSource, TaskSourceName};
#[dom_struct]
pub struct ServiceWorkerContainer {
eventtarget: EventTarget,
controller: MutNullableDom<ServiceWorker>,
client: Dom<Client>,
}
impl ServiceWorkerContainer {
fn new_inherited(client: &Client) -> ServiceWorkerContainer {
ServiceWorkerContainer {
eventtarget: EventTarget::new_inherited(),
controller: Default::default(),
client: Dom::from_ref(client),
}
}
#[allow(crown::unrooted_must_root)]
pub fn new(global: &GlobalScope) -> DomRoot<ServiceWorkerContainer> {
let client = Client::new(global.as_window());
let container = ServiceWorkerContainer::new_inherited(&client);
reflect_dom_object(Box::new(container), global)
}
}
impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
fn GetController(&self) -> Option<DomRoot<ServiceWorker>> {
self.client.get_controller()
}
fn Register(
&self,
script_url: USVString,
options: &RegistrationOptions,
comp: InRealm,
) -> Rc<Promise> {
let global = self.client.global();
let promise = Promise::new_in_current_realm(comp);
let USVString(ref script_url) = script_url;
let api_base_url = global.api_base_url();
let script_url = match api_base_url.join(script_url) {
Ok(url) => url,
Err(_) => {
promise.reject_error(Error::Type("Invalid script URL".to_owned()));
return promise;
},
};
let scope = match options.scope {
Some(ref scope) => {
let USVString(inner_scope) = scope;
match api_base_url.join(inner_scope) {
Ok(url) => url,
Err(_) => {
promise.reject_error(Error::Type("Invalid scope URL".to_owned()));
return promise;
},
}
},
None => script_url.join("./").unwrap(),
};
match script_url.scheme() {
"https" | "http" => {},
_ => {
promise.reject_error(Error::Type("Only secure origins are allowed".to_owned()));
return promise;
},
}
if script_url.path().to_ascii_lowercase().contains("%2f") ||
script_url.path().to_ascii_lowercase().contains("%5c")
{
promise.reject_error(Error::Type(
"Script URL contains forbidden characters".to_owned(),
));
return promise;
}
match scope.scheme() {
"https" | "http" => {},
_ => {
promise.reject_error(Error::Type("Only secure origins are allowed".to_owned()));
return promise;
},
}
if scope.path().to_ascii_lowercase().contains("%2f") ||
scope.path().to_ascii_lowercase().contains("%5c")
{
promise.reject_error(Error::Type(
"Scope URL contains forbidden characters".to_owned(),
));
return promise;
}
let (task_source, task_canceller) = (
global.dom_manipulation_task_source(),
global.task_canceller(TaskSourceName::DOMManipulation),
);
let mut handler = RegisterJobResultHandler {
trusted_promise: Some(TrustedPromise::new(promise.clone())),
task_source,
task_canceller,
};
let (job_result_sender, job_result_receiver) = ipc::channel().expect("ipc channel failure");
ROUTER.add_route(
job_result_receiver.to_opaque(),
Box::new(move |message| {
let msg = message.to();
match msg {
Ok(msg) => handler.handle(msg),
Err(err) => warn!("Error receiving a JobResult: {:?}", err),
}
}),
);
let scope_things =
ServiceWorkerRegistration::create_scope_things(&global, script_url.clone());
let job = Job::create_job(
JobType::Register,
scope,
script_url,
job_result_sender,
self.client.creation_url(),
Some(scope_things),
);
let _ = global
.script_to_constellation_chan()
.send(ScriptMsg::ScheduleJob(job));
promise
}
}
struct RegisterJobResultHandler {
trusted_promise: Option<TrustedPromise>,
task_source: DOMManipulationTaskSource,
task_canceller: TaskCanceller,
}
impl RegisterJobResultHandler {
pub fn handle(&mut self, result: JobResult) {
match result {
JobResult::RejectPromise(error) => {
let promise = self
.trusted_promise
.take()
.expect("No promise to resolve for SW Register job.");
let _ = self.task_source.queue_with_canceller(
task!(reject_promise_with_security_error: move || {
let promise = promise.root();
let _ac = enter_realm(&*promise.global());
match error {
JobError::TypeError => {
promise.reject_error(Error::Type("Failed to register a ServiceWorker".to_string()));
},
JobError::SecurityError => {
promise.reject_error(Error::Security);
},
}
}),
&self.task_canceller,
);
},
JobResult::ResolvePromise(job, value) => {
let promise = self
.trusted_promise
.take()
.expect("No promise to resolve for SW Register job.");
let _ = self.task_source.queue_with_canceller(
task!(resolve_promise: move || {
let promise = promise.root();
let global = promise.global();
let _ac = enter_realm(&*global);
let JobResultValue::Registration {
id,
installing_worker,
waiting_worker,
active_worker,
} = value;
let registration = global.get_serviceworker_registration(
&job.script_url,
&job.scope_url,
id,
installing_worker,
waiting_worker,
active_worker,
);
promise.resolve_native(&*registration);
}),
&self.task_canceller,
);
},
}
}
}