script/dom/
dissimilaroriginlocation.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;
6
7use crate::dom::bindings::codegen::Bindings::DissimilarOriginLocationBinding::DissimilarOriginLocationMethods;
8use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
9use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
10use crate::dom::bindings::root::{Dom, DomRoot};
11use crate::dom::bindings::str::{DOMString, USVString};
12use crate::dom::dissimilaroriginwindow::DissimilarOriginWindow;
13use crate::script_runtime::CanGc;
14
15/// Represents a dissimilar-origin `Location` that exists in another script thread.
16///
17/// Since the `Location` is in a different script thread, we cannot access it
18/// directly, but some of its accessors (for example setting `location.href`)
19/// still need to function.
20
21#[dom_struct]
22pub(crate) struct DissimilarOriginLocation {
23    /// The reflector. Once we have XOWs, this will have a cross-origin
24    /// wrapper placed around it.
25    reflector: Reflector,
26
27    /// The window associated with this location.
28    window: Dom<DissimilarOriginWindow>,
29}
30
31impl DissimilarOriginLocation {
32    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
33    fn new_inherited(window: &DissimilarOriginWindow) -> DissimilarOriginLocation {
34        DissimilarOriginLocation {
35            reflector: Reflector::new(),
36            window: Dom::from_ref(window),
37        }
38    }
39
40    pub(crate) fn new(
41        window: &DissimilarOriginWindow,
42        can_gc: CanGc,
43    ) -> DomRoot<DissimilarOriginLocation> {
44        reflect_dom_object(
45            Box::new(DissimilarOriginLocation::new_inherited(window)),
46            window,
47            can_gc,
48        )
49    }
50}
51
52impl DissimilarOriginLocationMethods<crate::DomTypeHolder> for DissimilarOriginLocation {
53    // https://html.spec.whatwg.org/multipage/#dom-location-href
54    fn GetHref(&self) -> Fallible<USVString> {
55        Err(Error::Security)
56    }
57
58    // https://html.spec.whatwg.org/multipage/#dom-location-href
59    fn SetHref(&self, _: USVString) -> ErrorResult {
60        // TODO: setting href on a cross-origin window should succeed?
61        Err(Error::Security)
62    }
63
64    // https://html.spec.whatwg.org/multipage/#dom-location-assign
65    fn Assign(&self, _: USVString) -> Fallible<()> {
66        // TODO: setting href on a cross-origin window should succeed?
67        Err(Error::Security)
68    }
69
70    // https://html.spec.whatwg.org/multipage/#dom-location-replace
71    fn Replace(&self, _: USVString) -> Fallible<()> {
72        // TODO: replacing href on a cross-origin window should succeed?
73        Err(Error::Security)
74    }
75
76    // https://html.spec.whatwg.org/multipage/#dom-location-reload
77    fn Reload(&self) -> Fallible<()> {
78        Err(Error::Security)
79    }
80
81    // https://html.spec.whatwg.org/multipage/#dom-location-href
82    fn Stringifier(&self) -> Fallible<DOMString> {
83        Err(Error::Security)
84    }
85}