Chromium Code Reviews| 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 {string} 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 === WebInspector.Geolocation.Error.None) |
| 31 target.emulationAgent().setGeolocationOverride(this.latitude, th is.longitude, WebInspector.Geolocation.DefaultMockAccuracy); | |
| 32 else | |
| 31 target.emulationAgent().setGeolocationOverride(); | 33 target.emulationAgent().setGeolocationOverride(); |
| 32 else | |
| 33 target.emulationAgent().setGeolocationOverride(this.latitude, th is.longitude, 150); | |
| 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) |
|
lushnikov
2016/04/25 23:33:04
lets add jsdoc while we're here
luoe
2016/04/26 02:16:23
Done.
| |
| 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, WebInspector.Geolocation.Error.Non e); |
| 59 } | 58 } |
| 60 | 59 |
| 61 /** | 60 /** |
| 62 * @return {?WebInspector.Geolocation} | 61 * @return {?WebInspector.Geolocation} |
| 63 */ | 62 */ |
| 64 WebInspector.Geolocation.parseUserInput = function(latitudeString, longitudeStri ng, errorStatus) | 63 WebInspector.Geolocation.parseUserInput = function(latitudeString, longitudeStri ng, errorStatus) |
|
lushnikov
2016/04/25 23:33:04
lets add jsdoc while we are here
luoe
2016/04/26 02:16:23
Done.
| |
| 65 { | 64 { |
| 66 if (!latitudeString && !longitudeString) | 65 if (!latitudeString && !longitudeString) |
| 67 return null; | 66 return null; |
| 68 | 67 |
| 69 var isLatitudeValid = WebInspector.Geolocation.latitudeValidator(latitudeStr ing); | 68 var isLatitudeValid = WebInspector.Geolocation.latitudeValidator(latitudeStr ing); |
| 70 var isLongitudeValid = WebInspector.Geolocation.longitudeValidator(longitude String); | 69 var isLongitudeValid = WebInspector.Geolocation.longitudeValidator(longitude String); |
| 71 | 70 |
| 72 if (!isLatitudeValid && !isLongitudeValid) | 71 if (!isLatitudeValid && !isLongitudeValid) |
| 73 return null; | 72 return null; |
| 74 | 73 |
| 75 var latitude = isLatitudeValid ? parseFloat(latitudeString) : -1; | 74 var latitude = isLatitudeValid ? parseFloat(latitudeString) : -1; |
| 76 var longitude = isLongitudeValid ? parseFloat(longitudeString) : -1; | 75 var longitude = isLongitudeValid ? parseFloat(longitudeString) : -1; |
| 77 | 76 var error = errorStatus ? WebInspector.Geolocation.Error.PositionUnavailable : WebInspector.Geolocation.Error.None; |
| 78 return new WebInspector.Geolocation(latitude, longitude, errorStatus ? "Posi tionUnavailable" : ""); | 77 return new WebInspector.Geolocation(latitude, longitude, error); |
| 79 } | 78 } |
| 80 | 79 |
| 81 /** | 80 /** |
| 82 * @param {string} value | 81 * @param {string} value |
| 83 * @return {boolean} | 82 * @return {boolean} |
| 84 */ | 83 */ |
| 85 WebInspector.Geolocation.latitudeValidator = function(value) | 84 WebInspector.Geolocation.latitudeValidator = function(value) |
| 86 { | 85 { |
| 87 return !value || (/^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && value > = -90 && value <= 90); | 86 return !value || (/^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && value > = -90 && value <= 90); |
| 88 } | 87 } |
| 89 | 88 |
| 90 /** | 89 /** |
| 91 * @param {string} value | 90 * @param {string} value |
| 92 * @return {boolean} | 91 * @return {boolean} |
| 93 */ | 92 */ |
| 94 WebInspector.Geolocation.longitudeValidator = function(value) | 93 WebInspector.Geolocation.longitudeValidator = function(value) |
| 95 { | 94 { |
| 96 return !value || (/^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && value > = -180 && value <= 180); | 95 return !value || (/^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && value > = -180 && value <= 180); |
| 97 } | 96 } |
| 97 | |
| 98 /** @enum {string} */ | |
| 99 WebInspector.Geolocation.Error = { | |
|
lushnikov
2016/04/25 23:33:04
i think having the enum is a bit of overkill here.
luoe
2016/04/26 02:16:23
Done.
| |
| 100 None: "None", | |
| 101 PositionUnavailable: "PositionUnavailable" | |
| 102 } | |
| 103 | |
| 104 WebInspector.Geolocation.DefaultMockAccuracy = 150; | |
| OLD | NEW |