2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2019 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-03-04 17:57:35 -08:00
|
|
|
import { helper } from '../helper';
|
2019-12-19 16:53:24 -08:00
|
|
|
import * as js from '../javascript';
|
|
|
|
import { FFSession } from './ffConnection';
|
|
|
|
import { Protocol } from './protocol';
|
2020-05-26 14:08:32 -07:00
|
|
|
import * as debugSupport from '../debug/debugSupport';
|
2020-05-27 22:19:05 -07:00
|
|
|
import { parseEvaluationResultValue } from '../utilityScriptSerializers';
|
2019-12-19 16:53:24 -08:00
|
|
|
|
|
|
|
export class FFExecutionContext implements js.ExecutionContextDelegate {
|
|
|
|
_session: FFSession;
|
|
|
|
_executionContextId: string;
|
|
|
|
|
|
|
|
constructor(session: FFSession, executionContextId: string) {
|
|
|
|
this._session = session;
|
|
|
|
this._executionContextId = executionContextId;
|
|
|
|
}
|
|
|
|
|
2020-05-27 22:19:05 -07:00
|
|
|
async rawEvaluate(expression: string): Promise<string> {
|
2020-05-20 15:55:33 -07:00
|
|
|
const payload = 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
|
|
|
returnByValue: false,
|
|
|
|
executionContextId: this._executionContextId,
|
|
|
|
}).catch(rewriteError);
|
|
|
|
checkException(payload.exceptionDetails);
|
2020-05-27 22:19:05 -07:00
|
|
|
return payload.result!.objectId!;
|
2020-05-20 15:55:33 -07:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async evaluate(context: js.ExecutionContext, returnByValue: boolean, pageFunction: Function | string, ...args: any[]): Promise<any> {
|
|
|
|
if (helper.isString(pageFunction)) {
|
2020-05-21 16:00:55 -07:00
|
|
|
return this._callOnUtilityScript(context,
|
|
|
|
`evaluate`, [
|
2020-05-26 14:08:32 -07:00
|
|
|
{ value: debugSupport.ensureSourceUrl(pageFunction) },
|
2020-05-21 16:00:55 -07:00
|
|
|
], returnByValue, () => {});
|
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-02 13:51:32 -08:00
|
|
|
|
2020-05-21 16:00:55 -07:00
|
|
|
return this._callOnUtilityScript(context,
|
|
|
|
`callFunction`, [
|
|
|
|
{ value: functionText },
|
|
|
|
...values.map(value => ({ value })),
|
2020-05-27 17:19:05 -07:00
|
|
|
...handles.map(handle => ({ objectId: handle.objectId, value: handle.value })),
|
2020-05-21 16:00:55 -07:00
|
|
|
], returnByValue, dispose);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _callOnUtilityScript(context: js.ExecutionContext, method: string, args: Protocol.Runtime.CallFunctionArgument[], returnByValue: boolean, dispose: () => void) {
|
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
|
|
|
const payload = await this._session.send('Runtime.callFunction', {
|
2020-05-26 14:08:32 -07:00
|
|
|
functionDeclaration: `(utilityScript, ...args) => utilityScript.${method}(...args)` + debugSupport.generateSourceUrl(),
|
2020-03-19 13:07:33 -07:00
|
|
|
args: [
|
2020-05-27 17:19:05 -07:00
|
|
|
{ objectId: utilityScript._objectId, value: undefined },
|
2020-05-21 16:00:55 -07:00
|
|
|
{ value: returnByValue },
|
|
|
|
...args
|
2020-03-19 13:07:33 -07:00
|
|
|
],
|
|
|
|
returnByValue,
|
|
|
|
executionContextId: this._executionContextId
|
|
|
|
}).catch(rewriteError);
|
|
|
|
checkException(payload.exceptionDetails);
|
|
|
|
if (returnByValue)
|
2020-05-27 17:19:05 -07:00
|
|
|
return parseEvaluationResultValue(payload.result!.value);
|
|
|
|
return context.createHandle(payload.result!);
|
2020-03-19 13:07:33 -07:00
|
|
|
} finally {
|
|
|
|
dispose();
|
|
|
|
}
|
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 15:36:22 -08:00
|
|
|
if (!objectId)
|
|
|
|
return new Map();
|
2019-12-19 16:53:24 -08:00
|
|
|
const response = await this._session.send('Runtime.getObjectProperties', {
|
|
|
|
executionContextId: this._executionContextId,
|
2020-01-13 15:36:22 -08:00
|
|
|
objectId,
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
|
|
|
const result = new Map();
|
|
|
|
for (const property of response.properties)
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-27 22:19:05 -07:00
|
|
|
createHandle(context: js.ExecutionContext, remoteObject: Protocol.Runtime.RemoteObject): js.JSHandle {
|
|
|
|
return new js.JSHandle(context, remoteObject.subtype || remoteObject.type || '', remoteObject.objectId, potentiallyUnserializableValue(remoteObject));
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async releaseHandle(handle: js.JSHandle): Promise<void> {
|
2020-05-27 17:19:05 -07:00
|
|
|
if (!handle._objectId)
|
2019-12-19 16:53:24 -08:00
|
|
|
return;
|
|
|
|
await this._session.send('Runtime.disposeObject', {
|
|
|
|
executionContextId: this._executionContextId,
|
2020-05-27 17:19:05 -07:00
|
|
|
objectId: handle._objectId,
|
2020-03-04 17:57:35 -08:00
|
|
|
}).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) {
|
|
|
|
return await this._callOnUtilityScript(handle._context,
|
|
|
|
`jsonValue`, [
|
|
|
|
{ objectId: handle._objectId, value: undefined },
|
|
|
|
], true, () => {});
|
|
|
|
}
|
|
|
|
return handle._value;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 18:46:53 -07:00
|
|
|
function checkException(exceptionDetails?: Protocol.Runtime.ExceptionDetails) {
|
|
|
|
if (!exceptionDetails)
|
|
|
|
return;
|
|
|
|
if (exceptionDetails.value)
|
|
|
|
throw new Error('Evaluation failed: ' + JSON.stringify(exceptionDetails.value));
|
|
|
|
else
|
|
|
|
throw new Error('Evaluation failed: ' + exceptionDetails.text + '\n' + exceptionDetails.stack);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-05-20 15:55:33 -07:00
|
|
|
function rewriteError(error: Error): (Protocol.Runtime.evaluateReturnValue | Protocol.Runtime.callFunctionReturnValue) {
|
|
|
|
if (error.message.includes('cyclic object value') || error.message.includes('Object is not serializable'))
|
|
|
|
return {result: {type: 'undefined', value: undefined}};
|
|
|
|
if (error.message.includes('Failed to find execution context with id') || error.message.includes('Execution context was destroyed!'))
|
|
|
|
throw new Error('Execution context was destroyed, most likely because of a navigation.');
|
|
|
|
if (error instanceof TypeError && error.message.startsWith('Converting circular structure to JSON'))
|
|
|
|
error.message += ' Are you passing a nested JSHandle?';
|
|
|
|
throw error;
|
|
|
|
}
|
2020-05-27 22:19:05 -07:00
|
|
|
|
|
|
|
function potentiallyUnserializableValue(remoteObject: Protocol.Runtime.RemoteObject): any {
|
|
|
|
const value = remoteObject.value;
|
|
|
|
const unserializableValue = remoteObject.unserializableValue;
|
|
|
|
return unserializableValue ? js.parseUnserializableValue(unserializableValue) : value;
|
|
|
|
}
|