2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
* Modifications 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.
|
|
|
|
*/
|
|
|
|
|
2020-01-07 12:59:01 -08:00
|
|
|
import { WKSession, isSwappedOutError } from './wkConnection';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { helper } from '../helper';
|
|
|
|
import { Protocol } from './protocol';
|
|
|
|
import * as js from '../javascript';
|
2020-05-26 14:08:32 -07:00
|
|
|
import * as debugSupport from '../debug/debugSupport';
|
2020-05-27 17:19:05 -07:00
|
|
|
import { RemoteObject, parseEvaluationResultValue } from '../remoteObject';
|
2020-03-18 10:41:46 -07:00
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
export class WKExecutionContext implements js.ExecutionContextDelegate {
|
2020-01-31 17:23:17 -08:00
|
|
|
private readonly _session: WKSession;
|
|
|
|
readonly _contextId: number | undefined;
|
2020-01-13 13:33:25 -08:00
|
|
|
private _contextDestroyedCallback: () => void = () => {};
|
2020-01-31 17:23:17 -08:00
|
|
|
private readonly _executionContextDestroyedPromise: Promise<unknown>;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-01-31 17:23:17 -08:00
|
|
|
constructor(session: WKSession, contextId: number | undefined) {
|
|
|
|
this._session = session;
|
2020-01-07 12:59:01 -08:00
|
|
|
this._contextId = contextId;
|
2019-12-19 16:53:24 -08:00
|
|
|
this._executionContextDestroyedPromise = new Promise((resolve, reject) => {
|
|
|
|
this._contextDestroyedCallback = resolve;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_dispose() {
|
|
|
|
this._contextDestroyedCallback();
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:19:05 -07:00
|
|
|
async rawEvaluate(expression: string): Promise<RemoteObject> {
|
2020-05-20 15:55:33 -07:00
|
|
|
const contextId = this._contextId;
|
|
|
|
const response = await this._session.send('Runtime.evaluate', {
|
2020-05-26 14:08:32 -07:00
|
|
|
expression: debugSupport.ensureSourceUrl(expression),
|
2020-05-20 15:55:33 -07:00
|
|
|
contextId,
|
|
|
|
returnByValue: false
|
|
|
|
});
|
|
|
|
if (response.wasThrown)
|
|
|
|
throw new Error('Evaluation failed: ' + response.result.description);
|
|
|
|
return response.result;
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async evaluate(context: js.ExecutionContext, returnByValue: boolean, pageFunction: Function | string, ...args: any[]): Promise<any> {
|
2020-01-31 17:23:17 -08:00
|
|
|
try {
|
2020-05-21 16:00:55 -07:00
|
|
|
let response = await this._evaluateRemoteObject(context, pageFunction, args, returnByValue);
|
|
|
|
if (response.result.objectId && response.result.className === 'Promise') {
|
2020-01-31 17:23:17 -08:00
|
|
|
response = await Promise.race([
|
|
|
|
this._executionContextDestroyedPromise.then(() => contextDestroyedResult),
|
|
|
|
this._session.send('Runtime.awaitPromise', {
|
|
|
|
promiseObjectId: response.result.objectId,
|
|
|
|
returnByValue: false
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if (response.wasThrown)
|
|
|
|
throw new Error('Evaluation failed: ' + response.result.description);
|
|
|
|
if (!returnByValue)
|
2020-05-15 15:21:49 -07:00
|
|
|
return context.createHandle(response.result);
|
2020-01-31 17:23:17 -08:00
|
|
|
if (response.result.objectId)
|
2020-05-27 17:19:05 -07:00
|
|
|
return await this._returnObjectByValue(context, response.result.objectId);
|
|
|
|
return parseEvaluationResultValue(response.result.value);
|
2020-01-31 17:23:17 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (isSwappedOutError(error) || error.message.includes('Missing injected script for given'))
|
|
|
|
throw new Error('Execution context was destroyed, most likely because of a navigation.');
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 16:00:55 -07:00
|
|
|
private async _evaluateRemoteObject(context: js.ExecutionContext, pageFunction: Function | string, args: any[], returnByValue: boolean): Promise<Protocol.Runtime.callFunctionOnReturnValue> {
|
2019-12-19 16:53:24 -08:00
|
|
|
if (helper.isString(pageFunction)) {
|
2020-05-21 16:00:55 -07:00
|
|
|
const utilityScript = await context.utilityScript();
|
2020-05-26 14:08:32 -07:00
|
|
|
const functionDeclaration = `function (returnByValue, pageFunction) { return this.evaluate(returnByValue, pageFunction); }` + debugSupport.generateSourceUrl();
|
2020-05-21 16:00:55 -07:00
|
|
|
return await this._session.send('Runtime.callFunctionOn', {
|
|
|
|
functionDeclaration,
|
2020-05-27 17:19:05 -07:00
|
|
|
objectId: utilityScript._objectId!,
|
|
|
|
arguments: [
|
|
|
|
{ value: returnByValue },
|
|
|
|
{ value: debugSupport.ensureSourceUrl(pageFunction) } ],
|
2020-05-21 16:00:55 -07:00
|
|
|
returnByValue: false, // We need to return real Promise if that is a promise.
|
2019-12-19 16:53:24 -08:00
|
|
|
emulateUserGesture: true
|
2020-01-31 17:23:17 -08:00
|
|
|
});
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof pageFunction !== 'function')
|
|
|
|
throw new Error(`Expected to get |string| or |function| as the first argument, but got "${pageFunction}" instead.`);
|
|
|
|
|
2020-05-27 17:19:05 -07:00
|
|
|
const { functionText, values, handles, dispose } = await js.prepareFunctionCall(pageFunction, context, args);
|
2020-03-18 10:41:46 -07:00
|
|
|
|
2020-03-19 13:07:33 -07:00
|
|
|
try {
|
2020-05-20 15:55:33 -07:00
|
|
|
const utilityScript = await context.utilityScript();
|
2020-03-19 13:07:33 -07:00
|
|
|
return await this._session.send('Runtime.callFunctionOn', {
|
2020-05-27 17:19:05 -07:00
|
|
|
functionDeclaration: `function (...args) { return this.callFunction(...args) }` + debugSupport.generateSourceUrl(),
|
|
|
|
objectId: utilityScript._objectId!,
|
|
|
|
arguments: [
|
|
|
|
{ value: returnByValue },
|
|
|
|
{ value: functionText },
|
|
|
|
...values.map(value => ({ value })),
|
|
|
|
...handles,
|
|
|
|
],
|
2020-05-21 16:00:55 -07:00
|
|
|
returnByValue: false, // We need to return real Promise if that is a promise.
|
2020-03-19 13:07:33 -07:00
|
|
|
emulateUserGesture: true
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
dispose();
|
|
|
|
}
|
2020-03-18 10:41:46 -07:00
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-05-27 17:19:05 -07:00
|
|
|
private async _returnObjectByValue(context: js.ExecutionContext, objectId: Protocol.Runtime.RemoteObjectId): Promise<any> {
|
|
|
|
// This is different from handleJSONValue in that it does not throw.
|
2020-01-31 17:23:17 -08:00
|
|
|
try {
|
2020-05-27 17:19:05 -07:00
|
|
|
const utilityScript = await context.utilityScript();
|
2020-01-31 17:23:17 -08:00
|
|
|
const serializeResponse = await this._session.send('Runtime.callFunctionOn', {
|
2020-05-27 17:19:05 -07:00
|
|
|
functionDeclaration: 'object => object' + debugSupport.generateSourceUrl(),
|
|
|
|
objectId: utilityScript._objectId!,
|
|
|
|
arguments: [ { objectId } ],
|
2020-01-31 17:23:17 -08:00
|
|
|
returnByValue: true
|
|
|
|
});
|
2019-12-19 16:53:24 -08:00
|
|
|
if (serializeResponse.wasThrown)
|
2020-01-31 17:23:17 -08:00
|
|
|
return undefined;
|
2020-05-27 17:19:05 -07:00
|
|
|
return parseEvaluationResultValue(serializeResponse.result.value);
|
2020-01-31 17:23:17 -08:00
|
|
|
} catch (e) {
|
|
|
|
if (isSwappedOutError(e))
|
|
|
|
return contextDestroyedResult;
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async getProperties(handle: js.JSHandle): Promise<Map<string, js.JSHandle>> {
|
2020-05-27 17:19:05 -07:00
|
|
|
const objectId = handle._objectId;
|
2020-01-13 13:33:25 -08:00
|
|
|
if (!objectId)
|
|
|
|
return new Map();
|
2019-12-19 16:53:24 -08:00
|
|
|
const response = await this._session.send('Runtime.getProperties', {
|
2020-01-13 13:33:25 -08:00
|
|
|
objectId,
|
2019-12-19 16:53:24 -08:00
|
|
|
ownProperties: true
|
|
|
|
});
|
|
|
|
const result = new Map();
|
|
|
|
for (const property of response.properties) {
|
2020-05-27 17:19:05 -07:00
|
|
|
if (!property.enumerable || !property.value)
|
2019-12-19 16:53:24 -08:00
|
|
|
continue;
|
2020-05-15 15:21:49 -07:00
|
|
|
result.set(property.name, handle._context.createHandle(property.value));
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async releaseHandle(handle: js.JSHandle): Promise<void> {
|
2020-05-27 17:19:05 -07:00
|
|
|
if (!handle._objectId)
|
|
|
|
return;
|
|
|
|
await this._session.send('Runtime.releaseObject', {objectId: handle._objectId}).catch(error => {});
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async handleJSONValue<T>(handle: js.JSHandle<T>): Promise<T> {
|
2020-05-27 17:19:05 -07:00
|
|
|
if (handle._objectId) {
|
|
|
|
const utilityScript = await handle._context.utilityScript();
|
2019-12-19 16:53:24 -08:00
|
|
|
const response = await this._session.send('Runtime.callFunctionOn', {
|
2020-05-27 17:19:05 -07:00
|
|
|
functionDeclaration: 'function (object) { return this.jsonValue(true, object); }' + debugSupport.generateSourceUrl(),
|
|
|
|
objectId: utilityScript._objectId!,
|
|
|
|
arguments: [ { objectId: handle._objectId } ],
|
2019-12-19 16:53:24 -08:00
|
|
|
returnByValue: true
|
|
|
|
});
|
2020-05-27 17:19:05 -07:00
|
|
|
if (response.wasThrown)
|
|
|
|
throw new Error('Evaluation failed: ' + response.result.description);
|
|
|
|
return parseEvaluationResultValue(response.result.value);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
2020-05-27 17:19:05 -07:00
|
|
|
return handle._value;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const contextDestroyedResult = {
|
|
|
|
wasThrown: true,
|
|
|
|
result: {
|
|
|
|
description: 'Protocol error: Execution context was destroyed, most likely because of a navigation.'
|
|
|
|
} as Protocol.Runtime.RemoteObject
|
|
|
|
};
|