OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @constructor | 6 * @constructor |
7 * @param {number} latitude | 7 * @param {number} latitude |
8 * @param {number} longitude | 8 * @param {number} longitude |
9 * @param {string} error | 9 * @param {boolean} error |
10 */ | 10 */ |
11 WebInspector.Geolocation = function(latitude, longitude, error) | 11 WebInspector.Geolocation = function(latitude, longitude, error) |
12 { | 12 { |
13 this.latitude = latitude; | 13 this.latitude = latitude; |
14 this.longitude = longitude; | 14 this.longitude = longitude; |
15 this.error = error; | 15 this.error = error; |
16 } | 16 } |
17 | 17 |
18 WebInspector.Geolocation.prototype = { | 18 WebInspector.Geolocation.prototype = { |
19 /** | 19 /** |
20 * @return {string} | 20 * @return {string} |
21 */ | 21 */ |
22 toSetting: function() | 22 toSetting: function() |
23 { | 23 { |
24 return (typeof this.latitude === "number" && typeof this.longitude === "
number" && typeof this.error === "string") ? this.latitude + "@" + this.longitud
e + ":" + this.error : ""; | 24 return (typeof this.latitude === "number" && typeof this.longitude === "
number" && typeof this.error === "string") ? this.latitude + "@" + this.longitud
e + ":" + this.error : ""; |
25 }, | 25 }, |
26 | 26 |
27 apply: function() | 27 apply: function() |
28 { | 28 { |
29 for (var target of WebInspector.targetManager.targets(WebInspector.Targe
t.Type.Page)) { | 29 for (var target of WebInspector.targetManager.targets(WebInspector.Targe
t.Type.Page)) { |
30 if (this.error) | 30 if (this.error) |
31 target.emulationAgent().setGeolocationOverride(); | 31 target.emulationAgent().setGeolocationOverride(); |
32 else | 32 else |
33 target.emulationAgent().setGeolocationOverride(this.latitude, th
is.longitude, 150); | 33 target.emulationAgent().setGeolocationOverride(this.latitude, th
is.longitude, WebInspector.Geolocation.DefaultMockAccuracy); |
34 | |
35 } | 34 } |
36 }, | 35 }, |
37 | 36 |
38 clear: function() | 37 clear: function() |
39 { | 38 { |
40 for (var target of WebInspector.targetManager.targets(WebInspector.Targe
t.Type.Page)) | 39 for (var target of WebInspector.targetManager.targets(WebInspector.Targe
t.Type.Page)) |
41 target.emulationAgent().clearGeolocationOverride(); | 40 target.emulationAgent().clearGeolocationOverride(); |
42 } | 41 } |
43 } | 42 } |
44 | 43 |
45 /** | 44 /** |
46 * @return {!WebInspector.Geolocation} | 45 * @return {!WebInspector.Geolocation} |
47 */ | 46 */ |
48 WebInspector.Geolocation.parseSetting = function(value) | 47 WebInspector.Geolocation.parseSetting = function(value) |
49 { | 48 { |
50 if (value) { | 49 if (value) { |
51 var splitError = value.split(":"); | 50 var splitError = value.split(":"); |
52 if (splitError.length === 2) { | 51 if (splitError.length === 2) { |
53 var splitPosition = splitError[0].split("@"); | 52 var splitPosition = splitError[0].split("@"); |
54 if (splitPosition.length === 2) | 53 if (splitPosition.length === 2) |
55 return new WebInspector.Geolocation(parseFloat(splitPosition[0])
, parseFloat(splitPosition[1]), splitError[1]); | 54 return new WebInspector.Geolocation(parseFloat(splitPosition[0])
, parseFloat(splitPosition[1]), splitError[1]); |
56 } | 55 } |
57 } | 56 } |
58 return new WebInspector.Geolocation(0, 0, ""); | 57 return new WebInspector.Geolocation(0, 0, false); |
59 } | 58 } |
60 | 59 |
61 /** | 60 /** |
| 61 * @param {string} latitudeString |
| 62 * @param {string} longitudeString |
| 63 * @param {string} errorStatus |
62 * @return {?WebInspector.Geolocation} | 64 * @return {?WebInspector.Geolocation} |
63 */ | 65 */ |
64 WebInspector.Geolocation.parseUserInput = function(latitudeString, longitudeStri
ng, errorStatus) | 66 WebInspector.Geolocation.parseUserInput = function(latitudeString, longitudeStri
ng, errorStatus) |
65 { | 67 { |
66 if (!latitudeString && !longitudeString) | 68 if (!latitudeString && !longitudeString) |
67 return null; | 69 return null; |
68 | 70 |
69 var isLatitudeValid = WebInspector.Geolocation.latitudeValidator(latitudeStr
ing); | 71 var isLatitudeValid = WebInspector.Geolocation.latitudeValidator(latitudeStr
ing); |
70 var isLongitudeValid = WebInspector.Geolocation.longitudeValidator(longitude
String); | 72 var isLongitudeValid = WebInspector.Geolocation.longitudeValidator(longitude
String); |
71 | 73 |
72 if (!isLatitudeValid && !isLongitudeValid) | 74 if (!isLatitudeValid && !isLongitudeValid) |
73 return null; | 75 return null; |
74 | 76 |
75 var latitude = isLatitudeValid ? parseFloat(latitudeString) : -1; | 77 var latitude = isLatitudeValid ? parseFloat(latitudeString) : -1; |
76 var longitude = isLongitudeValid ? parseFloat(longitudeString) : -1; | 78 var longitude = isLongitudeValid ? parseFloat(longitudeString) : -1; |
77 | 79 return new WebInspector.Geolocation(latitude, longitude, !!errorStatus); |
78 return new WebInspector.Geolocation(latitude, longitude, errorStatus ? "Posi
tionUnavailable" : ""); | |
79 } | 80 } |
80 | 81 |
81 /** | 82 /** |
82 * @param {string} value | 83 * @param {string} value |
83 * @return {boolean} | 84 * @return {boolean} |
84 */ | 85 */ |
85 WebInspector.Geolocation.latitudeValidator = function(value) | 86 WebInspector.Geolocation.latitudeValidator = function(value) |
86 { | 87 { |
87 return !value || (/^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && value >
= -90 && value <= 90); | 88 return !value || (/^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && value >
= -90 && value <= 90); |
88 } | 89 } |
89 | 90 |
90 /** | 91 /** |
91 * @param {string} value | 92 * @param {string} value |
92 * @return {boolean} | 93 * @return {boolean} |
93 */ | 94 */ |
94 WebInspector.Geolocation.longitudeValidator = function(value) | 95 WebInspector.Geolocation.longitudeValidator = function(value) |
95 { | 96 { |
96 return !value || (/^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && value >
= -180 && value <= 180); | 97 return !value || (/^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && value >
= -180 && value <= 180); |
97 } | 98 } |
| 99 |
| 100 WebInspector.Geolocation.DefaultMockAccuracy = 150; |
OLD | NEW |