refactor: simplify EmulationManager (#2816)

EmualationManager used to be injecting touch hooks to properly
support touch emulation.

However, these are no longer necessary, since https://crbug.com/133915
is long fixed.
This commit is contained in:
Andrey Lushnikov
2018-06-28 18:48:44 -07:00
committed by GitHub
parent c4acc63775
commit 871b204fd1
5 changed files with 16 additions and 36 deletions

View File

@@ -21,7 +21,7 @@ class EmulationManager {
constructor(client) {
this._client = client;
this._emulatingMobile = false;
this._injectedTouchScriptId = null;
this._hasTouch = false;
}
/**
@@ -35,44 +35,19 @@ class EmulationManager {
const deviceScaleFactor = viewport.deviceScaleFactor || 1;
/** @type {Protocol.Emulation.ScreenOrientation} */
const screenOrientation = viewport.isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
const hasTouch = viewport.hasTouch || false;
await Promise.all([
this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }),
this._client.send('Emulation.setTouchEmulationEnabled', {
enabled: viewport.hasTouch || false
enabled: hasTouch
})
]);
let reloadNeeded = false;
if (viewport.hasTouch && !this._injectedTouchScriptId) {
const source = `(${injectedTouchEventsFunction})()`;
this._injectedTouchScriptId = (await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier;
reloadNeeded = true;
} else if (!viewport.hasTouch && this._injectedTouchScriptId) {
await this._client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: this._injectedTouchScriptId});
this._injectedTouchScriptId = null;
reloadNeeded = true;
}
if (this._emulatingMobile !== mobile)
reloadNeeded = true;
const reloadNeeded = this._emulatingMobile !== mobile || this._hasTouch !== hasTouch;
this._emulatingMobile = mobile;
this._hasTouch = hasTouch;
return reloadNeeded;
function injectedTouchEventsFunction() {
const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
// @ts-ignore
const recipients = [window.__proto__, document.__proto__];
for (let i = 0; i < touchEvents.length; ++i) {
for (let j = 0; j < recipients.length; ++j) {
if (!(touchEvents[i] in recipients[j])) {
Object.defineProperty(recipients[j], touchEvents[i], {
value: null, writable: true, configurable: true, enumerable: true
});
}
}
}
}
}
}