Skip to main content

script/dom/serviceworker/
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::context::JSContext;
7use js::rust::{HandleObject, HandleValue};
8use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::ExtendableEventBinding::{
13    ExtendableEventInit, ExtendableEventMethods,
14};
15use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::event::Event;
20use crate::dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
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        cx: &mut JSContext,
39        worker: &ServiceWorkerGlobalScope,
40        type_: Atom,
41        bubbles: bool,
42        cancelable: bool,
43    ) -> DomRoot<ExtendableEvent> {
44        Self::new_with_proto(cx, worker, None, type_, bubbles, cancelable)
45    }
46
47    fn new_with_proto(
48        cx: &mut JSContext,
49        worker: &ServiceWorkerGlobalScope,
50        proto: Option<HandleObject>,
51        type_: Atom,
52        bubbles: bool,
53        cancelable: bool,
54    ) -> DomRoot<ExtendableEvent> {
55        let ev = reflect_dom_object_with_proto_and_cx(
56            Box::new(ExtendableEvent::new_inherited()),
57            worker,
58            proto,
59            cx,
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        cx: &mut JSContext,
73        worker: &ServiceWorkerGlobalScope,
74        proto: Option<HandleObject>,
75        type_: DOMString,
76        init: &ExtendableEventInit,
77    ) -> Fallible<DomRoot<ExtendableEvent>> {
78        Ok(ExtendableEvent::new_with_proto(
79            cx,
80            worker,
81            proto,
82            Atom::from(type_),
83            init.parent.bubbles,
84            init.parent.cancelable,
85        ))
86    }
87
88    /// <https://w3c.github.io/ServiceWorker/#wait-until-method>
89    fn WaitUntil(&self, _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}