OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 /** | |
5 * @unrestricted | |
6 */ | |
7 Emulation.Geolocation = class { | |
8 /** | |
9 * @param {number} latitude | |
10 * @param {number} longitude | |
11 * @param {boolean} error | |
12 */ | |
13 constructor(latitude, longitude, error) { | |
14 this.latitude = latitude; | |
15 this.longitude = longitude; | |
16 this.error = error; | |
17 } | |
18 | |
19 /** | |
20 * @return {!Emulation.Geolocation} | |
21 */ | |
22 static parseSetting(value) { | |
23 if (value) { | |
24 var splitError = value.split(':'); | |
25 if (splitError.length === 2) { | |
26 var splitPosition = splitError[0].split('@'); | |
27 if (splitPosition.length === 2) | |
28 return new Emulation.Geolocation(parseFloat(splitPosition[0]), parseFl
oat(splitPosition[1]), splitError[1]); | |
29 } | |
30 } | |
31 return new Emulation.Geolocation(0, 0, false); | |
32 } | |
33 | |
34 /** | |
35 * @param {string} latitudeString | |
36 * @param {string} longitudeString | |
37 * @param {string} errorStatus | |
38 * @return {?Emulation.Geolocation} | |
39 */ | |
40 static parseUserInput(latitudeString, longitudeString, errorStatus) { | |
41 if (!latitudeString && !longitudeString) | |
42 return null; | |
43 | |
44 var isLatitudeValid = Emulation.Geolocation.latitudeValidator(latitudeString
); | |
45 var isLongitudeValid = Emulation.Geolocation.longitudeValidator(longitudeStr
ing); | |
46 | |
47 if (!isLatitudeValid && !isLongitudeValid) | |
48 return null; | |
49 | |
50 var latitude = isLatitudeValid ? parseFloat(latitudeString) : -1; | |
51 var longitude = isLongitudeValid ? parseFloat(longitudeString) : -1; | |
52 return new Emulation.Geolocation(latitude, longitude, !!errorStatus); | |
53 } | |
54 | |
55 /** | |
56 * @param {string} value | |
57 * @return {boolean} | |
58 */ | |
59 static latitudeValidator(value) { | |
60 var numValue = parseFloat(value); | |
61 return /^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && numValue >= -90 &&
numValue <= 90; | |
62 } | |
63 | |
64 /** | |
65 * @param {string} value | |
66 * @return {boolean} | |
67 */ | |
68 static longitudeValidator(value) { | |
69 var numValue = parseFloat(value); | |
70 return /^([+-]?[\d]+(\.\d+)?|[+-]?\.\d+)$/.test(value) && numValue >= -180 &
& numValue <= 180; | |
71 } | |
72 | |
73 /** | |
74 * @return {string} | |
75 */ | |
76 toSetting() { | |
77 return (typeof this.latitude === 'number' && typeof this.longitude === 'numb
er' && typeof this.error === 'string') ? | |
78 this.latitude + '@' + this.longitude + ':' + this.error : | |
79 ''; | |
80 } | |
81 | |
82 apply() { | |
83 for (var target of SDK.targetManager.targets(SDK.Target.Capability.Browser))
{ | |
84 if (this.error) { | |
85 target.emulationAgent().setGeolocationOverride(); | |
86 } else { | |
87 target.emulationAgent().setGeolocationOverride( | |
88 this.latitude, this.longitude, Emulation.Geolocation.DefaultMockAccu
racy); | |
89 } | |
90 } | |
91 } | |
92 | |
93 clear() { | |
94 for (var target of SDK.targetManager.targets(SDK.Target.Capability.Browser)) | |
95 target.emulationAgent().clearGeolocationOverride(); | |
96 } | |
97 }; | |
98 | |
99 | |
100 Emulation.Geolocation.DefaultMockAccuracy = 150; | |
OLD | NEW |