Commit 08b31fc7 authored by dgozman's avatar dgozman Committed by Commit bot

[DevTools] Move the rest of emulation to EmulationModel

BUG=none

Review-Url: https://codereview.chromium.org/2844383003
Cr-Commit-Position: refs/heads/master@{#467938}
parent 6b932ecd
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
/** /**
* @implements {SDK.TargetManager.Observer} * @implements {SDK.SDKModelObserver<!SDK.EmulationModel>}
* @unrestricted * @unrestricted
*/ */
Emulation.DeviceModeModel = class { Emulation.DeviceModeModel = class {
...@@ -58,11 +58,11 @@ Emulation.DeviceModeModel = class { ...@@ -58,11 +58,11 @@ Emulation.DeviceModeModel = class {
/** @type {number} */ /** @type {number} */
this._fitScale = 1; this._fitScale = 1;
/** @type {?SDK.Target} */ /** @type {?SDK.EmulationModel} */
this._target = null; this._emulationModel = null;
/** @type {?function()} */ /** @type {?function()} */
this._onTargetAvailable = null; this._onModelAvailable = null;
SDK.targetManager.observeTargets(this, SDK.Target.Capability.Browser); SDK.targetManager.observeModels(SDK.EmulationModel, this);
} }
/** /**
...@@ -326,14 +326,14 @@ Emulation.DeviceModeModel = class { ...@@ -326,14 +326,14 @@ Emulation.DeviceModeModel = class {
/** /**
* @override * @override
* @param {!SDK.Target} target * @param {!SDK.EmulationModel} emulationModel
*/ */
targetAdded(target) { modelAdded(emulationModel) {
if (!this._target) { if (!this._emulationModel && emulationModel.supportsDeviceEmulation()) {
this._target = target; this._emulationModel = emulationModel;
if (this._onTargetAvailable) { if (this._onModelAvailable) {
var callback = this._onTargetAvailable; var callback = this._onModelAvailable;
this._onTargetAvailable = null; this._onModelAvailable = null;
callback(); callback();
} }
} }
...@@ -341,18 +341,18 @@ Emulation.DeviceModeModel = class { ...@@ -341,18 +341,18 @@ Emulation.DeviceModeModel = class {
/** /**
* @override * @override
* @param {!SDK.Target} target * @param {!SDK.EmulationModel} emulationModel
*/ */
targetRemoved(target) { modelRemoved(emulationModel) {
if (this._target === target) if (this._emulationModel === emulationModel)
this._target = null; this._emulationModel = null;
} }
/** /**
* @return {?SDK.Target} * @return {?string}
*/ */
target() { inspectedURL() {
return this._target; return this._emulationModel ? this._emulationModel.target().inspectedURL() : null;
} }
_scaleSettingChanged() { _scaleSettingChanged() {
...@@ -419,8 +419,8 @@ Emulation.DeviceModeModel = class { ...@@ -419,8 +419,8 @@ Emulation.DeviceModeModel = class {
* @param {boolean} resetPageScaleFactor * @param {boolean} resetPageScaleFactor
*/ */
_calculateAndEmulate(resetPageScaleFactor) { _calculateAndEmulate(resetPageScaleFactor) {
if (!this._target) if (!this._emulationModel)
this._onTargetAvailable = this._calculateAndEmulate.bind(this, resetPageScaleFactor); this._onModelAvailable = this._calculateAndEmulate.bind(this, resetPageScaleFactor);
var mobile = this._isMobile(); var mobile = this._isMobile();
if (this._type === Emulation.DeviceModeModel.Type.Device) { if (this._type === Emulation.DeviceModeModel.Type.Device) {
var orientation = this._device.orientationByName(this._mode.orientation); var orientation = this._device.orientationByName(this._mode.orientation);
...@@ -472,7 +472,7 @@ Emulation.DeviceModeModel = class { ...@@ -472,7 +472,7 @@ Emulation.DeviceModeModel = class {
this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile, this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile,
this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile); this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile);
} }
var overlayModel = this._target ? this._target.model(SDK.OverlayModel) : null; var overlayModel = this._emulationModel ? this._emulationModel.overlayModel() : null;
if (overlayModel) if (overlayModel)
overlayModel.setShowViewportSizeOnResize(this._type === Emulation.DeviceModeModel.Type.None); overlayModel.setShowViewportSizeOnResize(this._type === Emulation.DeviceModeModel.Type.None);
this._updateCallback.call(null); this._updateCallback.call(null);
...@@ -590,18 +590,16 @@ Emulation.DeviceModeModel = class { ...@@ -590,18 +590,16 @@ Emulation.DeviceModeModel = class {
* @return {!Promise.<?>} * @return {!Promise.<?>}
*/ */
function setDeviceMetricsOverride() { function setDeviceMetricsOverride() {
if (!this._target) if (!this._emulationModel)
return Promise.resolve(); return Promise.resolve();
var clear = !pageWidth && !pageHeight && !mobile && !deviceScaleFactor && scale === 1 && !screenOrientation; var clear = !pageWidth && !pageHeight && !mobile && !deviceScaleFactor && scale === 1 && !screenOrientation;
var allPromises = []; var allPromises = [];
if (resetPageScaleFactor) if (resetPageScaleFactor)
allPromises.push(this._target.emulationAgent().resetPageScaleFactor()); allPromises.push(this._emulationModel.resetPageScaleFactor());
var setDevicePromise; var metrics = null;
if (clear) { if (!clear) {
setDevicePromise = this._target.emulationAgent().clearDeviceMetricsOverride(); metrics = {
} else {
var params = {
width: pageWidth, width: pageWidth,
height: pageHeight, height: pageHeight,
deviceScaleFactor: deviceScaleFactor, deviceScaleFactor: deviceScaleFactor,
...@@ -614,10 +612,9 @@ Emulation.DeviceModeModel = class { ...@@ -614,10 +612,9 @@ Emulation.DeviceModeModel = class {
positionY: positionY positionY: positionY
}; };
if (screenOrientation) if (screenOrientation)
params.screenOrientation = {type: screenOrientation, angle: screenOrientationAngle}; metrics.screenOrientation = {type: screenOrientation, angle: screenOrientationAngle};
setDevicePromise = this._target.emulationAgent().invoke_setDeviceMetricsOverride(params);
} }
allPromises.push(setDevicePromise); allPromises.push(this._emulationModel.emulateDevice(metrics));
return Promise.all(allPromises); return Promise.all(allPromises);
} }
} }
...@@ -627,7 +624,7 @@ Emulation.DeviceModeModel = class { ...@@ -627,7 +624,7 @@ Emulation.DeviceModeModel = class {
* @return {!Promise<?string>} * @return {!Promise<?string>}
*/ */
async captureScreenshot(fullSize) { async captureScreenshot(fullSize) {
var screenCaptureModel = this._target ? this._target.model(SDK.ScreenCaptureModel) : null; var screenCaptureModel = this._emulationModel ? this._emulationModel.target().model(SDK.ScreenCaptureModel) : null;
if (!screenCaptureModel) if (!screenCaptureModel)
return null; return null;
...@@ -637,21 +634,20 @@ Emulation.DeviceModeModel = class { ...@@ -637,21 +634,20 @@ Emulation.DeviceModeModel = class {
if (!this._emulatedPageSize) if (!this._emulatedPageSize)
this._calculateAndEmulate(false); this._calculateAndEmulate(false);
var overlayModel = this._target ? this._target.model(SDK.OverlayModel) : null; var overlayModel = this._emulationModel ? this._emulationModel.overlayModel() : null;
if (overlayModel) if (overlayModel)
overlayModel.setShowViewportSizeOnResize(false); overlayModel.setShowViewportSizeOnResize(false);
var pageSize = fullSize ? new UI.Size(metrics.contentWidth, metrics.contentHeight) : this._emulatedPageSize; var pageSize = fullSize ? new UI.Size(metrics.contentWidth, metrics.contentHeight) : this._emulatedPageSize;
var promises = []; var promises = [];
promises.push( promises.push(this._emulationModel.setVisibleSize(Math.floor(pageSize.width), Math.floor(pageSize.height)));
this._target.emulationAgent().setVisibleSize(Math.floor(pageSize.width), Math.floor(pageSize.height)));
if (fullSize) { if (fullSize) {
promises.push(this._target.emulationAgent().forceViewport(0, 0, 1)); promises.push(this._emulationModel.forceViewport({x: 0, y: 0, scale: 1}));
} else { } else {
promises.push(this._target.emulationAgent().forceViewport( promises.push(this._emulationModel.forceViewport(
Math.floor(metrics.viewportX), Math.floor(metrics.viewportY), metrics.viewportScale)); {x: Math.floor(metrics.viewportX), y: Math.floor(metrics.viewportY), scale: metrics.viewportScale}));
} }
promises.push(this._target.emulationAgent().invoke_setDeviceMetricsOverride({ promises.push(this._emulationModel.emulateDevice({
width: 0, width: 0,
height: 0, height: 0,
deviceScaleFactor: this._appliedDeviceScaleFactor, deviceScaleFactor: this._appliedDeviceScaleFactor,
...@@ -662,10 +658,10 @@ Emulation.DeviceModeModel = class { ...@@ -662,10 +658,10 @@ Emulation.DeviceModeModel = class {
await Promise.all(promises); await Promise.all(promises);
var screenshot = await screenCaptureModel.captureScreenshot('png', 100); var screenshot = await screenCaptureModel.captureScreenshot('png', 100);
this._target.emulationAgent().setVisibleSize( this._emulationModel.setVisibleSize(
Math.floor(this._emulatedPageSize.width * this._scale), Math.floor(this._emulatedPageSize.width * this._scale),
Math.floor(this._emulatedPageSize.height * this._scale)); Math.floor(this._emulatedPageSize.height * this._scale));
this._target.emulationAgent().resetViewport(); this._emulationModel.forceViewport(null);
this._calculateAndEmulate(false); this._calculateAndEmulate(false);
return screenshot; return screenshot;
} }
......
...@@ -452,7 +452,7 @@ Emulation.DeviceModeView = class extends UI.VBox { ...@@ -452,7 +452,7 @@ Emulation.DeviceModeView = class extends UI.VBox {
* @param {!Element} canvas * @param {!Element} canvas
*/ */
_saveScreenshot(canvas) { _saveScreenshot(canvas) {
var url = this._model.target() && this._model.target().inspectedURL(); var url = this._model.inspectedURL();
var fileName = url ? url.trimURL().removeURLFragment() : ''; var fileName = url ? url.trimURL().removeURLFragment() : '';
if (this._model.type() === Emulation.DeviceModeModel.Type.Device) if (this._model.type() === Emulation.DeviceModeModel.Type.Device)
fileName += Common.UIString('(%s)', this._model.device().title); fileName += Common.UIString('(%s)', this._model.device().title);
......
...@@ -35,6 +35,51 @@ SDK.EmulationModel = class extends SDK.SDKModel { ...@@ -35,6 +35,51 @@ SDK.EmulationModel = class extends SDK.SDKModel {
return this.target().hasAllCapabilities(SDK.Target.Capability.DeviceEmulation); return this.target().hasAllCapabilities(SDK.Target.Capability.DeviceEmulation);
} }
/**
* @return {!Promise}
*/
resetPageScaleFactor() {
return this._emulationAgent.resetPageScaleFactor();
}
/**
* @param {?Protocol.PageAgent.SetDeviceMetricsOverrideRequest} metrics
* @return {!Promise}
*/
emulateDevice(metrics) {
if (metrics)
return this._emulationAgent.invoke_setDeviceMetricsOverride(metrics);
else
return this._emulationAgent.clearDeviceMetricsOverride();
}
/**
* @param {number} width
* @param {number} height
* @return {!Promise}
*/
setVisibleSize(width, height) {
return this._emulationAgent.setVisibleSize(width, height);
}
/**
* @param {{x: number, y: number, scale: number}|null} viewport
* @return {!Promise}
*/
forceViewport(viewport) {
if (viewport)
return this._emulationAgent.forceViewport(viewport.x, viewport.y, viewport.scale);
else
return this._emulationAgent.resetViewport();
}
/**
* @return {?SDK.OverlayModel}
*/
overlayModel() {
return this._overlayModel;
}
/** /**
* @param {?SDK.EmulationModel.Geolocation} geolocation * @param {?SDK.EmulationModel.Geolocation} geolocation
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment