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 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   * A JavaScript object for WorkerLocation.
28   *
29   * @author Ronald Brill
30   */
31  @JsxClass
32  public class WorkerLocation extends HtmlUnitScriptable {
33  
34      private final URL url_;
35      private final String origin_;
36  
37      /**
38       * Default constructor.
39       */
40      public WorkerLocation() {
41          // prototype constructor
42          super();
43          url_ = null;
44          origin_ = null;
45      }
46  
47      WorkerLocation(final URL url, final String origin) {
48          url_ = url;
49          origin_ = origin;
50      }
51  
52      /**
53       * JavaScript constructor.
54       */
55      @JsxConstructor
56      public void jsConstructor() {
57          throw JavaScriptEngine.typeErrorIllegalConstructor();
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public Object getDefaultValue(final Class<?> hint) {
65          if (url_ != null && (hint == null || String.class.equals(hint))) {
66              return getHref();
67          }
68          return super.getDefaultValue(hint);
69      }
70  
71      /**
72       * @return a string containing the serialized URL for the worker's location.
73       */
74      @JsxGetter
75      public String getHref() {
76          String s = url_.toExternalForm();
77          if (s.startsWith("file:/") && !s.startsWith("file:///")) {
78              // Java (sometimes?) returns file URLs with a single slash; however, browsers return
79              // three slashes. See http://www.cyanwerks.com/file-url-formats.html for more info.
80              s = "file:///" + s.substring("file:/".length());
81          }
82          return s;
83      }
84  
85      /**
86       * @return the protocol portion of the location URL, including the trailing ':'
87       */
88      @JsxGetter
89      public String getProtocol() {
90          return url_.getProtocol() + ":";
91      }
92  
93      /**
94       * @return the host portion of the location URL
95       */
96      @JsxGetter
97      public String getHost() {
98          final int port = url_.getPort();
99          final String host = url_.getHost();
100 
101         if (port == -1) {
102             return host;
103         }
104         return host + ":" + port;
105     }
106 
107     /**
108      * @return the hostname portion of the location URL
109      */
110     @JsxGetter
111     public String getHostname() {
112         return url_.getHost();
113     }
114 
115     /**
116      * @return the {@code origin} property
117      */
118     @JsxGetter
119     public String getOrigin() {
120         return origin_;
121     }
122 
123     /**
124      * @return the port portion of the location URL
125      */
126     @JsxGetter
127     public String getPort() {
128         final int port = url_.getPort();
129         if (port == -1) {
130             return "";
131         }
132         return Integer.toString(port);
133     }
134 
135     /**
136      * @return the pathname portion of the location URL
137      */
138     @JsxGetter
139     public String getPathname() {
140         if (UrlUtils.URL_ABOUT_BLANK == url_) {
141             return "blank";
142         }
143         return url_.getPath();
144     }
145 
146     /**
147      * @return the search portion of the location URL
148      */
149     @JsxGetter
150     public String getSearch() {
151         final String search = url_.getQuery();
152         if (search == null) {
153             return "";
154         }
155         return "?" + search;
156     }
157 
158     /**
159      * @return the hash portion of the location URL
160      */
161     @JsxGetter
162     public String getHash() {
163         final String ref = url_.getRef();
164         return ref == null ? "" : "#" + ref;
165     }
166 }