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::inheritance::Castable;
9use script_bindings::reflector::{Reflector, reflect_dom_object};
10use servo_config::pref;
11
12use crate::dom::bindings::codegen::Bindings::WorkerNavigatorBinding::WorkerNavigatorMethods;
13use crate::dom::bindings::reflector::DomGlobal;
14use crate::dom::bindings::root::{DomRoot, MutNullableDom};
15use crate::dom::bindings::str::DOMString;
16use crate::dom::bindings::utils::to_frozen_array;
17use crate::dom::navigator::hardware_concurrency;
18use crate::dom::navigatorinfo;
19use crate::dom::permissions::Permissions;
20use crate::dom::storagemanager::StorageManager;
21#[cfg(feature = "webgpu")]
22use crate::dom::webgpu::gpu::GPU;
23use crate::dom::workerglobalscope::WorkerGlobalScope;
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(cx: &mut JSContext, global: &WorkerGlobalScope) -> DomRoot<WorkerNavigator> {
47        reflect_dom_object(cx, Box::new(WorkerNavigator::new_inherited()), global)
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        self.global()
115            .downcast::<WorkerGlobalScope>()
116            .expect("This always belongs to a WorkerGlobalScope")
117            .online()
118    }
119
120    /// <https://w3c.github.io/permissions/#navigator-and-workernavigator-extension>
121    fn Permissions(&self, cx: &mut JSContext) -> DomRoot<Permissions> {
122        self.permissions
123            .or_init(|| Permissions::new(cx, &self.global()))
124    }
125
126    /// <https://storage.spec.whatwg.org/#api>
127    fn Storage(&self, cx: &mut JSContext) -> DomRoot<StorageManager> {
128        self.storage
129            .or_init(|| StorageManager::new(cx, &self.global()))
130    }
131
132    // https://gpuweb.github.io/gpuweb/#dom-navigator-gpu
133    #[cfg(feature = "webgpu")]
134    fn Gpu(&self, cx: &mut JSContext) -> DomRoot<GPU> {
135        self.gpu.or_init(|| GPU::new(cx, &self.global()))
136    }
137
138    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-hardwareconcurrency>
139    fn HardwareConcurrency(&self) -> u64 {
140        hardware_concurrency()
141    }
142}