OverridesSupport.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. */
  33. WebInspector.OverridesSupport = function()
  34. {
  35. this._overridesActive = WebInspector.settings.enableOverridesOnStartup.get();
  36. this._updateAllOverrides();
  37. WebInspector.settings.overrideUserAgent.addChangeListener(this._userAgentChanged, this);
  38. WebInspector.settings.userAgent.addChangeListener(this._userAgentChanged, this);
  39. WebInspector.settings.overrideDeviceMetrics.addChangeListener(this._deviceMetricsChanged, this);
  40. WebInspector.settings.deviceMetrics.addChangeListener(this._deviceMetricsChanged, this);
  41. WebInspector.settings.deviceFitWindow.addChangeListener(this._deviceMetricsChanged, this);
  42. WebInspector.settings.overrideGeolocation.addChangeListener(this._geolocationPositionChanged, this);
  43. WebInspector.settings.geolocationOverride.addChangeListener(this._geolocationPositionChanged, this);
  44. WebInspector.settings.overrideDeviceOrientation.addChangeListener(this._deviceOrientationChanged, this);
  45. WebInspector.settings.deviceOrientationOverride.addChangeListener(this._deviceOrientationChanged, this);
  46. WebInspector.settings.emulateTouchEvents.addChangeListener(this._emulateTouchEventsChanged, this);
  47. WebInspector.settings.overrideCSSMedia.addChangeListener(this._cssMediaChanged, this);
  48. WebInspector.settings.emulatedCSSMedia.addChangeListener(this._cssMediaChanged, this);
  49. }
  50. /**
  51. * @constructor
  52. * @param {number} width
  53. * @param {number} height
  54. * @param {number} fontScaleFactor
  55. */
  56. WebInspector.OverridesSupport.DeviceMetrics = function(width, height, fontScaleFactor)
  57. {
  58. this.width = width;
  59. this.height = height;
  60. this.fontScaleFactor = fontScaleFactor;
  61. }
  62. /**
  63. * @return {WebInspector.OverridesSupport.DeviceMetrics}
  64. */
  65. WebInspector.OverridesSupport.DeviceMetrics.parseSetting = function(value)
  66. {
  67. if (value) {
  68. var splitMetrics = value.split("x");
  69. if (splitMetrics.length === 3)
  70. return new WebInspector.OverridesSupport.DeviceMetrics(parseInt(splitMetrics[0], 10), parseInt(splitMetrics[1], 10), parseFloat(splitMetrics[2]));
  71. }
  72. return new WebInspector.OverridesSupport.DeviceMetrics(0, 0, 1);
  73. }
  74. /**
  75. * @return {?WebInspector.OverridesSupport.DeviceMetrics}
  76. */
  77. WebInspector.OverridesSupport.DeviceMetrics.parseUserInput = function(widthString, heightString, fontScaleFactorString)
  78. {
  79. function isUserInputValid(value, isInteger)
  80. {
  81. if (!value)
  82. return true;
  83. return isInteger ? /^[0]*[1-9][\d]*$/.test(value) : /^[0]*([1-9][\d]*(\.\d+)?|\.\d+)$/.test(value);
  84. }
  85. if (!widthString ^ !heightString)
  86. return null;
  87. var isWidthValid = isUserInputValid(widthString, true);
  88. var isHeightValid = isUserInputValid(heightString, true);
  89. var isFontScaleFactorValid = isUserInputValid(fontScaleFactorString, false);
  90. if (!isWidthValid && !isHeightValid && !isFontScaleFactorValid)
  91. return null;
  92. var width = isWidthValid ? parseInt(widthString || "0", 10) : -1;
  93. var height = isHeightValid ? parseInt(heightString || "0", 10) : -1;
  94. var fontScaleFactor = isFontScaleFactorValid ? parseFloat(fontScaleFactorString) : -1;
  95. return new WebInspector.OverridesSupport.DeviceMetrics(width, height, fontScaleFactor);
  96. }
  97. WebInspector.OverridesSupport.DeviceMetrics.prototype = {
  98. /**
  99. * @return {boolean}
  100. */
  101. isValid: function()
  102. {
  103. return this.isWidthValid() && this.isHeightValid() && this.isFontScaleFactorValid();
  104. },
  105. /**
  106. * @return {boolean}
  107. */
  108. isWidthValid: function()
  109. {
  110. return this.width >= 0;
  111. },
  112. /**
  113. * @return {boolean}
  114. */
  115. isHeightValid: function()
  116. {
  117. return this.height >= 0;
  118. },
  119. /**
  120. * @return {boolean}
  121. */
  122. isFontScaleFactorValid: function()
  123. {
  124. return this.fontScaleFactor > 0;
  125. },
  126. /**
  127. * @return {string}
  128. */
  129. toSetting: function()
  130. {
  131. if (!this.isValid())
  132. return "";
  133. return this.width && this.height ? this.width + "x" + this.height + "x" + this.fontScaleFactor : "";
  134. },
  135. /**
  136. * @return {string}
  137. */
  138. widthToInput: function()
  139. {
  140. return this.isWidthValid() && this.width ? String(this.width) : "";
  141. },
  142. /**
  143. * @return {string}
  144. */
  145. heightToInput: function()
  146. {
  147. return this.isHeightValid() && this.height ? String(this.height) : "";
  148. },
  149. /**
  150. * @return {string}
  151. */
  152. fontScaleFactorToInput: function()
  153. {
  154. return this.isFontScaleFactorValid() && this.fontScaleFactor ? String(this.fontScaleFactor) : "";
  155. }
  156. }
  157. /**
  158. * @constructor
  159. * @param {number} latitude
  160. * @param {number} longitude
  161. */
  162. WebInspector.OverridesSupport.GeolocationPosition = function(latitude, longitude, error)
  163. {
  164. this.latitude = latitude;
  165. this.longitude = longitude;
  166. this.error = error;
  167. }
  168. WebInspector.OverridesSupport.GeolocationPosition.prototype = {
  169. /**
  170. * @return {string}
  171. */
  172. toSetting: function()
  173. {
  174. return (typeof this.latitude === "number" && typeof this.longitude === "number" && typeof this.error === "string") ? this.latitude + "@" + this.longitude + ":" + this.error : "";
  175. }
  176. }
  177. /**
  178. * @return {WebInspector.OverridesSupport.GeolocationPosition}
  179. */
  180. WebInspector.OverridesSupport.GeolocationPosition.parseSetting = function(value)
  181. {
  182. if (value) {
  183. var splitError = value.split(":");
  184. if (splitError.length === 2) {
  185. var splitPosition = splitError[0].split("@")
  186. if (splitPosition.length === 2)
  187. return new WebInspector.OverridesSupport.GeolocationPosition(parseFloat(splitPosition[0]), parseFloat(splitPosition[1]), splitError[1]);
  188. }
  189. }
  190. return new WebInspector.OverridesSupport.GeolocationPosition(0, 0, "");
  191. }
  192. /**
  193. * @return {?WebInspector.OverridesSupport.GeolocationPosition}
  194. */
  195. WebInspector.OverridesSupport.GeolocationPosition.parseUserInput = function(latitudeString, longitudeString, errorStatus)
  196. {
  197. function isUserInputValid(value)
  198. {
  199. if (!value)
  200. return true;
  201. return /^[-]?[0-9]*[.]?[0-9]*$/.test(value);
  202. }
  203. if (!latitudeString ^ !latitudeString)
  204. return null;
  205. var isLatitudeValid = isUserInputValid(latitudeString);
  206. var isLongitudeValid = isUserInputValid(longitudeString);
  207. if (!isLatitudeValid && !isLongitudeValid)
  208. return null;
  209. var latitude = isLatitudeValid ? parseFloat(latitudeString) : -1;
  210. var longitude = isLongitudeValid ? parseFloat(longitudeString) : -1;
  211. return new WebInspector.OverridesSupport.GeolocationPosition(latitude, longitude, errorStatus ? "PositionUnavailable" : "");
  212. }
  213. WebInspector.OverridesSupport.GeolocationPosition.clearGeolocationOverride = function()
  214. {
  215. PageAgent.clearGeolocationOverride();
  216. }
  217. /**
  218. * @constructor
  219. * @param {number} alpha
  220. * @param {number} beta
  221. * @param {number} gamma
  222. */
  223. WebInspector.OverridesSupport.DeviceOrientation = function(alpha, beta, gamma)
  224. {
  225. this.alpha = alpha;
  226. this.beta = beta;
  227. this.gamma = gamma;
  228. }
  229. WebInspector.OverridesSupport.DeviceOrientation.prototype = {
  230. /**
  231. * @return {string}
  232. */
  233. toSetting: function()
  234. {
  235. return JSON.stringify(this);
  236. }
  237. }
  238. /**
  239. * @return {WebInspector.OverridesSupport.DeviceOrientation}
  240. */
  241. WebInspector.OverridesSupport.DeviceOrientation.parseSetting = function(value)
  242. {
  243. if (value) {
  244. var jsonObject = JSON.parse(value);
  245. return new WebInspector.OverridesSupport.DeviceOrientation(jsonObject.alpha, jsonObject.beta, jsonObject.gamma);
  246. }
  247. return new WebInspector.OverridesSupport.DeviceOrientation(0, 0, 0);
  248. }
  249. /**
  250. * @return {?WebInspector.OverridesSupport.DeviceOrientation}
  251. */
  252. WebInspector.OverridesSupport.DeviceOrientation.parseUserInput = function(alphaString, betaString, gammaString)
  253. {
  254. function isUserInputValid(value)
  255. {
  256. if (!value)
  257. return true;
  258. return /^[-]?[0-9]*[.]?[0-9]*$/.test(value);
  259. }
  260. if (!alphaString ^ !betaString ^ !gammaString)
  261. return null;
  262. var isAlphaValid = isUserInputValid(alphaString);
  263. var isBetaValid = isUserInputValid(betaString);
  264. var isGammaValid = isUserInputValid(gammaString);
  265. if (!isAlphaValid && !isBetaValid && !isGammaValid)
  266. return null;
  267. var alpha = isAlphaValid ? parseFloat(alphaString) : -1;
  268. var beta = isBetaValid ? parseFloat(betaString) : -1;
  269. var gamma = isGammaValid ? parseFloat(gammaString) : -1;
  270. return new WebInspector.OverridesSupport.DeviceOrientation(alpha, beta, gamma);
  271. }
  272. WebInspector.OverridesSupport.DeviceOrientation.clearDeviceOrientationOverride = function()
  273. {
  274. PageAgent.clearDeviceOrientationOverride();
  275. }
  276. WebInspector.OverridesSupport.prototype = {
  277. setOverridesActive: function(enabled)
  278. {
  279. if (this._overridesActive === enabled)
  280. return;
  281. this._overridesActive = enabled;
  282. this._updateAllOverrides();
  283. },
  284. _updateAllOverrides: function()
  285. {
  286. this._userAgentChanged();
  287. this._deviceMetricsChanged();
  288. this._deviceOrientationChanged();
  289. this._geolocationPositionChanged();
  290. this._emulateTouchEventsChanged();
  291. this._cssMediaChanged();
  292. },
  293. _userAgentChanged: function()
  294. {
  295. NetworkAgent.setUserAgentOverride(this._overridesActive && WebInspector.settings.overrideUserAgent.get() ? WebInspector.settings.userAgent.get() : "");
  296. },
  297. _deviceMetricsChanged: function()
  298. {
  299. var metrics = WebInspector.OverridesSupport.DeviceMetrics.parseSetting(this._overridesActive && WebInspector.settings.overrideDeviceMetrics.get() ? WebInspector.settings.deviceMetrics.get() : "");
  300. if (metrics.isValid())
  301. PageAgent.setDeviceMetricsOverride(metrics.width, metrics.height, metrics.fontScaleFactor, WebInspector.settings.deviceFitWindow.get());
  302. },
  303. _geolocationPositionChanged: function()
  304. {
  305. if (!this._overridesActive || !WebInspector.settings.overrideGeolocation.get()) {
  306. PageAgent.clearGeolocationOverride();
  307. return;
  308. }
  309. var geolocation = WebInspector.OverridesSupport.GeolocationPosition.parseSetting(WebInspector.settings.geolocationOverride.get());
  310. if (geolocation.error)
  311. PageAgent.setGeolocationOverride();
  312. else
  313. PageAgent.setGeolocationOverride(geolocation.latitude, geolocation.longitude, 150);
  314. },
  315. _deviceOrientationChanged: function()
  316. {
  317. if (!this._overridesActive || !WebInspector.settings.overrideDeviceOrientation.get()) {
  318. PageAgent.clearDeviceOrientationOverride();
  319. return;
  320. }
  321. var deviceOrientation = WebInspector.OverridesSupport.DeviceOrientation.parseSetting(WebInspector.settings.deviceOrientationOverride.get());
  322. PageAgent.setDeviceOrientationOverride(deviceOrientation.alpha, deviceOrientation.beta, deviceOrientation.gamma);
  323. },
  324. _emulateTouchEventsChanged: function()
  325. {
  326. WebInspector.domAgent.emulateTouchEventObjects(this._overridesActive && WebInspector.settings.emulateTouchEvents.get());
  327. },
  328. _cssMediaChanged: function()
  329. {
  330. PageAgent.setEmulatedMedia(this._overridesActive && WebInspector.settings.overrideCSSMedia.get() ? WebInspector.settings.emulatedCSSMedia.get() : "");
  331. WebInspector.cssModel.mediaQueryResultChanged();
  332. }
  333. }
  334. /**
  335. * @type {WebInspector.OverridesSupport}
  336. */
  337. WebInspector.overridesSupport;