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 static org.htmlunit.BrowserVersionFeatures.JS_EVENT_SECURITY_POLICY_VIOLATION_CTOR_BUBBLES_COMPOSED;
18  import static org.htmlunit.BrowserVersionFeatures.JS_EVENT_SECURITY_POLICY_VIOLATION_POSITION_UNSIGNED;
19  
20  import org.htmlunit.corejs.javascript.Context;
21  import org.htmlunit.corejs.javascript.Function;
22  import org.htmlunit.corejs.javascript.FunctionObject;
23  import org.htmlunit.corejs.javascript.Scriptable;
24  import org.htmlunit.corejs.javascript.ScriptableObject;
25  import org.htmlunit.corejs.javascript.VarScope;
26  import org.htmlunit.html.DomNode;
27  import org.htmlunit.javascript.JavaScriptEngine;
28  import org.htmlunit.javascript.configuration.JsxClass;
29  import org.htmlunit.javascript.configuration.JsxConstructor;
30  import org.htmlunit.javascript.configuration.JsxGetter;
31  
32  /**
33   * A JavaScript object for {@code SecurityPolicyViolationEvent}.
34   *
35   * @author Ahmed Ashour
36   * @author Ronald Brill
37   */
38  @JsxClass
39  public class SecurityPolicyViolationEvent extends Event {
40  
41      /** The event type. */
42      public static final String TYPE_SECURITY_POLICY_VIOLATION = "securitypolicyviolation";
43  
44      /** Disposition of enforced policies. */
45      public static final String DISPOSITION_ENFORCE = "enforce";
46  
47      /** Disposition of report only policies. */
48      public static final String DISPOSITION_REPORT = "report";
49  
50      private String documentURI_ = "";
51      private String referrer_ = "";
52      private String blockedURI_ = "";
53      private String effectiveDirective_ = "";
54      private String violatedDirective_ = "";
55      private String originalPolicy_ = "";
56      private String sourceFile_ = "";
57      private String sample_ = "";
58      private String disposition_ = DISPOSITION_ENFORCE;
59      private boolean composed_;
60      private int statusCode_;
61      private long lineNumber_;
62      private long columnNumber_;
63  
64      /**
65       * Default constructor used to build the prototype.
66       */
67      public SecurityPolicyViolationEvent() {
68          super();
69      }
70  
71      /**
72       * Creates a new securitypolicyviolation event (bubbling, not cancelable) for the given target.
73       *
74       * @param target the DOM node the event is fired at
75       */
76      public SecurityPolicyViolationEvent(final DomNode target) {
77          super(target, TYPE_SECURITY_POLICY_VIOLATION);
78          setBubbles(true);
79          setCancelable(false);
80          composed_ = true;
81      }
82  
83      /**
84       * JavaScript constructor.
85       *
86       * @param cx the current context
87       * @param scope the scope
88       * @param args the arguments (the type and the optional init dictionary)
89       * @param ctorObj the function object
90       * @param inNewExpr whether invoked via {@code new}
91       * @return the new event
92       */
93      @JsxConstructor
94      public static SecurityPolicyViolationEvent jsConstructor(final Context cx, final VarScope scope,
95              final Object[] args, final Function ctorObj, final boolean inNewExpr) {
96          if (!inNewExpr) {
97              throw JavaScriptEngine.typeError("Failed to construct 'SecurityPolicyViolationEvent': Please use "
98                      + "the 'new' operator, this DOM object constructor cannot be called as a function.");
99          }
100         if (args.length < 1) {
101             throw JavaScriptEngine.typeError("Failed to construct 'SecurityPolicyViolationEvent': "
102                     + "1 argument required, but only 0 present.");
103         }
104 
105         final SecurityPolicyViolationEvent event = new SecurityPolicyViolationEvent();
106         event.setParentScope(getTopLevelScope(scope));
107         event.setPrototype(((FunctionObject) ctorObj).getClassPrototype());
108 
109         final ScriptableObject details =
110                 args.length > 1 && args[1] instanceof ScriptableObject so ? so : null;
111         event.init(JavaScriptEngine.toString(args[0]), details);
112         return event;
113     }
114 
115     private void init(final String type, final ScriptableObject details) {
116         super.jsConstructor(type, details);
117 
118         if (details != null) {
119             composed_ = JavaScriptEngine.toBoolean(details.get("composed"));
120 
121             final Object documentURI = ScriptableObject.getProperty(details, "documentURI");
122             if (isSet(documentURI)) {
123                 documentURI_ = JavaScriptEngine.toString(documentURI);
124             }
125             final Object referrer = ScriptableObject.getProperty(details, "referrer");
126             if (isSet(referrer)) {
127                 referrer_ = JavaScriptEngine.toString(referrer);
128             }
129             final Object blockedURI = ScriptableObject.getProperty(details, "blockedURI");
130             if (isSet(blockedURI)) {
131                 blockedURI_ = JavaScriptEngine.toString(blockedURI);
132             }
133             final Object effectiveDirective = ScriptableObject.getProperty(details, "effectiveDirective");
134             if (isSet(effectiveDirective)) {
135                 effectiveDirective_ = JavaScriptEngine.toString(effectiveDirective);
136             }
137             final Object violatedDirective = ScriptableObject.getProperty(details, "violatedDirective");
138             if (isSet(violatedDirective)) {
139                 violatedDirective_ = JavaScriptEngine.toString(violatedDirective);
140             }
141             final Object originalPolicy = ScriptableObject.getProperty(details, "originalPolicy");
142             if (isSet(originalPolicy)) {
143                 originalPolicy_ = JavaScriptEngine.toString(originalPolicy);
144             }
145             final Object sourceFile = ScriptableObject.getProperty(details, "sourceFile");
146             if (isSet(sourceFile)) {
147                 sourceFile_ = JavaScriptEngine.toString(sourceFile);
148             }
149             final Object sample = ScriptableObject.getProperty(details, "sample");
150             if (isSet(sample)) {
151                 sample_ = JavaScriptEngine.toString(sample);
152             }
153             final Object disposition = ScriptableObject.getProperty(details, "disposition");
154             if (isSet(disposition)) {
155                 final String value = JavaScriptEngine.toString(disposition);
156                 if (!DISPOSITION_ENFORCE.equals(value) && !DISPOSITION_REPORT.equals(value)) {
157                     throw JavaScriptEngine.typeError("Failed to construct 'SecurityPolicyViolationEvent': "
158                             + "The provided value '" + value
159                             + "' is not a valid enum value of type SecurityPolicyViolationEventDisposition.");
160                 }
161                 disposition_ = value;
162             }
163             final Object statusCode = ScriptableObject.getProperty(details, "statusCode");
164             if (isSet(statusCode)) {
165                 statusCode_ = JavaScriptEngine.toInt32(statusCode);
166             }
167 
168             // Firefox uses unsigned long for the position (-1 is 4294967295)
169             final boolean unsigned =
170                     getBrowserVersion().hasFeature(JS_EVENT_SECURITY_POLICY_VIOLATION_POSITION_UNSIGNED);
171             final Object lineNumber = ScriptableObject.getProperty(details, "lineNumber");
172             if (isSet(lineNumber)) {
173                 lineNumber_ = unsigned
174                         ? JavaScriptEngine.toUint32(lineNumber)
175                         : JavaScriptEngine.toInt32(lineNumber);
176             }
177             final Object columnNumber = ScriptableObject.getProperty(details, "columnNumber");
178             if (isSet(columnNumber)) {
179                 columnNumber_ = unsigned
180                         ? JavaScriptEngine.toUint32(columnNumber)
181                         : JavaScriptEngine.toInt32(columnNumber);
182             }
183         }
184 
185         // Chrome ignores the bubbles, cancelable and composed init values;
186         // the event always bubbles, is composed and is not cancelable
187         if (getBrowserVersion().hasFeature(JS_EVENT_SECURITY_POLICY_VIOLATION_CTOR_BUBBLES_COMPOSED)) {
188             setBubbles(true);
189             setCancelable(false);
190             composed_ = true;
191         }
192     }
193 
194     /**
195      * In contrast to the other event init values, {@code null} is converted to "null";
196      * only {@code undefined} (or a missing value) is ignored.
197      */
198     private static boolean isSet(final Object value) {
199         return value != Scriptable.NOT_FOUND && !JavaScriptEngine.isUndefined(value);
200     }
201 
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     public boolean isComposed() {
207         return composed_;
208     }
209 
210     /**
211      * Returns the documentURI property.
212      *
213      * @return the documentURI
214      */
215     @JsxGetter
216     public String getDocumentURI() {
217         return documentURI_;
218     }
219 
220     /**
221      * Sets the documentURI property.
222      *
223      * @param documentURI the new value
224      */
225     public void setDocumentURI(final String documentURI) {
226         documentURI_ = documentURI;
227     }
228 
229     /**
230      * Returns the referrer property.
231      *
232      * @return the referrer
233      */
234     @JsxGetter
235     public String getReferrer() {
236         return referrer_;
237     }
238 
239     /**
240      * Sets the referrer property.
241      *
242      * @param referrer the new value
243      */
244     public void setReferrer(final String referrer) {
245         referrer_ = referrer;
246     }
247 
248     /**
249      * Returns the blockedURI property.
250      *
251      * @return the blockedURI
252      */
253     @JsxGetter
254     public String getBlockedURI() {
255         return blockedURI_;
256     }
257 
258     /**
259      * Sets the blockedURI property.
260      *
261      * @param blockedURI the new value
262      */
263     public void setBlockedURI(final String blockedURI) {
264         blockedURI_ = blockedURI;
265     }
266 
267     /**
268      * Returns the effectiveDirective property.
269      *
270      * @return the effectiveDirective
271      */
272     @JsxGetter
273     public String getEffectiveDirective() {
274         return effectiveDirective_;
275     }
276 
277     /**
278      * Sets the effectiveDirective property.
279      *
280      * @param effectiveDirective the new value
281      */
282     public void setEffectiveDirective(final String effectiveDirective) {
283         effectiveDirective_ = effectiveDirective;
284     }
285 
286     /**
287      * Returns the violatedDirective property.
288      *
289      * @return the violatedDirective
290      */
291     @JsxGetter
292     public String getViolatedDirective() {
293         return violatedDirective_;
294     }
295 
296     /**
297      * Sets the violatedDirective property.
298      *
299      * @param violatedDirective the new value
300      */
301     public void setViolatedDirective(final String violatedDirective) {
302         violatedDirective_ = violatedDirective;
303     }
304 
305     /**
306      * Returns the originalPolicy property.
307      *
308      * @return the originalPolicy
309      */
310     @JsxGetter
311     public String getOriginalPolicy() {
312         return originalPolicy_;
313     }
314 
315     /**
316      * Sets the originalPolicy property.
317      *
318      * @param originalPolicy the new value
319      */
320     public void setOriginalPolicy(final String originalPolicy) {
321         originalPolicy_ = originalPolicy;
322     }
323 
324     /**
325      * Returns the sourceFile property.
326      *
327      * @return the sourceFile
328      */
329     @JsxGetter
330     public String getSourceFile() {
331         return sourceFile_;
332     }
333 
334     /**
335      * Sets the sourceFile property.
336      *
337      * @param sourceFile the new value
338      */
339     public void setSourceFile(final String sourceFile) {
340         sourceFile_ = sourceFile;
341     }
342 
343     /**
344      * Returns the sample property.
345      *
346      * @return the sample
347      */
348     @JsxGetter
349     public String getSample() {
350         return sample_;
351     }
352 
353     /**
354      * Sets the sample property.
355      *
356      * @param sample the new value
357      */
358     public void setSample(final String sample) {
359         sample_ = sample;
360     }
361 
362     /**
363      * Returns the disposition property.
364      *
365      * @return the disposition
366      */
367     @JsxGetter
368     public String getDisposition() {
369         return disposition_;
370     }
371 
372     /**
373      * Sets the disposition property.
374      *
375      * @param disposition the new value
376      */
377     public void setDisposition(final String disposition) {
378         disposition_ = disposition;
379     }
380 
381     /**
382      * Returns the statusCode property.
383      *
384      * @return the statusCode
385      */
386     @JsxGetter
387     public int getStatusCode() {
388         return statusCode_;
389     }
390 
391     /**
392      * Sets the statusCode property.
393      *
394      * @param statusCode the new value
395      */
396     public void setStatusCode(final int statusCode) {
397         statusCode_ = statusCode;
398     }
399 
400     /**
401      * Returns the lineNumber property.
402      *
403      * @return the lineNumber
404      */
405     @JsxGetter
406     public long getLineNumber() {
407         return lineNumber_;
408     }
409 
410     /**
411      * Sets the lineNumber property.
412      *
413      * @param lineNumber the new value
414      */
415     public void setLineNumber(final int lineNumber) {
416         lineNumber_ = lineNumber;
417     }
418 
419     /**
420      * Returns the columnNumber property.
421      *
422      * @return the columnNumber
423      */
424     @JsxGetter
425     public long getColumnNumber() {
426         return columnNumber_;
427     }
428 
429     /**
430      * Sets the columnNumber property.
431      *
432      * @param columnNumber the new value
433      */
434     public void setColumnNumber(final int columnNumber) {
435         columnNumber_ = columnNumber;
436     }
437 }