script/dom/workers/
workernavigator.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 dom_struct::dom_struct;
6use js::rust::MutableHandleValue;
7use servo_config::pref;
8
9use crate::dom::bindings::codegen::Bindings::WorkerNavigatorBinding::WorkerNavigatorMethods;
10use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
11use crate::dom::bindings::root::{DomRoot, MutNullableDom};
12use crate::dom::bindings::str::DOMString;
13use crate::dom::bindings::utils::to_frozen_array;
14use crate::dom::navigator::hardware_concurrency;
15use crate::dom::navigatorinfo;
16use crate::dom::permissions::Permissions;
17#[cfg(feature = "webgpu")]
18use crate::dom::webgpu::gpu::GPU;
19use crate::dom::workerglobalscope::WorkerGlobalScope;
20use crate::script_runtime::{CanGc, JSContext};
21
22// https://html.spec.whatwg.org/multipage/#workernavigator
23#[dom_struct]
24pub(crate) struct WorkerNavigator {
25    reflector_: Reflector,
26    permissions: MutNullableDom<Permissions>,
27    #[cfg(feature = "webgpu")]
28    gpu: MutNullableDom<GPU>,
29}
30
31impl WorkerNavigator {
32    fn new_inherited() -> WorkerNavigator {
33        WorkerNavigator {
34            reflector_: Reflector::new(),
35            permissions: Default::default(),
36            #[cfg(feature = "webgpu")]
37            gpu: Default::default(),
38        }
39    }
40
41    pub(crate) fn new(global: &WorkerGlobalScope, can_gc: CanGc) -> DomRoot<WorkerNavigator> {
42        reflect_dom_object(Box::new(WorkerNavigator::new_inherited()), global, can_gc)
43    }
44}
45
46impl WorkerNavigatorMethods<crate::DomTypeHolder> for WorkerNavigator {
47    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-product>
48    fn Product(&self) -> DOMString {
49        navigatorinfo::Product()
50    }
51
52    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-productsub>
53    fn ProductSub(&self) -> DOMString {
54        navigatorinfo::ProductSub()
55    }
56
57    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-vendor>
58    fn Vendor(&self) -> DOMString {
59        navigatorinfo::Vendor()
60    }
61
62    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-vendorsub>
63    fn VendorSub(&self) -> DOMString {
64        navigatorinfo::VendorSub()
65    }
66
67    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-taintenabled>
68    fn TaintEnabled(&self) -> bool {
69        navigatorinfo::TaintEnabled()
70    }
71
72    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-appname>
73    fn AppName(&self) -> DOMString {
74        navigatorinfo::AppName()
75    }
76
77    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-appcodename>
78    fn AppCodeName(&self) -> DOMString {
79        navigatorinfo::AppCodeName()
80    }
81
82    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-platform>
83    fn Platform(&self) -> DOMString {
84        navigatorinfo::Platform()
85    }
86
87    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-useragent>
88    fn UserAgent(&self) -> DOMString {
89        navigatorinfo::UserAgent(&pref!(user_agent))
90    }
91
92    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-appversion>
93    fn AppVersion(&self) -> DOMString {
94        navigatorinfo::AppVersion()
95    }
96
97    /// <https://html.spec.whatwg.org/multipage/#navigatorlanguage>
98    fn Language(&self) -> DOMString {
99        navigatorinfo::Language()
100    }
101
102    // https://html.spec.whatwg.org/multipage/#dom-navigator-languages
103    fn Languages(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
104        to_frozen_array(&[self.Language()], cx, retval, can_gc)
105    }
106
107    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-online>
108    fn OnLine(&self) -> bool {
109        true
110    }
111
112    /// <https://w3c.github.io/permissions/#navigator-and-workernavigator-extension>
113    fn Permissions(&self) -> DomRoot<Permissions> {
114        self.permissions
115            .or_init(|| Permissions::new(&self.global(), CanGc::note()))
116    }
117
118    // https://gpuweb.github.io/gpuweb/#dom-navigator-gpu
119    #[cfg(feature = "webgpu")]
120    fn Gpu(&self) -> DomRoot<GPU> {
121        self.gpu.or_init(|| GPU::new(&self.global(), CanGc::note()))
122    }
123
124    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-hardwareconcurrency>
125    fn HardwareConcurrency(&self) -> u64 {
126        hardware_concurrency()
127    }
128}