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