/* 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 dom_struct::dom_struct; use js::rust::HandleObject; use servo_atoms::Atom; use super::bindings::reflector::reflect_dom_object_with_proto; use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods; use crate::dom::bindings::codegen::Bindings::SecurityPolicyViolationEventBinding::{ SecurityPolicyViolationEventDisposition, SecurityPolicyViolationEventInit, SecurityPolicyViolationEventMethods, }; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::str::{DOMString, USVString}; use crate::dom::event::{Event, EventBubbles, EventCancelable}; use crate::dom::globalscope::GlobalScope; use crate::script_runtime::CanGc; // https://w3c.github.io/webappsec-csp/#securitypolicyviolationevent #[dom_struct] pub(crate) struct SecurityPolicyViolationEvent { event: Event, document_uri: USVString, referrer: USVString, blocked_uri: USVString, effective_directive: DOMString, violated_directive: DOMString, original_policy: DOMString, source_file: USVString, sample: DOMString, disposition: SecurityPolicyViolationEventDisposition, status_code: u16, line_number: u32, column_number: u32, } impl SecurityPolicyViolationEvent { fn new_inherited(init: &SecurityPolicyViolationEventInit) -> SecurityPolicyViolationEvent { SecurityPolicyViolationEvent { event: Event::new_inherited(), document_uri: init.documentURI.clone(), referrer: init.referrer.clone(), blocked_uri: init.blockedURI.clone(), effective_directive: init.effectiveDirective.clone(), violated_directive: init.violatedDirective.clone(), original_policy: init.originalPolicy.clone(), source_file: init.sourceFile.clone(), sample: init.sample.clone(), disposition: init.disposition, status_code: init.statusCode, line_number: init.lineNumber, column_number: init.columnNumber, } } pub(crate) fn new_initialized( global: &GlobalScope, init: &SecurityPolicyViolationEventInit, proto: Option, can_gc: CanGc, ) -> DomRoot { reflect_dom_object_with_proto( Box::new(SecurityPolicyViolationEvent::new_inherited(init)), global, proto, can_gc, ) } fn new_with_proto( global: &GlobalScope, proto: Option, type_: Atom, bubbles: EventBubbles, cancelable: EventCancelable, init: &SecurityPolicyViolationEventInit, can_gc: CanGc, ) -> DomRoot { let ev = SecurityPolicyViolationEvent::new_initialized(global, init, proto, can_gc); { let event = ev.upcast::(); event.init_event(type_, bool::from(bubbles), bool::from(cancelable)); } ev } pub(crate) fn new( global: &GlobalScope, type_: Atom, bubbles: EventBubbles, cancelable: EventCancelable, init: &SecurityPolicyViolationEventInit, can_gc: CanGc, ) -> DomRoot { Self::new_with_proto(global, None, type_, bubbles, cancelable, init, can_gc) } } #[allow(non_snake_case)] impl SecurityPolicyViolationEventMethods for SecurityPolicyViolationEvent { /// fn Constructor( global: &GlobalScope, proto: Option, can_gc: CanGc, type_: DOMString, init: &SecurityPolicyViolationEventInit, ) -> DomRoot { SecurityPolicyViolationEvent::new_with_proto( global, proto, Atom::from(type_), EventBubbles::from(init.parent.bubbles), EventCancelable::from(init.parent.cancelable), init, can_gc, ) } /// fn DocumentURI(&self) -> USVString { self.document_uri.clone() } /// fn Referrer(&self) -> USVString { self.referrer.clone() } /// fn BlockedURI(&self) -> USVString { self.blocked_uri.clone() } /// fn EffectiveDirective(&self) -> DOMString { self.effective_directive.clone() } /// fn ViolatedDirective(&self) -> DOMString { self.violated_directive.clone() } /// fn OriginalPolicy(&self) -> DOMString { self.original_policy.clone() } /// fn SourceFile(&self) -> USVString { self.source_file.clone() } /// fn Sample(&self) -> DOMString { self.sample.clone() } /// fn Disposition(&self) -> SecurityPolicyViolationEventDisposition { self.disposition } /// fn StatusCode(&self) -> u16 { self.status_code } /// fn LineNumber(&self) -> u32 { self.line_number } /// fn ColumnNumber(&self) -> u32 { self.column_number } /// fn IsTrusted(&self) -> bool { self.event.IsTrusted() } }