OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 18 matching lines...) Expand all Loading... |
29 */ | 29 */ |
30 | 30 |
31 /** | 31 /** |
32 * @constructor | 32 * @constructor |
33 * @implements {WebInspector.TargetManager.Observer} | 33 * @implements {WebInspector.TargetManager.Observer} |
34 * @extends {WebInspector.Object} | 34 * @extends {WebInspector.Object} |
35 */ | 35 */ |
36 WebInspector.OverridesSupport = function() | 36 WebInspector.OverridesSupport = function() |
37 { | 37 { |
38 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeMod
el.EventTypes.MainFrameNavigated, this._onMainFrameNavigated.bind(this), this); | 38 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeMod
el.EventTypes.MainFrameNavigated, this._onMainFrameNavigated.bind(this), this); |
39 this._deviceMetricsOverrideEnabled = false; | 39 this._overrideDeviceResolution = false; |
40 this._emulateViewportEnabled = false; | 40 this._emulateViewportEnabled = false; |
41 this._userAgent = ""; | 41 this._userAgent = ""; |
42 this._pageResizer = null; | 42 this._pageResizer = null; |
43 WebInspector.targetManager.observeTargets(this); | 43 WebInspector.targetManager.observeTargets(this); |
44 } | 44 } |
45 | 45 |
46 WebInspector.OverridesSupport.Events = { | 46 WebInspector.OverridesSupport.Events = { |
47 OverridesWarningUpdated: "OverridesWarningUpdated", | 47 OverridesWarningUpdated: "OverridesWarningUpdated", |
48 HasActiveOverridesChanged: "HasActiveOverridesChanged", | 48 HasActiveOverridesChanged: "HasActiveOverridesChanged", |
49 } | 49 } |
(...skipping 12 matching lines...) Expand all Loading... |
62 }; | 62 }; |
63 | 63 |
64 WebInspector.OverridesSupport.PageResizer.prototype = { | 64 WebInspector.OverridesSupport.PageResizer.prototype = { |
65 /** | 65 /** |
66 * Zero width and height mean default size. | 66 * Zero width and height mean default size. |
67 * Scale should be applied to page-scale-dependent UI bits. Zero means no sc
ale. | 67 * Scale should be applied to page-scale-dependent UI bits. Zero means no sc
ale. |
68 * @param {number} dipWidth | 68 * @param {number} dipWidth |
69 * @param {number} dipHeight | 69 * @param {number} dipHeight |
70 * @param {number} scale | 70 * @param {number} scale |
71 */ | 71 */ |
72 enable: function(dipWidth, dipHeight, scale) { }, | 72 update: function(dipWidth, dipHeight, scale) { }, |
73 | |
74 disable: function() { }, | |
75 | 73 |
76 /** | 74 /** |
77 * Available size for the page. | 75 * Available size for the page. |
78 * @return {!Size} | 76 * @return {!Size} |
79 */ | 77 */ |
80 availableDipSize: function() { } | 78 availableDipSize: function() { } |
81 }; | 79 }; |
82 | 80 |
83 /** | 81 /** |
84 * @constructor | 82 * @constructor |
(...skipping 26 matching lines...) Expand all Loading... |
111 height = parseInt(splitMetrics[1], 10); | 109 height = parseInt(splitMetrics[1], 10); |
112 deviceScaleFactor = parseFloat(splitMetrics[2]); | 110 deviceScaleFactor = parseFloat(splitMetrics[2]); |
113 if (splitMetrics.length == 4) | 111 if (splitMetrics.length == 4) |
114 textAutosizing = splitMetrics[3] == 1; | 112 textAutosizing = splitMetrics[3] == 1; |
115 } | 113 } |
116 } | 114 } |
117 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device
ScaleFactor, textAutosizing); | 115 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device
ScaleFactor, textAutosizing); |
118 } | 116 } |
119 | 117 |
120 /** | 118 /** |
121 * @return {?WebInspector.OverridesSupport.DeviceMetrics} | |
122 */ | |
123 WebInspector.OverridesSupport.DeviceMetrics._parseUserInput = function(widthStri
ng, heightString, deviceScaleFactorString, textAutosizing) | |
124 { | |
125 function isUserInputValid(value, isInteger) | |
126 { | |
127 if (!value) | |
128 return true; | |
129 return isInteger ? /^[\d]+$/.test(value) : /^[\d]+(\.\d+)?|\.\d+$/.test(
value); | |
130 } | |
131 | |
132 if (!widthString ^ !heightString) | |
133 return null; | |
134 | |
135 var isWidthValid = isUserInputValid(widthString, true); | |
136 var isHeightValid = isUserInputValid(heightString, true); | |
137 var isDeviceScaleFactorValid = isUserInputValid(deviceScaleFactorString, fal
se); | |
138 | |
139 if (!isWidthValid && !isHeightValid && !isDeviceScaleFactorValid) | |
140 return null; | |
141 | |
142 var width = isWidthValid ? parseInt(widthString || "0", 10) : -1; | |
143 var height = isHeightValid ? parseInt(heightString || "0", 10) : -1; | |
144 var deviceScaleFactor = isDeviceScaleFactorValid ? parseFloat(deviceScaleFac
torString) : -1; | |
145 | |
146 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device
ScaleFactor, textAutosizing); | |
147 } | |
148 | |
149 /** | |
150 * @param {!Element} widthInput | |
151 * @param {!Element} heightInput | |
152 * @param {!Element} deviceScaleFactorInput | |
153 * @param {!Element} textAutosizingInput | |
154 */ | |
155 WebInspector.OverridesSupport.DeviceMetrics.applyOverrides = function(widthInput
, heightInput, deviceScaleFactorInput, textAutosizingInput) | |
156 { | |
157 if (WebInspector.OverridesSupport.DeviceMetrics._applyOverridesTimer) | |
158 clearTimeout(WebInspector.OverridesSupport.DeviceMetrics._applyOverrides
Timer); | |
159 WebInspector.OverridesSupport.DeviceMetrics._applyOverridesTimer = setTimeou
t(onTimer, 50); | |
160 | |
161 function onTimer() | |
162 { | |
163 delete WebInspector.OverridesSupport.DeviceMetrics._applyOverridesTimer; | |
164 var metrics = WebInspector.OverridesSupport.DeviceMetrics._parseUserInpu
t(widthInput.value.trim(), heightInput.value.trim(), deviceScaleFactorInput.valu
e.trim(), textAutosizingInput.checked); | |
165 | |
166 function setValid(condition, element) | |
167 { | |
168 if (condition) | |
169 element.classList.remove("error-input"); | |
170 else | |
171 element.classList.add("error-input"); | |
172 } | |
173 | |
174 setValid(metrics && metrics.isWidthValid(), widthInput); | |
175 setValid(metrics && metrics.isHeightValid(), heightInput); | |
176 setValid(metrics && metrics.isDeviceScaleFactorValid(), deviceScaleFacto
rInput); | |
177 | |
178 if (!metrics) | |
179 return; | |
180 | |
181 if (metrics.isValid()) { | |
182 var value = metrics.toSetting(); | |
183 if (value !== WebInspector.overridesSupport.settings.deviceMetrics.g
et()) | |
184 WebInspector.overridesSupport.settings.deviceMetrics.set(value); | |
185 } | |
186 } | |
187 } | |
188 | |
189 WebInspector.OverridesSupport.DeviceMetrics.prototype = { | |
190 /** | |
191 * @return {boolean} | |
192 */ | |
193 isValid: function() | |
194 { | |
195 return this.isWidthValid() && this.isHeightValid() && this.isDeviceScale
FactorValid(); | |
196 }, | |
197 | |
198 /** | |
199 * @return {boolean} | |
200 */ | |
201 isWidthValid: function() | |
202 { | |
203 return this.width >= 0; | |
204 }, | |
205 | |
206 /** | |
207 * @return {boolean} | |
208 */ | |
209 isHeightValid: function() | |
210 { | |
211 return this.height >= 0; | |
212 }, | |
213 | |
214 /** | |
215 * @return {boolean} | |
216 */ | |
217 isDeviceScaleFactorValid: function() | |
218 { | |
219 return this.deviceScaleFactor >= 0; | |
220 }, | |
221 | |
222 /** | |
223 * @return {string} | |
224 */ | |
225 toSetting: function() | |
226 { | |
227 if (!this.isValid()) | |
228 return ""; | |
229 | |
230 return this.width + "x" + this.height + "x" + this.deviceScaleFactor + "
x" + (this.textAutosizing ? "1" : "0"); | |
231 }, | |
232 | |
233 /** | |
234 * @return {string} | |
235 */ | |
236 widthToInput: function() | |
237 { | |
238 return this.isWidthValid() ? String(this.width) : ""; | |
239 }, | |
240 | |
241 /** | |
242 * @return {string} | |
243 */ | |
244 heightToInput: function() | |
245 { | |
246 return this.isHeightValid() ? String(this.height) : ""; | |
247 }, | |
248 | |
249 /** | |
250 * @return {string} | |
251 */ | |
252 deviceScaleFactorToInput: function() | |
253 { | |
254 return this.isDeviceScaleFactorValid() ? String(this.deviceScaleFactor)
: ""; | |
255 }, | |
256 | |
257 /** | |
258 * Compute the font scale factor. | |
259 * | |
260 * Chromium on Android uses a device scale adjustment for fonts used in text
autosizing for | |
261 * improved legibility. This function computes this adjusted value for text
autosizing. | |
262 * | |
263 * For a description of the Android device scale adjustment algorithm, see: | |
264 * chrome/browser/chrome_content_browser_client.cc, GetFontScaleMultipli
er(...) | |
265 * | |
266 * @return {number} font scale factor. | |
267 */ | |
268 fontScaleFactor: function() | |
269 { | |
270 if (this.isValid()) { | |
271 // FIXME: this works bad with zero width/height. Create utility func
tion with parameters instead. | |
272 var minWidth = Math.min(this.width, this.height) / (this.deviceScale
Factor || 1); | |
273 | |
274 var kMinFSM = 1.05; | |
275 var kWidthForMinFSM = 320; | |
276 var kMaxFSM = 1.3; | |
277 var kWidthForMaxFSM = 800; | |
278 | |
279 if (minWidth <= kWidthForMinFSM) | |
280 return kMinFSM; | |
281 if (minWidth >= kWidthForMaxFSM) | |
282 return kMaxFSM; | |
283 | |
284 // The font scale multiplier varies linearly between kMinFSM and kMa
xFSM. | |
285 var ratio = (minWidth - kWidthForMinFSM) / (kWidthForMaxFSM - kWidth
ForMinFSM); | |
286 | |
287 return ratio * (kMaxFSM - kMinFSM) + kMinFSM; | |
288 } | |
289 | |
290 return 1; | |
291 } | |
292 } | |
293 | |
294 /** | |
295 * @constructor | 119 * @constructor |
296 * @param {number} latitude | 120 * @param {number} latitude |
297 * @param {number} longitude | 121 * @param {number} longitude |
298 */ | 122 */ |
299 WebInspector.OverridesSupport.GeolocationPosition = function(latitude, longitude
, error) | 123 WebInspector.OverridesSupport.GeolocationPosition = function(latitude, longitude
, error) |
300 { | 124 { |
301 this.latitude = latitude; | 125 this.latitude = latitude; |
302 this.longitude = longitude; | 126 this.longitude = longitude; |
303 this.error = error; | 127 this.error = error; |
304 } | 128 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 var gamma = isGammaValid ? parseFloat(gammaString) : -1; | 247 var gamma = isGammaValid ? parseFloat(gammaString) : -1; |
424 | 248 |
425 return new WebInspector.OverridesSupport.DeviceOrientation(alpha, beta, gamm
a); | 249 return new WebInspector.OverridesSupport.DeviceOrientation(alpha, beta, gamm
a); |
426 } | 250 } |
427 | 251 |
428 WebInspector.OverridesSupport.DeviceOrientation.clearDeviceOrientationOverride =
function() | 252 WebInspector.OverridesSupport.DeviceOrientation.clearDeviceOrientationOverride =
function() |
429 { | 253 { |
430 PageAgent.clearDeviceOrientationOverride(); | 254 PageAgent.clearDeviceOrientationOverride(); |
431 } | 255 } |
432 | 256 |
| 257 /** |
| 258 * @param {string} value |
| 259 */ |
| 260 WebInspector.OverridesSupport.inputValidator = function(value) |
| 261 { |
| 262 if (value >= 0 && value <= 10000) |
| 263 return ""; |
| 264 return WebInspector.UIString("Value must be non-negative integer"); |
| 265 } |
| 266 |
| 267 |
433 WebInspector.OverridesSupport.prototype = { | 268 WebInspector.OverridesSupport.prototype = { |
434 /** | 269 /** |
435 * @param {?WebInspector.OverridesSupport.PageResizer} pageResizer | 270 * @param {?WebInspector.OverridesSupport.PageResizer} pageResizer |
436 */ | 271 */ |
437 setPageResizer: function(pageResizer) | 272 setPageResizer: function(pageResizer) |
438 { | 273 { |
439 if (pageResizer === this._pageResizer) | 274 if (pageResizer === this._pageResizer) |
440 return; | 275 return; |
441 | 276 |
442 if (this._pageResizer) { | 277 if (this._pageResizer) { |
443 this._pageResizer.disable(); | |
444 this._pageResizer.removeEventListener(WebInspector.OverridesSupport.
PageResizer.Events.AvailableSizeChanged, this._onPageResizerAvailableSizeChanged
, this); | 278 this._pageResizer.removeEventListener(WebInspector.OverridesSupport.
PageResizer.Events.AvailableSizeChanged, this._onPageResizerAvailableSizeChanged
, this); |
445 this._pageResizer.removeEventListener(WebInspector.OverridesSupport.
PageResizer.Events.ResizeRequested, this._onPageResizerResizeRequested, this); | 279 this._pageResizer.removeEventListener(WebInspector.OverridesSupport.
PageResizer.Events.ResizeRequested, this._onPageResizerResizeRequested, this); |
446 } | 280 } |
447 this._pageResizer = pageResizer; | 281 this._pageResizer = pageResizer; |
448 if (this._pageResizer) { | 282 if (this._pageResizer) { |
449 this._pageResizer.addEventListener(WebInspector.OverridesSupport.Pag
eResizer.Events.AvailableSizeChanged, this._onPageResizerAvailableSizeChanged, t
his); | 283 this._pageResizer.addEventListener(WebInspector.OverridesSupport.Pag
eResizer.Events.AvailableSizeChanged, this._onPageResizerAvailableSizeChanged, t
his); |
450 this._pageResizer.addEventListener(WebInspector.OverridesSupport.Pag
eResizer.Events.ResizeRequested, this._onPageResizerResizeRequested, this); | 284 this._pageResizer.addEventListener(WebInspector.OverridesSupport.Pag
eResizer.Events.ResizeRequested, this._onPageResizerResizeRequested, this); |
451 } | 285 } |
452 this._deviceMetricsChanged(); | 286 this._deviceMetricsChanged(); |
453 }, | 287 }, |
454 | 288 |
455 /** | 289 /** |
456 * @param {string} deviceMetrics | 290 * @param {string} deviceMetrics |
457 * @param {string} userAgent | 291 * @param {string} userAgent |
458 */ | 292 */ |
459 emulateDevice: function(deviceMetrics, userAgent) | 293 emulateDevice: function(deviceMetrics, userAgent) |
460 { | 294 { |
| 295 var metrics = WebInspector.OverridesSupport.DeviceMetrics.parseSetting(d
eviceMetrics); |
461 this._deviceMetricsChangedListenerMuted = true; | 296 this._deviceMetricsChangedListenerMuted = true; |
462 this._userAgentChangedListenerMuted = true; | 297 this._userAgentChangedListenerMuted = true; |
463 this.settings.deviceMetrics.set(deviceMetrics); | |
464 this.settings.userAgent.set(userAgent); | 298 this.settings.userAgent.set(userAgent); |
465 this.settings.overrideDeviceMetrics.set(true); | 299 this.settings.overrideDeviceResolution.set(true); |
| 300 this.settings.deviceWidth.set(metrics.width); |
| 301 this.settings.deviceHeight.set(metrics.height); |
| 302 this.settings.deviceScaleFactor.set(metrics.deviceScaleFactor); |
| 303 this.settings.deviceTextAutosizing.set(metrics.textAutosizing); |
466 this.settings.overrideUserAgent.set(true); | 304 this.settings.overrideUserAgent.set(true); |
467 this.settings.emulateTouchEvents.set(true); | 305 this.settings.emulateTouchEvents.set(true); |
468 this.settings.emulateViewport.set(true); | 306 this.settings.emulateViewport.set(true); |
469 delete this._deviceMetricsChangedListenerMuted; | 307 delete this._deviceMetricsChangedListenerMuted; |
470 delete this._userAgentChangedListenerMuted; | 308 delete this._userAgentChangedListenerMuted; |
471 this._deviceMetricsChanged(); | 309 this._deviceMetricsChanged(); |
472 this._userAgentChanged(); | 310 this._userAgentChanged(); |
473 }, | 311 }, |
474 | 312 |
475 reset: function() | 313 reset: function() |
476 { | 314 { |
477 this._deviceMetricsChangedListenerMuted = true; | 315 this._deviceMetricsChangedListenerMuted = true; |
478 this._userAgentChangedListenerMuted = true; | 316 this._userAgentChangedListenerMuted = true; |
479 this.settings.overrideDeviceMetrics.set(false); | 317 this.settings.overrideDeviceResolution.set(false); |
480 this.settings.overrideUserAgent.set(false); | 318 this.settings.overrideUserAgent.set(false); |
481 this.settings.emulateTouchEvents.set(false); | 319 this.settings.emulateTouchEvents.set(false); |
482 this.settings.overrideDeviceOrientation.set(false); | 320 this.settings.overrideDeviceOrientation.set(false); |
483 this.settings.overrideGeolocation.set(false); | 321 this.settings.overrideGeolocation.set(false); |
484 this.settings.overrideCSSMedia.set(false); | 322 this.settings.overrideCSSMedia.set(false); |
485 this.settings.emulateViewport.set(false); | 323 this.settings.emulateViewport.set(false); |
486 this.settings.deviceMetrics.set(""); | |
487 delete this._deviceMetricsChangedListenerMuted; | 324 delete this._deviceMetricsChangedListenerMuted; |
488 delete this._userAgentChangedListenerMuted; | 325 delete this._userAgentChangedListenerMuted; |
489 this._deviceMetricsChanged(); | 326 this._deviceMetricsChanged(); |
490 this._userAgentChanged(); | 327 this._userAgentChanged(); |
491 }, | 328 }, |
492 | 329 |
493 applyInitialOverrides: function() | 330 applyInitialOverrides: function() |
494 { | 331 { |
495 if (!this._target) { | 332 if (!this._target) { |
496 this._applyInitialOverridesOnTargetAdded = true; | 333 this._applyInitialOverridesOnTargetAdded = true; |
497 return; | 334 return; |
498 } | 335 } |
499 | 336 |
500 if (this.settings.overrideDeviceOrientation.get()) | 337 if (this.settings.overrideDeviceOrientation.get()) |
501 this._deviceOrientationChanged(); | 338 this._deviceOrientationChanged(); |
502 | 339 |
503 if (this.settings.overrideGeolocation.get()) | 340 if (this.settings.overrideGeolocation.get()) |
504 this._geolocationPositionChanged(); | 341 this._geolocationPositionChanged(); |
505 | 342 |
506 if (this.settings.emulateTouchEvents.get()) | 343 if (this.settings.emulateTouchEvents.get()) |
507 this._emulateTouchEventsChanged(); | 344 this._emulateTouchEventsChanged(); |
508 | 345 |
509 if (this.settings.overrideCSSMedia.get()) | 346 if (this.settings.overrideCSSMedia.get()) |
510 this._cssMediaChanged(); | 347 this._cssMediaChanged(); |
511 | 348 |
512 if (this.settings.overrideDeviceMetrics.get()) | 349 if (this.settings.overrideDeviceResolution.get() || this.settings.emulat
eViewport.get()) |
513 this._deviceMetricsChanged(); | 350 this._deviceMetricsChanged(); |
514 | 351 |
515 if (this.settings.overrideUserAgent.get()) | 352 if (this.settings.overrideUserAgent.get()) |
516 this._userAgentChanged(); | 353 this._userAgentChanged(); |
517 | 354 |
518 this._showRulersChanged(); | 355 this._showRulersChanged(); |
519 }, | 356 }, |
520 | 357 |
521 _userAgentChanged: function() | 358 _userAgentChanged: function() |
522 { | 359 { |
523 if (this._userAgentChangedListenerMuted) | 360 if (this._userAgentChangedListenerMuted) |
524 return; | 361 return; |
525 var userAgent = this.settings.overrideUserAgent.get() ? this.settings.us
erAgent.get() : ""; | 362 var userAgent = this.settings.overrideUserAgent.get() ? this.settings.us
erAgent.get() : ""; |
526 NetworkAgent.setUserAgentOverride(userAgent); | 363 NetworkAgent.setUserAgentOverride(userAgent); |
527 this._updateUserAgentWarningMessage(this._userAgent !== userAgent ? WebI
nspector.UIString("You might need to reload the page for proper user agent spoof
ing and viewport rendering.") : ""); | 364 this._updateUserAgentWarningMessage(this._userAgent !== userAgent ? WebI
nspector.UIString("You might need to reload the page for proper user agent spoof
ing and viewport rendering.") : ""); |
528 this._userAgent = userAgent; | 365 this._userAgent = userAgent; |
529 this.maybeHasActiveOverridesChanged(); | 366 this.maybeHasActiveOverridesChanged(); |
530 }, | 367 }, |
531 | 368 |
532 _onPageResizerAvailableSizeChanged: function() | 369 _onPageResizerAvailableSizeChanged: function() |
533 { | 370 { |
534 var metrics = WebInspector.OverridesSupport.DeviceMetrics.parseSetting(t
his.settings.deviceMetrics.get()); | |
535 if (!metrics.isValid()) | |
536 return; | |
537 | |
538 var available = this._pageResizer.availableDipSize(); | |
539 if (available.width > metrics.width && available.height > metrics.height
) | |
540 return; | |
541 | |
542 this._deviceMetricsChanged(); | 371 this._deviceMetricsChanged(); |
543 }, | 372 }, |
544 | 373 |
545 _onPageResizerResizeRequested: function(event) | 374 _onPageResizerResizeRequested: function(event) |
546 { | 375 { |
547 if (!this.settings.overrideDeviceMetrics.get()) | |
548 return; | |
549 | |
550 var size = /** @type {!Size} */ (event.data); | 376 var size = /** @type {!Size} */ (event.data); |
551 var metrics = WebInspector.OverridesSupport.DeviceMetrics.parseSetting(t
his.settings.deviceMetrics.get()); | 377 if (size.width !== this.settings.deviceWidth.get()) |
552 if (!metrics.isValid()) | 378 this.settings.deviceWidth.set(size.width); |
553 return; | 379 if (size.height !== this.settings.deviceHeight.get()) |
554 | 380 this.settings.deviceHeight.set(size.height); |
555 metrics.width = size.width; | |
556 metrics.height = size.height; | |
557 var value = metrics.toSetting(); | |
558 if (this.settings.deviceMetrics.get() === value) | |
559 return; | |
560 | |
561 this.settings.deviceMetrics.set(metrics.toSetting()); | |
562 }, | 381 }, |
563 | 382 |
564 _deviceMetricsChanged: function() | 383 _deviceMetricsChanged: function() |
565 { | 384 { |
566 this._showRulersChanged(); | 385 this._showRulersChanged(); |
567 | 386 |
568 if (this._deviceMetricsChangedListenerMuted) | 387 if (this._deviceMetricsChangedListenerMuted) |
569 return; | 388 return; |
570 | 389 |
571 var metricsOverrideEnabled = this.settings.overrideDeviceMetrics.get(); | 390 var overrideDeviceResolution = this.settings.overrideDeviceResolution.ge
t(); |
572 if (!metricsOverrideEnabled) { | 391 if (!overrideDeviceResolution && !this.settings.emulateViewport.get()) { |
| 392 PageAgent.clearDeviceMetricsOverride(apiCallback.bind(this)); |
573 if (this._pageResizer) | 393 if (this._pageResizer) |
574 this._pageResizer.disable(); | 394 this._pageResizer.update(0, 0, 0); |
575 PageAgent.clearDeviceMetricsOverride(apiCallback.bind(this)); | |
576 this.maybeHasActiveOverridesChanged(); | 395 this.maybeHasActiveOverridesChanged(); |
577 return; | 396 return; |
578 } | 397 } |
579 | 398 |
580 var metrics = WebInspector.OverridesSupport.DeviceMetrics.parseSetting(t
his.settings.deviceMetrics.get()); | 399 var dipWidth = overrideDeviceResolution ? this.settings.deviceWidth.get(
) : 0; |
581 if (!metrics.isValid()) | 400 var dipHeight = overrideDeviceResolution ? this.settings.deviceHeight.ge
t() : 0; |
582 return; | |
583 | |
584 var dipWidth = Math.round(metrics.width); | |
585 var dipHeight = Math.round(metrics.height); | |
586 | 401 |
587 // Disable override without checks. | 402 // Disable override without checks. |
588 if (this.isInspectingDevice()) | 403 if (this.isInspectingDevice()) |
589 return; | 404 return; |
590 | 405 |
591 var overrideWidth = dipWidth; | 406 var overrideWidth = dipWidth; |
592 var overrideHeight = dipHeight; | 407 var overrideHeight = dipHeight; |
593 if (this._pageResizer) { | 408 if (this._pageResizer) { |
594 var available = this._pageResizer.availableDipSize(); | 409 var available = this._pageResizer.availableDipSize(); |
595 if (available.width >= dipWidth && available.height >= dipHeight) { | 410 if (available.width >= dipWidth && available.height >= dipHeight) { |
596 this._pageResizer.enable(dipWidth, dipHeight, 0); | 411 this._pageResizer.update(dipWidth, dipHeight, 0); |
597 // When we have enough space, no page size override is required.
This will speed things up and remove lag. | 412 // When we have enough space, no page size override is required.
This will speed things up and remove lag. |
598 overrideWidth = 0; | 413 overrideWidth = 0; |
599 overrideHeight = 0; | 414 overrideHeight = 0; |
600 } else { | 415 } else { |
601 this._pageResizer.enable(Math.min(dipWidth, available.width), Ma
th.min(dipHeight, available.height), 0); | 416 this._pageResizer.update(Math.min(dipWidth, available.width), Ma
th.min(dipHeight, available.height), 0); |
602 } | 417 } |
603 } | 418 } |
604 | 419 |
605 // Do not emulate resolution more often than 10Hz. | 420 // Do not emulate resolution more often than 10Hz. |
606 this._setDeviceMetricsTimers = (this._setDeviceMetricsTimers || 0) + 1; | 421 this._setDeviceMetricsTimers = (this._setDeviceMetricsTimers || 0) + 1; |
607 if (overrideWidth || overrideHeight) | 422 if (overrideWidth || overrideHeight) |
608 setTimeout(setDeviceMetricsOverride.bind(this), 100); | 423 setTimeout(setDeviceMetricsOverride.bind(this), 100); |
609 else | 424 else |
610 setDeviceMetricsOverride.call(this); | 425 setDeviceMetricsOverride.call(this); |
611 | 426 |
612 /** | 427 /** |
613 * @this {WebInspector.OverridesSupport} | 428 * @this {WebInspector.OverridesSupport} |
614 */ | 429 */ |
615 function setDeviceMetricsOverride() | 430 function setDeviceMetricsOverride() |
616 { | 431 { |
617 // Drop heavy intermediate commands. | 432 // Drop heavy intermediate commands. |
618 this._setDeviceMetricsTimers--; | 433 this._setDeviceMetricsTimers--; |
619 var isExpensive = overrideWidth || overrideHeight; | 434 var isExpensive = overrideWidth || overrideHeight; |
620 if (isExpensive && this._setDeviceMetricsTimers) { | 435 if (isExpensive && this._setDeviceMetricsTimers) { |
621 var commandThreshold = 100; | 436 var commandThreshold = 100; |
622 var time = window.performance.now(); | 437 var time = window.performance.now(); |
623 if (time - this._lastExpensivePageAgentCommandTime < commandThre
shold) | 438 if (time - this._lastExpensivePageAgentCommandTime < commandThre
shold) |
624 return; | 439 return; |
625 this._lastExpensivePageAgentCommandTime = time; | 440 this._lastExpensivePageAgentCommandTime = time; |
626 } | 441 } |
627 | 442 |
628 PageAgent.setDeviceMetricsOverride( | 443 PageAgent.setDeviceMetricsOverride( |
629 overrideWidth, overrideHeight, metrics.deviceScaleFactor, | 444 overrideWidth, overrideHeight, this.settings.deviceScaleFactor.g
et(), |
630 this.settings.emulateViewport.get(), this._pageResizer ? false :
this.settings.deviceFitWindow.get(), | 445 this.settings.emulateViewport.get(), this._pageResizer ? false :
this.settings.deviceFitWindow.get(), |
631 metrics.textAutosizing, metrics.fontScaleFactor(), | 446 this.settings.deviceTextAutosizing.get(), this._fontScaleFactor(
overrideWidth || dipWidth, overrideHeight || dipHeight), |
632 apiCallback.bind(this)); | 447 apiCallback.bind(this)); |
633 } | 448 } |
634 | 449 |
635 this.maybeHasActiveOverridesChanged(); | 450 this.maybeHasActiveOverridesChanged(); |
636 | 451 |
637 /** | 452 /** |
638 * @param {?Protocol.Error} error | 453 * @param {?Protocol.Error} error |
639 * @this {WebInspector.OverridesSupport} | 454 * @this {WebInspector.OverridesSupport} |
640 */ | 455 */ |
641 function apiCallback(error) | 456 function apiCallback(error) |
642 { | 457 { |
643 if (error) { | 458 if (error) { |
644 this._updateDeviceMetricsWarningMessage(WebInspector.UIString("S
creen emulation is not available on this page.")); | 459 this._updateDeviceMetricsWarningMessage(WebInspector.UIString("S
creen emulation is not available on this page.")); |
645 if (this._pageResizer) | 460 this._deviceMetricsOverrideAppliedForTest(); |
646 this._pageResizer.disable(); | |
647 return; | 461 return; |
648 } | 462 } |
649 | 463 |
650 var viewportEnabled = this.settings.emulateViewport.get(); | 464 var overrideDeviceResolution = this.settings.overrideDeviceResolutio
n.get(); |
651 this._updateDeviceMetricsWarningMessage(this._deviceMetricsOverrideE
nabled !== metricsOverrideEnabled || (metricsOverrideEnabled && this._emulateVie
wportEnabled != viewportEnabled) ? | 465 var viewportEnabled = this.settings.emulateViewport.get(); |
| 466 this._updateDeviceMetricsWarningMessage(this._overrideDeviceResoluti
on !== overrideDeviceResolution || this._emulateViewportEnabled != viewportEnabl
ed ? |
652 WebInspector.UIString("You might need to reload the page for pro
per user agent spoofing and viewport rendering.") : ""); | 467 WebInspector.UIString("You might need to reload the page for pro
per user agent spoofing and viewport rendering.") : ""); |
653 this._deviceMetricsOverrideEnabled = metricsOverrideEnabled; | 468 this._overrideDeviceResolution = overrideDeviceResolution; |
654 this._emulateViewportEnabled = viewportEnabled; | 469 this._emulateViewportEnabled = viewportEnabled; |
655 this._deviceMetricsOverrideAppliedForTest(); | 470 this._deviceMetricsOverrideAppliedForTest(); |
656 } | 471 } |
657 }, | 472 }, |
658 | 473 |
659 _deviceMetricsOverrideAppliedForTest: function() | 474 _deviceMetricsOverrideAppliedForTest: function() |
660 { | 475 { |
661 // Used for sniffing in tests. | 476 // Used for sniffing in tests. |
662 }, | 477 }, |
663 | 478 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 for (var i = 0; i < targets.length; ++i) | 524 for (var i = 0; i < targets.length; ++i) |
710 targets[i].cssModel.mediaQueryResultChanged(); | 525 targets[i].cssModel.mediaQueryResultChanged(); |
711 this.maybeHasActiveOverridesChanged(); | 526 this.maybeHasActiveOverridesChanged(); |
712 }, | 527 }, |
713 | 528 |
714 /** | 529 /** |
715 * @return {boolean} | 530 * @return {boolean} |
716 */ | 531 */ |
717 showMetricsRulers: function() | 532 showMetricsRulers: function() |
718 { | 533 { |
719 var rulersInPageResizer = this._pageResizer && this.settings.overrideDev
iceMetrics.get(); | 534 var rulersInPageResizer = this._pageResizer && this.settings.overrideDev
iceResolution.get(); |
720 return WebInspector.settings.showMetricsRulers.get() && !rulersInPageRes
izer; | 535 return WebInspector.settings.showMetricsRulers.get() && !rulersInPageRes
izer; |
721 }, | 536 }, |
722 | 537 |
723 _showRulersChanged: function() | 538 _showRulersChanged: function() |
724 { | 539 { |
725 if (WebInspector.experimentsSettings.responsiveDesign.isEnabled()) | 540 if (WebInspector.experimentsSettings.responsiveDesign.isEnabled()) |
726 return; | 541 return; |
727 PageAgent.setShowViewportSizeOnResize(true, this.showMetricsRulers()); | 542 PageAgent.setShowViewportSizeOnResize(true, this.showMetricsRulers()); |
728 }, | 543 }, |
729 | 544 |
730 /** | 545 /** |
731 * @return {boolean} | 546 * @return {boolean} |
732 */ | 547 */ |
733 hasActiveOverrides: function() | 548 hasActiveOverrides: function() |
734 { | 549 { |
735 return this._hasActiveOverrides; | 550 return this._hasActiveOverrides; |
736 }, | 551 }, |
737 | 552 |
738 maybeHasActiveOverridesChanged: function() | 553 maybeHasActiveOverridesChanged: function() |
739 { | 554 { |
740 var hasActiveOverrides = | 555 var hasActiveOverrides = |
741 this.settings.overrideUserAgent.get() || | 556 this.settings.overrideUserAgent.get() || |
742 (this.settings.overrideDeviceMetrics.get() && !this.isInspectingDevi
ce()) || | 557 ((this.settings.overrideDeviceResolution.get() || this.settings.emul
ateViewport.get()) && !this.isInspectingDevice()) || |
743 this.settings.overrideGeolocation.get() || | 558 this.settings.overrideGeolocation.get() || |
744 this.settings.overrideDeviceOrientation.get() || | 559 this.settings.overrideDeviceOrientation.get() || |
745 (this.settings.emulateTouchEvents.get() && !this.hasTouchInputs()) |
| | 560 (this.settings.emulateTouchEvents.get() && !this.hasTouchInputs()) |
| |
746 (this.settings.overrideCSSMedia.get() && !this.isInspectingDevice())
; | 561 (this.settings.overrideCSSMedia.get() && !this.isInspectingDevice())
; |
747 if (this._hasActiveOverrides !== hasActiveOverrides) { | 562 if (this._hasActiveOverrides !== hasActiveOverrides) { |
748 this._hasActiveOverrides = hasActiveOverrides; | 563 this._hasActiveOverrides = hasActiveOverrides; |
749 this.dispatchEventToListeners(WebInspector.OverridesSupport.Events.H
asActiveOverridesChanged); | 564 this.dispatchEventToListeners(WebInspector.OverridesSupport.Events.H
asActiveOverridesChanged); |
750 } | 565 } |
751 }, | 566 }, |
752 | 567 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 targetAdded: function(target) | 603 targetAdded: function(target) |
789 { | 604 { |
790 // FIXME: adapt this to multiple targets. | 605 // FIXME: adapt this to multiple targets. |
791 if (this._target) | 606 if (this._target) |
792 return; | 607 return; |
793 this._target = target; | 608 this._target = target; |
794 | 609 |
795 this.settings = {}; | 610 this.settings = {}; |
796 this.settings.overrideUserAgent = WebInspector.settings.createSetting("o
verrideUserAgent", false); | 611 this.settings.overrideUserAgent = WebInspector.settings.createSetting("o
verrideUserAgent", false); |
797 this.settings.userAgent = WebInspector.settings.createSetting("userAgent
", ""); | 612 this.settings.userAgent = WebInspector.settings.createSetting("userAgent
", ""); |
798 this.settings.overrideDeviceMetrics = WebInspector.settings.createSettin
g("overrideDeviceMetrics", false); | 613 |
799 this.settings.deviceMetrics = WebInspector.settings.createSetting("devic
eMetrics", ""); | 614 this.settings.overrideDeviceResolution = WebInspector.settings.createSet
ting("overrideDeviceResolution", false); |
| 615 this.settings.deviceWidth = WebInspector.settings.createSetting("deviceW
idth", 800); |
| 616 this.settings.deviceHeight = WebInspector.settings.createSetting("device
Height", 600); |
| 617 this.settings.deviceScaleFactor = WebInspector.settings.createSetting("d
eviceScaleFactor", window.devicePixelRatio); |
| 618 this.settings.deviceTextAutosizing = WebInspector.settings.createSetting
("deviceTextAutosizing", true); |
| 619 |
800 this.settings.deviceFitWindow = WebInspector.settings.createSetting("dev
iceFitWindow", true); | 620 this.settings.deviceFitWindow = WebInspector.settings.createSetting("dev
iceFitWindow", true); |
801 this.settings.emulateViewport = WebInspector.settings.createSetting("emu
lateViewport", false); | 621 this.settings.emulateViewport = WebInspector.settings.createSetting("emu
lateViewport", false); |
802 this.settings.emulateTouchEvents = WebInspector.settings.createSetting("
emulateTouchEvents", false); | 622 this.settings.emulateTouchEvents = WebInspector.settings.createSetting("
emulateTouchEvents", false); |
803 this.settings.overrideGeolocation = WebInspector.settings.createSetting(
"overrideGeolocation", false); | 623 this.settings.overrideGeolocation = WebInspector.settings.createSetting(
"overrideGeolocation", false); |
804 this.settings.geolocationOverride = WebInspector.settings.createSetting(
"geolocationOverride", ""); | 624 this.settings.geolocationOverride = WebInspector.settings.createSetting(
"geolocationOverride", ""); |
805 this.settings.overrideDeviceOrientation = WebInspector.settings.createSe
tting("overrideDeviceOrientation", false); | 625 this.settings.overrideDeviceOrientation = WebInspector.settings.createSe
tting("overrideDeviceOrientation", false); |
806 this.settings.deviceOrientationOverride = WebInspector.settings.createSe
tting("deviceOrientationOverride", ""); | 626 this.settings.deviceOrientationOverride = WebInspector.settings.createSe
tting("deviceOrientationOverride", ""); |
807 this.settings.overrideCSSMedia = WebInspector.settings.createSetting("ov
errideCSSMedia", false); | 627 this.settings.overrideCSSMedia = WebInspector.settings.createSetting("ov
errideCSSMedia", false); |
808 this.settings.emulatedCSSMedia = WebInspector.settings.createSetting("em
ulatedCSSMedia", "print"); | 628 this.settings.emulatedCSSMedia = WebInspector.settings.createSetting("em
ulatedCSSMedia", "print"); |
809 | 629 |
810 this.maybeHasActiveOverridesChanged(); | 630 this.maybeHasActiveOverridesChanged(); |
811 | 631 |
812 this.settings.overrideUserAgent.addChangeListener(this._userAgentChanged
, this); | 632 this.settings.overrideUserAgent.addChangeListener(this._userAgentChanged
, this); |
813 this.settings.userAgent.addChangeListener(this._userAgentChanged, this); | 633 this.settings.userAgent.addChangeListener(this._userAgentChanged, this); |
814 | 634 |
815 this.settings.overrideDeviceMetrics.addChangeListener(this._deviceMetric
sChanged, this); | 635 this.settings.overrideDeviceResolution.addChangeListener(this._deviceMet
ricsChanged, this); |
816 this.settings.deviceMetrics.addChangeListener(this._deviceMetricsChanged
, this); | 636 this.settings.deviceWidth.addChangeListener(this._deviceMetricsChanged,
this); |
| 637 this.settings.deviceHeight.addChangeListener(this._deviceMetricsChanged,
this); |
| 638 this.settings.deviceScaleFactor.addChangeListener(this._deviceMetricsCha
nged, this); |
| 639 this.settings.deviceTextAutosizing.addChangeListener(this._deviceMetrics
Changed, this); |
817 this.settings.emulateViewport.addChangeListener(this._deviceMetricsChang
ed, this); | 640 this.settings.emulateViewport.addChangeListener(this._deviceMetricsChang
ed, this); |
818 this.settings.deviceFitWindow.addChangeListener(this._deviceMetricsChang
ed, this); | 641 this.settings.deviceFitWindow.addChangeListener(this._deviceMetricsChang
ed, this); |
819 | 642 |
820 this.settings.overrideGeolocation.addChangeListener(this._geolocationPos
itionChanged, this); | 643 this.settings.overrideGeolocation.addChangeListener(this._geolocationPos
itionChanged, this); |
821 this.settings.geolocationOverride.addChangeListener(this._geolocationPos
itionChanged, this); | 644 this.settings.geolocationOverride.addChangeListener(this._geolocationPos
itionChanged, this); |
822 | 645 |
823 this.settings.overrideDeviceOrientation.addChangeListener(this._deviceOr
ientationChanged, this); | 646 this.settings.overrideDeviceOrientation.addChangeListener(this._deviceOr
ientationChanged, this); |
824 this.settings.deviceOrientationOverride.addChangeListener(this._deviceOr
ientationChanged, this); | 647 this.settings.deviceOrientationOverride.addChangeListener(this._deviceOr
ientationChanged, this); |
825 | 648 |
826 this.settings.emulateTouchEvents.addChangeListener(this._emulateTouchEve
ntsChanged, this); | 649 this.settings.emulateTouchEvents.addChangeListener(this._emulateTouchEve
ntsChanged, this); |
827 | 650 |
828 this.settings.overrideCSSMedia.addChangeListener(this._cssMediaChanged,
this); | 651 this.settings.overrideCSSMedia.addChangeListener(this._cssMediaChanged,
this); |
829 this.settings.emulatedCSSMedia.addChangeListener(this._cssMediaChanged,
this); | 652 this.settings.emulatedCSSMedia.addChangeListener(this._cssMediaChanged,
this); |
830 | 653 |
831 WebInspector.settings.showMetricsRulers.addChangeListener(this._showRule
rsChanged, this); | 654 WebInspector.settings.showMetricsRulers.addChangeListener(this._showRule
rsChanged, this); |
832 | 655 |
833 if (this._applyInitialOverridesOnTargetAdded) { | 656 if (this._applyInitialOverridesOnTargetAdded) { |
834 delete this._applyInitialOverridesOnTargetAdded; | 657 delete this._applyInitialOverridesOnTargetAdded; |
835 this.applyInitialOverrides(); | 658 this.applyInitialOverrides(); |
836 } | 659 } |
837 }, | 660 }, |
838 | 661 |
| 662 swapDimensions: function() |
| 663 { |
| 664 var width = WebInspector.overridesSupport.settings.deviceWidth.get(); |
| 665 var height = WebInspector.overridesSupport.settings.deviceHeight.get(); |
| 666 WebInspector.overridesSupport.settings.deviceWidth.set(height); |
| 667 WebInspector.overridesSupport.settings.deviceHeight.set(width); |
| 668 }, |
| 669 |
839 /** | 670 /** |
840 * @param {!WebInspector.Target} target | 671 * @param {!WebInspector.Target} target |
841 */ | 672 */ |
842 targetRemoved: function(target) | 673 targetRemoved: function(target) |
843 { | 674 { |
844 // FIXME: adapt this to multiple targets. | 675 // FIXME: adapt this to multiple targets. |
845 }, | 676 }, |
846 | 677 |
847 /** | 678 /** |
848 * @return {boolean} | 679 * @return {boolean} |
849 */ | 680 */ |
850 isInspectingDevice: function() | 681 isInspectingDevice: function() |
851 { | 682 { |
852 return !!this._target && this._target.isMobile(); | 683 return !!this._target && this._target.isMobile(); |
853 }, | 684 }, |
854 | 685 |
855 /** | 686 /** |
856 * @return {boolean} | 687 * @return {boolean} |
857 */ | 688 */ |
858 hasTouchInputs: function() | 689 hasTouchInputs: function() |
859 { | 690 { |
860 return !!this._target && this._target.hasTouchInputs; | 691 return !!this._target && this._target.hasTouchInputs; |
861 }, | 692 }, |
862 | 693 |
| 694 /** |
| 695 * Compute the font scale factor. |
| 696 * |
| 697 * Chromium on Android uses a device scale adjustment for fonts used in text
autosizing for |
| 698 * improved legibility. This function computes this adjusted value for text
autosizing. |
| 699 * |
| 700 * For a description of the Android device scale adjustment algorithm, see: |
| 701 * chrome/browser/chrome_content_browser_client.cc, GetFontScaleMultipli
er(...) |
| 702 * |
| 703 * @param {number} width |
| 704 * @param {number} height |
| 705 * @return {number} font scale factor. |
| 706 */ |
| 707 _fontScaleFactor: function(width, height) |
| 708 { |
| 709 if (!this.settings.overrideDeviceResolution.get()) |
| 710 return 1; |
| 711 if (!width && !height) |
| 712 return 1; |
| 713 |
| 714 var deviceScaleFactor = this.settings.deviceScaleFactor.get(); |
| 715 |
| 716 var minWidth = Math.min(width, height) / deviceScaleFactor; |
| 717 |
| 718 var kMinFSM = 1.05; |
| 719 var kWidthForMinFSM = 320; |
| 720 var kMaxFSM = 1.3; |
| 721 var kWidthForMaxFSM = 800; |
| 722 |
| 723 if (minWidth <= kWidthForMinFSM) |
| 724 return kMinFSM; |
| 725 if (minWidth >= kWidthForMaxFSM) |
| 726 return kMaxFSM; |
| 727 |
| 728 // The font scale multiplier varies linearly between kMinFSM and kMaxFSM
. |
| 729 var ratio = (minWidth - kWidthForMinFSM) / (kWidthForMaxFSM - kWidthForM
inFSM); |
| 730 return ratio * (kMaxFSM - kMinFSM) + kMinFSM; |
| 731 }, |
| 732 |
863 __proto__: WebInspector.Object.prototype | 733 __proto__: WebInspector.Object.prototype |
864 } | 734 } |
865 | 735 |
866 | 736 |
867 /** | 737 /** |
868 * @type {!WebInspector.OverridesSupport} | 738 * @type {!WebInspector.OverridesSupport} |
869 */ | 739 */ |
870 WebInspector.overridesSupport; | 740 WebInspector.overridesSupport; |
OLD | NEW |