/* 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 crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use crate::dom::bindings::codegen::Bindings::StorageEventBinding;
use crate::dom::bindings::codegen::Bindings::StorageEventBinding::StorageEventMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::storage::Storage;
use crate::dom::window::Window;
use dom_struct::dom_struct;
use servo_atoms::Atom;
#[dom_struct]
pub struct StorageEvent {
event: Event,
key: DomRefCell>,
old_value: DomRefCell >,
new_value: DomRefCell >,
url: DomRefCell,
storage_area: MutNullableDom,
}
impl StorageEvent {
pub fn new_inherited(
key: Option,
old_value: Option,
new_value: Option,
url: DOMString,
storage_area: Option<&Storage>,
) -> StorageEvent {
StorageEvent {
event: Event::new_inherited(),
key: DomRefCell::new(key),
old_value: DomRefCell::new(old_value),
new_value: DomRefCell::new(new_value),
url: DomRefCell::new(url),
storage_area: MutNullableDom::new(storage_area),
}
}
pub fn new_uninitialized(window: &Window, url: DOMString) -> DomRoot {
reflect_dom_object(
Box::new(StorageEvent::new_inherited(None, None, None, url, None)),
window,
StorageEventBinding::Wrap,
)
}
pub fn new(
global: &Window,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
key: Option,
oldValue: Option,
newValue: Option,
url: DOMString,
storageArea: Option<&Storage>,
) -> DomRoot {
let ev = reflect_dom_object(
Box::new(StorageEvent::new_inherited(
key,
oldValue,
newValue,
url,
storageArea,
)),
global,
StorageEventBinding::Wrap,
);
{
let event = ev.upcast::();
event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
}
ev
}
pub fn Constructor(
global: &Window,
type_: DOMString,
init: &StorageEventBinding::StorageEventInit,
) -> Fallible> {
let key = init.key.clone();
let oldValue = init.oldValue.clone();
let newValue = init.newValue.clone();
let url = init.url.clone();
let storageArea = init.storageArea.deref();
let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.cancelable);
let event = StorageEvent::new(
global,
Atom::from(type_),
bubbles,
cancelable,
key,
oldValue,
newValue,
url,
storageArea,
);
Ok(event)
}
}
impl StorageEventMethods for StorageEvent {
// https://html.spec.whatwg.org/multipage/#dom-storageevent-key
fn GetKey(&self) -> Option {
self.key.borrow().clone()
}
// https://html.spec.whatwg.org/multipage/#dom-storageevent-oldvalue
fn GetOldValue(&self) -> Option {
self.old_value.borrow().clone()
}
// https://html.spec.whatwg.org/multipage/#dom-storageevent-newvalue
fn GetNewValue(&self) -> Option {
self.new_value.borrow().clone()
}
// https://html.spec.whatwg.org/multipage/#dom-storageevent-url
fn Url(&self) -> DOMString {
self.url.borrow().clone()
}
// https://html.spec.whatwg.org/multipage/#dom-storageevent-storagearea
fn GetStorageArea(&self) -> Option> {
self.storage_area.get()
}
// https://dom.spec.whatwg.org/#dom-event-istrusted
fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
// https://html.spec.whatwg.org/multipage/#dom-storageevent-initstorageevent
fn InitStorageEvent(
&self,
type_: DOMString,
bubbles: bool,
cancelable: bool,
key: Option,
oldValue: Option,
newValue: Option,
url: USVString,
storageArea: Option<&Storage>,
) {
self.event.init_event(
Atom::from(type_),
bool::from(bubbles),
bool::from(cancelable),
);
*self.key.borrow_mut() = key;
*self.old_value.borrow_mut() = oldValue;
*self.new_value.borrow_mut() = newValue;
*self.url.borrow_mut() = DOMString::from_string(url.0);
self.storage_area.set(storageArea);
}
}