mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Introduce NavigationPreloadManager for ServiceWorkerRegistration
This commit is contained in:
parent
3c19cd49ec
commit
235fb5952d
5 changed files with 199 additions and 4 deletions
|
@ -398,6 +398,7 @@ pub mod mouseevent;
|
||||||
pub mod mutationobserver;
|
pub mod mutationobserver;
|
||||||
pub mod mutationrecord;
|
pub mod mutationrecord;
|
||||||
pub mod namednodemap;
|
pub mod namednodemap;
|
||||||
|
pub mod navigationpreloadmanager;
|
||||||
pub mod navigator;
|
pub mod navigator;
|
||||||
pub mod navigatorinfo;
|
pub mod navigatorinfo;
|
||||||
pub mod node;
|
pub mod node;
|
||||||
|
|
141
components/script/dom/navigationpreloadmanager.rs
Normal file
141
components/script/dom/navigationpreloadmanager.rs
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use crate::dom::bindings::codegen::Bindings::NavigationPreloadManagerBinding::NavigationPreloadState;
|
||||||
|
use crate::dom::bindings::codegen::Bindings::NavigationPreloadManagerBinding::{
|
||||||
|
NavigationPreloadManagerMethods, Wrap,
|
||||||
|
};
|
||||||
|
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 dom_struct::dom_struct;
|
||||||
|
use js::jsval::UndefinedValue;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
#[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(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, Wrap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NavigationPreloadManagerMethods for NavigationPreloadManager {
|
||||||
|
#[allow(unrooted_must_root)]
|
||||||
|
// https://w3c.github.io/ServiceWorker/#navigation-preload-manager-enable
|
||||||
|
fn Enable(&self) -> Rc<Promise> {
|
||||||
|
let promise = Promise::new(&*self.global());
|
||||||
|
|
||||||
|
// 2.
|
||||||
|
if self.serviceworker_registration.active().is_none() {
|
||||||
|
promise.reject_native(&DOMException::new(
|
||||||
|
&self.global(),
|
||||||
|
DOMErrorName::InvalidStateError,
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
// 3.
|
||||||
|
self.serviceworker_registration
|
||||||
|
.set_navigation_preload_enabled(true);
|
||||||
|
|
||||||
|
// 4.
|
||||||
|
promise.resolve_native(&UndefinedValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
promise
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unrooted_must_root)]
|
||||||
|
// https://w3c.github.io/ServiceWorker/#navigation-preload-manager-disable
|
||||||
|
fn Disable(&self) -> Rc<Promise> {
|
||||||
|
let promise = Promise::new(&*self.global());
|
||||||
|
|
||||||
|
// 2.
|
||||||
|
if self.serviceworker_registration.active().is_none() {
|
||||||
|
promise.reject_native(&DOMException::new(
|
||||||
|
&self.global(),
|
||||||
|
DOMErrorName::InvalidStateError,
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
// 3.
|
||||||
|
self.serviceworker_registration
|
||||||
|
.set_navigation_preload_enabled(false);
|
||||||
|
|
||||||
|
// 4.
|
||||||
|
promise.resolve_native(&UndefinedValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
promise
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unrooted_must_root)]
|
||||||
|
// https://w3c.github.io/ServiceWorker/#navigation-preload-manager-setheadervalue
|
||||||
|
fn SetHeaderValue(&self, value: ByteString) -> Rc<Promise> {
|
||||||
|
let promise = Promise::new(&*self.global());
|
||||||
|
|
||||||
|
// 2.
|
||||||
|
if self.serviceworker_registration.active().is_none() {
|
||||||
|
promise.reject_native(&DOMException::new(
|
||||||
|
&self.global(),
|
||||||
|
DOMErrorName::InvalidStateError,
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
// 3.
|
||||||
|
self.serviceworker_registration
|
||||||
|
.set_navigation_preload_header_value(value);
|
||||||
|
|
||||||
|
// 4.
|
||||||
|
promise.resolve_native(&UndefinedValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
promise
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unrooted_must_root)]
|
||||||
|
// https://w3c.github.io/ServiceWorker/#navigation-preload-manager-getstate
|
||||||
|
fn GetState(&self) -> Rc<Promise> {
|
||||||
|
let promise = Promise::new(&*self.global());
|
||||||
|
// 2.
|
||||||
|
let mut state = NavigationPreloadState::empty();
|
||||||
|
|
||||||
|
// 3.
|
||||||
|
if let Some(_) = self.serviceworker_registration.active() {
|
||||||
|
if self
|
||||||
|
.serviceworker_registration
|
||||||
|
.get_navigation_preload_enabled()
|
||||||
|
{
|
||||||
|
state.enabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4.
|
||||||
|
state.headerValue = self
|
||||||
|
.serviceworker_registration
|
||||||
|
.get_navigation_preload_header_value();
|
||||||
|
|
||||||
|
// 5.
|
||||||
|
promise.resolve_native(&state);
|
||||||
|
|
||||||
|
promise
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,16 +2,18 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use crate::dom::bindings::cell::DomRefCell;
|
||||||
use crate::dom::bindings::codegen::Bindings::ServiceWorkerBinding::ServiceWorkerState;
|
use crate::dom::bindings::codegen::Bindings::ServiceWorkerBinding::ServiceWorkerState;
|
||||||
use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::ServiceWorkerUpdateViaCache;
|
use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::ServiceWorkerUpdateViaCache;
|
||||||
use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::{
|
use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::{
|
||||||
ServiceWorkerRegistrationMethods, Wrap,
|
ServiceWorkerRegistrationMethods, Wrap,
|
||||||
};
|
};
|
||||||
use crate::dom::bindings::reflector::reflect_dom_object;
|
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
|
||||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
|
||||||
use crate::dom::bindings::str::USVString;
|
use crate::dom::bindings::str::{ByteString, USVString};
|
||||||
use crate::dom::eventtarget::EventTarget;
|
use crate::dom::eventtarget::EventTarget;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
|
use crate::dom::navigationpreloadmanager::NavigationPreloadManager;
|
||||||
use crate::dom::serviceworker::ServiceWorker;
|
use crate::dom::serviceworker::ServiceWorker;
|
||||||
use crate::dom::workerglobalscope::prepare_workerscope_init;
|
use crate::dom::workerglobalscope::prepare_workerscope_init;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
@ -25,7 +27,10 @@ pub struct ServiceWorkerRegistration {
|
||||||
active: Option<Dom<ServiceWorker>>,
|
active: Option<Dom<ServiceWorker>>,
|
||||||
installing: Option<Dom<ServiceWorker>>,
|
installing: Option<Dom<ServiceWorker>>,
|
||||||
waiting: Option<Dom<ServiceWorker>>,
|
waiting: Option<Dom<ServiceWorker>>,
|
||||||
|
navigation_preload: MutNullableDom<NavigationPreloadManager>,
|
||||||
scope: ServoUrl,
|
scope: ServoUrl,
|
||||||
|
navigation_preload_enabled: Cell<bool>,
|
||||||
|
navigation_preload_header_value: DomRefCell<Option<ByteString>>,
|
||||||
update_via_cache: ServiceWorkerUpdateViaCache,
|
update_via_cache: ServiceWorkerUpdateViaCache,
|
||||||
uninstalling: Cell<bool>,
|
uninstalling: Cell<bool>,
|
||||||
}
|
}
|
||||||
|
@ -37,7 +42,10 @@ impl ServiceWorkerRegistration {
|
||||||
active: Some(Dom::from_ref(active_sw)),
|
active: Some(Dom::from_ref(active_sw)),
|
||||||
installing: None,
|
installing: None,
|
||||||
waiting: None,
|
waiting: None,
|
||||||
|
navigation_preload: MutNullableDom::new(None),
|
||||||
scope: scope,
|
scope: scope,
|
||||||
|
navigation_preload_enabled: Cell::new(false),
|
||||||
|
navigation_preload_header_value: DomRefCell::new(None),
|
||||||
update_via_cache: ServiceWorkerUpdateViaCache::Imports,
|
update_via_cache: ServiceWorkerUpdateViaCache::Imports,
|
||||||
uninstalling: Cell::new(false),
|
uninstalling: Cell::new(false),
|
||||||
}
|
}
|
||||||
|
@ -52,6 +60,7 @@ impl ServiceWorkerRegistration {
|
||||||
let active_worker =
|
let active_worker =
|
||||||
ServiceWorker::install_serviceworker(global, script_url.clone(), scope.clone(), true);
|
ServiceWorker::install_serviceworker(global, script_url.clone(), scope.clone(), true);
|
||||||
active_worker.set_transition_state(ServiceWorkerState::Installed);
|
active_worker.set_transition_state(ServiceWorkerState::Installed);
|
||||||
|
|
||||||
reflect_dom_object(
|
reflect_dom_object(
|
||||||
Box::new(ServiceWorkerRegistration::new_inherited(
|
Box::new(ServiceWorkerRegistration::new_inherited(
|
||||||
&*active_worker,
|
&*active_worker,
|
||||||
|
@ -62,10 +71,31 @@ impl ServiceWorkerRegistration {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn active(&self) -> Option<&ServiceWorker> {
|
||||||
|
self.active.as_ref().map(|sw| &**sw)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_installed(&self) -> &ServiceWorker {
|
pub fn get_installed(&self) -> &ServiceWorker {
|
||||||
self.active.as_ref().unwrap()
|
self.active.as_ref().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_navigation_preload_header_value(&self) -> Option<ByteString> {
|
||||||
|
self.navigation_preload_header_value.borrow().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_navigation_preload_header_value(&self, value: ByteString) {
|
||||||
|
let mut header_value = self.navigation_preload_header_value.borrow_mut();
|
||||||
|
*header_value = Some(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_navigation_preload_enabled(&self) -> bool {
|
||||||
|
self.navigation_preload_enabled.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_navigation_preload_enabled(&self, flag: bool) {
|
||||||
|
self.navigation_preload_enabled.set(flag)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_uninstalling(&self) -> bool {
|
pub fn get_uninstalling(&self) -> bool {
|
||||||
self.uninstalling.get()
|
self.uninstalling.get()
|
||||||
}
|
}
|
||||||
|
@ -147,4 +177,10 @@ impl ServiceWorkerRegistrationMethods for ServiceWorkerRegistration {
|
||||||
fn UpdateViaCache(&self) -> ServiceWorkerUpdateViaCache {
|
fn UpdateViaCache(&self) -> ServiceWorkerUpdateViaCache {
|
||||||
self.update_via_cache
|
self.update_via_cache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://w3c.github.io/ServiceWorker/#service-worker-registration-navigationpreload
|
||||||
|
fn NavigationPreload(&self) -> DomRoot<NavigationPreloadManager> {
|
||||||
|
self.navigation_preload
|
||||||
|
.or_init(|| NavigationPreloadManager::new(&self.global(), &self))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
// https://w3c.github.io/ServiceWorker/#navigation-preload-manager
|
||||||
|
[Pref="dom.serviceworker.enabled", SecureContext, Exposed=(Window,Worker)]
|
||||||
|
interface NavigationPreloadManager {
|
||||||
|
Promise<void> enable();
|
||||||
|
Promise<void> disable();
|
||||||
|
Promise<void> setHeaderValue(ByteString value);
|
||||||
|
Promise<NavigationPreloadState> getState();
|
||||||
|
};
|
||||||
|
|
||||||
|
dictionary NavigationPreloadState {
|
||||||
|
boolean enabled = false;
|
||||||
|
ByteString headerValue;
|
||||||
|
};
|
|
@ -8,7 +8,7 @@ interface ServiceWorkerRegistration : EventTarget {
|
||||||
readonly attribute ServiceWorker? installing;
|
readonly attribute ServiceWorker? installing;
|
||||||
readonly attribute ServiceWorker? waiting;
|
readonly attribute ServiceWorker? waiting;
|
||||||
readonly attribute ServiceWorker? active;
|
readonly attribute ServiceWorker? active;
|
||||||
// [SameObject] readonly attribute NavigationPreloadManager navigationPreload;
|
[SameObject] readonly attribute NavigationPreloadManager navigationPreload;
|
||||||
|
|
||||||
readonly attribute USVString scope;
|
readonly attribute USVString scope;
|
||||||
readonly attribute ServiceWorkerUpdateViaCache updateViaCache;
|
readonly attribute ServiceWorkerUpdateViaCache updateViaCache;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue