Refactor EmulationManager

This patch refactors EmulationManager so that it is a normal
class with a state.
This commit is contained in:
Andrey Lushnikov 2017-07-29 19:12:17 -07:00
parent 445dce46f6
commit c013a531cf
2 changed files with 30 additions and 38 deletions

View File

@ -17,41 +17,47 @@
class EmulationManager {
/**
* @param {!Connection} client
*/
constructor(client) {
this._client = client;
this._emulatingMobile = false;
this._injectedTouchScriptId = null;
}
/**
* @param {!Page.Viewport} viewport
* @return {Promise<boolean>}
*/
static async emulateViewport(client, viewport) {
async emulateViewport(client, viewport) {
const mobile = viewport.isMobile || false;
const landscape = viewport.isLandscape || false;
const width = viewport.width;
const height = viewport.height;
const deviceScaleFactor = viewport.deviceScaleFactor || 1;
const screenOrientation = landscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
const screenOrientation = viewport.isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
await Promise.all([
client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }),
client.send('Emulation.setTouchEmulationEnabled', {
this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }),
this._client.send('Emulation.setTouchEmulationEnabled', {
enabled: viewport.hasTouch || false,
configuration: viewport.isMobile ? 'mobile' : 'desktop'
})
]);
let reloadNeeded = false;
if (viewport.hasTouch && !client[EmulationManager._touchScriptId]) {
if (viewport.hasTouch && !this._injectedTouchScriptId) {
const source = `(${injectedTouchEventsFunction})()`;
client[EmulationManager._touchScriptId] = (await client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier;
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 (!viewport.hasTouch && client[EmulationManager._touchScriptId]) {
await client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: client[EmulationManager._touchScriptId]});
client[EmulationManager._touchScriptId] = null;
if (this._emulatingMobile !== mobile)
reloadNeeded = true;
}
if (client[EmulationManager._emulatingMobile] !== mobile)
reloadNeeded = true;
client[EmulationManager._emulatingMobile] = mobile;
this._emulatingMobile = mobile;
return reloadNeeded;
function injectedTouchEventsFunction() {
const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
@ -66,11 +72,7 @@ class EmulationManager {
}
}
}
return reloadNeeded;
}
}
EmulationManager._touchScriptId = Symbol('emulatingTouchScriptId');
EmulationManager._emulatingMobile = Symbol('emulatingMobile');
module.exports = EmulationManager;

View File

@ -38,36 +38,28 @@ class Page extends EventEmitter {
client.send('Runtime.enable', {}),
client.send('Security.enable', {}),
]);
const keyboard = new Keyboard(client);
const mouse = new Mouse(client, keyboard);
const frameManager = new FrameManager(client, mouse);
const networkManager = new NetworkManager(client);
const page = new Page(client, frameManager, networkManager, screenshotTaskQueue, mouse, keyboard);
const page = new Page(client, screenshotTaskQueue);
await page.navigate('about:blank');
// Initialize default page size.
await page.setViewport({width: 400, height: 300});
await page.navigate('about:blank');
return page;
}
/**
* @param {!Connection} client
* @param {!FrameManager} frameManager
* @param {!NetworkManager} networkManager
* @param {!TaskQueue} screenshotTaskQueue
* @param {!Mouse} mouse
* @param {!Keyboard} keyboard
*/
constructor(client, frameManager, networkManager, screenshotTaskQueue, mouse, keyboard) {
constructor(client, screenshotTaskQueue) {
super();
this._client = client;
this._frameManager = frameManager;
this._networkManager = networkManager;
this._keyboard = new Keyboard(client);
this._mouse = new Mouse(client, this._keyboard);
this._frameManager = new FrameManager(client, this._mouse);
this._networkManager = new NetworkManager(client);
this._emulationManager = new EmulationManager(client);
/** @type {!Map<string, function>} */
this._inPageCallbacks = new Map();
this._keyboard = keyboard;
this._mouse = mouse;
this._screenshotTaskQueue = screenshotTaskQueue;
this._frameManager.on(FrameManager.Events.FrameAttached, event => this.emit(Page.Events.FrameAttached, event));
@ -274,8 +266,6 @@ class Page extends EventEmitter {
* @return {!Promise<?Response>}
*/
async reload(options) {
if (!this.mainFrame())
return;
this._client.send('Page.reload');
return this.waitForNavigation(options);
}
@ -330,7 +320,7 @@ class Page extends EventEmitter {
* @return {!Promise}
*/
async setViewport(viewport) {
const needsReload = await EmulationManager.emulateViewport(this._client, viewport);
const needsReload = await this._emulationManager.emulateViewport(this._client, viewport);
this._viewport = viewport;
if (needsReload)
await this.reload();