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 java.net.URL;
18  
19  import org.htmlunit.javascript.HtmlUnitScriptable;
20  import org.htmlunit.javascript.JavaScriptEngine;
21  import org.htmlunit.javascript.configuration.JsxClass;
22  import org.htmlunit.javascript.configuration.JsxConstructor;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  import org.htmlunit.util.UrlUtils;
25  
26  /**
27   * JavaScript host object for {@code WorkerLocation}.
28   *
29   * @author Ronald Brill
30   *
31   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/WorkerLocation">MDN Documentation</a>
32   */
33  @JsxClass
34  public class WorkerLocation extends HtmlUnitScriptable {
35  
36      private final URL url_;
37      private final String origin_;
38  
39      /**
40       * Default constructor.
41       */
42      public WorkerLocation() {
43          // prototype constructor
44          super();
45          url_ = null;
46          origin_ = null;
47      }
48  
49      WorkerLocation(final URL url, final String origin) {
50          url_ = url;
51          origin_ = origin;
52      }
53  
54      /**
55       * Creates an instance of this object.
56       */
57      @JsxConstructor
58      public void jsConstructor() {
59          throw JavaScriptEngine.typeErrorIllegalConstructor();
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public Object getDefaultValue(final Class<?> hint) {
67          if (url_ != null && (hint == null || String.class.equals(hint))) {
68              return getHref();
69          }
70          return super.getDefaultValue(hint);
71      }
72  
73      /**
74       * Returns the serialized URL for the worker's location.
75       *
76       * @return a string containing the serialized URL
77       */
78      @JsxGetter
79      public String getHref() {
80          String s = url_.toExternalForm();
81          if (s.startsWith("file:/") && !s.startsWith("file:///")) {
82              // Java (sometimes?) returns file URLs with a single slash; however, browsers return
83              // three slashes. See http://www.cyanwerks.com/file-url-formats.html for more info.
84              s = "file:///" + s.substring("file:/".length());
85          }
86          return s;
87      }
88  
89      /**
90       * Returns the protocol portion of the location URL, including the trailing {@code :}.
91       *
92       * @return the protocol
93       */
94      @JsxGetter
95      public String getProtocol() {
96          return url_.getProtocol() + ":";
97      }
98  
99      /**
100      * Returns the host portion of the location URL.
101      *
102      * @return the host, including the port if non-default
103      */
104     @JsxGetter
105     public String getHost() {
106         final int port = url_.getPort();
107         final String host = url_.getHost();
108 
109         if (port == -1) {
110             return host;
111         }
112         return host + ":" + port;
113     }
114 
115     /**
116      * Returns the hostname portion of the location URL.
117      *
118      * @return the hostname
119      */
120     @JsxGetter
121     public String getHostname() {
122         return url_.getHost();
123     }
124 
125     /**
126      * Returns the {@code origin} property.
127      *
128      * @return the origin
129      */
130     @JsxGetter
131     public String getOrigin() {
132         return origin_;
133     }
134 
135     /**
136      * Returns the port portion of the location URL.
137      *
138      * @return the port as a string, or an empty string if no port is specified
139      */
140     @JsxGetter
141     public String getPort() {
142         final int port = url_.getPort();
143         if (port == -1) {
144             return "";
145         }
146         return Integer.toString(port);
147     }
148 
149     /**
150      * Returns the pathname portion of the location URL.
151      *
152      * @return the pathname
153      */
154     @JsxGetter
155     public String getPathname() {
156         if (UrlUtils.URL_ABOUT_BLANK == url_) {
157             return "blank";
158         }
159         return url_.getPath();
160     }
161 
162     /**
163      * Returns the search portion of the location URL.
164      *
165      * @return the query string, prefixed with {@code ?}, or an empty string if none
166      */
167     @JsxGetter
168     public String getSearch() {
169         final String search = url_.getQuery();
170         if (search == null) {
171             return "";
172         }
173         return "?" + search;
174     }
175 
176     /**
177      * Returns the hash portion of the location URL.
178      *
179      * @return the fragment identifier, prefixed with {@code #}, or an empty string if none
180      */
181     @JsxGetter
182     public String getHash() {
183         final String ref = url_.getRef();
184         return ref == null ? "" : "#" + ref;
185     }
186 }