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.ScriptableObject;
18 import org.htmlunit.javascript.JavaScriptEngine;
19 import org.htmlunit.javascript.configuration.JsxClass;
20 import org.htmlunit.javascript.configuration.JsxConstructor;
21 import org.htmlunit.javascript.configuration.JsxGetter;
22
23 /**
24 * JavaScript host object for {@code CloseEvent}.
25 *
26 * @author Ahmed Ashour
27 * @author Ronald Brill
28 *
29 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent">MDN Documentation</a>
30 */
31 @JsxClass
32 public class CloseEvent extends Event {
33
34 private String reason_;
35 private int code_;
36 private boolean wasClean_;
37
38 /**
39 * Creates a new event instance.
40 */
41 public CloseEvent() {
42 super();
43 setType(TYPE_CLOSE);
44 reason_ = "";
45 }
46
47 /**
48 * {@inheritDoc}
49 */
50 @Override
51 public void eventCreated() {
52 super.eventCreated();
53 setType("");
54 }
55
56 /**
57 * Creates an instance of this event.
58 *
59 * @param type the event type
60 * @param details the event details (optional)
61 */
62 @Override
63 @JsxConstructor
64 public void jsConstructor(final String type, final ScriptableObject details) {
65 super.jsConstructor(type, details);
66
67 if (details != null && !JavaScriptEngine.isUndefined(details)) {
68 code_ = JavaScriptEngine.toInt32(details.get("code"));
69 wasClean_ = JavaScriptEngine.toBoolean(details.get("wasClean"));
70
71 final Object reason = details.get("reason");
72 if (!isNullMissingOrUndefined(reason)) {
73 reason_ = JavaScriptEngine.toString(reason);
74 }
75 }
76 }
77
78 /**
79 * Returns the close code.
80 *
81 * @return the close code
82 */
83 @JsxGetter
84 public int getCode() {
85 return code_;
86 }
87
88 /**
89 * Sets the close code.
90 *
91 * @param code the close code
92 */
93 public void setCode(final int code) {
94 code_ = code;
95 }
96
97 /**
98 * Returns the reason the connection was closed.
99 *
100 * @return the reason
101 */
102 @JsxGetter
103 public String getReason() {
104 return reason_;
105 }
106
107 /**
108 * Sets the reason the connection was closed.
109 *
110 * @param reason the reason
111 */
112 public void setReason(final String reason) {
113 reason_ = reason;
114 }
115
116 /**
117 * Returns whether the connection was closed cleanly.
118 *
119 * @return {@code true} if the connection was closed cleanly
120 */
121 @JsxGetter
122 public boolean isWasClean() {
123 return wasClean_;
124 }
125
126 /**
127 * Sets whether the connection was closed cleanly.
128 *
129 * @param wasClean {@code true} if the connection was closed cleanly
130 */
131 public void setWasClean(final boolean wasClean) {
132 wasClean_ = wasClean;
133 }
134 }