forked from HtmlUnit/htmlunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowOrWorkerGlobalScopeMixin.java
More file actions
229 lines (208 loc) · 9.41 KB
/
WindowOrWorkerGlobalScopeMixin.java
File metadata and controls
229 lines (208 loc) · 9.41 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.javascript.host;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import org.htmlunit.Page;
import org.htmlunit.WebWindow;
import org.htmlunit.corejs.javascript.Context;
import org.htmlunit.corejs.javascript.Function;
import org.htmlunit.corejs.javascript.FunctionObject;
import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.corejs.javascript.ScriptableObject;
import org.htmlunit.javascript.HtmlUnitScriptable;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.background.BackgroundJavaScriptFactory;
import org.htmlunit.javascript.background.JavaScriptJob;
import org.htmlunit.util.StringUtils;
/**
* The implementation of {@code WindowOrWorkerGlobalScope}
* to be used by the implementers of the mixin.
*
* @author Ronald Brill
* @author Rural Hunter
* @author Lai Quang Duong
*/
public final class WindowOrWorkerGlobalScopeMixin {
/**
* The minimum delay that can be used with setInterval() or setTimeout(). Browser minimums are
* usually in the 10ms to 15ms range, but there's really no reason for us to waste that much time.
* <a href="http://jsninja.com/Timers#Minimum_Timer_Delay_and_Reliability">
* http://jsninja.com/Timers#Minimum_Timer_Delay_and_Reliability</a>
*/
private static final int MIN_TIMER_DELAY = 1;
private WindowOrWorkerGlobalScopeMixin() {
super();
}
/**
* Decodes a string of data which has been encoded using base-64 encoding.
* @param encodedData the encoded string
* @param scriptable the HtmlUnitScriptable scope
* @return the decoded value
*/
public static String atob(final String encodedData, final HtmlUnitScriptable scriptable) {
final String withoutWhitespace = StringUtils.replaceChars(encodedData, " \t\r\n\u000c", "");
final byte[] bytes = withoutWhitespace.getBytes(StandardCharsets.ISO_8859_1);
try {
return new String(Base64.getDecoder().decode(bytes), StandardCharsets.ISO_8859_1);
}
catch (final IllegalArgumentException e) {
throw JavaScriptEngine.asJavaScriptException(
scriptable,
"Failed to execute atob(): " + e.getMessage(),
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
}
}
/**
* Creates a base-64 encoded ASCII string from a string of binary data.
* @param stringToEncode string to encode
* @param scriptable the HtmlUnitScriptable scope
* @return the encoded string
*/
public static String btoa(final String stringToEncode, final HtmlUnitScriptable scriptable) {
final int l = stringToEncode.length();
for (int i = 0; i < l; i++) {
if (stringToEncode.charAt(i) > 255) {
throw JavaScriptEngine.asJavaScriptException(
scriptable,
"Function btoa supports only latin1 characters",
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
}
}
final byte[] bytes = stringToEncode.getBytes(StandardCharsets.ISO_8859_1);
try {
return new String(Base64.getEncoder().encode(bytes), StandardCharsets.UTF_8);
}
catch (final IllegalArgumentException e) {
throw JavaScriptEngine.asJavaScriptException(
scriptable,
"Failed to execute btoa(): " + e.getMessage(),
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
}
}
/**
* Sets a chunk of JavaScript to be invoked at some specified time later.
* The invocation occurs only if the window is opened after the delay
* and does not contain an other page than the one that originated the setTimeout.
*
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">
* MDN web docs</a>
*
* @param context the JavaScript context
* @param thisObj the scriptable
* @param args the arguments passed into the method
* @param function the function
* @return the id of the created timer
*/
public static Object setTimeout(final Context context, final Scriptable thisObj,
final Object[] args, final Function function) {
if (args.length < 1) {
throw JavaScriptEngine.typeError("Function not provided");
}
final int timeout = JavaScriptEngine.toInt32((args.length > 1) ? args[1] : JavaScriptEngine.UNDEFINED);
final Object[] params = (args.length > 2)
? Arrays.copyOfRange(args, 2, args.length)
: JavaScriptEngine.EMPTY_ARGS;
return setTimeoutIntervalImpl((Window) thisObj, args[0], timeout, true, params);
}
/**
* Sets a chunk of JavaScript to be invoked each time a specified number of milliseconds has elapsed.
*
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval">
* MDN web docs</a>
* @param context the JavaScript context
* @param thisObj the scriptable
* @param args the arguments passed into the method
* @param function the function
* @return the id of the created interval
*/
public static Object setInterval(final Context context, final Scriptable thisObj,
final Object[] args, final Function function) {
if (args.length < 1) {
throw JavaScriptEngine.typeError("Function not provided");
}
final int timeout = JavaScriptEngine.toInt32((args.length > 1) ? args[1] : JavaScriptEngine.UNDEFINED);
final Object[] params = (args.length > 2)
? Arrays.copyOfRange(args, 2, args.length)
: JavaScriptEngine.EMPTY_ARGS;
return setTimeoutIntervalImpl((Window) thisObj, args[0], timeout, false, params);
}
/**
* Queues a microtask to be executed.
*
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask">
* MDN web docs</a>
* @param thisObj the scriptable
* @param args the arguments passed into the method
* @return undefined
*/
public static Object queueMicrotask(final Scriptable thisObj, final Object[] args) {
if (args.length < 1) {
throw JavaScriptEngine.typeError("At least 1 argument required");
}
if (!(args[0] instanceof Function)) {
throw JavaScriptEngine.typeError("Argument 1 is not callable");
}
final Function callback = (Function) args[0];
final Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
final Context cx = Context.getCurrentContext();
cx.enqueueMicrotask(() -> {
try {
callback.call(cx, scope, thisObj, JavaScriptEngine.EMPTY_ARGS);
}
catch (final Exception e) {
// uncaught exception in a microtask must not prevent remaining microtasks from running.
}
});
return JavaScriptEngine.UNDEFINED;
}
private static int setTimeoutIntervalImpl(final Window window, final Object code,
int timeout, final boolean isTimeout, final Object[] params) {
if (timeout < MIN_TIMER_DELAY) {
timeout = MIN_TIMER_DELAY;
}
final WebWindow webWindow = window.getWebWindow();
final Page page = (Page) window.getDomNodeOrNull();
Integer period = null;
if (!isTimeout) {
period = timeout;
}
if (code instanceof String s) {
final String description = "window.set"
+ (isTimeout ? "Timeout" : "Interval")
+ "(" + s + ", " + timeout + ")";
final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().
createJavaScriptJob(timeout, period, description, webWindow, s);
return webWindow.getJobManager().addJob(job, page);
}
if (code instanceof Function f) {
final String functionName;
if (f instanceof FunctionObject object) {
functionName = object.getFunctionName();
}
else {
functionName = String.valueOf(f); // can this happen?
}
final String description = "window.set"
+ (isTimeout ? "Timeout" : "Interval")
+ "(" + functionName + ", " + timeout + ")";
final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().
createJavaScriptJob(timeout, period, description, webWindow, f, params);
return webWindow.getJobManager().addJob(job, page);
}
throw JavaScriptEngine.reportRuntimeError("Unknown type for function.");
}
}