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.worker;
16  
17  import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
18  import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
19  
20  import java.util.ArrayList;
21  
22  import org.htmlunit.BrowserVersion;
23  import org.htmlunit.corejs.javascript.Scriptable;
24  import org.htmlunit.javascript.HtmlUnitScriptable;
25  import org.htmlunit.javascript.JavaScriptEngine;
26  import org.htmlunit.javascript.configuration.JsxClass;
27  import org.htmlunit.javascript.configuration.JsxConstructor;
28  import org.htmlunit.javascript.configuration.JsxGetter;
29  import org.htmlunit.javascript.host.network.NetworkInformation;
30  import org.htmlunit.util.StringUtils;
31  
32  /**
33   * JavaScript host object for {@code WorkerNavigator}.
34   *
35   * @author Ronald Brill
36   *
37   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/WorkerNavigator">MDN Documentation</a>
38   */
39  @JsxClass
40  public class WorkerNavigator extends HtmlUnitScriptable {
41  
42      private final BrowserVersion browserVersion_;
43  
44      /**
45       * Default constructor.
46       */
47      public WorkerNavigator() {
48          // prototype constructor
49          super();
50          browserVersion_ = null;
51      }
52  
53      WorkerNavigator(final BrowserVersion browserVersion) {
54          browserVersion_ = browserVersion;
55      }
56  
57      /**
58       * Creates an instance of this object.
59       */
60      @JsxConstructor
61      public void jsConstructor() {
62          throw JavaScriptEngine.typeErrorIllegalConstructor();
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public BrowserVersion getBrowserVersion() {
70          return browserVersion_;
71      }
72  
73      /**
74       * Returns the {@code appCodeName} property.
75       *
76       * @return the {@code appCodeName} property
77       */
78      @JsxGetter
79      public String getAppCodeName() {
80          return getBrowserVersion().getApplicationCodeName();
81      }
82  
83      /**
84       * Returns the {@code appName} property.
85       *
86       * @return the {@code appName} property
87       */
88      @JsxGetter
89      public String getAppName() {
90          return getBrowserVersion().getApplicationName();
91      }
92  
93      /**
94       * Returns the {@code appVersion} property.
95       *
96       * @return the {@code appVersion} property
97       */
98      @JsxGetter
99      public String getAppVersion() {
100         return getBrowserVersion().getApplicationVersion();
101     }
102 
103     /**
104      * Returns the language of the browser.
105      *
106      * @return the browser language
107      */
108     @JsxGetter
109     public String getLanguage() {
110         return getBrowserVersion().getBrowserLanguage();
111     }
112 
113     /**
114      * Returns the preferred languages of the browser as an array.
115      *
116      * @return the languages array
117      */
118     @JsxGetter
119     public Scriptable getLanguages() {
120         final String acceptLang = getBrowserVersion().getAcceptLanguageHeader();
121         if (StringUtils.isEmptyOrNull(acceptLang)) {
122             return JavaScriptEngine.newArray(getParentScope(), 0);
123         }
124 
125         final ArrayList<String> res = new ArrayList<>();
126         final String[] parts = StringUtils.splitAtComma(acceptLang);
127         for (final String part : parts) {
128             if (!StringUtils.isEmptyOrNull(part)) {
129                 final String lang = StringUtils.substringBefore(part, ";").trim();
130                 if (!StringUtils.isEmptyOrNull(part)) {
131                     res.add(lang);
132                 }
133             }
134         }
135 
136         return JavaScriptEngine.newArray(getParentScope(), res.toArray());
137     }
138 
139     /**
140      * Returns the {@code platform} property.
141      *
142      * @return the {@code platform} property
143      */
144     @JsxGetter
145     public String getPlatform() {
146         return getBrowserVersion().getPlatform();
147     }
148 
149     /**
150      * Returns the {@code product} property.
151      *
152      * @return the {@code product} property
153      */
154     @JsxGetter
155     public String getProduct() {
156         return "Gecko";
157     }
158 
159     /**
160      * Returns the {@code userAgent} property.
161      *
162      * @return the {@code userAgent} property
163      */
164     @JsxGetter
165     public String getUserAgent() {
166         return getBrowserVersion().getUserAgent();
167     }
168 
169     /**
170      * Returns the {@code connection} property.
171      *
172      * @return the {@code connection} property
173      */
174     @JsxGetter({CHROME, EDGE})
175     public NetworkInformation getConnection() {
176         final NetworkInformation networkInformation = new NetworkInformation();
177         networkInformation.setPrototype(getPrototype(networkInformation.getClass()));
178         networkInformation.setParentScope(getParentScope());
179         return networkInformation;
180     }
181 }