chore: follow up to the execution context changes (#104)

This commit is contained in:
Pavel Feldman 2019-11-27 13:31:13 -08:00 committed by Dmitry Gozman
parent b2c31b7317
commit 1a1732407f
5 changed files with 43 additions and 36 deletions

View file

@ -15,6 +15,7 @@ src
node_modules
.local-chromium
.local-browser
.local-webkit
.dev_profile*
.DS_Store
*.swp

View file

@ -16,17 +16,17 @@
*/
import { EventEmitter } from 'events';
import * as frames from '../frames';
import { assert, debugError } from '../helper';
import * as js from '../javascript';
import { TimeoutSettings } from '../TimeoutSettings';
import { CDPSession } from './Connection';
import { EVALUATION_SCRIPT_URL, ExecutionContextDelegate, ExecutionContext, JSHandle, toRemoteObject } from './ExecutionContext';
import * as frames from '../frames';
import * as js from '../javascript';
import { EVALUATION_SCRIPT_URL, ExecutionContext, ExecutionContextDelegate, toRemoteObject } from './ExecutionContext';
import { ElementHandle } from './JSHandle';
import { LifecycleWatcher } from './LifecycleWatcher';
import { NetworkManager, Response } from './NetworkManager';
import { Page } from './Page';
import { Protocol } from './protocol';
import { ElementHandle, createJSHandle } from './JSHandle';
const UTILITY_WORLD_NAME = '__playwright_utility_world__';

View file

@ -15,17 +15,17 @@
* limitations under the License.
*/
import { JugglerSession } from './Connection';
import { Page } from './Page';
import {RegisteredListener, helper, assert} from '../helper';
import {TimeoutError} from '../Errors';
import {EventEmitter} from 'events';
import { JSHandle, ExecutionContext, ExecutionContextDelegate } from './ExecutionContext';
import {NavigationWatchdog, NextNavigationWatchdog} from './NavigationWatchdog';
import { ElementHandle } from './JSHandle';
import { TimeoutSettings } from '../TimeoutSettings';
import { EventEmitter } from 'events';
import { TimeoutError } from '../Errors';
import * as frames from '../frames';
import { assert, helper, RegisteredListener } from '../helper';
import * as js from '../javascript';
import { TimeoutSettings } from '../TimeoutSettings';
import { JugglerSession } from './Connection';
import { ExecutionContext, ExecutionContextDelegate } from './ExecutionContext';
import { ElementHandle } from './JSHandle';
import { NavigationWatchdog, NextNavigationWatchdog } from './NavigationWatchdog';
import { Page } from './Page';
export const FrameManagerEvents = {
FrameNavigated: Symbol('FrameManagerEvents.FrameNavigated'),

View file

@ -31,7 +31,7 @@ export type JSHandle = js.JSHandle<ElementHandle>;
export class ExecutionContextDelegate implements js.ExecutionContextDelegate<ElementHandle> {
private _globalObjectId?: string;
_session: TargetSession;
private _contextId: number;
_contextId: number;
private _contextDestroyedCallback: () => void;
private _executionContextDestroyedPromise: Promise<unknown>;
@ -157,7 +157,7 @@ export class ExecutionContextDelegate implements js.ExecutionContextDelegate<Ele
functionDeclaration: functionText + '\n' + suffix + '\n',
// TODO(yurys): support executionContextId in WebKit
objectId: thisObjectId,
arguments: serializableArgs.map(convertArgument.bind(this)),
arguments: serializableArgs.map((arg: any) => this._convertArgument(arg)),
returnByValue: false,
emulateUserGesture: true
});
@ -217,21 +217,6 @@ export class ExecutionContextDelegate implements js.ExecutionContextDelegate<Ele
return valueFromRemoteObject(response.result);
}).catch(rewriteError);
function convertArgument(this: ExecutionContext, arg: JSHandle | any) : Protocol.Runtime.CallArgument{
const objectHandle = arg && (arg instanceof js.JSHandle) ? arg : null;
if (objectHandle) {
if (objectHandle._context !== this)
throw new Error('JSHandles can be evaluated only in the context they were created!');
if (objectHandle._disposed)
throw new Error('JSHandle is disposed!');
const remoteObject = toRemoteObject(arg);
if (!remoteObject.objectId)
return { value: valueFromRemoteObject(remoteObject) };
return { objectId: remoteObject.objectId };
}
return { value: arg };
}
function unserializableToString(arg) {
if (Object.is(arg, -0))
return '-0';
@ -332,6 +317,22 @@ export class ExecutionContextDelegate implements js.ExecutionContextDelegate<Ele
}
return 'JSHandle:' + valueFromRemoteObject(object);
}
private _convertArgument(arg: JSHandle | any) : Protocol.Runtime.CallArgument {
const objectHandle = arg && (arg instanceof js.JSHandle) ? arg : null;
if (objectHandle) {
if (objectHandle._context._delegate !== this)
throw new Error('JSHandles can be evaluated only in the context they were created!');
if (objectHandle._disposed)
throw new Error('JSHandle is disposed!');
const remoteObject = toRemoteObject(arg);
if (!remoteObject.objectId)
return { value: valueFromRemoteObject(remoteObject) };
return { objectId: remoteObject.objectId };
}
return { value: arg };
}
}
const remoteObjectSymbol = Symbol('RemoteObject');

View file

@ -17,17 +17,17 @@
import * as EventEmitter from 'events';
import { TimeoutError } from '../Errors';
import { Events } from './events';
import * as frames from '../frames';
import { assert, debugError, helper, RegisteredListener } from '../helper';
import * as js from '../javascript';
import { TimeoutSettings } from '../TimeoutSettings';
import { TargetSession } from './Connection';
import { JSHandle, ExecutionContext, ExecutionContextDelegate } from './ExecutionContext';
import { Events } from './events';
import { ExecutionContext, ExecutionContextDelegate } from './ExecutionContext';
import { ElementHandle } from './JSHandle';
import { NetworkManager, NetworkManagerEvents, Request, Response } from './NetworkManager';
import { Page } from './Page';
import { Protocol } from './protocol';
import * as frames from '../frames';
import * as js from '../javascript';
export const FrameManagerEvents = {
FrameNavigatedWithinDocument: Symbol('FrameNavigatedWithinDocument'),
@ -178,6 +178,7 @@ export class FrameManager extends EventEmitter implements frames.FrameDelegate<E
const data = this._frameData(frame);
this._frames.delete(data.id);
data.id = framePayload.id;
this._frames.set(data.id, frame);
}
} else if (isMainFrame) {
// Initial frame navigation.
@ -199,7 +200,9 @@ export class FrameManager extends EventEmitter implements frames.FrameDelegate<E
frame._navigated(framePayload.url, framePayload.name);
for (const context of this._contextIdToContext.values()) {
if (context.frame() === frame) {
(context._delegate as ExecutionContextDelegate)._dispose();
const delegate = context._delegate as ExecutionContextDelegate;
delegate._dispose();
this._contextIdToContext.delete(delegate._contextId);
frame._contextDestroyed(context);
}
}
@ -223,6 +226,8 @@ export class FrameManager extends EventEmitter implements frames.FrameDelegate<E
}
_onExecutionContextCreated(contextPayload : Protocol.Runtime.ExecutionContextDescription) {
if (this._contextIdToContext.has(contextPayload.id))
return;
if (!contextPayload.isPageContext)
return;
const frameId = contextPayload.frameId;