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.ClientRect;
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         AbortController.class, AbortSignal.class, Atomics.class,
142         Blob.class, BroadcastChannel.class,
143         Cache.class, CacheStorage.class, CanvasGradient.class, CanvasPattern.class,
144         ClientRect.class, CloseEvent.class,
145         Crypto.class, CryptoKey.class, CustomEvent.class,
146         DedicatedWorkerGlobalScope.class, DOMException.class, DOMMatrix.class, DOMMatrixReadOnly.class,
147         DOMPoint.class, DOMPointReadOnly.class, DOMRectReadOnly.class, DOMStringList.class,
148         ErrorEvent.class, Event.class, EventSource.class, EventTarget.class,
149         File.class, FileList.class, FileReader.class,
150         FontFace.class, FontFaceSet.class, FormData.class,
151         Headers.class,
152         IDBCursor.class, IDBCursorWithValue.class, IDBDatabase.class, IDBFactory.class, IDBIndex.class,
153         IDBKeyRange.class, IDBObjectStore.class, IDBOpenDBRequest.class, IDBRequest.class, IDBTransaction.class,
154         IDBVersionChangeEvent.class, ImageBitmap.class, ImageBitmapRenderingContext.class, ImageData.class,
155         MediaSource.class, MessageChannel.class, MessageEvent.class, MessagePort.class,
156         NetworkInformation.class, Notification.class,
157         Path2D.class, Performance.class, PerformanceEntry.class, PerformanceMark.class, PerformanceMeasure.class,
158         PerformanceObserver.class, PerformanceObserverEntryList.class, PerformanceResourceTiming.class,
159         PeriodicSyncManager.class, Permissions.class, PermissionStatus.class,
160         ProgressEvent.class, PromiseRejectionEvent.class,
161         PushManager.class, PushSubscription.class, PushSubscriptionOptions.class,
162         ReadableStream.class, Request.class, Response.class,
163         SecurityPolicyViolationEvent.class, ServiceWorkerRegistration.class, SourceBuffer.class, SourceBufferList.class,
164         StorageManager.class, SubtleCrypto.class, SyncManager.class,
165         TextDecoder.class, TextEncoder.class, TextMetrics.class,
166         URL.class, URLSearchParams.class,
167         WebGLContextEvent.class, WebGL2RenderingContext.class, WebGLActiveInfo.class, WebGLBuffer.class,
168         WebGLFramebuffer.class, WebGLProgram.class, WebGLQuery.class,
169         WebGLRenderbuffer.class, WebGLRenderingContext.class,
170         WebGLSampler.class, WebGLShader.class, WebGLShaderPrecisionFormat.class, WebGLSync.class, WebGLTexture.class,
171         WebGLTransformFeedback.class, WebGLUniformLocation.class, WebGLVertexArrayObject.class, WebSocket.class,
172         Worker.class, WorkerGlobalScope.class, WorkerLocation.class, WorkerNavigator.class,
173         XMLHttpRequest.class, XMLHttpRequestEventTarget.class, XMLHttpRequestUpload.class
174     };
175 
176     /** Cache of browser versions and their corresponding JavaScript configurations. */
177     private static final Map<String, WorkerJavaScriptConfiguration> CONFIGURATION_MAP_ = new WeakHashMap<>();
178 
179     /**
180      * Constructor is only called from {@link #getInstance(BrowserVersion)} which is synchronized.
181      * @param browser the browser version to use
182      */
183     private WorkerJavaScriptConfiguration(final BrowserVersion browser) {
184         super(browser);
185     }
186 
187     /**
188      * Returns the instance that represents the configuration for the specified {@link BrowserVersion}.
189      * This method is synchronized to allow multi-threaded access to the JavaScript configuration.
190      * @param browserVersion the {@link BrowserVersion}
191      * @return the instance for the specified {@link BrowserVersion}
192      */
193     @SuppressWarnings("PMD.SingletonClassReturningNewInstance")
194     public static synchronized WorkerJavaScriptConfiguration getInstance(final BrowserVersion browserVersion) {
195         if (browserVersion == null) {
196             throw new IllegalArgumentException("BrowserVersion must be provided");
197         }
198         WorkerJavaScriptConfiguration configuration = CONFIGURATION_MAP_.get(browserVersion.getNickname());
199 
200         if (configuration == null) {
201             configuration = new WorkerJavaScriptConfiguration(browserVersion);
202             CONFIGURATION_MAP_.put(browserVersion.getNickname(), configuration);
203         }
204         return configuration;
205     }
206 
207     @Override
208     protected Class<? extends HtmlUnitScriptable>[] getClasses() {
209         return CLASSES_;
210     }
211 }