View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.event;
16  
17  import org.htmlunit.corejs.javascript.Scriptable;
18  import org.htmlunit.corejs.javascript.ScriptableObject;
19  import org.htmlunit.html.DomNode;
20  import org.htmlunit.javascript.JavaScriptEngine;
21  import org.htmlunit.javascript.configuration.JsxClass;
22  import org.htmlunit.javascript.configuration.JsxConstructor;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  
25  /**
26   * A JavaScript object for {@code SecurityPolicyViolationEvent}.
27   *
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   */
31  @JsxClass
32  public class SecurityPolicyViolationEvent extends Event {
33  
34      /** The event type. */
35      public static final String TYPE_SECURITY_POLICY_VIOLATION = "securitypolicyviolation";
36  
37      /** Disposition of enforced policies. */
38      public static final String DISPOSITION_ENFORCE = "enforce";
39  
40      /** Disposition of report only policies. */
41      public static final String DISPOSITION_REPORT = "report";
42  
43      private String documentURI_ = "";
44      private String referrer_ = "";
45      private String blockedURI_ = "";
46      private String effectiveDirective_ = "";
47      private String violatedDirective_ = "";
48      private String originalPolicy_ = "";
49      private String sourceFile_ = "";
50      private String sample_ = "";
51      private String disposition_ = DISPOSITION_ENFORCE;
52      private boolean composed_;
53      private int statusCode_;
54      private int lineNumber_;
55      private int columnNumber_;
56  
57      /**
58       * Default constructor used to build the prototype.
59       */
60      public SecurityPolicyViolationEvent() {
61          super();
62      }
63  
64      /**
65       * Creates a new securitypolicyviolation event (bubbling, not cancelable) for the given target.
66       *
67       * @param target the DOM node the event is fired at
68       */
69      public SecurityPolicyViolationEvent(final DomNode target) {
70          super(target, TYPE_SECURITY_POLICY_VIOLATION);
71          setBubbles(true);
72          setCancelable(false);
73          composed_ = true;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      @JsxConstructor
81      public void jsConstructor(final String type, final ScriptableObject details) {
82          super.jsConstructor(type, details);
83  
84          if (details != null && !JavaScriptEngine.isUndefined(details)) {
85              composed_ = JavaScriptEngine.toBoolean(details.get("composed"));
86  
87              final Object documentURI = ScriptableObject.getProperty(details, "documentURI");
88              if (isSet(documentURI)) {
89                  documentURI_ = JavaScriptEngine.toString(documentURI);
90              }
91              final Object referrer = ScriptableObject.getProperty(details, "referrer");
92              if (isSet(referrer)) {
93                  referrer_ = JavaScriptEngine.toString(referrer);
94              }
95              final Object blockedURI = ScriptableObject.getProperty(details, "blockedURI");
96              if (isSet(blockedURI)) {
97                  blockedURI_ = JavaScriptEngine.toString(blockedURI);
98              }
99              final Object effectiveDirective = ScriptableObject.getProperty(details, "effectiveDirective");
100             if (isSet(effectiveDirective)) {
101                 effectiveDirective_ = JavaScriptEngine.toString(effectiveDirective);
102             }
103             final Object violatedDirective = ScriptableObject.getProperty(details, "violatedDirective");
104             if (isSet(violatedDirective)) {
105                 violatedDirective_ = JavaScriptEngine.toString(violatedDirective);
106             }
107             final Object originalPolicy = ScriptableObject.getProperty(details, "originalPolicy");
108             if (isSet(originalPolicy)) {
109                 originalPolicy_ = JavaScriptEngine.toString(originalPolicy);
110             }
111             final Object sourceFile = ScriptableObject.getProperty(details, "sourceFile");
112             if (isSet(sourceFile)) {
113                 sourceFile_ = JavaScriptEngine.toString(sourceFile);
114             }
115             final Object sample = ScriptableObject.getProperty(details, "sample");
116             if (isSet(sample)) {
117                 sample_ = JavaScriptEngine.toString(sample);
118             }
119             final Object disposition = ScriptableObject.getProperty(details, "disposition");
120             if (isSet(disposition)) {
121                 final String value = JavaScriptEngine.toString(disposition);
122                 if (!DISPOSITION_ENFORCE.equals(value) && !DISPOSITION_REPORT.equals(value)) {
123                     throw JavaScriptEngine.typeError("Failed to construct 'SecurityPolicyViolationEvent': "
124                             + "The provided value '" + value
125                             + "' is not a valid enum value of type SecurityPolicyViolationEventDisposition.");
126                 }
127                 disposition_ = value;
128             }
129             final Object statusCode = ScriptableObject.getProperty(details, "statusCode");
130             if (isSet(statusCode)) {
131                 statusCode_ = JavaScriptEngine.toInt32(statusCode);
132             }
133             final Object lineNumber = ScriptableObject.getProperty(details, "lineNumber");
134             if (isSet(lineNumber)) {
135                 lineNumber_ = JavaScriptEngine.toInt32(lineNumber);
136             }
137             final Object columnNumber = ScriptableObject.getProperty(details, "columnNumber");
138             if (isSet(columnNumber)) {
139                 columnNumber_ = JavaScriptEngine.toInt32(columnNumber);
140             }
141         }
142     }
143 
144     /**
145      * In contrast to the other event init values, {@code null} is converted to "null";
146      * only {@code undefined} (or a missing value) is ignored.
147      */
148     private static boolean isSet(final Object value) {
149         return value != Scriptable.NOT_FOUND && !JavaScriptEngine.isUndefined(value);
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public boolean isComposed() {
157         return composed_;
158     }
159 
160     /**
161      * Returns the documentURI property.
162      *
163      * @return the documentURI
164      */
165     @JsxGetter
166     public String getDocumentURI() {
167         return documentURI_;
168     }
169 
170     /**
171      * Sets the documentURI property.
172      *
173      * @param documentURI the new value
174      */
175     public void setDocumentURI(final String documentURI) {
176         documentURI_ = documentURI;
177     }
178 
179     /**
180      * Returns the referrer property.
181      *
182      * @return the referrer
183      */
184     @JsxGetter
185     public String getReferrer() {
186         return referrer_;
187     }
188 
189     /**
190      * Sets the referrer property.
191      *
192      * @param referrer the new value
193      */
194     public void setReferrer(final String referrer) {
195         referrer_ = referrer;
196     }
197 
198     /**
199      * Returns the blockedURI property.
200      *
201      * @return the blockedURI
202      */
203     @JsxGetter
204     public String getBlockedURI() {
205         return blockedURI_;
206     }
207 
208     /**
209      * Sets the blockedURI property.
210      *
211      * @param blockedURI the new value
212      */
213     public void setBlockedURI(final String blockedURI) {
214         blockedURI_ = blockedURI;
215     }
216 
217     /**
218      * Returns the effectiveDirective property.
219      *
220      * @return the effectiveDirective
221      */
222     @JsxGetter
223     public String getEffectiveDirective() {
224         return effectiveDirective_;
225     }
226 
227     /**
228      * Sets the effectiveDirective property.
229      *
230      * @param effectiveDirective the new value
231      */
232     public void setEffectiveDirective(final String effectiveDirective) {
233         effectiveDirective_ = effectiveDirective;
234     }
235 
236     /**
237      * Returns the violatedDirective property.
238      *
239      * @return the violatedDirective
240      */
241     @JsxGetter
242     public String getViolatedDirective() {
243         return violatedDirective_;
244     }
245 
246     /**
247      * Sets the violatedDirective property.
248      *
249      * @param violatedDirective the new value
250      */
251     public void setViolatedDirective(final String violatedDirective) {
252         violatedDirective_ = violatedDirective;
253     }
254 
255     /**
256      * Returns the originalPolicy property.
257      *
258      * @return the originalPolicy
259      */
260     @JsxGetter
261     public String getOriginalPolicy() {
262         return originalPolicy_;
263     }
264 
265     /**
266      * Sets the originalPolicy property.
267      *
268      * @param originalPolicy the new value
269      */
270     public void setOriginalPolicy(final String originalPolicy) {
271         originalPolicy_ = originalPolicy;
272     }
273 
274     /**
275      * Returns the sourceFile property.
276      *
277      * @return the sourceFile
278      */
279     @JsxGetter
280     public String getSourceFile() {
281         return sourceFile_;
282     }
283 
284     /**
285      * Sets the sourceFile property.
286      *
287      * @param sourceFile the new value
288      */
289     public void setSourceFile(final String sourceFile) {
290         sourceFile_ = sourceFile;
291     }
292 
293     /**
294      * Returns the sample property.
295      *
296      * @return the sample
297      */
298     @JsxGetter
299     public String getSample() {
300         return sample_;
301     }
302 
303     /**
304      * Sets the sample property.
305      *
306      * @param sample the new value
307      */
308     public void setSample(final String sample) {
309         sample_ = sample;
310     }
311 
312     /**
313      * Returns the disposition property.
314      *
315      * @return the disposition
316      */
317     @JsxGetter
318     public String getDisposition() {
319         return disposition_;
320     }
321 
322     /**
323      * Sets the disposition property.
324      *
325      * @param disposition the new value
326      */
327     public void setDisposition(final String disposition) {
328         disposition_ = disposition;
329     }
330 
331     /**
332      * Returns the statusCode property.
333      *
334      * @return the statusCode
335      */
336     @JsxGetter
337     public int getStatusCode() {
338         return statusCode_;
339     }
340 
341     /**
342      * Sets the statusCode property.
343      *
344      * @param statusCode the new value
345      */
346     public void setStatusCode(final int statusCode) {
347         statusCode_ = statusCode;
348     }
349 
350     /**
351      * Returns the lineNumber property.
352      *
353      * @return the lineNumber
354      */
355     @JsxGetter
356     public int getLineNumber() {
357         return lineNumber_;
358     }
359 
360     /**
361      * Sets the lineNumber property.
362      *
363      * @param lineNumber the new value
364      */
365     public void setLineNumber(final int lineNumber) {
366         lineNumber_ = lineNumber;
367     }
368 
369     /**
370      * Returns the columnNumber property.
371      *
372      * @return the columnNumber
373      */
374     @JsxGetter
375     public int getColumnNumber() {
376         return columnNumber_;
377     }
378 
379     /**
380      * Sets the columnNumber property.
381      *
382      * @param columnNumber the new value
383      */
384     public void setColumnNumber(final int columnNumber) {
385         columnNumber_ = columnNumber;
386     }
387 }