script/dom/
workerlocation.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 servo_url::{ImmutableOrigin, ServoUrl};
7
8use crate::dom::bindings::codegen::Bindings::WorkerLocationBinding::WorkerLocationMethods;
9use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
10use crate::dom::bindings::root::DomRoot;
11use crate::dom::bindings::str::USVString;
12use crate::dom::urlhelper::UrlHelper;
13use crate::dom::workerglobalscope::WorkerGlobalScope;
14use crate::script_runtime::CanGc;
15
16// https://html.spec.whatwg.org/multipage/#worker-locations
17#[dom_struct]
18pub(crate) struct WorkerLocation {
19    reflector_: Reflector,
20    #[no_trace]
21    url: ServoUrl,
22}
23
24impl WorkerLocation {
25    fn new_inherited(url: ServoUrl) -> WorkerLocation {
26        WorkerLocation {
27            reflector_: Reflector::new(),
28            url,
29        }
30    }
31
32    pub(crate) fn new(
33        global: &WorkerGlobalScope,
34        url: ServoUrl,
35        can_gc: CanGc,
36    ) -> DomRoot<WorkerLocation> {
37        reflect_dom_object(Box::new(WorkerLocation::new_inherited(url)), global, can_gc)
38    }
39
40    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-origin
41    #[allow(dead_code)]
42    pub(crate) fn origin(&self) -> ImmutableOrigin {
43        self.url.origin()
44    }
45}
46
47impl WorkerLocationMethods<crate::DomTypeHolder> for WorkerLocation {
48    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-hash
49    fn Hash(&self) -> USVString {
50        UrlHelper::Hash(&self.url)
51    }
52
53    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-host
54    fn Host(&self) -> USVString {
55        UrlHelper::Host(&self.url)
56    }
57
58    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-hostname
59    fn Hostname(&self) -> USVString {
60        UrlHelper::Hostname(&self.url)
61    }
62
63    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-href
64    fn Href(&self) -> USVString {
65        UrlHelper::Href(&self.url)
66    }
67
68    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-origin
69    fn Origin(&self) -> USVString {
70        UrlHelper::Origin(&self.url)
71    }
72
73    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-pathname
74    fn Pathname(&self) -> USVString {
75        UrlHelper::Pathname(&self.url)
76    }
77
78    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-port
79    fn Port(&self) -> USVString {
80        UrlHelper::Port(&self.url)
81    }
82
83    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-protocol
84    fn Protocol(&self) -> USVString {
85        UrlHelper::Protocol(&self.url)
86    }
87
88    // https://html.spec.whatwg.org/multipage/#dom-workerlocation-search
89    fn Search(&self) -> USVString {
90        UrlHelper::Search(&self.url)
91    }
92}