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;
6
7use devtools_traits::WorkerId;
8use dom_struct::dom_struct;
9use net_traits::request::Referrer;
10use script_bindings::cell::DomRefCell;
11use script_bindings::reflector::reflect_dom_object;
12use servo_base::id::ServiceWorkerRegistrationId;
13use servo_constellation_traits::{ScopeThings, WorkerScriptLoadOrigin};
14use servo_url::ServoUrl;
15use uuid::Uuid;
16
17use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::{
18    ServiceWorkerRegistrationMethods, ServiceWorkerUpdateViaCache,
19};
20use crate::dom::bindings::inheritance::Castable;
21use crate::dom::bindings::reflector::DomGlobal;
22use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
23use crate::dom::bindings::str::{ByteString, USVString};
24use crate::dom::eventtarget::EventTarget;
25use crate::dom::globalscope::GlobalScope;
26use crate::dom::navigationpreloadmanager::NavigationPreloadManager;
27use crate::dom::serviceworker::ServiceWorker;
28use crate::dom::window::Window;
29use crate::dom::workerglobalscope::prepare_workerscope_init;
30use crate::script_runtime::CanGc;
31
32#[dom_struct]
33pub(crate) struct ServiceWorkerRegistration {
34    eventtarget: EventTarget,
35    active: DomRefCell<Option<Dom<ServiceWorker>>>,
36    installing: DomRefCell<Option<Dom<ServiceWorker>>>,
37    waiting: DomRefCell<Option<Dom<ServiceWorker>>>,
38    navigation_preload: MutNullableDom<NavigationPreloadManager>,
39    #[no_trace]
40    scope: ServoUrl,
41    navigation_preload_enabled: Cell<bool>,
42    navigation_preload_header_value: DomRefCell<Option<ByteString>>,
43    update_via_cache: ServiceWorkerUpdateViaCache,
44    uninstalling: Cell<bool>,
45    #[no_trace]
46    registration_id: ServiceWorkerRegistrationId,
47}
48
49impl ServiceWorkerRegistration {
50    fn new_inherited(
51        scope: ServoUrl,
52        registration_id: ServiceWorkerRegistrationId,
53    ) -> ServiceWorkerRegistration {
54        ServiceWorkerRegistration {
55            eventtarget: EventTarget::new_inherited(),
56            active: DomRefCell::new(None),
57            installing: DomRefCell::new(None),
58            waiting: DomRefCell::new(None),
59            navigation_preload: MutNullableDom::new(None),
60            scope,
61            navigation_preload_enabled: Cell::new(false),
62            navigation_preload_header_value: DomRefCell::new(None),
63            update_via_cache: ServiceWorkerUpdateViaCache::Imports,
64            uninstalling: Cell::new(false),
65            registration_id,
66        }
67    }
68
69    pub(crate) fn new(
70        global: &GlobalScope,
71        scope: ServoUrl,
72        registration_id: ServiceWorkerRegistrationId,
73        can_gc: CanGc,
74    ) -> DomRoot<ServiceWorkerRegistration> {
75        reflect_dom_object(
76            Box::new(ServiceWorkerRegistration::new_inherited(
77                scope,
78                registration_id,
79            )),
80            global,
81            can_gc,
82        )
83    }
84
85    /// Does this registration have an active worker?
86    pub(crate) fn is_active(&self) -> bool {
87        self.active.borrow().is_some()
88    }
89
90    pub(crate) fn set_installing(&self, worker: &ServiceWorker) {
91        *self.installing.borrow_mut() = Some(Dom::from_ref(worker));
92    }
93
94    pub(crate) fn get_navigation_preload_header_value(&self) -> Option<ByteString> {
95        self.navigation_preload_header_value.borrow().clone()
96    }
97
98    pub(crate) fn set_navigation_preload_header_value(&self, value: ByteString) {
99        let mut header_value = self.navigation_preload_header_value.borrow_mut();
100        *header_value = Some(value);
101    }
102
103    pub(crate) fn get_navigation_preload_enabled(&self) -> bool {
104        self.navigation_preload_enabled.get()
105    }
106
107    pub(crate) fn set_navigation_preload_enabled(&self, flag: bool) {
108        self.navigation_preload_enabled.set(flag)
109    }
110
111    pub(crate) fn get_uninstalling(&self) -> bool {
112        self.uninstalling.get()
113    }
114
115    pub(crate) fn set_uninstalling(&self, flag: bool) {
116        self.uninstalling.set(flag)
117    }
118
119    pub(crate) fn create_scope_things(global: &GlobalScope, script_url: ServoUrl) -> ScopeThings {
120        let worker_load_origin = WorkerScriptLoadOrigin {
121            referrer_url: match global.get_referrer() {
122                Referrer::Client(url) => Some(url),
123                Referrer::ReferrerUrl(url) => Some(url),
124                _ => None,
125            },
126            referrer_policy: global.get_referrer_policy(),
127            pipeline_id: global.pipeline_id(),
128        };
129
130        let webgl_chan = global
131            .downcast::<Window>()
132            .and_then(|window| window.webgl_chan_value());
133        let worker_id = WorkerId(Uuid::new_v4());
134        let devtools_chan = global.devtools_chan().cloned();
135        let init = prepare_workerscope_init(global, None, Some(worker_id), webgl_chan);
136        let browsing_context_id = global
137            .downcast::<Window>()
138            .map(|w: &Window| w.window_proxy().browsing_context_id())
139            .expect("Service worker must be registered from a Window global");
140        let webview_id = global
141            .webview_id()
142            .expect("Service worker must have a WebViewId");
143        ScopeThings {
144            script_url,
145            init,
146            worker_load_origin,
147            devtools_chan,
148            worker_id,
149            browsing_context_id,
150            webview_id,
151        }
152    }
153
154    // https://w3c.github.io/ServiceWorker/#get-newest-worker-algorithm
155    pub(crate) fn get_newest_worker(&self) -> Option<DomRoot<ServiceWorker>> {
156        let installing = self.installing.borrow();
157        let waiting = self.waiting.borrow();
158        let active = self.active.borrow();
159        installing
160            .as_ref()
161            .map(|sw| DomRoot::from_ref(&**sw))
162            .or_else(|| waiting.as_ref().map(|sw| DomRoot::from_ref(&**sw)))
163            .or_else(|| active.as_ref().map(|sw| DomRoot::from_ref(&**sw)))
164    }
165}
166
167pub(crate) fn longest_prefix_match(stored_scope: &ServoUrl, potential_match: &ServoUrl) -> bool {
168    if stored_scope.origin() != potential_match.origin() {
169        return false;
170    }
171    let scope_chars = stored_scope.path().chars();
172    let matching_chars = potential_match.path().chars();
173    if scope_chars.count() > matching_chars.count() {
174        return false;
175    }
176
177    stored_scope
178        .path()
179        .chars()
180        .zip(potential_match.path().chars())
181        .all(|(scope, matched)| scope == matched)
182}
183
184impl ServiceWorkerRegistrationMethods<crate::DomTypeHolder> for ServiceWorkerRegistration {
185    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-installing-attribute>
186    fn GetInstalling(&self) -> Option<DomRoot<ServiceWorker>> {
187        self.installing
188            .borrow()
189            .as_ref()
190            .map(|sw| DomRoot::from_ref(&**sw))
191    }
192
193    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-active-attribute>
194    fn GetActive(&self) -> Option<DomRoot<ServiceWorker>> {
195        self.active
196            .borrow()
197            .as_ref()
198            .map(|sw| DomRoot::from_ref(&**sw))
199    }
200
201    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-waiting-attribute>
202    fn GetWaiting(&self) -> Option<DomRoot<ServiceWorker>> {
203        self.waiting
204            .borrow()
205            .as_ref()
206            .map(|sw| DomRoot::from_ref(&**sw))
207    }
208
209    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-scope-attribute>
210    fn Scope(&self) -> USVString {
211        USVString(self.scope.as_str().to_owned())
212    }
213
214    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-updateviacache>
215    fn UpdateViaCache(&self) -> ServiceWorkerUpdateViaCache {
216        self.update_via_cache
217    }
218
219    /// <https://w3c.github.io/ServiceWorker/#service-worker-registration-navigationpreload>
220    fn NavigationPreload(&self) -> DomRoot<NavigationPreloadManager> {
221        self.navigation_preload.or_init(|| {
222            NavigationPreloadManager::new(&self.global(), self, CanGc::deprecated_note())
223        })
224    }
225}