Permalink
Browse files

Auto merge of #22218 - CYBAI:sw-up-to-date, r=<try>

Implement NavigationPreloadManager for ServiceWorker

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix part of #19302 .
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/22218)
<!-- Reviewable:end -->
  • Loading branch information...
bors-servo committed Dec 6, 2018
2 parents 16e4eb6 + b8b1100 commit 332640a39a6c4a47a90e236332f590f0ca7b2df9
@@ -398,6 +398,7 @@ pub mod mouseevent;
pub mod mutationobserver;
pub mod mutationrecord;
pub mod namednodemap;
pub mod navigationpreloadmanager;
pub mod navigator;
pub mod navigatorinfo;
pub mod node;
@@ -0,0 +1,139 @@
/* 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::cell::DomRefCell;
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,
enabled: DomRefCell<bool>,
header_value: DomRefCell<Option<ByteString>>,
serviceworker_registration: Dom<ServiceWorkerRegistration>,
}
impl NavigationPreloadManager {
fn new_inherited(registration: &ServiceWorkerRegistration) -> NavigationPreloadManager {
NavigationPreloadManager {
reflector_: Reflector::new(),
enabled: DomRefCell::new(false),
header_value: DomRefCell::new(None),
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.
let mut enabled = self.enabled.borrow_mut();
*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.
let mut enabled = self.enabled.borrow_mut();
*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.
let mut header_value = self.header_value.borrow_mut();
*header_value = Some(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 *self.enabled.borrow() {
state.enabled = true;
}
// 4.
state.headerValue = self.header_value.borrow().clone();
// 5.
promise.resolve_native(&state);
promise
}
}
@@ -7,11 +7,12 @@ use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::S
use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::{
ServiceWorkerRegistrationMethods, Wrap,
};
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::{Dom, DomRoot};
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::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::navigationpreloadmanager::NavigationPreloadManager;
use crate::dom::serviceworker::ServiceWorker;
use crate::dom::workerglobalscope::prepare_workerscope_init;
use dom_struct::dom_struct;
@@ -25,6 +26,7 @@ pub struct ServiceWorkerRegistration {
active: Option<Dom<ServiceWorker>>,
installing: Option<Dom<ServiceWorker>>,
waiting: Option<Dom<ServiceWorker>>,
navigation_preload: MutNullableDom<NavigationPreloadManager>,
scope: ServoUrl,
update_via_cache: ServiceWorkerUpdateViaCache,
uninstalling: Cell<bool>,
@@ -37,6 +39,7 @@ impl ServiceWorkerRegistration {
active: Some(Dom::from_ref(active_sw)),
installing: None,
waiting: None,
navigation_preload: MutNullableDom::new(None),
scope: scope,
update_via_cache: ServiceWorkerUpdateViaCache::Imports,
uninstalling: Cell::new(false),
@@ -52,6 +55,7 @@ impl ServiceWorkerRegistration {
let active_worker =
ServiceWorker::install_serviceworker(global, script_url.clone(), scope.clone(), true);
active_worker.set_transition_state(ServiceWorkerState::Installed);
reflect_dom_object(
Box::new(ServiceWorkerRegistration::new_inherited(
&*active_worker,
@@ -62,6 +66,10 @@ impl ServiceWorkerRegistration {
)
}
pub fn active(&self) -> Option<&ServiceWorker> {
self.active.as_ref().map(|sw| &**sw)
}
pub fn get_installed(&self) -> &ServiceWorker {
self.active.as_ref().unwrap()
}
@@ -147,4 +155,10 @@ impl ServiceWorkerRegistrationMethods for ServiceWorkerRegistration {
fn UpdateViaCache(&self) -> ServiceWorkerUpdateViaCache {
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? waiting;
readonly attribute ServiceWorker? active;
// [SameObject] readonly attribute NavigationPreloadManager navigationPreload;
[SameObject] readonly attribute NavigationPreloadManager navigationPreload;
readonly attribute USVString scope;
readonly attribute ServiceWorkerUpdateViaCache updateViaCache;

0 comments on commit 332640a

Please sign in to comment.