chore(webkit): simplify session init logic (#523)

* chore(webkit): simplify session init logic

* update remaining license headers
This commit is contained in:
Yury Semikhatsky 2020-01-17 14:02:57 -08:00 committed by Dmitry Gozman
parent 9e27d140c3
commit fc9ddb7c3c
4 changed files with 53 additions and 26 deletions

View File

@ -1,5 +1,18 @@
// Copyright (c) Microsoft Corporation. /**
// Licensed under the MIT license. * Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Note: this is the only file outside of src/server which can import external dependencies. // Note: this is the only file outside of src/server which can import external dependencies.
// All dependencies must be listed in web.webpack.config.js to avoid bundling them. // All dependencies must be listed in web.webpack.config.js to avoid bundling them.

View File

@ -46,7 +46,6 @@ export class WKPage implements PageDelegate {
private readonly _networkManager: WKNetworkManager; private readonly _networkManager: WKNetworkManager;
private readonly _workers: WKWorkers; private readonly _workers: WKWorkers;
private readonly _contextIdToContext: Map<number, dom.FrameExecutionContext>; private readonly _contextIdToContext: Map<number, dom.FrameExecutionContext>;
private _isolatedWorlds: Set<string>;
private _sessionListeners: RegisteredListener[] = []; private _sessionListeners: RegisteredListener[] = [];
private readonly _bootstrapScripts: string[] = []; private readonly _bootstrapScripts: string[] = [];
@ -55,7 +54,6 @@ export class WKPage implements PageDelegate {
this.rawKeyboard = new RawKeyboardImpl(pageProxySession); this.rawKeyboard = new RawKeyboardImpl(pageProxySession);
this.rawMouse = new RawMouseImpl(pageProxySession); this.rawMouse = new RawMouseImpl(pageProxySession);
this._contextIdToContext = new Map(); this._contextIdToContext = new Map();
this._isolatedWorlds = new Set();
this._page = new Page(this, browserContext); this._page = new Page(this, browserContext);
this._networkManager = new WKNetworkManager(this._page, pageProxySession); this._networkManager = new WKNetworkManager(this._page, pageProxySession);
this._workers = new WKWorkers(this._page); this._workers = new WKWorkers(this._page);
@ -83,7 +81,6 @@ export class WKPage implements PageDelegate {
this._addSessionListeners(); this._addSessionListeners();
this._networkManager.setSession(session); this._networkManager.setSession(session);
this._workers.setSession(session); this._workers.setSession(session);
this._isolatedWorlds = new Set();
// New bootstrap scripts may have been added during provisional load, push them // New bootstrap scripts may have been added during provisional load, push them
// again to be on the safe side. // again to be on the safe side.
if (this._bootstrapScripts.length) if (this._bootstrapScripts.length)
@ -98,7 +95,8 @@ export class WKPage implements PageDelegate {
session.send('Page.enable'), session.send('Page.enable'),
session.send('Page.getResourceTree').then(({frameTree}) => this._handleFrameTree(frameTree)), session.send('Page.getResourceTree').then(({frameTree}) => this._handleFrameTree(frameTree)),
// Resource tree should be received before first execution context. // Resource tree should be received before first execution context.
session.send('Runtime.enable').then(() => this._ensureIsolatedWorld(UTILITY_WORLD_NAME)), session.send('Runtime.enable'),
session.send('Page.createIsolatedWorld', { name: UTILITY_WORLD_NAME, source: `//# sourceURL=${EVALUATION_SCRIPT_URL}` }),
session.send('Console.enable'), session.send('Console.enable'),
session.send('Page.setInterceptFileChooserDialog', { enabled: true }), session.send('Page.setInterceptFileChooserDialog', { enabled: true }),
this._networkManager.initializeSession(session, this._page._state.interceptNetwork, this._page._state.offlineMode), this._networkManager.initializeSession(session, this._page._state.interceptNetwork, this._page._state.offlineMode),
@ -108,13 +106,13 @@ export class WKPage implements PageDelegate {
if (contextOptions.userAgent) if (contextOptions.userAgent)
promises.push(session.send('Page.overrideUserAgent', { value: contextOptions.userAgent })); promises.push(session.send('Page.overrideUserAgent', { value: contextOptions.userAgent }));
if (this._page._state.mediaType || this._page._state.colorScheme) if (this._page._state.mediaType || this._page._state.colorScheme)
promises.push(this._setEmulateMedia(session, this._page._state.mediaType, this._page._state.colorScheme)); promises.push(WKPage._setEmulateMedia(session, this._page._state.mediaType, this._page._state.colorScheme));
if (isProvisional) if (isProvisional)
promises.push(this._setBootstrapScripts(session)); promises.push(this._setBootstrapScripts(session));
if (contextOptions.bypassCSP) if (contextOptions.bypassCSP)
promises.push(session.send('Page.setBypassCSP', { enabled: true })); promises.push(session.send('Page.setBypassCSP', { enabled: true }));
if (this._page._state.extraHTTPHeaders !== null) if (this._page._state.extraHTTPHeaders !== null)
promises.push(this._setExtraHTTPHeaders(session, this._page._state.extraHTTPHeaders)); promises.push(WKPage._setExtraHTTPHeaders(session, this._page._state.extraHTTPHeaders));
if (this._page._state.hasTouch) if (this._page._state.hasTouch)
promises.push(session.send('Page.setTouchEmulationEnabled', { enabled: true })); promises.push(session.send('Page.setTouchEmulationEnabled', { enabled: true }));
await Promise.all(promises).catch(e => { await Promise.all(promises).catch(e => {
@ -285,21 +283,11 @@ export class WKPage implements PageDelegate {
this._page._onFileChooserOpened(handle); this._page._onFileChooserOpened(handle);
} }
async _ensureIsolatedWorld(name: string) { private static async _setExtraHTTPHeaders(session: WKSession, headers: network.Headers): Promise<void> {
if (this._isolatedWorlds.has(name))
return;
this._isolatedWorlds.add(name);
await this._session.send('Page.createIsolatedWorld', {
name,
source: `//# sourceURL=${EVALUATION_SCRIPT_URL}`
});
}
private async _setExtraHTTPHeaders(session: WKSession, headers: network.Headers): Promise<void> {
await session.send('Network.setExtraHTTPHeaders', { headers }); await session.send('Network.setExtraHTTPHeaders', { headers });
} }
private async _setEmulateMedia(session: WKSession, mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> { private static async _setEmulateMedia(session: WKSession, mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
const promises = []; const promises = [];
promises.push(session.send('Page.setEmulatedMedia', { media: mediaType || '' })); promises.push(session.send('Page.setEmulatedMedia', { media: mediaType || '' }));
if (colorScheme !== null) { if (colorScheme !== null) {
@ -314,11 +302,11 @@ export class WKPage implements PageDelegate {
} }
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> { async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
await this._setExtraHTTPHeaders(this._session, headers); await WKPage._setExtraHTTPHeaders(this._session, headers);
} }
async setEmulateMedia(mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> { async setEmulateMedia(mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
await this._setEmulateMedia(this._session, mediaType, colorScheme); await WKPage._setEmulateMedia(this._session, mediaType, colorScheme);
} }
async setViewport(viewport: types.Viewport): Promise<void> { async setViewport(viewport: types.Viewport): Promise<void> {

View File

@ -1,5 +1,18 @@
// Copyright (c) Microsoft Corporation. /**
// Licensed under the MIT license. * Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BrowserContext } from '../browserContext'; import { BrowserContext } from '../browserContext';

View File

@ -1,5 +1,18 @@
// Copyright (c) Microsoft Corporation. /**
// Licensed under the MIT license. * Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');