servo/components/script/dom/idbversionchangeevent.rs
Ashwin Naren 0a79918849
Add initial IndexedDB Support
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>

Co-authored-by: Rasmus Viitanen <rasviitanen@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
2025-05-23 13:13:19 -07:00

104 lines
3.1 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use servo_atoms::Atom;
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use crate::dom::bindings::codegen::Bindings::IDBVersionChangeEventBinding::{
IDBVersionChangeEventInit, IDBVersionChangeEventMethods,
};
use crate::dom::bindings::import::module::HandleObject;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{
reflect_dom_object, reflect_dom_object_with_proto, DomObject,
};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::globalscope::GlobalScope;
use crate::dom::window::Window;
#[dom_struct]
pub struct IDBVersionChangeEvent {
event: Event,
old_version: u64,
new_version: Option<u64>,
}
impl IDBVersionChangeEvent {
pub fn new_inherited(old_version: u64, new_version: Option<u64>) -> IDBVersionChangeEvent {
IDBVersionChangeEvent {
event: Event::new_inherited(),
old_version: old_version,
new_version: new_version,
}
}
pub fn new(
global: &GlobalScope,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
old_version: u64,
new_version: Option<u64>,
) -> DomRoot<IDBVersionChangeEvent> {
let ev = reflect_dom_object(
Box::new(IDBVersionChangeEvent::new_inherited(
old_version,
new_version,
)),
global,
);
{
let event = ev.upcast::<Event>();
event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
}
ev
}
fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
type_: Atom,
init: &IDBVersionChangeEventInit,
) -> DomRoot<Self> {
let ev = reflect_dom_object_with_proto(
Box::new(Self::new_inherited(init.oldVersion, init.newVersion)),
global,
proto,
);
{
let event = ev.upcast::<Event>();
event.init_event(type_, init.parent.bubbles, init.parent.cancelable);
}
ev
}
pub fn Constructor(
window: &Window,
proto: Option<HandleObject>,
type_: DOMString,
init: &IDBVersionChangeEventInit,
) -> DomRoot<Self> {
Self::new_with_proto(&window.global(), proto, Atom::from(type_), init)
}
}
impl IDBVersionChangeEventMethods for IDBVersionChangeEvent {
// https://www.w3.org/TR/IndexedDB-2/#dom-idbversionchangeevent-oldversion
fn OldVersion(&self) -> u64 {
self.old_version
}
// https://www.w3.org/TR/IndexedDB-2/#dom-idbversionchangeevent-newversion
fn GetNewVersion(&self) -> Option<u64> {
self.new_version
}
// https://dom.spec.whatwg.org/#dom-event-istrusted
fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
}