script/dom/
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::rust::HandleObject;
7use script_bindings::num::Finite;
8use stylo_atoms::Atom;
9
10use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
11use crate::dom::bindings::codegen::Bindings::ProgressEventBinding;
12use crate::dom::bindings::codegen::Bindings::ProgressEventBinding::ProgressEventMethods;
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::event::{Event, EventBubbles, EventCancelable};
19use crate::dom::globalscope::GlobalScope;
20use crate::script_runtime::CanGc;
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    #[allow(clippy::too_many_arguments)]
45    pub(crate) fn new(
46        global: &GlobalScope,
47        type_: Atom,
48        can_bubble: EventBubbles,
49        cancelable: EventCancelable,
50        length_computable: bool,
51        loaded: Finite<f64>,
52        total: Finite<f64>,
53        can_gc: CanGc,
54    ) -> DomRoot<ProgressEvent> {
55        Self::new_with_proto(
56            global,
57            None,
58            type_,
59            can_bubble,
60            cancelable,
61            length_computable,
62            loaded,
63            total,
64            can_gc,
65        )
66    }
67
68    #[allow(clippy::too_many_arguments)]
69    fn new_with_proto(
70        global: &GlobalScope,
71        proto: Option<HandleObject>,
72        type_: Atom,
73        can_bubble: EventBubbles,
74        cancelable: EventCancelable,
75        length_computable: bool,
76        loaded: Finite<f64>,
77        total: Finite<f64>,
78        can_gc: CanGc,
79    ) -> DomRoot<ProgressEvent> {
80        let ev = reflect_dom_object_with_proto(
81            Box::new(ProgressEvent::new_inherited(
82                length_computable,
83                loaded,
84                total,
85            )),
86            global,
87            proto,
88            can_gc,
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        global: &GlobalScope,
102        proto: Option<HandleObject>,
103        can_gc: CanGc,
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            global,
111            proto,
112            Atom::from(type_),
113            bubbles,
114            cancelable,
115            init.lengthComputable,
116            init.loaded,
117            init.total,
118            can_gc,
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}