Skip to main content

script/dom/serviceworker/
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 create_scope_things(global: &GlobalScope, script_url: ServoUrl) -> ScopeThings {
118        let worker_load_origin = WorkerScriptLoadOrigin {
119            referrer_url: match global.get_referrer() {
120                Referrer::Client(url) => Some(url),
121                Referrer::ReferrerUrl(url) => Some(url),
122                _ => None,
123            },
124            referrer_policy: global.get_referrer_policy(),
125            pipeline_id: global.pipeline_id(),
126        };
127
128        let webgl_chan = global
129            .downcast::<Window>()
130            .and_then(|window| window.webgl_chan_value());
131        let worker_id = WorkerId(Uuid::new_v4());
132        let devtools_chan = global.devtools_chan().cloned();
133        let init = prepare_workerscope_init(global, None, Some(worker_id), webgl_chan);
134        let browsing_context_id = global
135            .downcast::<Window>()
136            .map(|w: &Window| w.window_proxy().browsing_context_id())
137            .expect("Service worker must be registered from a Window global");
138        let webview_id = global
139            .webview_id()
140            .expect("Service worker must have a WebViewId");
141        ScopeThings {
142            script_url,
143            init,
144            worker_load_origin,
145            devtools_chan,
146            worker_id,
147            browsing_context_id,
148            webview_id,
149        }
150    }
151
152    // https://w3c.github.io/ServiceWorker/#get-newest-worker-algorithm
153    pub(crate) fn get_newest_worker(&self) -> Option<DomRoot<ServiceWorker>> {
154        let installing = self.installing.borrow();
155        let waiting = self.waiting.borrow();
156        let active = self.active.borrow();
157        installing
158            .as_ref()
159            .map(|sw| DomRoot::from_ref(&**sw))
160            .or_else(|| waiting.as_ref().map(|sw| DomRoot::from_ref(&**sw)))
161            .or_else(|| active.as_ref().map(|sw| DomRoot::from_ref(&**sw)))
162    }
163}
164
165pub(crate) fn longest_prefix_match(stored_scope: &ServoUrl, potential_match: &ServoUrl) -> bool {
166    if stored_scope.origin() != potential_match.origin() {
167        return false;
168    }
169    let scope_chars = stored_scope.path().chars();
170    let matching_chars = potential_match.path().chars();
171    if scope_chars.count() > matching_chars.count() {
172        return false;
173    }
174
175    stored_scope
176        .path()
177        .chars()
178        .zip(potential_match.path().chars())
179        .all(|(scope, matched)| scope == matched)
180}
181
182impl ServiceWorkerRegistrationMethods<crate::DomTypeHolder> for ServiceWorkerRegistration {
183    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-installing-attribute>
184    fn GetInstalling(&self) -> Option<DomRoot<ServiceWorker>> {
185        self.installing
186            .borrow()
187            .as_ref()
188            .map(|sw| DomRoot::from_ref(&**sw))
189    }
190
191    /// <https://w3c.github.io/ServiceWorker/#dom-serviceworkerregistration-unregister>
192    fn Unregister(&self, cx: &mut JSContext) -> Rc<Promise> {
193        // Step 1: Let registration be the service worker registration.
194        // Note: `self` is the registration.
195
196        // Step 2: Let promise be a new promise.
197        let promise = Promise::new(cx, &self.global());
198
199        let Some(worker) = self.get_newest_worker() else {
200            promise.resolve_native(cx, &true);
201            return promise;
202        };
203
204        let global = self.global();
205        let Some(window) = global.downcast::<Window>() else {
206            // Worker navigator does not have a service woker container yet.
207            promise.resolve_native(cx, &false);
208            return promise;
209        };
210        let service_worker_container = window.Navigator().ServiceWorker(cx);
211
212        // Step 3: Let job be the result of running Create Job with unregister,
213        // registration’s storage key, registration’s scope url, null, promise,
214        // and this’s relevant settings object.
215        // Step 4: Invoke Schedule Job with job.
216        // Note: done in the container.
217        let Some(storage_key) = global.obtain_storage_key() else {
218            promise.reject_error(
219                cx,
220                Error::Type(c"Failed to obtain a storage key".to_owned()),
221            );
222            return promise;
223        };
224        service_worker_container.create_and_schedule_unregister_job(
225            cx,
226            storage_key,
227            self.scope.clone(),
228            worker.get_script_url(),
229            promise.clone(),
230        );
231
232        // Set all workers to none.
233        // Note: not clear where the spec does this.
234        *self.installing.borrow_mut() = None;
235        *self.waiting.borrow_mut() = None;
236        *self.active.borrow_mut() = None;
237
238        // Step 5: Return promise.
239        promise
240    }
241
242    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-active-attribute>
243    fn GetActive(&self) -> Option<DomRoot<ServiceWorker>> {
244        self.active
245            .borrow()
246            .as_ref()
247            .map(|sw| DomRoot::from_ref(&**sw))
248    }
249
250    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-waiting-attribute>
251    fn GetWaiting(&self) -> Option<DomRoot<ServiceWorker>> {
252        self.waiting
253            .borrow()
254            .as_ref()
255            .map(|sw| DomRoot::from_ref(&**sw))
256    }
257
258    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-scope-attribute>
259    fn Scope(&self) -> USVString {
260        USVString(self.scope.as_str().to_owned())
261    }
262
263    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-updateviacache>
264    fn UpdateViaCache(&self) -> ServiceWorkerUpdateViaCache {
265        self.update_via_cache
266    }
267
268    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-navigationpreload>
269    fn NavigationPreload(&self, cx: &mut JSContext) -> DomRoot<NavigationPreloadManager> {
270        self.navigation_preload
271            .or_init(|| NavigationPreloadManager::new(cx, &self.global(), self))
272    }
273}