script/dom/workers/
workerglobalscope.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::{RefCell, RefMut};
6use std::default::Default;
7use std::rc::Rc;
8use std::sync::Arc;
9use std::sync::atomic::{AtomicBool, Ordering};
10use std::time::Duration;
11
12use base::cross_process_instant::CrossProcessInstant;
13use base::generic_channel::{GenericSend, GenericSender, RoutedReceiver};
14use base::id::{PipelineId, PipelineNamespace};
15use constellation_traits::WorkerGlobalScopeInit;
16use content_security_policy::CspList;
17use devtools_traits::{DevtoolScriptControlMsg, WorkerId};
18use dom_struct::dom_struct;
19use encoding_rs::UTF_8;
20use fonts::FontContext;
21use headers::{HeaderMapExt, ReferrerPolicy as ReferrerPolicyHeader};
22use js::realm::CurrentRealm;
23use js::rust::{HandleValue, MutableHandleValue, ParentRuntime};
24use mime::Mime;
25use net_traits::policy_container::PolicyContainer;
26use net_traits::request::{
27    CredentialsMode, Destination, InsecureRequestsPolicy, ParserMetadata, RequestBuilder, RequestId,
28};
29use net_traits::{FetchMetadata, Metadata, NetworkError, ReferrerPolicy, ResourceFetchTiming};
30use profile_traits::mem::{ProcessReports, perform_memory_report};
31use servo_url::{MutableOrigin, ServoUrl};
32use timers::TimerScheduler;
33use uuid::Uuid;
34
35use crate::dom::bindings::cell::{DomRefCell, Ref};
36use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
37    ImageBitmapOptions, ImageBitmapSource,
38};
39use crate::dom::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
40use crate::dom::bindings::codegen::Bindings::ReportingObserverBinding::Report;
41use crate::dom::bindings::codegen::Bindings::RequestBinding::RequestInit;
42use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
43use crate::dom::bindings::codegen::Bindings::WorkerBinding::WorkerType;
44use crate::dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
45use crate::dom::bindings::codegen::UnionTypes::{
46    RequestOrUSVString, TrustedScriptOrString, TrustedScriptOrStringOrFunction,
47    TrustedScriptURLOrUSVString,
48};
49use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
50use crate::dom::bindings::inheritance::Castable;
51use crate::dom::bindings::refcounted::Trusted;
52use crate::dom::bindings::reflector::DomGlobal;
53use crate::dom::bindings::root::{DomRoot, MutNullableDom};
54use crate::dom::bindings::str::{DOMString, USVString};
55use crate::dom::bindings::trace::RootedTraceableBox;
56use crate::dom::bindings::utils::define_all_exposed_interfaces;
57use crate::dom::crypto::Crypto;
58use crate::dom::csp::{GlobalCspReporting, Violation, parse_csp_list_from_metadata};
59use crate::dom::dedicatedworkerglobalscope::{
60    AutoWorkerReset, DedicatedWorkerGlobalScope, interrupt_callback,
61};
62use crate::dom::global_scope_script_execution::{ClassicScript, ErrorReporting, RethrowErrors};
63use crate::dom::globalscope::GlobalScope;
64use crate::dom::htmlscriptelement::SCRIPT_JS_MIMES;
65use crate::dom::idbfactory::IDBFactory;
66use crate::dom::performance::performance::Performance;
67use crate::dom::performance::performanceresourcetiming::InitiatorType;
68use crate::dom::promise::Promise;
69use crate::dom::reportingendpoint::{ReportingEndpoint, SendReportsToEndpoints};
70use crate::dom::reportingobserver::ReportingObserver;
71use crate::dom::trustedtypes::trustedscripturl::TrustedScriptURL;
72use crate::dom::trustedtypes::trustedtypepolicyfactory::TrustedTypePolicyFactory;
73use crate::dom::types::ImageBitmap;
74#[cfg(feature = "webgpu")]
75use crate::dom::webgpu::identityhub::IdentityHub;
76use crate::dom::window::{base64_atob, base64_btoa};
77use crate::dom::worker::TrustedWorkerAddress;
78use crate::dom::workerlocation::WorkerLocation;
79use crate::dom::workernavigator::WorkerNavigator;
80use crate::fetch::{CspViolationsProcessor, Fetch, RequestWithGlobalScope, load_whole_resource};
81use crate::messaging::{CommonScriptMsg, ScriptEventLoopReceiver, ScriptEventLoopSender};
82use crate::microtask::{Microtask, MicrotaskQueue, UserMicrotask};
83use crate::network_listener::{FetchResponseListener, ResourceTimingListener, submit_timing};
84use crate::realms::{InRealm, enter_auto_realm};
85use crate::script_module::ScriptFetchOptions;
86use crate::script_runtime::{CanGc, IntroductionType, JSContext, JSContextHelper, Runtime};
87use crate::task::TaskCanceller;
88use crate::timers::{IsInterval, TimerCallback};
89
90pub(crate) fn prepare_workerscope_init(
91    global: &GlobalScope,
92    devtools_sender: Option<GenericSender<DevtoolScriptControlMsg>>,
93    worker_id: Option<WorkerId>,
94) -> WorkerGlobalScopeInit {
95    WorkerGlobalScopeInit {
96        resource_threads: global.resource_threads().clone(),
97        storage_threads: global.storage_threads().clone(),
98        mem_profiler_chan: global.mem_profiler_chan().clone(),
99        to_devtools_sender: global.devtools_chan().cloned(),
100        time_profiler_chan: global.time_profiler_chan().clone(),
101        from_devtools_sender: devtools_sender,
102        script_to_constellation_chan: global.script_to_constellation_chan().clone(),
103        script_to_embedder_chan: global.script_to_embedder_chan().clone(),
104        worker_id: worker_id.unwrap_or_else(|| WorkerId(Uuid::new_v4())),
105        pipeline_id: global.pipeline_id(),
106        origin: global.origin().immutable().clone(),
107        inherited_secure_context: Some(global.is_secure_context()),
108        unminify_js: global.unminify_js(),
109    }
110}
111
112pub(crate) struct ScriptFetchContext {
113    scope: Trusted<WorkerGlobalScope>,
114    response: Option<Metadata>,
115    body_bytes: Vec<u8>,
116    url: ServoUrl,
117    worker: TrustedWorkerAddress,
118    policy_container: PolicyContainer,
119}
120
121impl ScriptFetchContext {
122    pub(crate) fn new(
123        scope: Trusted<WorkerGlobalScope>,
124        url: ServoUrl,
125        worker: TrustedWorkerAddress,
126        policy_container: PolicyContainer,
127    ) -> ScriptFetchContext {
128        ScriptFetchContext {
129            scope,
130            response: None,
131            body_bytes: Vec::new(),
132            url,
133            worker,
134            policy_container,
135        }
136    }
137}
138
139impl FetchResponseListener for ScriptFetchContext {
140    fn process_request_body(&mut self, _request_id: RequestId) {}
141
142    fn process_request_eof(&mut self, _request_id: RequestId) {}
143
144    fn process_response(
145        &mut self,
146        _request_id: RequestId,
147        metadata: Result<FetchMetadata, NetworkError>,
148    ) {
149        self.response = metadata.ok().map(|m| match m {
150            FetchMetadata::Unfiltered(m) => m,
151            FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
152        });
153    }
154
155    fn process_response_chunk(&mut self, _request_id: RequestId, mut chunk: Vec<u8>) {
156        self.body_bytes.append(&mut chunk);
157    }
158
159    fn process_response_eof(
160        mut self,
161        cx: &mut js::context::JSContext,
162        _request_id: RequestId,
163        response: Result<(), NetworkError>,
164        timing: ResourceFetchTiming,
165    ) {
166        let scope = self.scope.root();
167
168        if response
169            .as_ref()
170            .inspect_err(|e| error!("error loading script {} ({:?})", self.url, e))
171            .is_err() ||
172            self.response.is_none()
173        {
174            scope.on_complete(None, self.worker.clone(), cx);
175            return;
176        }
177        let metadata = self.response.take().unwrap();
178
179        // The processResponseConsumeBody steps defined inside
180        // [run a worker](https://html.spec.whatwg.org/multipage/#run-a-worker)
181
182        let global_scope = scope.upcast::<GlobalScope>();
183
184        // Step 1. Set worker global scope's url to response's url.
185        scope.set_url(metadata.final_url.clone());
186
187        // Step 2. Set inside settings's creation URL to response's url.
188        global_scope.set_creation_url(metadata.final_url.clone());
189
190        // Step 3. Initialize worker global scope's policy container given worker global scope, response, and inside settings.
191        scope
192            .initialize_policy_container_for_worker_global_scope(&metadata, &self.policy_container);
193        scope.set_endpoints_list(ReportingEndpoint::parse_reporting_endpoints_header(
194            &metadata.final_url.clone(),
195            &metadata.headers,
196        ));
197        global_scope.set_https_state(metadata.https_state);
198
199        // The processResponseConsumeBody steps defined inside
200        // [fetch a classic worker script](https://html.spec.whatwg.org/multipage/#fetch-a-classic-worker-script)
201
202        // Step 1 Set response to response's unsafe response. Done in process_response
203
204        // Step 2 If any of the following are true: bodyBytes is null or failure; or response's status is not an ok status,
205        if !metadata.status.is_success() {
206            // then run onComplete given null, and abort these steps.
207            scope.on_complete(None, self.worker.clone(), cx);
208            return;
209        }
210
211        // Step 3 If all of the following are true:
212        // response's URL's scheme is an HTTP(S) scheme;
213        let is_http_scheme = matches!(metadata.final_url.scheme(), "http" | "https");
214        // and the result of extracting a MIME type from response's header list is not a JavaScript MIME type,
215        let not_a_javascript_mime_type = !metadata.content_type.is_some_and(|ct| {
216            let mime: Mime = ct.into_inner().into();
217            SCRIPT_JS_MIMES.contains(&mime.essence_str())
218        });
219
220        if is_http_scheme && not_a_javascript_mime_type {
221            // then run onComplete given null, and abort these steps.
222            scope.on_complete(None, self.worker.clone(), cx);
223            return;
224        }
225
226        // Step 4 Let sourceText be the result of UTF-8 decoding bodyBytes.
227        let (source, _) = UTF_8.decode_with_bom_removal(&self.body_bytes);
228
229        // Step 5 Let script be the result of creating a classic script using
230        // sourceText, settingsObject, response's URL, and the default script fetch options.
231        let script = global_scope.create_a_classic_script(
232            source,
233            scope.worker_url.borrow().clone(),
234            ScriptFetchOptions::default_classic_script(global_scope),
235            ErrorReporting::Unmuted,
236            Some(IntroductionType::WORKER),
237            1,
238            true,
239        );
240
241        // Step 6 Run onComplete given script.
242        scope.on_complete(Some(script), self.worker.clone(), cx);
243
244        submit_timing(&self, &response, &timing, CanGc::from_cx(cx));
245    }
246
247    fn process_csp_violations(
248        &mut self,
249        _request_id: RequestId,
250        violations: Vec<content_security_policy::Violation>,
251    ) {
252        let scope = self.scope.root();
253
254        if let Some(worker_scope) = scope.downcast::<DedicatedWorkerGlobalScope>() {
255            worker_scope.report_csp_violations(violations);
256        }
257    }
258}
259
260impl ResourceTimingListener for ScriptFetchContext {
261    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
262        (InitiatorType::Other, self.url.clone())
263    }
264
265    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
266        self.scope.root().global()
267    }
268}
269
270// https://html.spec.whatwg.org/multipage/#the-workerglobalscope-common-interface
271#[dom_struct]
272pub(crate) struct WorkerGlobalScope {
273    globalscope: GlobalScope,
274
275    /// <https://html.spec.whatwg.org/multipage/#microtask-queue>
276    #[conditional_malloc_size_of]
277    microtask_queue: Rc<MicrotaskQueue>,
278
279    worker_name: DOMString,
280    worker_type: WorkerType,
281
282    #[no_trace]
283    worker_id: WorkerId,
284    #[no_trace]
285    worker_url: DomRefCell<ServoUrl>,
286    #[conditional_malloc_size_of]
287    closing: Arc<AtomicBool>,
288    execution_ready: AtomicBool,
289    #[ignore_malloc_size_of = "Defined in js"]
290    runtime: DomRefCell<Option<Runtime>>,
291    location: MutNullableDom<WorkerLocation>,
292    navigator: MutNullableDom<WorkerNavigator>,
293    #[no_trace]
294    /// <https://html.spec.whatwg.org/multipage/#the-workerglobalscope-common-interface:policy-container>
295    policy_container: DomRefCell<PolicyContainer>,
296
297    #[ignore_malloc_size_of = "Defined in base"]
298    #[no_trace]
299    /// A `Sender` for sending messages to devtools. This is unused but is stored here to
300    /// keep the channel alive.
301    _devtools_sender: Option<GenericSender<DevtoolScriptControlMsg>>,
302
303    #[ignore_malloc_size_of = "Defined in base"]
304    #[no_trace]
305    /// A `Receiver` for receiving messages from devtools.
306    devtools_receiver: Option<RoutedReceiver<DevtoolScriptControlMsg>>,
307
308    #[no_trace]
309    navigation_start: CrossProcessInstant,
310    performance: MutNullableDom<Performance>,
311    trusted_types: MutNullableDom<TrustedTypePolicyFactory>,
312
313    /// A [`TimerScheduler`] used to schedule timers for this [`WorkerGlobalScope`].
314    /// Timers are handled in the service worker event loop.
315    #[no_trace]
316    timer_scheduler: RefCell<TimerScheduler>,
317
318    #[no_trace]
319    insecure_requests_policy: InsecureRequestsPolicy,
320
321    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-registered-reporting-observer-list>
322    reporting_observer_list: DomRefCell<Vec<DomRoot<ReportingObserver>>>,
323
324    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-reports>
325    report_list: DomRefCell<Vec<Report>>,
326
327    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-endpoints>
328    #[no_trace]
329    endpoints_list: DomRefCell<Vec<ReportingEndpoint>>,
330}
331
332impl WorkerGlobalScope {
333    #[allow(clippy::too_many_arguments)]
334    pub(crate) fn new_inherited(
335        init: WorkerGlobalScopeInit,
336        worker_name: DOMString,
337        worker_type: WorkerType,
338        worker_url: ServoUrl,
339        runtime: Runtime,
340        devtools_receiver: RoutedReceiver<DevtoolScriptControlMsg>,
341        closing: Arc<AtomicBool>,
342        #[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
343        insecure_requests_policy: InsecureRequestsPolicy,
344        font_context: Option<Arc<FontContext>>,
345    ) -> Self {
346        // Install a pipeline-namespace in the current thread.
347        PipelineNamespace::auto_install();
348
349        let devtools_receiver = match init.from_devtools_sender {
350            Some(..) => Some(devtools_receiver),
351            None => None,
352        };
353
354        Self {
355            globalscope: GlobalScope::new_inherited(
356                init.pipeline_id,
357                init.to_devtools_sender,
358                init.mem_profiler_chan,
359                init.time_profiler_chan,
360                init.script_to_constellation_chan,
361                init.script_to_embedder_chan,
362                init.resource_threads,
363                init.storage_threads,
364                MutableOrigin::new(init.origin),
365                worker_url.clone(),
366                None,
367                #[cfg(feature = "webgpu")]
368                gpu_id_hub,
369                init.inherited_secure_context,
370                init.unminify_js,
371                font_context,
372            ),
373            microtask_queue: runtime.microtask_queue.clone(),
374            worker_id: init.worker_id,
375            worker_name,
376            worker_type,
377            worker_url: DomRefCell::new(worker_url),
378            closing,
379            execution_ready: AtomicBool::new(false),
380            runtime: DomRefCell::new(Some(runtime)),
381            location: Default::default(),
382            navigator: Default::default(),
383            policy_container: Default::default(),
384            devtools_receiver,
385            _devtools_sender: init.from_devtools_sender,
386            navigation_start: CrossProcessInstant::now(),
387            performance: Default::default(),
388            timer_scheduler: RefCell::default(),
389            insecure_requests_policy,
390            trusted_types: Default::default(),
391            reporting_observer_list: Default::default(),
392            report_list: Default::default(),
393            endpoints_list: Default::default(),
394        }
395    }
396
397    pub(crate) fn enqueue_microtask(&self, job: Microtask) {
398        self.microtask_queue.enqueue(job, GlobalScope::get_cx());
399    }
400
401    /// Perform a microtask checkpoint.
402    pub(crate) fn perform_a_microtask_checkpoint(&self, cx: &mut js::context::JSContext) {
403        // Only perform the checkpoint if we're not shutting down.
404        if !self.is_closing() {
405            self.microtask_queue.checkpoint(
406                cx,
407                |_| Some(DomRoot::from_ref(&self.globalscope)),
408                vec![DomRoot::from_ref(&self.globalscope)],
409            );
410        }
411    }
412
413    /// Returns a policy value that should be used by fetches initiated by this worker.
414    pub(crate) fn insecure_requests_policy(&self) -> InsecureRequestsPolicy {
415        self.insecure_requests_policy
416    }
417
418    /// Clear various items when the worker event-loop shuts-down.
419    pub(crate) fn clear_js_runtime(&self) {
420        self.upcast::<GlobalScope>()
421            .remove_web_messaging_and_dedicated_workers_infra();
422
423        // Drop the runtime.
424        let runtime = self.runtime.borrow_mut().take();
425        drop(runtime);
426    }
427
428    pub(crate) fn runtime_handle(&self) -> ParentRuntime {
429        self.runtime
430            .borrow()
431            .as_ref()
432            .unwrap()
433            .prepare_for_new_child()
434    }
435
436    pub(crate) fn devtools_receiver(&self) -> Option<&RoutedReceiver<DevtoolScriptControlMsg>> {
437        self.devtools_receiver.as_ref()
438    }
439
440    #[expect(unsafe_code)]
441    pub(crate) fn get_cx(&self) -> JSContext {
442        unsafe { JSContext::from_ptr(js::rust::Runtime::get().unwrap().as_ptr()) }
443    }
444
445    pub(crate) fn is_closing(&self) -> bool {
446        self.closing.load(Ordering::SeqCst)
447    }
448
449    pub(crate) fn is_execution_ready(&self) -> bool {
450        self.execution_ready.load(Ordering::Relaxed)
451    }
452
453    pub(crate) fn get_url(&self) -> Ref<'_, ServoUrl> {
454        self.worker_url.borrow()
455    }
456
457    pub(crate) fn set_url(&self, url: ServoUrl) {
458        *self.worker_url.borrow_mut() = url;
459    }
460
461    pub(crate) fn worker_name(&self) -> DOMString {
462        self.worker_name.clone()
463    }
464
465    pub(crate) fn worker_id(&self) -> WorkerId {
466        self.worker_id
467    }
468
469    pub(crate) fn pipeline_id(&self) -> PipelineId {
470        self.globalscope.pipeline_id()
471    }
472
473    pub(crate) fn policy_container(&self) -> Ref<'_, PolicyContainer> {
474        self.policy_container.borrow()
475    }
476
477    pub(crate) fn set_csp_list(&self, csp_list: Option<CspList>) {
478        self.policy_container.borrow_mut().set_csp_list(csp_list);
479    }
480
481    pub(crate) fn set_referrer_policy(&self, referrer_policy: ReferrerPolicy) {
482        self.policy_container
483            .borrow_mut()
484            .set_referrer_policy(referrer_policy);
485    }
486
487    pub(crate) fn append_reporting_observer(&self, reporting_observer: DomRoot<ReportingObserver>) {
488        self.reporting_observer_list
489            .borrow_mut()
490            .push(reporting_observer);
491    }
492
493    pub(crate) fn remove_reporting_observer(&self, reporting_observer: &ReportingObserver) {
494        if let Some(index) = self
495            .reporting_observer_list
496            .borrow()
497            .iter()
498            .position(|observer| &**observer == reporting_observer)
499        {
500            self.reporting_observer_list.borrow_mut().remove(index);
501        }
502    }
503
504    pub(crate) fn registered_reporting_observers(&self) -> Vec<DomRoot<ReportingObserver>> {
505        self.reporting_observer_list.borrow().clone()
506    }
507
508    pub(crate) fn append_report(&self, report: Report) {
509        self.report_list.borrow_mut().push(report);
510        let trusted_worker = Trusted::new(self);
511        self.upcast::<GlobalScope>()
512            .task_manager()
513            .dom_manipulation_task_source()
514            .queue(task!(send_to_reporting_endpoints: move || {
515                let worker = trusted_worker.root();
516                let reports = std::mem::take(&mut *worker.report_list.borrow_mut());
517                worker.upcast::<GlobalScope>().send_reports_to_endpoints(
518                    reports,
519                    worker.endpoints_list.borrow().clone(),
520                );
521            }));
522    }
523
524    pub(crate) fn buffered_reports(&self) -> Vec<Report> {
525        self.report_list.borrow().clone()
526    }
527
528    pub(crate) fn set_endpoints_list(&self, endpoints: Option<Vec<ReportingEndpoint>>) {
529        if let Some(endpoints) = endpoints {
530            *self.endpoints_list.borrow_mut() = endpoints;
531        }
532    }
533
534    /// Get a mutable reference to the [`TimerScheduler`] for this [`ServiceWorkerGlobalScope`].
535    pub(crate) fn timer_scheduler(&self) -> RefMut<'_, TimerScheduler> {
536        self.timer_scheduler.borrow_mut()
537    }
538
539    /// Return a copy to the shared task canceller that is used to cancel all tasks
540    /// when this worker is closing.
541    pub(crate) fn shared_task_canceller(&self) -> TaskCanceller {
542        TaskCanceller {
543            cancelled: self.closing.clone(),
544        }
545    }
546
547    /// <https://html.spec.whatwg.org/multipage/#initialize-worker-policy-container> and
548    /// <https://html.spec.whatwg.org/multipage/#creating-a-policy-container-from-a-fetch-response>
549    fn initialize_policy_container_for_worker_global_scope(
550        &self,
551        metadata: &Metadata,
552        parent_policy_container: &PolicyContainer,
553    ) {
554        // Step 1. If workerGlobalScope's url is local but its scheme is not "blob":
555        //
556        // Note that we also allow for blob here, as the parent_policy_container is in both cases
557        // the container that we need to clone.
558        if metadata.final_url.is_local_scheme() {
559            // Step 1.2. Set workerGlobalScope's policy container to a clone of workerGlobalScope's
560            // owner set[0]'s relevant settings object's policy container.
561            //
562            // Step 1. If response's URL's scheme is "blob", then return a clone of response's URL's
563            // blob URL entry's environment's policy container.
564            self.set_csp_list(parent_policy_container.csp_list.clone());
565            self.set_referrer_policy(parent_policy_container.get_referrer_policy());
566            return;
567        }
568        // Step 3. Set result's CSP list to the result of parsing a response's Content Security Policies given response.
569        self.set_csp_list(parse_csp_list_from_metadata(&metadata.headers));
570        // Step 5. Set result's referrer policy to the result of parsing the `Referrer-Policy`
571        // header given response. [REFERRERPOLICY]
572        let referrer_policy = metadata
573            .headers
574            .as_ref()
575            .and_then(|headers| headers.typed_get::<ReferrerPolicyHeader>())
576            .into();
577        self.set_referrer_policy(referrer_policy);
578    }
579
580    /// onComplete algorithm defined inside <https://html.spec.whatwg.org/multipage/#run-a-worker>
581    #[expect(unsafe_code)]
582    fn on_complete(
583        &self,
584        script: Option<ClassicScript>,
585        worker: TrustedWorkerAddress,
586        cx: &mut js::context::JSContext,
587    ) {
588        let dedicated_worker_scope = self
589            .downcast::<DedicatedWorkerGlobalScope>()
590            .expect("Only DedicatedWorkerGlobalScope is supported for now");
591
592        // Step 1. If script is null or if script's error to rethrow is non-null, then:
593        let script = match script {
594            Some(script) if script.record.is_ok() => script,
595            _ => {
596                // Step 1.1 Queue a global task on the DOM manipulation task source given
597                // worker's relevant global object to fire an event named error at worker.
598                dedicated_worker_scope.forward_simple_error_at_worker(worker);
599
600                // TODO Step 1.2. Run the environment discarding steps for inside settings.
601                // Step 1.3 Abort these steps.
602                return;
603            },
604        };
605
606        unsafe {
607            // Handle interrupt requests
608            js::rust::wrappers2::JS_AddInterruptCallback(cx, Some(interrupt_callback));
609        }
610
611        if self.is_closing() {
612            return;
613        }
614
615        {
616            let _ar = AutoWorkerReset::new(dedicated_worker_scope, worker);
617            let mut realm = enter_auto_realm(cx, self);
618            let mut realm = realm.current_realm();
619            define_all_exposed_interfaces(&mut realm, dedicated_worker_scope.upcast());
620            self.execution_ready.store(true, Ordering::Relaxed);
621            _ = self.globalscope.run_a_classic_script(
622                script,
623                RethrowErrors::No,
624                CanGc::from_cx(&mut realm),
625            );
626            dedicated_worker_scope.fire_queued_messages(CanGc::from_cx(&mut realm));
627        }
628    }
629}
630
631impl WorkerGlobalScopeMethods<crate::DomTypeHolder> for WorkerGlobalScope {
632    /// <https://html.spec.whatwg.org/multipage/#dom-workerglobalscope-self>
633    fn Self_(&self) -> DomRoot<WorkerGlobalScope> {
634        DomRoot::from_ref(self)
635    }
636
637    /// <https://w3c.github.io/IndexedDB/#factory-interface>
638    fn IndexedDB(&self) -> DomRoot<IDBFactory> {
639        self.upcast::<GlobalScope>().get_indexeddb()
640    }
641
642    /// <https://html.spec.whatwg.org/multipage/#dom-workerglobalscope-location>
643    fn Location(&self) -> DomRoot<WorkerLocation> {
644        self.location
645            .or_init(|| WorkerLocation::new(self, self.worker_url.borrow().clone(), CanGc::note()))
646    }
647
648    /// <https://html.spec.whatwg.org/multipage/#dom-workerglobalscope-importscripts>
649    fn ImportScripts(
650        &self,
651        cx: &mut js::context::JSContext,
652        url_strings: Vec<TrustedScriptURLOrUSVString>,
653    ) -> ErrorResult {
654        // https://html.spec.whatwg.org/multipage/#import-scripts-into-worker-global-scope
655        // Step 1: If worker global scope's type is "module", throw a TypeError exception.
656        if self.worker_type == WorkerType::Module {
657            return Err(Error::Type(
658                c"importScripts() is not allowed in module workers".to_owned(),
659            ));
660        }
661
662        // Step 4: Let urlStrings be « ».
663        let mut urls = Vec::with_capacity(url_strings.len());
664        // Step 5: For each url of urls:
665        for url in url_strings {
666            // Step 3: Append the result of invoking the Get Trusted Type compliant string algorithm
667            // with TrustedScriptURL, this's relevant global object, url, "WorkerGlobalScope importScripts",
668            // and "script" to urlStrings.
669            let url = TrustedScriptURL::get_trusted_type_compliant_string(
670                cx,
671                self.upcast::<GlobalScope>(),
672                url,
673                "WorkerGlobalScope importScripts",
674            )?;
675            let url = self.worker_url.borrow().join(&url.str());
676            match url {
677                Ok(url) => urls.push(url),
678                Err(_) => return Err(Error::Syntax(None)),
679            };
680        }
681
682        for url in urls {
683            let global_scope = self.upcast::<GlobalScope>();
684            let request = RequestBuilder::new(
685                global_scope.webview_id(),
686                url.clone(),
687                global_scope.get_referrer(),
688            )
689            .destination(Destination::Script)
690            .credentials_mode(CredentialsMode::Include)
691            .parser_metadata(ParserMetadata::NotParserInserted)
692            .use_url_credentials(true)
693            .with_global_scope(global_scope);
694
695            // https://html.spec.whatwg.org/multipage/#fetch-a-classic-worker-imported-script
696            let (url, bytes, muted_errors) = match load_whole_resource(
697                request,
698                &global_scope.resource_threads().sender(),
699                global_scope,
700                &WorkerCspProcessor {
701                    global_scope: DomRoot::from_ref(global_scope),
702                },
703                cx,
704            ) {
705                Err(_) => return Err(Error::Network(None)),
706                Ok((metadata, bytes, muted_errors)) => {
707                    // Step 7: Check if response status is not an ok status
708                    if !metadata.status.is_success() {
709                        return Err(Error::Network(None));
710                    }
711
712                    // Step 7: Check if the MIME type is not a JavaScript MIME type
713                    let not_a_javascript_mime_type =
714                        !metadata.content_type.clone().is_some_and(|ct| {
715                            let mime: Mime = ct.into_inner().into();
716                            SCRIPT_JS_MIMES.contains(&mime.essence_str())
717                        });
718                    if not_a_javascript_mime_type {
719                        return Err(Error::Network(None));
720                    }
721
722                    (metadata.final_url, bytes, muted_errors)
723                },
724            };
725
726            // Step 8. Let sourceText be the result of UTF-8 decoding bodyBytes.
727            let (source, _) = UTF_8.decode_with_bom_removal(&bytes);
728
729            // Step 9. Let mutedErrors be true if response was CORS-cross-origin, and false otherwise.
730            // Note: done inside load_whole_resource
731
732            // Step 10. Let script be the result of creating a classic script
733            // given sourceText, settingsObject, response's URL, the default script fetch options, and mutedErrors.
734            let script = self.globalscope.create_a_classic_script(
735                source,
736                url,
737                ScriptFetchOptions::default_classic_script(&self.globalscope),
738                ErrorReporting::from(muted_errors),
739                Some(IntroductionType::WORKER),
740                1,
741                true,
742            );
743
744            // Run the classic script script, with rethrow errors set to true.
745            let result = self.globalscope.run_a_classic_script(
746                script,
747                RethrowErrors::Yes,
748                CanGc::from_cx(cx),
749            );
750
751            if let Err(error) = result {
752                if self.is_closing() {
753                    // Don't return JSFailed as we might not have
754                    // any pending exceptions.
755                    error!("evaluate_script failed (terminated)");
756                } else {
757                    error!("evaluate_script failed");
758                    return Err(error);
759                }
760            }
761        }
762
763        Ok(())
764    }
765
766    // https://html.spec.whatwg.org/multipage/#handler-workerglobalscope-onerror
767    error_event_handler!(error, GetOnerror, SetOnerror);
768
769    // https://html.spec.whatwg.org/multipage/#handler-workerglobalscope-onlanguagechange
770    event_handler!(languagechange, GetOnlanguagechange, SetOnlanguagechange);
771
772    // https://html.spec.whatwg.org/multipage/#handler-workerglobalscope-onoffline
773    event_handler!(offline, GetOnoffline, SetOnoffline);
774
775    // https://html.spec.whatwg.org/multipage/#handler-workerglobalscope-ononline
776    event_handler!(online, GetOnonline, SetOnonline);
777
778    // https://html.spec.whatwg.org/multipage/#handler-workerglobalscope-onrejectionhandled
779    event_handler!(
780        rejectionhandled,
781        GetOnrejectionhandled,
782        SetOnrejectionhandled
783    );
784
785    // https://html.spec.whatwg.org/multipage/#handler-workerglobalscope-onunhandledrejection
786    event_handler!(
787        unhandledrejection,
788        GetOnunhandledrejection,
789        SetOnunhandledrejection
790    );
791
792    /// <https://html.spec.whatwg.org/multipage/#dom-worker-navigator>
793    fn Navigator(&self) -> DomRoot<WorkerNavigator> {
794        self.navigator
795            .or_init(|| WorkerNavigator::new(self, CanGc::note()))
796    }
797
798    /// <https://html.spec.whatwg.org/multipage/#dfn-Crypto>
799    fn Crypto(&self) -> DomRoot<Crypto> {
800        self.upcast::<GlobalScope>().crypto(CanGc::note())
801    }
802
803    /// <https://html.spec.whatwg.org/multipage/#dom-reporterror>
804    fn ReportError(&self, cx: JSContext, error: HandleValue, can_gc: CanGc) {
805        self.upcast::<GlobalScope>()
806            .report_an_exception(cx, error, can_gc);
807    }
808
809    /// <https://html.spec.whatwg.org/multipage/#dom-windowbase64-btoa>
810    fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
811        base64_btoa(btoa)
812    }
813
814    /// <https://html.spec.whatwg.org/multipage/#dom-windowbase64-atob>
815    fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
816        base64_atob(atob)
817    }
818
819    /// <https://html.spec.whatwg.org/multipage/#dom-windowtimers-settimeout>
820    fn SetTimeout(
821        &self,
822        _cx: JSContext,
823        callback: TrustedScriptOrStringOrFunction,
824        timeout: i32,
825        args: Vec<HandleValue>,
826        can_gc: CanGc,
827    ) -> Fallible<i32> {
828        let callback = match callback {
829            TrustedScriptOrStringOrFunction::String(i) => {
830                TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
831            },
832            TrustedScriptOrStringOrFunction::TrustedScript(i) => {
833                TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
834            },
835            TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
836        };
837        self.upcast::<GlobalScope>().set_timeout_or_interval(
838            callback,
839            args,
840            Duration::from_millis(timeout.max(0) as u64),
841            IsInterval::NonInterval,
842            can_gc,
843        )
844    }
845
846    /// <https://html.spec.whatwg.org/multipage/#dom-windowtimers-cleartimeout>
847    fn ClearTimeout(&self, handle: i32) {
848        self.upcast::<GlobalScope>()
849            .clear_timeout_or_interval(handle);
850    }
851
852    /// <https://html.spec.whatwg.org/multipage/#dom-windowtimers-setinterval>
853    fn SetInterval(
854        &self,
855        _cx: JSContext,
856        callback: TrustedScriptOrStringOrFunction,
857        timeout: i32,
858        args: Vec<HandleValue>,
859        can_gc: CanGc,
860    ) -> Fallible<i32> {
861        let callback = match callback {
862            TrustedScriptOrStringOrFunction::String(i) => {
863                TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
864            },
865            TrustedScriptOrStringOrFunction::TrustedScript(i) => {
866                TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
867            },
868            TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
869        };
870        self.upcast::<GlobalScope>().set_timeout_or_interval(
871            callback,
872            args,
873            Duration::from_millis(timeout.max(0) as u64),
874            IsInterval::Interval,
875            can_gc,
876        )
877    }
878
879    /// <https://html.spec.whatwg.org/multipage/#dom-windowtimers-clearinterval>
880    fn ClearInterval(&self, handle: i32) {
881        self.ClearTimeout(handle);
882    }
883
884    /// <https://html.spec.whatwg.org/multipage/#dom-queuemicrotask>
885    fn QueueMicrotask(&self, callback: Rc<VoidFunction>) {
886        self.enqueue_microtask(Microtask::User(UserMicrotask {
887            callback,
888            pipeline: self.pipeline_id(),
889        }));
890    }
891
892    /// <https://html.spec.whatwg.org/multipage/#dom-createimagebitmap>
893    fn CreateImageBitmap(
894        &self,
895        realm: &mut CurrentRealm,
896        image: ImageBitmapSource,
897        options: &ImageBitmapOptions,
898    ) -> Rc<Promise> {
899        ImageBitmap::create_image_bitmap(self.upcast(), image, 0, 0, None, None, options, realm)
900    }
901
902    /// <https://html.spec.whatwg.org/multipage/#dom-createimagebitmap>
903    fn CreateImageBitmap_(
904        &self,
905        realm: &mut CurrentRealm,
906        image: ImageBitmapSource,
907        sx: i32,
908        sy: i32,
909        sw: i32,
910        sh: i32,
911        options: &ImageBitmapOptions,
912    ) -> Rc<Promise> {
913        ImageBitmap::create_image_bitmap(
914            self.upcast(),
915            image,
916            sx,
917            sy,
918            Some(sw),
919            Some(sh),
920            options,
921            realm,
922        )
923    }
924
925    /// <https://fetch.spec.whatwg.org/#dom-global-fetch>
926    fn Fetch(
927        &self,
928        input: RequestOrUSVString,
929        init: RootedTraceableBox<RequestInit>,
930        comp: InRealm,
931        can_gc: CanGc,
932    ) -> Rc<Promise> {
933        Fetch(self.upcast(), input, init, comp, can_gc)
934    }
935
936    /// <https://w3c.github.io/hr-time/#the-performance-attribute>
937    fn Performance(&self) -> DomRoot<Performance> {
938        self.performance.or_init(|| {
939            let global_scope = self.upcast::<GlobalScope>();
940            Performance::new(global_scope, self.navigation_start, CanGc::note())
941        })
942    }
943
944    /// <https://html.spec.whatwg.org/multipage/#dom-origin>
945    fn Origin(&self) -> USVString {
946        USVString(
947            self.upcast::<GlobalScope>()
948                .origin()
949                .immutable()
950                .ascii_serialization(),
951        )
952    }
953
954    /// <https://w3c.github.io/webappsec-secure-contexts/#dom-windoworworkerglobalscope-issecurecontext>
955    fn IsSecureContext(&self) -> bool {
956        self.upcast::<GlobalScope>().is_secure_context()
957    }
958
959    /// <https://html.spec.whatwg.org/multipage/#dom-structuredclone>
960    fn StructuredClone(
961        &self,
962        cx: JSContext,
963        value: HandleValue,
964        options: RootedTraceableBox<StructuredSerializeOptions>,
965        can_gc: CanGc,
966        retval: MutableHandleValue,
967    ) -> Fallible<()> {
968        self.upcast::<GlobalScope>()
969            .structured_clone(cx, value, options, retval, can_gc)
970    }
971
972    /// <https://www.w3.org/TR/trusted-types/#dom-windoworworkerglobalscope-trustedtypes>
973    fn TrustedTypes(&self, can_gc: CanGc) -> DomRoot<TrustedTypePolicyFactory> {
974        self.trusted_types.or_init(|| {
975            let global_scope = self.upcast::<GlobalScope>();
976            TrustedTypePolicyFactory::new(global_scope, can_gc)
977        })
978    }
979}
980
981impl WorkerGlobalScope {
982    pub(crate) fn new_script_pair(&self) -> (ScriptEventLoopSender, ScriptEventLoopReceiver) {
983        let dedicated = self.downcast::<DedicatedWorkerGlobalScope>();
984        if let Some(dedicated) = dedicated {
985            dedicated.new_script_pair()
986        } else {
987            panic!("need to implement a sender for SharedWorker/ServiceWorker")
988        }
989    }
990
991    /// Process a single event as if it were the next event
992    /// in the queue for this worker event-loop.
993    /// Returns a boolean indicating whether further events should be processed.
994    pub(crate) fn process_event(
995        &self,
996        msg: CommonScriptMsg,
997        cx: &mut js::context::JSContext,
998    ) -> bool {
999        if self.is_closing() {
1000            return false;
1001        }
1002        match msg {
1003            CommonScriptMsg::Task(_, task, _, _) => task.run_box(cx),
1004            CommonScriptMsg::CollectReports(reports_chan) => {
1005                let cx: JSContext = cx.into();
1006                perform_memory_report(|ops| {
1007                    let reports = cx.get_reports(format!("url({})", self.get_url()), ops);
1008                    reports_chan.send(ProcessReports::new(reports));
1009                });
1010            },
1011            CommonScriptMsg::ReportCspViolations(_, violations) => {
1012                self.upcast::<GlobalScope>()
1013                    .report_csp_violations(violations, None, None);
1014            },
1015        }
1016        true
1017    }
1018
1019    pub(crate) fn close(&self) {
1020        self.closing.store(true, Ordering::SeqCst);
1021        self.upcast::<GlobalScope>()
1022            .task_manager()
1023            .cancel_all_tasks_and_ignore_future_tasks();
1024        if let Some(factory) = self.upcast::<GlobalScope>().get_existing_indexeddb() {
1025            factory.abort_pending_upgrades();
1026        }
1027    }
1028}
1029
1030struct WorkerCspProcessor {
1031    global_scope: DomRoot<GlobalScope>,
1032}
1033
1034impl CspViolationsProcessor for WorkerCspProcessor {
1035    fn process_csp_violations(&self, violations: Vec<Violation>) {
1036        self.global_scope
1037            .report_csp_violations(violations, None, None);
1038    }
1039}