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