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