Skip to main content

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