OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 {function()} updateCallback | 7 * @param {function()} updateCallback |
8 * @implements {WebInspector.TargetManager.Observer} | 8 * @implements {WebInspector.TargetManager.Observer} |
9 */ | 9 */ |
10 WebInspector.DeviceModeModel = function(updateCallback) | 10 WebInspector.DeviceModeModel = function(updateCallback) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 this._uaSetting.addChangeListener(this._uaSettingChanged, this); | 44 this._uaSetting.addChangeListener(this._uaSettingChanged, this); |
45 this._deviceScaleFactorSetting = WebInspector.settings.createSetting("emulat
ion.deviceScaleFactor", 0); | 45 this._deviceScaleFactorSetting = WebInspector.settings.createSetting("emulat
ion.deviceScaleFactor", 0); |
46 this._deviceScaleFactorSetting.addChangeListener(this._deviceScaleFactorSett
ingChanged, this); | 46 this._deviceScaleFactorSetting.addChangeListener(this._deviceScaleFactorSett
ingChanged, this); |
47 | 47 |
48 /** @type {!WebInspector.DeviceModeModel.Type} */ | 48 /** @type {!WebInspector.DeviceModeModel.Type} */ |
49 this._type = WebInspector.DeviceModeModel.Type.None; | 49 this._type = WebInspector.DeviceModeModel.Type.None; |
50 /** @type {?WebInspector.EmulatedDevice} */ | 50 /** @type {?WebInspector.EmulatedDevice} */ |
51 this._device = null; | 51 this._device = null; |
52 /** @type {?WebInspector.EmulatedDevice.Mode} */ | 52 /** @type {?WebInspector.EmulatedDevice.Mode} */ |
53 this._mode = null; | 53 this._mode = null; |
54 /** @type {boolean} */ | |
55 this._touchEnabled = false; | |
56 /** @type {string} */ | |
57 this._touchConfiguration = ""; | |
58 /** @type {number} */ | 54 /** @type {number} */ |
59 this._fitScale = 1; | 55 this._fitScale = 1; |
60 | 56 |
61 /** @type {?WebInspector.Target} */ | 57 /** @type {?WebInspector.Target} */ |
62 this._target = null; | 58 this._target = null; |
63 /** @type {?function()} */ | 59 /** @type {?function()} */ |
64 this._onTargetAvailable = null; | 60 this._onTargetAvailable = null; |
65 WebInspector.targetManager.observeTargets(this, WebInspector.Target.Type.Pag
e); | 61 WebInspector.targetManager.observeTargets(this, WebInspector.Target.Type.Pag
e); |
66 } | 62 } |
67 | 63 |
(...skipping 30 matching lines...) Expand all Loading... |
98 * @param {string} value | 94 * @param {string} value |
99 * @return {boolean} | 95 * @return {boolean} |
100 */ | 96 */ |
101 WebInspector.DeviceModeModel.deviceScaleFactorValidator = function(value) | 97 WebInspector.DeviceModeModel.deviceScaleFactorValidator = function(value) |
102 { | 98 { |
103 if (!value || (/^[\d]+(\.\d+)?|\.\d+$/.test(value) && value >= 0 && value <=
10)) | 99 if (!value || (/^[\d]+(\.\d+)?|\.\d+$/.test(value) && value >= 0 && value <=
10)) |
104 return true; | 100 return true; |
105 return false; | 101 return false; |
106 } | 102 } |
107 | 103 |
108 WebInspector.DeviceModeModel._touchEventsScriptIdSymbol = Symbol("DeviceModeMode
l.touchEventsScriptIdSymbol"); | |
109 WebInspector.DeviceModeModel._defaultMobileUserAgent = "Mozilla/5.0 (Linux; Andr
oid 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.
0.2490.76 Mobile Safari/537.36"; | 104 WebInspector.DeviceModeModel._defaultMobileUserAgent = "Mozilla/5.0 (Linux; Andr
oid 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.
0.2490.76 Mobile Safari/537.36"; |
110 WebInspector.DeviceModeModel.defaultMobileScaleFactor = 2; | 105 WebInspector.DeviceModeModel.defaultMobileScaleFactor = 2; |
111 | 106 |
112 WebInspector.DeviceModeModel.prototype = { | 107 WebInspector.DeviceModeModel.prototype = { |
113 /** | 108 /** |
114 * @param {!Size} availableSize | 109 * @param {!Size} availableSize |
115 * @param {!Size} preferredSize | 110 * @param {!Size} preferredSize |
116 */ | 111 */ |
117 setAvailableSize: function(availableSize, preferredSize) | 112 setAvailableSize: function(availableSize, preferredSize) |
118 { | 113 { |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 }, | 325 }, |
331 | 326 |
332 /** | 327 /** |
333 * @override | 328 * @override |
334 * @param {!WebInspector.Target} target | 329 * @param {!WebInspector.Target} target |
335 */ | 330 */ |
336 targetAdded: function(target) | 331 targetAdded: function(target) |
337 { | 332 { |
338 if (!this._target) { | 333 if (!this._target) { |
339 this._target = target; | 334 this._target = target; |
340 var domModel = WebInspector.DOMModel.fromTarget(this._target); | |
341 if (domModel) | |
342 domModel.addEventListener(WebInspector.DOMModel.Events.InspectMo
deWillBeToggled, this._reapplyTouch, this); | |
343 if (this._onTargetAvailable) { | 335 if (this._onTargetAvailable) { |
344 var callback = this._onTargetAvailable; | 336 var callback = this._onTargetAvailable; |
345 this._onTargetAvailable = null; | 337 this._onTargetAvailable = null; |
346 callback(); | 338 callback(); |
347 } else { | |
348 this._reapplyTouch(); | |
349 } | 339 } |
350 } | 340 } |
351 }, | 341 }, |
352 | 342 |
353 /** | 343 /** |
354 * @override | 344 * @override |
355 * @param {!WebInspector.Target} target | 345 * @param {!WebInspector.Target} target |
356 */ | 346 */ |
357 targetRemoved: function(target) | 347 targetRemoved: function(target) |
358 { | 348 { |
359 if (this._target === target) { | 349 if (this._target === target) |
360 var domModel = WebInspector.DOMModel.fromTarget(this._target); | |
361 if (domModel) | |
362 domModel.removeEventListener(WebInspector.DOMModel.Events.Inspec
tModeWillBeToggled, this._reapplyTouch, this); | |
363 this._target = null; | 350 this._target = null; |
364 } | |
365 }, | 351 }, |
366 | 352 |
367 _scaleSettingChanged: function() | 353 _scaleSettingChanged: function() |
368 { | 354 { |
369 this._calculateAndEmulate(false); | 355 this._calculateAndEmulate(false); |
370 }, | 356 }, |
371 | 357 |
372 _widthSettingChanged: function() | 358 _widthSettingChanged: function() |
373 { | 359 { |
374 this._calculateAndEmulate(false); | 360 this._calculateAndEmulate(false); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 | 401 |
416 if (this._type === WebInspector.DeviceModeModel.Type.Device) { | 402 if (this._type === WebInspector.DeviceModeModel.Type.Device) { |
417 var orientation = this._device.orientationByName(this._mode.orientat
ion); | 403 var orientation = this._device.orientationByName(this._mode.orientat
ion); |
418 this._fitScale = this._calculateFitScale(orientation.width, orientat
ion.height); | 404 this._fitScale = this._calculateFitScale(orientation.width, orientat
ion.height); |
419 if (this._device.mobile()) | 405 if (this._device.mobile()) |
420 this._appliedUserAgentType = this._device.touch() ? WebInspector
.DeviceModeModel.UA.Mobile : WebInspector.DeviceModeModel.UA.MobileNoTouch; | 406 this._appliedUserAgentType = this._device.touch() ? WebInspector
.DeviceModeModel.UA.Mobile : WebInspector.DeviceModeModel.UA.MobileNoTouch; |
421 else | 407 else |
422 this._appliedUserAgentType = this._device.touch() ? WebInspector
.DeviceModeModel.UA.DesktopTouch : WebInspector.DeviceModeModel.UA.Desktop; | 408 this._appliedUserAgentType = this._device.touch() ? WebInspector
.DeviceModeModel.UA.DesktopTouch : WebInspector.DeviceModeModel.UA.Desktop; |
423 this._applyDeviceMetrics(new Size(orientation.width, orientation.hei
ght), this._mode.insets, this._scaleSetting.get(), this._device.deviceScaleFacto
r, this._device.mobile(), this._mode.orientation == WebInspector.EmulatedDevice.
Horizontal ? "landscapePrimary" : "portraitPrimary", resetPageScaleFactor); | 409 this._applyDeviceMetrics(new Size(orientation.width, orientation.hei
ght), this._mode.insets, this._scaleSetting.get(), this._device.deviceScaleFacto
r, this._device.mobile(), this._mode.orientation == WebInspector.EmulatedDevice.
Horizontal ? "landscapePrimary" : "portraitPrimary", resetPageScaleFactor); |
424 this._applyUserAgent(this._device.userAgent); | 410 this._applyUserAgent(this._device.userAgent); |
| 411 this._applyTouch(this._device.touch(), this._device.mobile()); |
425 } else if (this._type === WebInspector.DeviceModeModel.Type.None) { | 412 } else if (this._type === WebInspector.DeviceModeModel.Type.None) { |
426 this._fitScale = this._calculateFitScale(this._availableSize.width,
this._availableSize.height); | 413 this._fitScale = this._calculateFitScale(this._availableSize.width,
this._availableSize.height); |
427 this._appliedUserAgentType = WebInspector.DeviceModeModel.UA.Desktop
; | 414 this._appliedUserAgentType = WebInspector.DeviceModeModel.UA.Desktop
; |
428 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0)
, 1, 0, false, "", resetPageScaleFactor); | 415 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0)
, 1, 0, false, "", resetPageScaleFactor); |
429 this._applyUserAgent(""); | 416 this._applyUserAgent(""); |
| 417 this._applyTouch(false, false); |
430 } else if (this._type === WebInspector.DeviceModeModel.Type.Responsive)
{ | 418 } else if (this._type === WebInspector.DeviceModeModel.Type.Responsive)
{ |
431 var screenWidth = this._widthSetting.get(); | 419 var screenWidth = this._widthSetting.get(); |
432 if (!screenWidth || screenWidth > this._preferredScaledWidth()) | 420 if (!screenWidth || screenWidth > this._preferredScaledWidth()) |
433 screenWidth = this._preferredScaledWidth(); | 421 screenWidth = this._preferredScaledWidth(); |
434 var screenHeight = this._heightSetting.get(); | 422 var screenHeight = this._heightSetting.get(); |
435 if (!screenHeight || screenHeight > this._preferredScaledHeight()) | 423 if (!screenHeight || screenHeight > this._preferredScaledHeight()) |
436 screenHeight = this._preferredScaledHeight(); | 424 screenHeight = this._preferredScaledHeight(); |
437 var mobile = this._uaSetting.get() === WebInspector.DeviceModeModel.
UA.Mobile || this._uaSetting.get() === WebInspector.DeviceModeModel.UA.MobileNoT
ouch; | 425 var mobile = this._uaSetting.get() === WebInspector.DeviceModeModel.
UA.Mobile || this._uaSetting.get() === WebInspector.DeviceModeModel.UA.MobileNoT
ouch; |
438 var defaultDeviceScaleFactor = mobile ? WebInspector.DeviceModeModel
.defaultMobileScaleFactor : 0; | 426 var defaultDeviceScaleFactor = mobile ? WebInspector.DeviceModeModel
.defaultMobileScaleFactor : 0; |
439 this._fitScale = this._calculateFitScale(this._widthSetting.get(), t
his._heightSetting.get()); | 427 this._fitScale = this._calculateFitScale(this._widthSetting.get(), t
his._heightSetting.get()); |
440 this._appliedUserAgentType = this._uaSetting.get(); | 428 this._appliedUserAgentType = this._uaSetting.get(); |
441 this._applyDeviceMetrics(new Size(screenWidth, screenHeight), new In
sets(0, 0, 0, 0), this._scaleSetting.get(), this._deviceScaleFactorSetting.get()
|| defaultDeviceScaleFactor, mobile, screenHeight >= screenWidth ? "portraitPri
mary" : "landscapePrimary", resetPageScaleFactor); | 429 this._applyDeviceMetrics(new Size(screenWidth, screenHeight), new In
sets(0, 0, 0, 0), this._scaleSetting.get(), this._deviceScaleFactorSetting.get()
|| defaultDeviceScaleFactor, mobile, screenHeight >= screenWidth ? "portraitPri
mary" : "landscapePrimary", resetPageScaleFactor); |
442 this._applyUserAgent(mobile ? WebInspector.DeviceModeModel._defaultM
obileUserAgent : ""); | 430 this._applyUserAgent(mobile ? WebInspector.DeviceModeModel._defaultM
obileUserAgent : ""); |
| 431 this._applyTouch(this._uaSetting.get() === WebInspector.DeviceModeMo
del.UA.DesktopTouch || this._uaSetting.get() === WebInspector.DeviceModeModel.UA
.Mobile, this._uaSetting.get() === WebInspector.DeviceModeModel.UA.Mobile); |
443 } | 432 } |
444 this._reapplyTouch(); | |
445 this._updateCallback.call(null); | 433 this._updateCallback.call(null); |
446 }, | 434 }, |
447 | 435 |
448 /** | 436 /** |
449 * @param {number} screenWidth | 437 * @param {number} screenWidth |
450 * @param {number} screenHeight | 438 * @param {number} screenHeight |
451 * @return {number} | 439 * @return {number} |
452 */ | 440 */ |
453 _calculateFitScale: function(screenWidth, screenHeight) | 441 _calculateFitScale: function(screenWidth, screenHeight) |
454 { | 442 { |
455 var scale = Math.min(screenWidth ? this._preferredSize.width / screenWid
th: 1, screenHeight ? this._preferredSize.height / screenHeight : 1); | 443 var scale = Math.min(screenWidth ? this._preferredSize.width / screenWid
th: 1, screenHeight ? this._preferredSize.height / screenHeight : 1); |
456 return Math.min(scale, 1); | 444 return Math.min(scale, 1); |
457 }, | 445 }, |
458 | 446 |
459 /** | 447 /** |
460 * @param {number} width | 448 * @param {number} width |
461 * @param {number} height | 449 * @param {number} height |
462 */ | 450 */ |
463 setSizeAndScaleToFit: function(width, height) | 451 setSizeAndScaleToFit: function(width, height) |
464 { | 452 { |
465 this._scaleSetting.set(this._calculateFitScale(width, height)); | 453 this._scaleSetting.set(this._calculateFitScale(width, height)); |
466 this.setWidth(width); | 454 this.setWidth(width); |
467 }, | 455 }, |
468 | 456 |
469 _reapplyTouch: function() | |
470 { | |
471 var domModel = this._target ? WebInspector.DOMModel.fromTarget(this._tar
get) : null; | |
472 var inspectModeEnabled = domModel ? domModel.inspectModeEnabled() : fals
e; | |
473 if (inspectModeEnabled) { | |
474 this._applyTouch(false, false); | |
475 return; | |
476 } | |
477 | |
478 if (this._type === WebInspector.DeviceModeModel.Type.Device) | |
479 this._applyTouch(this._device.touch(), this._device.mobile()); | |
480 else if (this._type === WebInspector.DeviceModeModel.Type.None) | |
481 this._applyTouch(false, false); | |
482 else if (this._type === WebInspector.DeviceModeModel.Type.Responsive) | |
483 this._applyTouch(this._uaSetting.get() === WebInspector.DeviceModeMo
del.UA.DesktopTouch || this._uaSetting.get() === WebInspector.DeviceModeModel.UA
.Mobile, this._uaSetting.get() === WebInspector.DeviceModeModel.UA.Mobile); | |
484 }, | |
485 | |
486 /** | 457 /** |
487 * @param {string} userAgent | 458 * @param {string} userAgent |
488 */ | 459 */ |
489 _applyUserAgent: function(userAgent) | 460 _applyUserAgent: function(userAgent) |
490 { | 461 { |
491 WebInspector.multitargetNetworkManager.setUserAgentOverride(userAgent); | 462 WebInspector.multitargetNetworkManager.setUserAgentOverride(userAgent); |
492 }, | 463 }, |
493 | 464 |
494 /** | 465 /** |
495 * @param {!Size} screenSize | 466 * @param {!Size} screenSize |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 allPromises.push(setDevicePromise); | 534 allPromises.push(setDevicePromise); |
564 return Promise.all(allPromises); | 535 return Promise.all(allPromises); |
565 } | 536 } |
566 }, | 537 }, |
567 | 538 |
568 _deviceMetricsOverrideAppliedForTest: function() | 539 _deviceMetricsOverrideAppliedForTest: function() |
569 { | 540 { |
570 // Used for sniffing in tests. | 541 // Used for sniffing in tests. |
571 }, | 542 }, |
572 | 543 |
| 544 /** |
| 545 * @param {boolean} touchEnabled |
| 546 * @param {boolean} mobile |
| 547 */ |
573 _applyTouch: function(touchEnabled, mobile) | 548 _applyTouch: function(touchEnabled, mobile) |
574 { | 549 { |
575 var configuration = mobile ? "mobile" : "desktop"; | 550 WebInspector.MultitargetTouchModel.instance().setTouchEnabled(touchEnabl
ed, mobile); |
576 if (!this._target || (this._touchEnabled === touchEnabled && this._touch
Configuration === configuration)) | |
577 return; | |
578 | |
579 var target = this._target; | |
580 | |
581 /** | |
582 * @suppressGlobalPropertiesCheck | |
583 */ | |
584 const injectedFunction = function() { | |
585 const touchEvents = ["ontouchstart", "ontouchend", "ontouchmove", "o
ntouchcancel"]; | |
586 var recepients = [window.__proto__, document.__proto__]; | |
587 for (var i = 0; i < touchEvents.length; ++i) { | |
588 for (var j = 0; j < recepients.length; ++j) { | |
589 if (!(touchEvents[i] in recepients[j])) | |
590 Object.defineProperty(recepients[j], touchEvents[i], { v
alue: null, writable: true, configurable: true, enumerable: true }); | |
591 } | |
592 } | |
593 }; | |
594 | |
595 var symbol = WebInspector.DeviceModeModel._touchEventsScriptIdSymbol; | |
596 | |
597 if (typeof target[symbol] !== "undefined") { | |
598 target.pageAgent().removeScriptToEvaluateOnLoad(target[symbol]); | |
599 delete target[symbol]; | |
600 } | |
601 | |
602 if (touchEnabled) | |
603 target.pageAgent().addScriptToEvaluateOnLoad("(" + injectedFunction.
toString() + ")()", scriptAddedCallback); | |
604 | |
605 /** | |
606 * @param {?Protocol.Error} error | |
607 * @param {string} scriptId | |
608 */ | |
609 function scriptAddedCallback(error, scriptId) | |
610 { | |
611 if (error) | |
612 delete target[symbol]; | |
613 else | |
614 target[symbol] = scriptId; | |
615 } | |
616 | |
617 target.emulationAgent().setTouchEmulationEnabled(touchEnabled, configura
tion); | |
618 this._touchEnabled = touchEnabled; | |
619 this._touchConfiguration = configuration; | |
620 } | 551 } |
621 } | 552 } |
OLD | NEW |