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