Skip to main content

script/dom/event/
popstateevent.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::jsapi::Heap;
7use js::jsval::JSVal;
8use js::rust::{HandleObject, HandleValue, MutableHandleValue};
9use script_bindings::reflector::reflect_dom_object_with_proto;
10use stylo_atoms::Atom;
11
12use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
13use crate::dom::bindings::codegen::Bindings::PopStateEventBinding;
14use crate::dom::bindings::codegen::Bindings::PopStateEventBinding::PopStateEventMethods;
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::bindings::trace::RootedTraceableBox;
20use crate::dom::event::Event;
21use crate::dom::eventtarget::EventTarget;
22use crate::dom::window::Window;
23use crate::script_runtime::{CanGc, JSContext};
24
25// https://html.spec.whatwg.org/multipage/#the-popstateevent-interface
26#[dom_struct]
27pub(crate) struct PopStateEvent {
28    event: Event,
29    #[ignore_malloc_size_of = "Defined in rust-mozjs"]
30    state: Heap<JSVal>,
31}
32
33impl PopStateEvent {
34    fn new_inherited() -> PopStateEvent {
35        PopStateEvent {
36            event: Event::new_inherited(),
37            state: Heap::default(),
38        }
39    }
40
41    fn new_uninitialized(
42        window: &Window,
43        proto: Option<HandleObject>,
44        can_gc: CanGc,
45    ) -> DomRoot<PopStateEvent> {
46        reflect_dom_object_with_proto(
47            Box::new(PopStateEvent::new_inherited()),
48            window,
49            proto,
50            can_gc,
51        )
52    }
53
54    fn new(
55        window: &Window,
56        proto: Option<HandleObject>,
57        type_: Atom,
58        bubbles: bool,
59        cancelable: bool,
60        state: HandleValue,
61        can_gc: CanGc,
62    ) -> DomRoot<PopStateEvent> {
63        let ev = PopStateEvent::new_uninitialized(window, proto, can_gc);
64        ev.state.set(state.get());
65        {
66            let event = ev.upcast::<Event>();
67            event.init_event(type_, bubbles, cancelable);
68        }
69        ev
70    }
71
72    pub(crate) fn dispatch_jsval(
73        cx: &mut js::context::JSContext,
74        target: &EventTarget,
75        window: &Window,
76        state: HandleValue,
77    ) {
78        let event = PopStateEvent::new(
79            window,
80            None,
81            atom!("popstate"),
82            false,
83            false,
84            state,
85            CanGc::from_cx(cx),
86        );
87        event.upcast::<Event>().fire(cx, target);
88    }
89}
90
91impl PopStateEventMethods<crate::DomTypeHolder> for PopStateEvent {
92    /// <https://html.spec.whatwg.org/multipage/#popstateevent>
93    fn Constructor(
94        window: &Window,
95        proto: Option<HandleObject>,
96        can_gc: CanGc,
97        type_: DOMString,
98        init: RootedTraceableBox<PopStateEventBinding::PopStateEventInit>,
99    ) -> Fallible<DomRoot<PopStateEvent>> {
100        Ok(PopStateEvent::new(
101            window,
102            proto,
103            Atom::from(type_),
104            init.parent.bubbles,
105            init.parent.cancelable,
106            init.state.handle(),
107            can_gc,
108        ))
109    }
110
111    /// <https://html.spec.whatwg.org/multipage/#dom-popstateevent-state>
112    fn State(&self, _cx: JSContext, mut retval: MutableHandleValue) {
113        retval.set(self.state.get())
114    }
115
116    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
117    fn IsTrusted(&self) -> bool {
118        self.event.IsTrusted()
119    }
120}