|
| 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 | + |
| 5 | +use std::rc::Rc; |
| 6 | + |
| 7 | +use dom_struct::dom_struct; |
| 8 | +use js::conversions::ToJSValConvertible; |
| 9 | +use js::jsapi::{ExceptionStackBehavior, Heap, JS_IsExceptionPending}; |
| 10 | +use js::jsval::{JSVal, UndefinedValue}; |
| 11 | +use js::rust::wrappers::JS_SetPendingException; |
| 12 | +use js::rust::HandleValue; |
| 13 | + |
| 14 | +use crate::dom::bindings::cell::DomRefCell; |
| 15 | +use crate::dom::bindings::codegen::Bindings::AbortSignalBinding::AbortSignalMethods; |
| 16 | +use crate::dom::bindings::codegen::Bindings::EventListenerBinding::EventListener; |
| 17 | +use crate::dom::bindings::codegen::Bindings::EventTargetBinding::EventListenerOptions; |
| 18 | +use crate::dom::bindings::inheritance::Castable; |
| 19 | +use crate::dom::bindings::reflector::{reflect_dom_object, DomObject}; |
| 20 | +use crate::dom::bindings::root::DomRoot; |
| 21 | +use crate::dom::bindings::str::DOMString; |
| 22 | +use crate::dom::domexception::DOMErrorName; |
| 23 | +use crate::dom::event::{Event, EventBubbles, EventCancelable}; |
| 24 | +use crate::dom::eventtarget::EventTarget; |
| 25 | +use crate::dom::globalscope::GlobalScope; |
| 26 | +use crate::dom::types::DOMException; |
| 27 | +use crate::script_runtime::JSContext; |
| 28 | + |
| 29 | +#[derive(JSTraceable, MallocSizeOf)] |
| 30 | +pub enum AbortAlgorithm { |
| 31 | + RemoveEventListener( |
| 32 | + DomRoot<EventTarget>, |
| 33 | + DOMString, |
| 34 | + #[ignore_malloc_size_of = "Rc"] Rc<EventListener>, |
| 35 | + #[ignore_malloc_size_of = "generated"] EventListenerOptions, |
| 36 | + ), |
| 37 | +} |
| 38 | +impl AbortAlgorithm { |
| 39 | + fn exec(self) { |
| 40 | + match self { |
| 41 | + Self::RemoveEventListener(target, ty, listener, options) => { |
| 42 | + target.remove_event_listener(ty, Some(listener), options) |
| 43 | + }, |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[dom_struct] |
| 49 | +pub struct AbortSignal { |
| 50 | + event_target: EventTarget, |
| 51 | + #[ignore_malloc_size_of = "Defined in rust-mozjs"] |
| 52 | + reason: Heap<JSVal>, |
| 53 | + abort_algorithms: DomRefCell<Vec<AbortAlgorithm>>, |
| 54 | +} |
| 55 | + |
| 56 | +impl AbortSignal { |
| 57 | + pub fn new_inherited() -> Self { |
| 58 | + Self { |
| 59 | + event_target: EventTarget::new_inherited(), |
| 60 | + reason: Heap::default(), |
| 61 | + abort_algorithms: DomRefCell::default(), |
| 62 | + } |
| 63 | + } |
| 64 | + pub fn new(global: &GlobalScope) -> DomRoot<Self> { |
| 65 | + reflect_dom_object(Box::new(Self::new_inherited()), global) |
| 66 | + } |
| 67 | + /// <https://dom.spec.whatwg.org/#abortsignal-add> |
| 68 | + pub fn add_abort_algorithm(&self, alg: AbortAlgorithm) { |
| 69 | + if !self.Aborted() { |
| 70 | + self.abort_algorithms.borrow_mut().push(alg); |
| 71 | + } |
| 72 | + } |
| 73 | + /// <https://dom.spec.whatwg.org/#abortsignal-signal-abort> |
| 74 | + #[allow(unsafe_code)] |
| 75 | + pub fn signal_abort(&self, reason: HandleValue) { |
| 76 | + // 1. If signal is aborted, then return. |
| 77 | + if self.Aborted() { |
| 78 | + return; |
| 79 | + } |
| 80 | + // 2. Set signal’s abort reason to reason if it is given; otherwise to a new "AbortError" DOMException. |
| 81 | + let cx = *GlobalScope::get_cx(); |
| 82 | + rooted!(in(cx) let mut new_reason = UndefinedValue()); |
| 83 | + let reason = if reason.is_undefined() { |
| 84 | + let exception = DOMException::new(&self.global(), DOMErrorName::AbortError); |
| 85 | + unsafe { |
| 86 | + exception.to_jsval(cx, new_reason.handle_mut()); |
| 87 | + }; |
| 88 | + new_reason.handle() |
| 89 | + } else { |
| 90 | + reason |
| 91 | + }; |
| 92 | + self.reason.set(reason.get()); |
| 93 | + |
| 94 | + // 3. For each algorithm of signal’s abort algorithms: run algorithm. |
| 95 | + // 4. Empty signal’s abort algorithms. |
| 96 | + for algorithm in self.abort_algorithms.borrow_mut().drain(..) { |
| 97 | + algorithm.exec(); |
| 98 | + } |
| 99 | + |
| 100 | + // 5. Fire an event named abort at signal. |
| 101 | + let event = Event::new( |
| 102 | + &self.global(), |
| 103 | + atom!("abort"), |
| 104 | + EventBubbles::DoesNotBubble, |
| 105 | + EventCancelable::Cancelable, |
| 106 | + ); |
| 107 | + event.fire(self.upcast()); |
| 108 | + // 6. For each dependentSignal of signal’s dependent signals, |
| 109 | + // signal abort on dependentSignal with signal’s abort reason. |
| 110 | + // TODO |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl AbortSignalMethods for AbortSignal { |
| 115 | + // https://dom.spec.whatwg.org/#dom-abortsignal-onabort |
| 116 | + event_handler!(Abort, GetOnabort, SetOnabort); |
| 117 | + /// <https://dom.spec.whatwg.org/#dom-abortsignal-aborted> |
| 118 | + fn Aborted(&self) -> bool { |
| 119 | + !self.reason.get().is_undefined() |
| 120 | + } |
| 121 | + /// <https://dom.spec.whatwg.org/#dom-abortsignal-reason> |
| 122 | + fn Reason(&self, _cx: JSContext) -> JSVal { |
| 123 | + self.reason.get() |
| 124 | + } |
| 125 | + #[allow(unsafe_code)] |
| 126 | + /// <https://dom.spec.whatwg.org/#dom-abortsignal-throwifaborted> |
| 127 | + fn ThrowIfAborted(&self) { |
| 128 | + let reason = self.reason.get(); |
| 129 | + if !reason.is_undefined() { |
| 130 | + let cx = *GlobalScope::get_cx(); |
| 131 | + unsafe { |
| 132 | + assert!(!JS_IsExceptionPending(cx)); |
| 133 | + rooted!(in(cx) let mut thrown = UndefinedValue()); |
| 134 | + reason.to_jsval(cx, thrown.handle_mut()); |
| 135 | + JS_SetPendingException(cx, thrown.handle(), ExceptionStackBehavior::Capture); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments