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