Skip to main content

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::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::SecurityPolicyViolationEventBinding::{
13    SecurityPolicyViolationEventDisposition, SecurityPolicyViolationEventInit,
14    SecurityPolicyViolationEventMethods,
15};
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::{DOMString, USVString};
19use crate::dom::event::{Event, EventBubbles, EventCancelable, EventComposed};
20use crate::dom::globalscope::GlobalScope;
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        cx: &mut JSContext,
61        global: &GlobalScope,
62        init: &SecurityPolicyViolationEventInit,
63        proto: Option<HandleObject>,
64    ) -> DomRoot<SecurityPolicyViolationEvent> {
65        reflect_dom_object_with_proto_and_cx(
66            Box::new(SecurityPolicyViolationEvent::new_inherited(init)),
67            global,
68            proto,
69            cx,
70        )
71    }
72
73    #[allow(clippy::too_many_arguments)]
74    fn new_with_proto(
75        cx: &mut JSContext,
76        global: &GlobalScope,
77        proto: Option<HandleObject>,
78        type_: Atom,
79        bubbles: EventBubbles,
80        cancelable: EventCancelable,
81        composed: EventComposed,
82        init: &SecurityPolicyViolationEventInit,
83    ) -> DomRoot<Self> {
84        let ev = SecurityPolicyViolationEvent::new_initialized(cx, global, init, proto);
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        cx: &mut JSContext,
95        global: &GlobalScope,
96        type_: Atom,
97        bubbles: EventBubbles,
98        cancelable: EventCancelable,
99        composed: EventComposed,
100        init: &SecurityPolicyViolationEventInit,
101    ) -> DomRoot<Self> {
102        Self::new_with_proto(cx, global, None, type_, bubbles, cancelable, composed, init)
103    }
104}
105
106impl SecurityPolicyViolationEventMethods<crate::DomTypeHolder> for SecurityPolicyViolationEvent {
107    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-securitypolicyviolationevent>
108    fn Constructor(
109        cx: &mut JSContext,
110        global: &GlobalScope,
111        proto: Option<HandleObject>,
112        type_: DOMString,
113        init: &SecurityPolicyViolationEventInit,
114    ) -> DomRoot<Self> {
115        SecurityPolicyViolationEvent::new_with_proto(
116            cx,
117            global,
118            proto,
119            Atom::from(type_),
120            EventBubbles::from(init.parent.bubbles),
121            EventCancelable::from(init.parent.cancelable),
122            EventComposed::from(init.parent.composed),
123            init,
124        )
125    }
126
127    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-documenturi>
128    fn DocumentURI(&self) -> USVString {
129        self.document_uri.clone()
130    }
131
132    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-referrer>
133    fn Referrer(&self) -> USVString {
134        self.referrer.clone()
135    }
136
137    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-blockeduri>
138    fn BlockedURI(&self) -> USVString {
139        self.blocked_uri.clone()
140    }
141
142    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-effectivedirective>
143    fn EffectiveDirective(&self) -> DOMString {
144        self.effective_directive.clone()
145    }
146
147    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-violateddirective>
148    fn ViolatedDirective(&self) -> DOMString {
149        self.violated_directive.clone()
150    }
151
152    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-originalpolicy>
153    fn OriginalPolicy(&self) -> DOMString {
154        self.original_policy.clone()
155    }
156
157    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-sourcefile>
158    fn SourceFile(&self) -> USVString {
159        self.source_file.clone()
160    }
161
162    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-sample>
163    fn Sample(&self) -> DOMString {
164        self.sample.clone()
165    }
166
167    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-disposition>
168    fn Disposition(&self) -> SecurityPolicyViolationEventDisposition {
169        self.disposition
170    }
171
172    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-statuscode>
173    fn StatusCode(&self) -> u16 {
174        self.status_code
175    }
176
177    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-linenumber>
178    fn LineNumber(&self) -> u32 {
179        self.line_number
180    }
181
182    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-columnnumber>
183    fn ColumnNumber(&self) -> u32 {
184        self.column_number
185    }
186
187    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
188    fn IsTrusted(&self) -> bool {
189        self.event.IsTrusted()
190    }
191}