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.geo;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.htmlunit.WebClient;
20  import org.htmlunit.WebWindow;
21  import org.htmlunit.corejs.javascript.Function;
22  import org.htmlunit.html.HtmlPage;
23  import org.htmlunit.javascript.HtmlUnitScriptable;
24  import org.htmlunit.javascript.JavaScriptEngine;
25  import org.htmlunit.javascript.background.BackgroundJavaScriptFactory;
26  import org.htmlunit.javascript.background.JavaScriptJob;
27  import org.htmlunit.javascript.configuration.JsxClass;
28  import org.htmlunit.javascript.configuration.JsxConstructor;
29  import org.htmlunit.javascript.configuration.JsxFunction;
30  
31  /**
32   * A JavaScript object for Geolocation.
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   */
37  @JsxClass
38  public class Geolocation extends HtmlUnitScriptable {
39  
40      private static final Log LOG = LogFactory.getLog(Geolocation.class);
41  
42      private Function successHandler_;
43      private Function errorHandler_;
44  
45      /**
46       * Creates an instance.
47       */
48      @JsxConstructor
49      public void jsConstructor() {
50          throw JavaScriptEngine.typeErrorIllegalConstructor();
51      }
52  
53      /**
54       * Gets the current position.
55       * @param successCallback success callback
56       * @param errorCallback optional error callback
57       * @param options optional options
58       */
59      @JsxFunction
60      public void getCurrentPosition(final Function successCallback, final Function errorCallback,
61              final Object options) {
62          successHandler_ = successCallback;
63          errorHandler_ = errorCallback;
64  
65          final WebWindow webWindow = getWindow().getWebWindow();
66          if (webWindow.getWebClient().getOptions().isGeolocationEnabled()) {
67              final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory()
68                      .createJavaScriptJob(0, null, () -> doGetPosition());
69              webWindow.getJobManager().addJob(job, webWindow.getEnclosedPage());
70          }
71      }
72  
73      /**
74       * Notifies the callbacks whenever the position changes, till clearWatch() is called.
75       * @param successCallback success callback
76       * @param errorCallback optional error callback
77       * @param options optional options
78       * @return the watch id
79       */
80      @JsxFunction
81      public int watchPosition(final Function successCallback, final Object errorCallback,
82              final Object options) {
83          return 0;
84      }
85  
86      /**
87       * Clears the specified watch ID.
88       * @param watchId the watch id
89       */
90      @JsxFunction
91      public void clearWatch(final int watchId) {
92          // nothing to do
93      }
94  
95      void doGetPosition() {
96          final WebWindow ww = getWindow().getWebWindow();
97          final WebClient webClient = ww.getWebClient();
98  
99          final org.htmlunit.WebClientOptions.Geolocation geolocation = webClient.getOptions().getGeolocation();
100 
101         final GeolocationCoordinates coordinates = new GeolocationCoordinates(
102                 geolocation.getLatitude(), geolocation.getLongitude(), geolocation.getAccuracy());
103         coordinates.setPrototype(getPrototype(coordinates.getClass()));
104 
105         final GeolocationPosition position = new GeolocationPosition(coordinates);
106         position.setPrototype(getPrototype(position.getClass()));
107 
108         final JavaScriptEngine jsEngine = (JavaScriptEngine) ww.getWebClient().getJavaScriptEngine();
109         jsEngine.callFunction((HtmlPage) ww.getEnclosedPage(), successHandler_, this,
110                 getParentScope(), new Object[] {position});
111     }
112 }