Skip to main content

script/dom/event/
progressevent.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::HandleObject;
8use script_bindings::num::Finite;
9use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
10use stylo_atoms::Atom;
11
12use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
13use crate::dom::bindings::codegen::Bindings::ProgressEventBinding;
14use crate::dom::bindings::codegen::Bindings::ProgressEventBinding::ProgressEventMethods;
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::event::{Event, EventBubbles, EventCancelable};
20use crate::dom::globalscope::GlobalScope;
21
22#[dom_struct]
23pub(crate) struct ProgressEvent {
24    event: Event,
25    length_computable: bool,
26    loaded: Finite<f64>,
27    total: Finite<f64>,
28}
29
30impl ProgressEvent {
31    fn new_inherited(
32        length_computable: bool,
33        loaded: Finite<f64>,
34        total: Finite<f64>,
35    ) -> ProgressEvent {
36        ProgressEvent {
37            event: Event::new_inherited(),
38            length_computable,
39            loaded,
40            total,
41        }
42    }
43
44    #[expect(clippy::too_many_arguments)]
45    pub(crate) fn new(
46        cx: &mut JSContext,
47        global: &GlobalScope,
48        type_: Atom,
49        can_bubble: EventBubbles,
50        cancelable: EventCancelable,
51        length_computable: bool,
52        loaded: Finite<f64>,
53        total: Finite<f64>,
54    ) -> DomRoot<ProgressEvent> {
55        Self::new_with_proto(
56            cx,
57            global,
58            None,
59            type_,
60            can_bubble,
61            cancelable,
62            length_computable,
63            loaded,
64            total,
65        )
66    }
67
68    #[expect(clippy::too_many_arguments)]
69    fn new_with_proto(
70        cx: &mut JSContext,
71        global: &GlobalScope,
72        proto: Option<HandleObject>,
73        type_: Atom,
74        can_bubble: EventBubbles,
75        cancelable: EventCancelable,
76        length_computable: bool,
77        loaded: Finite<f64>,
78        total: Finite<f64>,
79    ) -> DomRoot<ProgressEvent> {
80        let ev = reflect_dom_object_with_proto_and_cx(
81            Box::new(ProgressEvent::new_inherited(
82                length_computable,
83                loaded,
84                total,
85            )),
86            global,
87            proto,
88            cx,
89        );
90        {
91            let event = ev.upcast::<Event>();
92            event.init_event(type_, bool::from(can_bubble), bool::from(cancelable));
93        }
94        ev
95    }
96}
97
98impl ProgressEventMethods<crate::DomTypeHolder> for ProgressEvent {
99    /// <https://xhr.spec.whatwg.org/#dom-progressevent-progressevent>
100    fn Constructor(
101        cx: &mut JSContext,
102        global: &GlobalScope,
103        proto: Option<HandleObject>,
104        type_: DOMString,
105        init: &ProgressEventBinding::ProgressEventInit,
106    ) -> Fallible<DomRoot<ProgressEvent>> {
107        let bubbles = EventBubbles::from(init.parent.bubbles);
108        let cancelable = EventCancelable::from(init.parent.cancelable);
109        let ev = ProgressEvent::new_with_proto(
110            cx,
111            global,
112            proto,
113            Atom::from(type_),
114            bubbles,
115            cancelable,
116            init.lengthComputable,
117            init.loaded,
118            init.total,
119        );
120        Ok(ev)
121    }
122
123    /// <https://xhr.spec.whatwg.org/#dom-progressevent-lengthcomputable>
124    fn LengthComputable(&self) -> bool {
125        self.length_computable
126    }
127
128    /// <https://xhr.spec.whatwg.org/#dom-progressevent-loaded>
129    fn Loaded(&self) -> Finite<f64> {
130        self.loaded
131    }
132
133    /// <https://xhr.spec.whatwg.org/#dom-progressevent-total>
134    fn Total(&self) -> Finite<f64> {
135        self.total
136    }
137
138    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
139    fn IsTrusted(&self) -> bool {
140        self.event.IsTrusted()
141    }
142}