Skip to main content

script/dom/workers/
serviceworkerregistration.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::rc::Rc;
7
8use devtools_traits::WorkerId;
9use dom_struct::dom_struct;
10use js::context::JSContext;
11use net_traits::request::Referrer;
12use script_bindings::cell::DomRefCell;
13use script_bindings::codegen::GenericBindings::NavigatorBinding::NavigatorMethods;
14use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
15use script_bindings::reflector::reflect_dom_object;
16use servo_base::id::ServiceWorkerRegistrationId;
17use servo_constellation_traits::{ScopeThings, WorkerScriptLoadOrigin};
18use servo_url::ServoUrl;
19use uuid::Uuid;
20
21use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::{
22    ServiceWorkerRegistrationMethods, ServiceWorkerUpdateViaCache,
23};
24use crate::dom::bindings::error::Error;
25use crate::dom::bindings::inheritance::Castable;
26use crate::dom::bindings::reflector::DomGlobal;
27use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
28use crate::dom::bindings::str::{ByteString, USVString};
29use crate::dom::eventtarget::EventTarget;
30use crate::dom::globalscope::GlobalScope;
31use crate::dom::navigationpreloadmanager::NavigationPreloadManager;
32use crate::dom::promise::Promise;
33use crate::dom::serviceworker::ServiceWorker;
34use crate::dom::window::Window;
35use crate::dom::workerglobalscope::prepare_workerscope_init;
36use crate::script_runtime::CanGc;
37
38#[dom_struct]
39pub(crate) struct ServiceWorkerRegistration {
40    eventtarget: EventTarget,
41    active: DomRefCell<Option<Dom<ServiceWorker>>>,
42    installing: DomRefCell<Option<Dom<ServiceWorker>>>,
43    waiting: DomRefCell<Option<Dom<ServiceWorker>>>,
44    navigation_preload: MutNullableDom<NavigationPreloadManager>,
45    #[no_trace]
46    scope: ServoUrl,
47    navigation_preload_enabled: Cell<bool>,
48    navigation_preload_header_value: DomRefCell<Option<ByteString>>,
49    update_via_cache: ServiceWorkerUpdateViaCache,
50    uninstalling: Cell<bool>,
51    #[no_trace]
52    registration_id: ServiceWorkerRegistrationId,
53}
54
55impl ServiceWorkerRegistration {
56    fn new_inherited(
57        scope: ServoUrl,
58        registration_id: ServiceWorkerRegistrationId,
59    ) -> ServiceWorkerRegistration {
60        ServiceWorkerRegistration {
61            eventtarget: EventTarget::new_inherited(),
62            active: DomRefCell::new(None),
63            installing: DomRefCell::new(None),
64            waiting: DomRefCell::new(None),
65            navigation_preload: MutNullableDom::new(None),
66            scope,
67            navigation_preload_enabled: Cell::new(false),
68            navigation_preload_header_value: DomRefCell::new(None),
69            update_via_cache: ServiceWorkerUpdateViaCache::Imports,
70            uninstalling: Cell::new(false),
71            registration_id,
72        }
73    }
74
75    pub(crate) fn new(
76        global: &GlobalScope,
77        scope: ServoUrl,
78        registration_id: ServiceWorkerRegistrationId,
79        can_gc: CanGc,
80    ) -> DomRoot<ServiceWorkerRegistration> {
81        reflect_dom_object(
82            Box::new(ServiceWorkerRegistration::new_inherited(
83                scope,
84                registration_id,
85            )),
86            global,
87            can_gc,
88        )
89    }
90
91    /// Does this registration have an active worker?
92    pub(crate) fn is_active(&self) -> bool {
93        self.active.borrow().is_some()
94    }
95
96    pub(crate) fn set_installing(&self, worker: &ServiceWorker) {
97        *self.installing.borrow_mut() = Some(Dom::from_ref(worker));
98    }
99
100    pub(crate) fn get_navigation_preload_header_value(&self) -> Option<ByteString> {
101        self.navigation_preload_header_value.borrow().clone()
102    }
103
104    pub(crate) fn set_navigation_preload_header_value(&self, value: ByteString) {
105        let mut header_value = self.navigation_preload_header_value.borrow_mut();
106        *header_value = Some(value);
107    }
108
109    pub(crate) fn get_navigation_preload_enabled(&self) -> bool {
110        self.navigation_preload_enabled.get()
111    }
112
113    pub(crate) fn set_navigation_preload_enabled(&self, flag: bool) {
114        self.navigation_preload_enabled.set(flag)
115    }
116
117    pub(crate) fn get_uninstalling(&self) -> bool {
118        self.uninstalling.get()
119    }
120
121    pub(crate) fn set_uninstalling(&self, flag: bool) {
122        self.uninstalling.set(flag)
123    }
124
125    pub(crate) fn create_scope_things(global: &GlobalScope, script_url: ServoUrl) -> ScopeThings {
126        let worker_load_origin = WorkerScriptLoadOrigin {
127            referrer_url: match global.get_referrer() {
128                Referrer::Client(url) => Some(url),
129                Referrer::ReferrerUrl(url) => Some(url),
130                _ => None,
131            },
132            referrer_policy: global.get_referrer_policy(),
133            pipeline_id: global.pipeline_id(),
134        };
135
136        let webgl_chan = global
137            .downcast::<Window>()
138            .and_then(|window| window.webgl_chan_value());
139        let worker_id = WorkerId(Uuid::new_v4());
140        let devtools_chan = global.devtools_chan().cloned();
141        let init = prepare_workerscope_init(global, None, Some(worker_id), webgl_chan);
142        let browsing_context_id = global
143            .downcast::<Window>()
144            .map(|w: &Window| w.window_proxy().browsing_context_id())
145            .expect("Service worker must be registered from a Window global");
146        let webview_id = global
147            .webview_id()
148            .expect("Service worker must have a WebViewId");
149        ScopeThings {
150            script_url,
151            init,
152            worker_load_origin,
153            devtools_chan,
154            worker_id,
155            browsing_context_id,
156            webview_id,
157        }
158    }
159
160    // https://w3c.github.io/ServiceWorker/#get-newest-worker-algorithm
161    pub(crate) fn get_newest_worker(&self) -> Option<DomRoot<ServiceWorker>> {
162        let installing = self.installing.borrow();
163        let waiting = self.waiting.borrow();
164        let active = self.active.borrow();
165        installing
166            .as_ref()
167            .map(|sw| DomRoot::from_ref(&**sw))
168            .or_else(|| waiting.as_ref().map(|sw| DomRoot::from_ref(&**sw)))
169            .or_else(|| active.as_ref().map(|sw| DomRoot::from_ref(&**sw)))
170    }
171}
172
173pub(crate) fn longest_prefix_match(stored_scope: &ServoUrl, potential_match: &ServoUrl) -> bool {
174    if stored_scope.origin() != potential_match.origin() {
175        return false;
176    }
177    let scope_chars = stored_scope.path().chars();
178    let matching_chars = potential_match.path().chars();
179    if scope_chars.count() > matching_chars.count() {
180        return false;
181    }
182
183    stored_scope
184        .path()
185        .chars()
186        .zip(potential_match.path().chars())
187        .all(|(scope, matched)| scope == matched)
188}
189
190impl ServiceWorkerRegistrationMethods<crate::DomTypeHolder> for ServiceWorkerRegistration {
191    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-installing-attribute>
192    fn GetInstalling(&self) -> Option<DomRoot<ServiceWorker>> {
193        self.installing
194            .borrow()
195            .as_ref()
196            .map(|sw| DomRoot::from_ref(&**sw))
197    }
198
199    /// <https://w3c.github.io/ServiceWorker/#dom-serviceworkerregistration-unregister>
200    fn Unregister(&self, cx: &mut JSContext) -> Rc<Promise> {
201        // Step 1: Let registration be the service worker registration.
202        // Note: `self` is the registration.
203
204        // Step 2: Let promise be a new promise.
205        let promise = Promise::new2(cx, &self.global());
206
207        let Some(worker) = self.get_newest_worker() else {
208            promise.resolve_native_with_cx(cx, &true);
209            return promise;
210        };
211
212        let global = self.global();
213        let Some(window) = global.downcast::<Window>() else {
214            // Worker navigator does not have a service woker container yet.
215            promise.resolve_native_with_cx(cx, &false);
216            return promise;
217        };
218        let service_worker_container = window.Navigator().ServiceWorker(cx);
219
220        // Step 3: Let job be the result of running Create Job with unregister,
221        // registration’s storage key, registration’s scope url, null, promise,
222        // and this’s relevant settings object.
223        // Step 4: Invoke Schedule Job with job.
224        // Note: done in the container.
225        let Some(storage_key) = global.obtain_storage_key() else {
226            promise.reject_error_with_cx(
227                cx,
228                Error::Type(c"Failed to obtain a storage key".to_owned()),
229            );
230            return promise;
231        };
232        service_worker_container.create_and_schedule_unregister_job(
233            cx,
234            storage_key,
235            self.scope.clone(),
236            worker.get_script_url(),
237            promise.clone(),
238        );
239
240        // Set all workers to none.
241        // Note: not clear where the spec does this.
242        *self.installing.borrow_mut() = None;
243        *self.waiting.borrow_mut() = None;
244        *self.active.borrow_mut() = None;
245
246        // Step 5: Return promise.
247        promise
248    }
249
250    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-active-attribute>
251    fn GetActive(&self) -> Option<DomRoot<ServiceWorker>> {
252        self.active
253            .borrow()
254            .as_ref()
255            .map(|sw| DomRoot::from_ref(&**sw))
256    }
257
258    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-waiting-attribute>
259    fn GetWaiting(&self) -> Option<DomRoot<ServiceWorker>> {
260        self.waiting
261            .borrow()
262            .as_ref()
263            .map(|sw| DomRoot::from_ref(&**sw))
264    }
265
266    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-scope-attribute>
267    fn Scope(&self) -> USVString {
268        USVString(self.scope.as_str().to_owned())
269    }
270
271    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-updateviacache>
272    fn UpdateViaCache(&self) -> ServiceWorkerUpdateViaCache {
273        self.update_via_cache
274    }
275
276    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-navigationpreload>
277    fn NavigationPreload(&self) -> DomRoot<NavigationPreloadManager> {
278        self.navigation_preload.or_init(|| {
279            NavigationPreloadManager::new(&self.global(), self, CanGc::deprecated_note())
280        })
281    }
282}