script/dom/security/
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 crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
10use crate::dom::bindings::codegen::Bindings::SecurityPolicyViolationEventBinding::{
11    SecurityPolicyViolationEventDisposition, SecurityPolicyViolationEventInit,
12    SecurityPolicyViolationEventMethods,
13};
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
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
108impl SecurityPolicyViolationEventMethods<crate::DomTypeHolder> for SecurityPolicyViolationEvent {
109    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-securitypolicyviolationevent>
110    fn Constructor(
111        global: &GlobalScope,
112        proto: Option<HandleObject>,
113        can_gc: CanGc,
114        type_: DOMString,
115        init: &SecurityPolicyViolationEventInit,
116    ) -> DomRoot<Self> {
117        SecurityPolicyViolationEvent::new_with_proto(
118            global,
119            proto,
120            Atom::from(type_),
121            EventBubbles::from(init.parent.bubbles),
122            EventCancelable::from(init.parent.cancelable),
123            EventComposed::from(init.parent.composed),
124            init,
125            can_gc,
126        )
127    }
128
129    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-documenturi>
130    fn DocumentURI(&self) -> USVString {
131        self.document_uri.clone()
132    }
133
134    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-referrer>
135    fn Referrer(&self) -> USVString {
136        self.referrer.clone()
137    }
138
139    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-blockeduri>
140    fn BlockedURI(&self) -> USVString {
141        self.blocked_uri.clone()
142    }
143
144    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-effectivedirective>
145    fn EffectiveDirective(&self) -> DOMString {
146        self.effective_directive.clone()
147    }
148
149    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-violateddirective>
150    fn ViolatedDirective(&self) -> DOMString {
151        self.violated_directive.clone()
152    }
153
154    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-originalpolicy>
155    fn OriginalPolicy(&self) -> DOMString {
156        self.original_policy.clone()
157    }
158
159    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-sourcefile>
160    fn SourceFile(&self) -> USVString {
161        self.source_file.clone()
162    }
163
164    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-sample>
165    fn Sample(&self) -> DOMString {
166        self.sample.clone()
167    }
168
169    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-disposition>
170    fn Disposition(&self) -> SecurityPolicyViolationEventDisposition {
171        self.disposition
172    }
173
174    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-statuscode>
175    fn StatusCode(&self) -> u16 {
176        self.status_code
177    }
178
179    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-linenumber>
180    fn LineNumber(&self) -> u32 {
181        self.line_number
182    }
183
184    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-columnnumber>
185    fn ColumnNumber(&self) -> u32 {
186        self.column_number
187    }
188
189    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
190    fn IsTrusted(&self) -> bool {
191        self.event.IsTrusted()
192    }
193}