script/dom/
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 stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::PopStateEventBinding;
13use crate::dom::bindings::codegen::Bindings::PopStateEventBinding::PopStateEventMethods;
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
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        target: &EventTarget,
74        window: &Window,
75        state: HandleValue,
76        can_gc: CanGc,
77    ) {
78        let event =
79            PopStateEvent::new(window, None, atom!("popstate"), false, false, state, can_gc);
80        event.upcast::<Event>().fire(target, can_gc);
81    }
82}
83
84impl PopStateEventMethods<crate::DomTypeHolder> for PopStateEvent {
85    // https://html.spec.whatwg.org/multipage/#popstateevent
86    fn Constructor(
87        window: &Window,
88        proto: Option<HandleObject>,
89        can_gc: CanGc,
90        type_: DOMString,
91        init: RootedTraceableBox<PopStateEventBinding::PopStateEventInit>,
92    ) -> Fallible<DomRoot<PopStateEvent>> {
93        Ok(PopStateEvent::new(
94            window,
95            proto,
96            Atom::from(type_),
97            init.parent.bubbles,
98            init.parent.cancelable,
99            init.state.handle(),
100            can_gc,
101        ))
102    }
103
104    // https://html.spec.whatwg.org/multipage/#dom-popstateevent-state
105    fn State(&self, _cx: JSContext, mut retval: MutableHandleValue) {
106        retval.set(self.state.get())
107    }
108
109    // https://dom.spec.whatwg.org/#dom-event-istrusted
110    fn IsTrusted(&self) -> bool {
111        self.event.IsTrusted()
112    }
113}