1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* 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 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 fn new_initialized(
        global: &GlobalScope,
        init: &SecurityPolicyViolationEventInit,
        proto: Option<HandleObject>,
        can_gc: CanGc,
    ) -> DomRoot<SecurityPolicyViolationEvent> {
        reflect_dom_object_with_proto(
            Box::new(SecurityPolicyViolationEvent::new_inherited(init)),
            global,
            proto,
            can_gc,
        )
    }

    fn new_with_proto(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        type_: Atom,
        bubbles: EventBubbles,
        cancelable: EventCancelable,
        init: &SecurityPolicyViolationEventInit,
        can_gc: CanGc,
    ) -> DomRoot<Self> {
        let ev = SecurityPolicyViolationEvent::new_initialized(global, init, proto, can_gc);
        {
            let event = ev.upcast::<Event>();
            event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
        }
        ev
    }

    pub fn new(
        global: &GlobalScope,
        type_: Atom,
        bubbles: EventBubbles,
        cancelable: EventCancelable,
        init: &SecurityPolicyViolationEventInit,
        can_gc: CanGc,
    ) -> DomRoot<Self> {
        Self::new_with_proto(global, None, type_, bubbles, cancelable, init, can_gc)
    }
}

#[allow(non_snake_case)]
impl SecurityPolicyViolationEventMethods for SecurityPolicyViolationEvent {
    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-securitypolicyviolationevent>
    fn Constructor(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
        type_: DOMString,
        init: &SecurityPolicyViolationEventInit,
    ) -> DomRoot<Self> {
        SecurityPolicyViolationEvent::new_with_proto(
            global,
            proto,
            Atom::from(type_),
            EventBubbles::from(init.parent.bubbles),
            EventCancelable::from(init.parent.cancelable),
            init,
            can_gc,
        )
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-documenturi>
    fn DocumentURI(&self) -> USVString {
        self.document_uri.clone()
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-referrer>
    fn Referrer(&self) -> USVString {
        self.referrer.clone()
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-blockeduri>
    fn BlockedURI(&self) -> USVString {
        self.blocked_uri.clone()
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-effectivedirective>
    fn EffectiveDirective(&self) -> DOMString {
        self.effective_directive.clone()
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-violateddirective>
    fn ViolatedDirective(&self) -> DOMString {
        self.violated_directive.clone()
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-originalpolicy>
    fn OriginalPolicy(&self) -> DOMString {
        self.original_policy.clone()
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-sourcefile>
    fn SourceFile(&self) -> USVString {
        self.source_file.clone()
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-sample>
    fn Sample(&self) -> DOMString {
        self.sample.clone()
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-disposition>
    fn Disposition(&self) -> SecurityPolicyViolationEventDisposition {
        self.disposition
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-statuscode>
    fn StatusCode(&self) -> u16 {
        self.status_code
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-linenumber>
    fn LineNumber(&self) -> u32 {
        self.line_number
    }

    /// <https://w3c.github.io/webappsec-csp/#dom-securitypolicyviolationevent-columnnumber>
    fn ColumnNumber(&self) -> u32 {
        self.column_number
    }

    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
    fn IsTrusted(&self) -> bool {
        self.event.IsTrusted()
    }
}