script/dom/
idbversionchangeevent.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 stylo_atoms::Atom;
7
8use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
9use crate::dom::bindings::codegen::Bindings::IDBVersionChangeEventBinding::{
10    IDBVersionChangeEventInit, IDBVersionChangeEventMethods,
11};
12use crate::dom::bindings::import::module::HandleObject;
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::event::{Event, EventBubbles, EventCancelable};
18use crate::dom::globalscope::GlobalScope;
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct IDBVersionChangeEvent {
23    event: Event,
24    old_version: u64,
25    new_version: Option<u64>,
26}
27
28impl IDBVersionChangeEvent {
29    pub fn new_inherited(old_version: u64, new_version: Option<u64>) -> IDBVersionChangeEvent {
30        IDBVersionChangeEvent {
31            event: Event::new_inherited(),
32            old_version,
33            new_version,
34        }
35    }
36
37    pub fn new(
38        global: &GlobalScope,
39        type_: Atom,
40        bubbles: EventBubbles,
41        cancelable: EventCancelable,
42        old_version: u64,
43        new_version: Option<u64>,
44        can_gc: CanGc,
45    ) -> DomRoot<IDBVersionChangeEvent> {
46        Self::new_with_proto(
47            global,
48            None,
49            type_,
50            bool::from(bubbles),
51            bool::from(cancelable),
52            old_version,
53            new_version,
54            can_gc,
55        )
56    }
57
58    #[allow(clippy::too_many_arguments)]
59    fn new_with_proto(
60        global: &GlobalScope,
61        proto: Option<HandleObject>,
62        type_: Atom,
63        bubbles: bool,
64        cancelable: bool,
65        old_version: u64,
66        new_version: Option<u64>,
67        can_gc: CanGc,
68    ) -> DomRoot<Self> {
69        let ev = reflect_dom_object_with_proto(
70            Box::new(IDBVersionChangeEvent::new_inherited(
71                old_version,
72                new_version,
73            )),
74            global,
75            proto,
76            can_gc,
77        );
78        {
79            let event = ev.upcast::<Event>();
80            event.init_event(type_, bubbles, cancelable);
81        }
82        ev
83    }
84}
85
86impl IDBVersionChangeEventMethods<crate::DomTypeHolder> for IDBVersionChangeEvent {
87    /// <https://w3c.github.io/IndexedDB/#dom-idbversionchangeevent-idbversionchangeevent>
88    fn Constructor(
89        global: &GlobalScope,
90        proto: Option<HandleObject>,
91        can_gc: CanGc,
92        type_: DOMString,
93        init: &IDBVersionChangeEventInit,
94    ) -> DomRoot<Self> {
95        Self::new_with_proto(
96            global,
97            proto,
98            Atom::from(type_),
99            init.parent.bubbles,
100            init.parent.cancelable,
101            init.oldVersion,
102            init.newVersion,
103            can_gc,
104        )
105    }
106
107    // https://www.w3.org/TR/IndexedDB-2/#dom-idbversionchangeevent-oldversion
108    fn OldVersion(&self) -> u64 {
109        self.old_version
110    }
111
112    // https://www.w3.org/TR/IndexedDB-2/#dom-idbversionchangeevent-newversion
113    fn GetNewVersion(&self) -> Option<u64> {
114        self.new_version
115    }
116
117    // https://dom.spec.whatwg.org/#dom-event-istrusted
118    fn IsTrusted(&self) -> bool {
119        self.event.IsTrusted()
120    }
121}