1414 */
1515package org .htmlunit .javascript .host .geo ;
1616
17- import java .io .BufferedReader ;
18- import java .io .IOException ;
19- import java .io .InputStreamReader ;
20- import java .nio .charset .Charset ;
21- import java .util .ArrayList ;
22- import java .util .Iterator ;
23- import java .util .List ;
24- import java .util .Locale ;
25-
26- import org .apache .commons .lang3 .StringUtils ;
2717import org .apache .commons .logging .Log ;
2818import org .apache .commons .logging .LogFactory ;
29- import org .htmlunit .BrowserVersion ;
30- import org .htmlunit .Page ;
3119import org .htmlunit .WebClient ;
3220import org .htmlunit .WebWindow ;
3321import org .htmlunit .corejs .javascript .Function ;
@@ -51,9 +39,8 @@ public class Geolocation extends HtmlUnitScriptable {
5139
5240 private static final Log LOG = LogFactory .getLog (Geolocation .class );
5341
54- /* Do not use this URL without Google permission! */
55- private static String PROVIDER_URL_ = "https://maps.googleapis.com/maps/api/browserlocation/json" ;
5642 private Function successHandler_ ;
43+ private Function errorHandler_ ;
5744
5845 /**
5946 * Creates an instance.
@@ -62,10 +49,11 @@ public Geolocation() {
6249 }
6350
6451 /**
65- * JavaScript constructor .
52+ * Creates an instance .
6653 */
6754 @ JsxConstructor
6855 public void jsConstructor () {
56+ throw JavaScriptEngine .reportRuntimeError ("Illegal constructor." );
6957 }
7058
7159 /**
@@ -75,9 +63,10 @@ public void jsConstructor() {
7563 * @param options optional options
7664 */
7765 @ JsxFunction
78- public void getCurrentPosition (final Function successCallback , final Object errorCallback ,
66+ public void getCurrentPosition (final Function successCallback , final Function errorCallback ,
7967 final Object options ) {
8068 successHandler_ = successCallback ;
69+ errorHandler_ = errorCallback ;
8170
8271 final WebWindow webWindow = getWindow ().getWebWindow ();
8372 if (webWindow .getWebClient ().getOptions ().isGeolocationEnabled ()) {
@@ -109,139 +98,20 @@ public void clearWatch(final int watchId) {
10998 }
11099
111100 void doGetPosition () {
112- final String os = System .getProperty ("os.name" ).toLowerCase (Locale .ROOT );
113- String wifiStringString = null ;
114- if (os .contains ("win" )) {
115- wifiStringString = getWifiStringWindows ();
116- }
117-
118- if (wifiStringString == null ) {
119- if (LOG .isErrorEnabled ()) {
120- LOG .error ("Operating System not supported: " + os );
121- }
122- }
123- else {
124- String url = PROVIDER_URL_ ;
125- if (url .contains ("?" )) {
126- url += '&' ;
127- }
128- else {
129- url += '?' ;
130- }
131- url += "browser=firefox&sensor=true" ;
132- url += wifiStringString ;
133-
134- while (url .length () >= 1900 ) {
135- url = url .substring (0 , url .lastIndexOf ("&wifi=" ));
136- }
137-
138- if (LOG .isInfoEnabled ()) {
139- LOG .info ("Invoking URL: " + url );
140- }
141-
142- try (WebClient webClient = new WebClient (BrowserVersion .FIREFOX )) {
143- final Page page = webClient .getPage (url );
144- final String content = page .getWebResponse ().getContentAsString ();
145- if (LOG .isDebugEnabled ()) {
146- LOG .debug ("Receieved Content: " + content );
147- }
148- final double latitude = Double .parseDouble (getJSONValue (content , "lat" ));
149- final double longitude = Double .parseDouble (getJSONValue (content , "lng" ));
150- final double accuracy = Double .parseDouble (getJSONValue (content , "accuracy" ));
151-
152- final GeolocationCoordinates coordinates = new GeolocationCoordinates (latitude , longitude , accuracy );
153- coordinates .setPrototype (getPrototype (coordinates .getClass ()));
154-
155- final GeolocationPosition position = new GeolocationPosition (coordinates );
156- position .setPrototype (getPrototype (position .getClass ()));
101+ final WebWindow ww = getWindow ().getWebWindow ();
102+ final WebClient webClient = ww .getWebClient ();
157103
158- final WebWindow ww = getWindow ().getWebWindow ();
159- final JavaScriptEngine jsEngine =
160- (JavaScriptEngine ) ww .getWebClient ().getJavaScriptEngine ();
161- jsEngine .callFunction ((HtmlPage ) ww .getEnclosedPage (), successHandler_ , this ,
162- getParentScope (), new Object [] {position });
163- }
164- catch (final Exception e ) {
165- LOG .error ("" , e );
166- }
167- }
168- }
169-
170- private static String getJSONValue (final String content , final String key ) {
171- final StringBuilder builder = new StringBuilder ();
172- int index = content .indexOf ("\" " + key + "\" " ) + key .length () + 2 ;
173- for (index = content .indexOf (':' , index ) + 1 ; index < content .length (); index ++) {
174- final char ch = content .charAt (index );
175- if (ch == ',' || ch == '}' ) {
176- break ;
177- }
178- builder .append (ch );
179- }
180- return builder .toString ().trim ();
181- }
104+ final org .htmlunit .WebClientOptions .Geolocation geolocation = webClient .getOptions ().getGeolocation ();
182105
183- static String getWifiStringWindows () {
184- final StringBuilder builder = new StringBuilder ();
185- try {
186- final List <String > lines = runCommand ("netsh wlan show networks mode=bssid" );
187- for (final Iterator <String > it = lines .iterator (); it .hasNext ();) {
188- String line = it .next ();
189- if (line .startsWith ("SSID " )) {
190- final String name = line .substring (line .lastIndexOf (' ' ) + 1 );
191- if (it .hasNext ()) {
192- it .next ();
193- }
194- if (it .hasNext ()) {
195- it .next ();
196- }
197- if (it .hasNext ()) {
198- it .next ();
199- }
200- while (it .hasNext ()) {
201- line = it .next ();
202- if (line .trim ().startsWith ("BSSID " )) {
203- final String mac = line .substring (line .lastIndexOf (' ' ) + 1 );
204- if (it .hasNext ()) {
205- line = it .next ().trim ();
206- if (line .startsWith ("Signal" )) {
207- final String signal = line .substring (line .lastIndexOf (' ' ) + 1 , line .length () - 1 );
208- final int signalStrength = Integer .parseInt (signal ) / 2 - 100 ;
209- builder .append ("&wifi=mac:" )
210- .append (mac .replace (':' , '-' ))
211- .append ("%7Cssid:" )
212- .append (name )
213- .append ("%7Css:" )
214- .append (signalStrength );
215- }
216- }
217- }
218- if (StringUtils .isBlank (line )) {
219- break ;
220- }
221- }
222- }
223- }
224- }
225- catch (final IOException e ) {
226- //
227- }
228- return builder .toString ();
229- }
106+ final GeolocationCoordinates coordinates = new GeolocationCoordinates (
107+ geolocation .getLatitude (), geolocation .getLongitude (), geolocation .getAccuracy ());
108+ coordinates .setPrototype (getPrototype (coordinates .getClass ()));
230109
231- private static List <String > runCommand (final String command ) throws IOException {
232- final List <String > list = new ArrayList <>();
233- final Process p = Runtime .getRuntime ().exec (command );
234- try (BufferedReader reader = new BufferedReader (
235- new InputStreamReader (p .getInputStream (), Charset .defaultCharset ()))) {
236- String line ;
237- while ((line = reader .readLine ()) != null ) {
238- list .add (line );
239- }
240- }
241- return list ;
242- }
110+ final GeolocationPosition position = new GeolocationPosition (coordinates );
111+ position .setPrototype (getPrototype (position .getClass ()));
243112
244- static void setProviderUrl (final String url ) {
245- PROVIDER_URL_ = url ;
113+ final JavaScriptEngine jsEngine = (JavaScriptEngine ) ww .getWebClient ().getJavaScriptEngine ();
114+ jsEngine .callFunction ((HtmlPage ) ww .getEnclosedPage (), successHandler_ , this ,
115+ getParentScope (), new Object [] {position });
246116 }
247117}
0 commit comments