script/dom/
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    #[allow(unsafe_code)]
104    fn Languages(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
105        to_frozen_array(&[self.Language()], cx, retval, can_gc)
106    }
107
108    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-online>
109    fn OnLine(&self) -> bool {
110        true
111    }
112
113    // https://w3c.github.io/permissions/#navigator-and-workernavigator-extension
114    fn Permissions(&self) -> DomRoot<Permissions> {
115        self.permissions
116            .or_init(|| Permissions::new(&self.global(), CanGc::note()))
117    }
118
119    // https://gpuweb.github.io/gpuweb/#dom-navigator-gpu
120    #[cfg(feature = "webgpu")]
121    fn Gpu(&self) -> DomRoot<GPU> {
122        self.gpu.or_init(|| GPU::new(&self.global(), CanGc::note()))
123    }
124
125    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-hardwareconcurrency>
126    fn HardwareConcurrency(&self) -> u64 {
127        hardware_concurrency()
128    }
129}