script/dom/
extendableevent.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, HandleValue};
7use stylo_atoms::Atom;
8
9use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
10use crate::dom::bindings::codegen::Bindings::ExtendableEventBinding::{
11    ExtendableEventInit, ExtendableEventMethods,
12};
13use crate::dom::bindings::error::{Error, ErrorResult, 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;
19use crate::dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
20use crate::script_runtime::{CanGc, JSContext};
21
22// https://w3c.github.io/ServiceWorker/#extendable-event
23#[dom_struct]
24pub(crate) struct ExtendableEvent {
25    event: Event,
26    extensions_allowed: bool,
27}
28
29impl ExtendableEvent {
30    pub(crate) fn new_inherited() -> ExtendableEvent {
31        ExtendableEvent {
32            event: Event::new_inherited(),
33            extensions_allowed: true,
34        }
35    }
36
37    pub(crate) fn new(
38        worker: &ServiceWorkerGlobalScope,
39        type_: Atom,
40        bubbles: bool,
41        cancelable: bool,
42        can_gc: CanGc,
43    ) -> DomRoot<ExtendableEvent> {
44        Self::new_with_proto(worker, None, type_, bubbles, cancelable, can_gc)
45    }
46
47    fn new_with_proto(
48        worker: &ServiceWorkerGlobalScope,
49        proto: Option<HandleObject>,
50        type_: Atom,
51        bubbles: bool,
52        cancelable: bool,
53        can_gc: CanGc,
54    ) -> DomRoot<ExtendableEvent> {
55        let ev = reflect_dom_object_with_proto(
56            Box::new(ExtendableEvent::new_inherited()),
57            worker,
58            proto,
59            can_gc,
60        );
61        {
62            let event = ev.upcast::<Event>();
63            event.init_event(type_, bubbles, cancelable);
64        }
65        ev
66    }
67}
68
69impl ExtendableEventMethods<crate::DomTypeHolder> for ExtendableEvent {
70    /// <https://w3c.github.io/ServiceWorker/#dom-extendableevent-extendableevent>
71    fn Constructor(
72        worker: &ServiceWorkerGlobalScope,
73        proto: Option<HandleObject>,
74        can_gc: CanGc,
75        type_: DOMString,
76        init: &ExtendableEventInit,
77    ) -> Fallible<DomRoot<ExtendableEvent>> {
78        Ok(ExtendableEvent::new_with_proto(
79            worker,
80            proto,
81            Atom::from(type_),
82            init.parent.bubbles,
83            init.parent.cancelable,
84            can_gc,
85        ))
86    }
87
88    /// <https://w3c.github.io/ServiceWorker/#wait-until-method>
89    fn WaitUntil(&self, _cx: JSContext, _val: HandleValue) -> ErrorResult {
90        // Step 1
91        if !self.extensions_allowed {
92            return Err(Error::InvalidState(None));
93        }
94        // Step 2
95        // TODO add a extended_promises array to enqueue the `val`
96        Ok(())
97    }
98
99    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
100    fn IsTrusted(&self) -> bool {
101        self.event.IsTrusted()
102    }
103}