|
| 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::ptr::NonNull; |
| 6 | + |
| 7 | +use dom_struct::dom_struct; |
| 8 | +use js::jsapi::{JSObject, Value}; |
| 9 | +use script_bindings::conversions::SafeToJSValConvertible; |
| 10 | +use script_bindings::reflector::DomObject; |
| 11 | + |
| 12 | +use crate::dom::bindings::codegen::Bindings::DebuggerEventBinding::DebuggerEventMethods; |
| 13 | +use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods; |
| 14 | +use crate::dom::bindings::reflector::reflect_dom_object; |
| 15 | +use crate::dom::bindings::root::{Dom, DomRoot}; |
| 16 | +use crate::dom::event::Event; |
| 17 | +use crate::dom::types::GlobalScope; |
| 18 | +use crate::script_runtime::CanGc; |
| 19 | + |
| 20 | +#[dom_struct] |
| 21 | +/// Event for Rust → JS calls in [`crate::dom::DebuggerGlobalScope`]. |
| 22 | +pub(crate) struct DebuggerEvent { |
| 23 | + event: Event, |
| 24 | + global: Dom<GlobalScope>, |
| 25 | +} |
| 26 | + |
| 27 | +impl DebuggerEvent { |
| 28 | + pub(crate) fn new( |
| 29 | + debugger_global: &GlobalScope, |
| 30 | + global: &GlobalScope, |
| 31 | + can_gc: CanGc, |
| 32 | + ) -> DomRoot<Self> { |
| 33 | + let result = Box::new(Self { |
| 34 | + event: Event::new_inherited(), |
| 35 | + global: Dom::from_ref(global), |
| 36 | + }); |
| 37 | + let result = reflect_dom_object(result, debugger_global, can_gc); |
| 38 | + result.event.init_event("addDebuggee".into(), false, false); |
| 39 | + |
| 40 | + result |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl DebuggerEventMethods<crate::DomTypeHolder> for DebuggerEvent { |
| 45 | + // check-tidy: no specs after this line |
| 46 | + fn Global(&self, cx: script_bindings::script_runtime::JSContext) -> NonNull<JSObject> { |
| 47 | + // Convert the debuggee global’s reflector to a Value, wrapping it from its originating realm (debuggee realm) |
| 48 | + // into the active realm (debugger realm) so that it can be passed across compartments. |
| 49 | + rooted!(in(*cx) let mut result: Value); |
| 50 | + self.global |
| 51 | + .reflector() |
| 52 | + .safe_to_jsval(cx, result.handle_mut()); |
| 53 | + NonNull::new(result.to_object()).unwrap() |
| 54 | + } |
| 55 | + |
| 56 | + fn IsTrusted(&self) -> bool { |
| 57 | + self.event.IsTrusted() |
| 58 | + } |
| 59 | +} |
0 commit comments