script/dom/
securitypolicyviolationevent.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;
7use stylo_atoms::Atom;
8
9use super::bindings::reflector::reflect_dom_object_with_proto;
10use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
11use crate::dom::bindings::codegen::Bindings::SecurityPolicyViolationEventBinding::{
12    SecurityPolicyViolationEventDisposition, SecurityPolicyViolationEventInit,
13    SecurityPolicyViolationEventMethods,
14};
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::{DOMString, USVString};
18use crate::dom::event::{Event, EventBubbles, EventCancelable, EventComposed};
19use crate::dom::globalscope::GlobalScope;
20use crate::script_runtime::CanGc;
21
22// https://w3c.github.io/webappsec-csp/#securitypolicyviolationevent
23#[dom_struct]
24pub(crate) struct SecurityPolicyViolationEvent {
25    event: Event,
26    document_uri: USVString,
27    referrer: USVString,
28    blocked_uri: USVString,
29    effective_directive: DOMString,
30    violated_directive: DOMString,
31    original_policy: DOMString,
32    source_file: USVString,
33    sample: DOMString,
34    disposition: SecurityPolicyViolationEventDisposition,
35    status_code: u16,
36    line_number: u32,
37    column_number: u32,
38}
39
40impl SecurityPolicyViolationEvent {
41    fn new_inherited(init: &SecurityPolicyViolationEventInit) -> SecurityPolicyViolationEvent {
42        SecurityPolicyViolationEvent {
43            event: Event::new_inherited(),
44            document_uri: init.documentURI.clone(),
45            referrer: init.referrer.clone(),
46            blocked_uri: init.blockedURI.clone(),
47            effective_directive: init.effectiveDirective.clone(),
48            violated_directive: init.violatedDirective.clone(),
49            original_policy: init.originalPolicy.clone(),
50            source_file: init.sourceFile.clone(),
51            sample: init.sample.clone(),
52            disposition: init.disposition,
53            status_code: init.statusCode,
54            line_number: init.lineNumber,
55            column_number: init.columnNumber,
56        }
57    }
58
59    pub(crate) fn new_initialized(
60        global: &GlobalScope,
61        init: &SecurityPolicyViolationEventInit,
62        proto: Option<HandleObject>,
63        can_gc: CanGc,
64    ) -> DomRoot<SecurityPolicyViolationEvent> {
65        reflect_dom_object_with_proto(
66            Box::new(SecurityPolicyViolationEvent::new_inherited(init)),
67            global,
68            proto,
69            can_gc,
70        )
71    }
72
73    #[allow(clippy::too_many_arguments)]
74    fn new_with_proto(
75        global: &GlobalScope,
76        proto: Option<HandleObject>,
77        type_: Atom,
78        bubbles: EventBubbles,
79        cancelable: EventCancelable,
80        composed: EventComposed,
81        init: &SecurityPolicyViolationEventInit,
82        can_gc: CanGc,
83    ) -> DomRoot<Self> {
84        let ev = SecurityPolicyViolationEvent::new_initialized(global, init, proto, can_gc);
85        {
86            let event = ev.upcast::<Event>();
87            event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
88            event.set_composed(bool::from(composed));
89        }
90        ev
91    }
92
93    pub(crate) fn new(
94        global: &GlobalScope,
95        type_: Atom,
96        bubbles: EventBubbles,
97        cancelable: EventCancelable,
98        composed: EventComposed,
99        init: &SecurityPolicyViolationEventInit,
100        can_gc: CanGc,
101    ) -> DomRoot<Self> {
102        Self::new_with_proto(
103            global, None, type_, bubbles, cancelable, composed, init, can_gc,
104        )
105    }
106}
107
108#[allow(non_snake_case)]
109impl SecurityPolicyViolationEventMethods<crate::DomTypeHolder> for SecurityPolicyViolationEvent {
110    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-securitypolicyviolationevent>
111    fn Constructor(
112        global: &GlobalScope,
113        proto: Option<HandleObject>,
114        can_gc: CanGc,
115        type_: DOMString,
116        init: &SecurityPolicyViolationEventInit,
117    ) -> DomRoot<Self> {
118        SecurityPolicyViolationEvent::new_with_proto(
119            global,
120            proto,
121            Atom::from(type_),
122            EventBubbles::from(init.parent.bubbles),
123            EventCancelable::from(init.parent.cancelable),
124            EventComposed::from(init.parent.composed),
125            init,
126            can_gc,
127        )
128    }
129
130    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-documenturi>
131    fn DocumentURI(&self) -> USVString {
132        self.document_uri.clone()
133    }
134
135    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-referrer>
136    fn Referrer(&self) -> USVString {
137        self.referrer.clone()
138    }
139
140    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-blockeduri>
141    fn BlockedURI(&self) -> USVString {
142        self.blocked_uri.clone()
143    }
144
145    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-effectivedirective>
146    fn EffectiveDirective(&self) -> DOMString {
147        self.effective_directive.clone()
148    }
149
150    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-violateddirective>
151    fn ViolatedDirective(&self) -> DOMString {
152        self.violated_directive.clone()
153    }
154
155    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-originalpolicy>
156    fn OriginalPolicy(&self) -> DOMString {
157        self.original_policy.clone()
158    }
159
160    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-sourcefile>
161    fn SourceFile(&self) -> USVString {
162        self.source_file.clone()
163    }
164
165    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-sample>
166    fn Sample(&self) -> DOMString {
167        self.sample.clone()
168    }
169
170    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-disposition>
171    fn Disposition(&self) -> SecurityPolicyViolationEventDisposition {
172        self.disposition
173    }
174
175    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-statuscode>
176    fn StatusCode(&self) -> u16 {
177        self.status_code
178    }
179
180    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-linenumber>
181    fn LineNumber(&self) -> u32 {
182        self.line_number
183    }
184
185    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-columnnumber>
186    fn ColumnNumber(&self) -> u32 {
187        self.column_number
188    }
189
190    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
191    fn IsTrusted(&self) -> bool {
192        self.event.IsTrusted()
193    }
194}