View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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.configuration;
16  
17  import java.util.Map;
18  import java.util.WeakHashMap;
19  
20  import org.htmlunit.BrowserVersion;
21  import org.htmlunit.javascript.HtmlUnitScriptable;
22  import org.htmlunit.javascript.host.BroadcastChannel;
23  import org.htmlunit.javascript.host.Cache;
24  import org.htmlunit.javascript.host.CacheStorage;
25  import org.htmlunit.javascript.host.DOMRect;
26  import org.htmlunit.javascript.host.FontFace;
27  import org.htmlunit.javascript.host.FontFaceSet;
28  import org.htmlunit.javascript.host.ImageBitmap;
29  import org.htmlunit.javascript.host.MessageChannel;
30  import org.htmlunit.javascript.host.MessagePort;
31  import org.htmlunit.javascript.host.Notification;
32  import org.htmlunit.javascript.host.PerformanceObserver;
33  import org.htmlunit.javascript.host.PerformanceObserverEntryList;
34  import org.htmlunit.javascript.host.PermissionStatus;
35  import org.htmlunit.javascript.host.Permissions;
36  import org.htmlunit.javascript.host.PushManager;
37  import org.htmlunit.javascript.host.PushSubscription;
38  import org.htmlunit.javascript.host.PushSubscriptionOptions;
39  import org.htmlunit.javascript.host.ReadableStream;
40  import org.htmlunit.javascript.host.StorageManager;
41  import org.htmlunit.javascript.host.TextDecoder;
42  import org.htmlunit.javascript.host.TextEncoder;
43  import org.htmlunit.javascript.host.URL;
44  import org.htmlunit.javascript.host.URLSearchParams;
45  import org.htmlunit.javascript.host.WebSocket;
46  import org.htmlunit.javascript.host.abort.AbortController;
47  import org.htmlunit.javascript.host.abort.AbortSignal;
48  import org.htmlunit.javascript.host.arrays.Atomics;
49  import org.htmlunit.javascript.host.canvas.CanvasGradient;
50  import org.htmlunit.javascript.host.canvas.CanvasPattern;
51  import org.htmlunit.javascript.host.canvas.ImageBitmapRenderingContext;
52  import org.htmlunit.javascript.host.canvas.ImageData;
53  import org.htmlunit.javascript.host.canvas.Path2D;
54  import org.htmlunit.javascript.host.canvas.TextMetrics;
55  import org.htmlunit.javascript.host.canvas.WebGL2RenderingContext;
56  import org.htmlunit.javascript.host.canvas.WebGLActiveInfo;
57  import org.htmlunit.javascript.host.canvas.WebGLBuffer;
58  import org.htmlunit.javascript.host.canvas.WebGLFramebuffer;
59  import org.htmlunit.javascript.host.canvas.WebGLProgram;
60  import org.htmlunit.javascript.host.canvas.WebGLQuery;
61  import org.htmlunit.javascript.host.canvas.WebGLRenderbuffer;
62  import org.htmlunit.javascript.host.canvas.WebGLRenderingContext;
63  import org.htmlunit.javascript.host.canvas.WebGLSampler;
64  import org.htmlunit.javascript.host.canvas.WebGLShader;
65  import org.htmlunit.javascript.host.canvas.WebGLShaderPrecisionFormat;
66  import org.htmlunit.javascript.host.canvas.WebGLSync;
67  import org.htmlunit.javascript.host.canvas.WebGLTexture;
68  import org.htmlunit.javascript.host.canvas.WebGLTransformFeedback;
69  import org.htmlunit.javascript.host.canvas.WebGLUniformLocation;
70  import org.htmlunit.javascript.host.canvas.WebGLVertexArrayObject;
71  import org.htmlunit.javascript.host.crypto.Crypto;
72  import org.htmlunit.javascript.host.crypto.CryptoKey;
73  import org.htmlunit.javascript.host.crypto.SubtleCrypto;
74  import org.htmlunit.javascript.host.dom.DOMException;
75  import org.htmlunit.javascript.host.dom.DOMMatrix;
76  import org.htmlunit.javascript.host.dom.DOMMatrixReadOnly;
77  import org.htmlunit.javascript.host.dom.DOMPoint;
78  import org.htmlunit.javascript.host.dom.DOMPointReadOnly;
79  import org.htmlunit.javascript.host.dom.DOMRectReadOnly;
80  import org.htmlunit.javascript.host.dom.DOMStringList;
81  import org.htmlunit.javascript.host.event.CloseEvent;
82  import org.htmlunit.javascript.host.event.CustomEvent;
83  import org.htmlunit.javascript.host.event.ErrorEvent;
84  import org.htmlunit.javascript.host.event.Event;
85  import org.htmlunit.javascript.host.event.EventSource;
86  import org.htmlunit.javascript.host.event.EventTarget;
87  import org.htmlunit.javascript.host.event.IDBVersionChangeEvent;
88  import org.htmlunit.javascript.host.event.MessageEvent;
89  import org.htmlunit.javascript.host.event.ProgressEvent;
90  import org.htmlunit.javascript.host.event.PromiseRejectionEvent;
91  import org.htmlunit.javascript.host.event.SecurityPolicyViolationEvent;
92  import org.htmlunit.javascript.host.event.WebGLContextEvent;
93  import org.htmlunit.javascript.host.fetch.Headers;
94  import org.htmlunit.javascript.host.fetch.Request;
95  import org.htmlunit.javascript.host.fetch.Response;
96  import org.htmlunit.javascript.host.file.Blob;
97  import org.htmlunit.javascript.host.file.File;
98  import org.htmlunit.javascript.host.file.FileList;
99  import org.htmlunit.javascript.host.file.FileReader;
100 import org.htmlunit.javascript.host.idb.IDBCursor;
101 import org.htmlunit.javascript.host.idb.IDBCursorWithValue;
102 import org.htmlunit.javascript.host.idb.IDBDatabase;
103 import org.htmlunit.javascript.host.idb.IDBFactory;
104 import org.htmlunit.javascript.host.idb.IDBIndex;
105 import org.htmlunit.javascript.host.idb.IDBKeyRange;
106 import org.htmlunit.javascript.host.idb.IDBObjectStore;
107 import org.htmlunit.javascript.host.idb.IDBOpenDBRequest;
108 import org.htmlunit.javascript.host.idb.IDBRequest;
109 import org.htmlunit.javascript.host.idb.IDBTransaction;
110 import org.htmlunit.javascript.host.media.MediaSource;
111 import org.htmlunit.javascript.host.media.PeriodicSyncManager;
112 import org.htmlunit.javascript.host.media.SourceBuffer;
113 import org.htmlunit.javascript.host.media.SourceBufferList;
114 import org.htmlunit.javascript.host.network.NetworkInformation;
115 import org.htmlunit.javascript.host.performance.Performance;
116 import org.htmlunit.javascript.host.performance.PerformanceEntry;
117 import org.htmlunit.javascript.host.performance.PerformanceMark;
118 import org.htmlunit.javascript.host.performance.PerformanceMeasure;
119 import org.htmlunit.javascript.host.performance.PerformanceResourceTiming;
120 import org.htmlunit.javascript.host.worker.DedicatedWorkerGlobalScope;
121 import org.htmlunit.javascript.host.worker.ServiceWorkerRegistration;
122 import org.htmlunit.javascript.host.worker.SyncManager;
123 import org.htmlunit.javascript.host.worker.Worker;
124 import org.htmlunit.javascript.host.worker.WorkerGlobalScope;
125 import org.htmlunit.javascript.host.worker.WorkerLocation;
126 import org.htmlunit.javascript.host.worker.WorkerNavigator;
127 import org.htmlunit.javascript.host.xml.FormData;
128 import org.htmlunit.javascript.host.xml.XMLHttpRequest;
129 import org.htmlunit.javascript.host.xml.XMLHttpRequestEventTarget;
130 import org.htmlunit.javascript.host.xml.XMLHttpRequestUpload;
131 
132 /**
133  * A container for all the JavaScript configuration information for workers.
134  *
135  * @author Ronald Brill
136  */
137 public final class WorkerJavaScriptConfiguration extends AbstractJavaScriptConfiguration {
138 
139     @SuppressWarnings("unchecked")
140     static final Class<? extends HtmlUnitScriptable>[] CLASSES_ = new Class[] {
141         // level 1
142         AbortController.class, Atomics.class, Blob.class, Cache.class, CacheStorage.class, CanvasGradient.class,
143         CanvasPattern.class, Crypto.class, CryptoKey.class, DOMException.class, DOMMatrixReadOnly.class,
144         DOMPointReadOnly.class, DOMRectReadOnly.class, DOMStringList.class, Event.class, EventTarget.class,
145         FileList.class, FontFace.class, FormData.class, Headers.class, IDBCursor.class, IDBFactory.class,
146         IDBIndex.class, IDBKeyRange.class, IDBObjectStore.class, ImageBitmap.class, ImageBitmapRenderingContext.class,
147         ImageData.class, MessageChannel.class, Path2D.class, PerformanceEntry.class, PerformanceObserver.class,
148         PerformanceObserverEntryList.class, PeriodicSyncManager.class, Permissions.class, PushManager.class,
149         PushSubscription.class, PushSubscriptionOptions.class, ReadableStream.class, Request.class, Response.class,
150         StorageManager.class, SubtleCrypto.class, SyncManager.class, TextDecoder.class, TextEncoder.class,
151         TextMetrics.class, URL.class, URLSearchParams.class, WebGL2RenderingContext.class, WebGLActiveInfo.class,
152         WebGLBuffer.class, WebGLFramebuffer.class, WebGLProgram.class, WebGLQuery.class, WebGLRenderbuffer.class,
153         WebGLRenderingContext.class, WebGLSampler.class, WebGLShader.class, WebGLShaderPrecisionFormat.class,
154         WebGLSync.class, WebGLTexture.class, WebGLTransformFeedback.class, WebGLUniformLocation.class,
155         WebGLVertexArrayObject.class, WorkerLocation.class, WorkerNavigator.class,
156         // level 2
157         AbortSignal.class, BroadcastChannel.class, CloseEvent.class, CustomEvent.class, DOMMatrix.class, DOMPoint.class,
158         DOMRect.class, ErrorEvent.class, EventSource.class, File.class, FileReader.class, FontFaceSet.class,
159         IDBCursorWithValue.class, IDBDatabase.class, IDBRequest.class, IDBTransaction.class,
160         IDBVersionChangeEvent.class, MediaSource.class, MessageEvent.class, MessagePort.class, NetworkInformation.class,
161         Notification.class, Performance.class, PerformanceMark.class, PerformanceMeasure.class,
162         PerformanceResourceTiming.class, PermissionStatus.class, ProgressEvent.class, PromiseRejectionEvent.class,
163         SecurityPolicyViolationEvent.class, ServiceWorkerRegistration.class, SourceBuffer.class, SourceBufferList.class,
164         WebGLContextEvent.class, WebSocket.class, Worker.class, WorkerGlobalScope.class,
165         XMLHttpRequestEventTarget.class,
166         // level 3
167         DedicatedWorkerGlobalScope.class, IDBOpenDBRequest.class, XMLHttpRequest.class, XMLHttpRequestUpload.class,
168     };
169 
170     /** Cache of browser versions and their corresponding JavaScript configurations. */
171     private static final Map<String, WorkerJavaScriptConfiguration> CONFIGURATION_MAP_ = new WeakHashMap<>();
172 
173     /**
174      * Constructor is only called from {@link #getInstance(BrowserVersion)} which is synchronized.
175      * @param browser the browser version to use
176      */
177     private WorkerJavaScriptConfiguration(final BrowserVersion browser) {
178         super(browser, DedicatedWorkerGlobalScope.class);
179     }
180 
181     /**
182      * Returns the instance that represents the configuration for the specified {@link BrowserVersion}.
183      * This method is synchronized to allow multi-threaded access to the JavaScript configuration.
184      * @param browserVersion the {@link BrowserVersion}
185      * @return the instance for the specified {@link BrowserVersion}
186      */
187     @SuppressWarnings("PMD.SingletonClassReturningNewInstance")
188     public static synchronized WorkerJavaScriptConfiguration getInstance(final BrowserVersion browserVersion) {
189         if (browserVersion == null) {
190             throw new IllegalArgumentException("BrowserVersion must be provided");
191         }
192         WorkerJavaScriptConfiguration configuration = CONFIGURATION_MAP_.get(browserVersion.getNickname());
193 
194         if (configuration == null) {
195             configuration = new WorkerJavaScriptConfiguration(browserVersion);
196             CONFIGURATION_MAP_.put(browserVersion.getNickname(), configuration);
197         }
198         return configuration;
199     }
200 
201     @Override
202     protected Class<? extends HtmlUnitScriptable>[] getClasses() {
203         return CLASSES_;
204     }
205 
206     /**
207      * @return the configuration of the scope class
208      */
209     public ClassConfiguration getDedicatedWorkerGlobalScopeClassConfiguration() {
210         return getScopeConfiguration();
211     }
212 }