use std::rc::Rc;
use dom_struct::dom_struct;
use js::jsval::UndefinedValue;
use crate::dom::bindings::codegen::Bindings::NavigationPreloadManagerBinding::{
NavigationPreloadManagerMethods, NavigationPreloadState,
};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::ByteString;
use crate::dom::domexception::{DOMErrorName, DOMException};
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::dom::serviceworkerregistration::ServiceWorkerRegistration;
use crate::realms::InRealm;
use crate::script_runtime::CanGc;
#[dom_struct]
pub struct NavigationPreloadManager {
reflector_: Reflector,
serviceworker_registration: Dom<ServiceWorkerRegistration>,
}
impl NavigationPreloadManager {
fn new_inherited(registration: &ServiceWorkerRegistration) -> NavigationPreloadManager {
NavigationPreloadManager {
reflector_: Reflector::new(),
serviceworker_registration: Dom::from_ref(registration),
}
}
#[allow(crown::unrooted_must_root)]
pub fn new(
global: &GlobalScope,
registration: &ServiceWorkerRegistration,
) -> DomRoot<NavigationPreloadManager> {
let manager = NavigationPreloadManager::new_inherited(registration);
reflect_dom_object(Box::new(manager), global, CanGc::note())
}
}
impl NavigationPreloadManagerMethods<crate::DomTypeHolder> for NavigationPreloadManager {
fn Enable(&self, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
let promise = Promise::new_in_current_realm(comp, can_gc);
if self.serviceworker_registration.is_active() {
promise.reject_native(&DOMException::new(
&self.global(),
DOMErrorName::InvalidStateError,
));
} else {
self.serviceworker_registration
.set_navigation_preload_enabled(true);
promise.resolve_native(&UndefinedValue());
}
promise
}
fn Disable(&self, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
let promise = Promise::new_in_current_realm(comp, can_gc);
if self.serviceworker_registration.is_active() {
promise.reject_native(&DOMException::new(
&self.global(),
DOMErrorName::InvalidStateError,
));
} else {
self.serviceworker_registration
.set_navigation_preload_enabled(false);
promise.resolve_native(&UndefinedValue());
}
promise
}
fn SetHeaderValue(&self, value: ByteString, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
let promise = Promise::new_in_current_realm(comp, can_gc);
if self.serviceworker_registration.is_active() {
promise.reject_native(&DOMException::new(
&self.global(),
DOMErrorName::InvalidStateError,
));
} else {
self.serviceworker_registration
.set_navigation_preload_header_value(value);
promise.resolve_native(&UndefinedValue());
}
promise
}
fn GetState(&self, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
let promise = Promise::new_in_current_realm(comp, can_gc);
let mut state = NavigationPreloadState::empty();
if self.serviceworker_registration.is_active() &&
self.serviceworker_registration
.get_navigation_preload_enabled()
{
state.enabled = true;
}
state.headerValue = self
.serviceworker_registration
.get_navigation_preload_header_value();
promise.resolve_native(&state);
promise
}
}