Skip to content

Commit

Permalink
fix(webkit): do not poll readyState if target is paused before first …
Browse files Browse the repository at this point in the history
…navigation (#721)
  • Loading branch information
yury-s authored and dgozman committed Jan 28, 2020
1 parent 9d34f28 commit 460527d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
28 changes: 15 additions & 13 deletions src/webkit/wkPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ export class WKPage implements PageDelegate {
this._workers.setSession(session);
}

async initialize(session: WKSession) {
async initialize(session: WKSession, pagePausedOnStart: boolean) {
this._setSession(session);
await Promise.all([
this._initializePageProxySession(),
this._initializeSession(this._session, ({frameTree}) => this._handleFrameTree(frameTree)),
this._initializeSession(this._session, ({frameTree}) => this._handleFrameTree(frameTree, pagePausedOnStart)),
]);
}

Expand Down Expand Up @@ -234,24 +234,26 @@ export class WKPage implements PageDelegate {
this._page._frameManager.frameLifecycleEvent(frameId, event);
}

private _handleFrameTree(frameTree: Protocol.Page.FrameResourceTree) {
private _handleFrameTree(frameTree: Protocol.Page.FrameResourceTree, pagePausedOnStart: boolean) {
const frame = this._onFrameAttached(frameTree.frame.id, frameTree.frame.parentId || null);
this._onFrameNavigated(frameTree.frame, true);

frame._utilityContext().then(async context => {
const readyState = await context.evaluate(() => document.readyState).catch(e => 'loading');
if (frame.isDetached())
return;
if (readyState === 'interactive' || readyState === 'complete')
this._page._frameManager.frameLifecycleEvent(frame._id, 'domcontentloaded');
if (readyState === 'complete')
this._page._frameManager.frameLifecycleEvent(frame._id, 'load');
});
if (!pagePausedOnStart) {
frame._utilityContext().then(async context => {
const readyState = await context.evaluate(() => document.readyState).catch(e => 'loading');
if (frame.isDetached())
return;
if (readyState === 'interactive' || readyState === 'complete')
this._page._frameManager.frameLifecycleEvent(frame._id, 'domcontentloaded');
if (readyState === 'complete')
this._page._frameManager.frameLifecycleEvent(frame._id, 'load');
});
}

if (!frameTree.childFrames)
return;
for (const child of frameTree.childFrames)
this._handleFrameTree(child);
this._handleFrameTree(child, pagePausedOnStart);
}

_onFrameAttached(frameId: string, parentFrameId: string | null): frames.Frame {
Expand Down
32 changes: 22 additions & 10 deletions src/webkit/wkPageProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class WKPageProxy {
private _wkPage: WKPage | null = null;
private readonly _firstTargetPromise: Promise<void>;
private _firstTargetCallback?: () => void;
private _pagePausedOnStart: boolean = false;
private readonly _sessions = new Map<string, WKSession>();
private readonly _eventListeners: RegisteredListener[];

Expand Down Expand Up @@ -121,7 +122,11 @@ export class WKPageProxy {
}
assert(session, 'One non-provisional target session must exist');
this._wkPage = new WKPage(this._browserContext, this._pageProxySession);
await this._wkPage.initialize(session!);
await this._wkPage.initialize(session!, this._pagePausedOnStart);
if (this._pagePausedOnStart) {
this._resumeTarget(session!.sessionId);
this._pagePausedOnStart = false;
}
return this._wkPage._page;
}

Expand All @@ -140,19 +145,26 @@ export class WKPageProxy {
this._firstTargetCallback();
this._firstTargetCallback = undefined;
}
if (targetInfo.isProvisional)
if (targetInfo.isProvisional) {
(session as any)[isPovisionalSymbol] = true;
if (targetInfo.isProvisional && this._wkPage)
this._wkPage.onProvisionalLoadStarted(session);
if (targetInfo.isPaused) {
const resume = () => this._pageProxySession.send('Target.resume', { targetId: targetInfo.targetId }).catch(debugError);
if (targetInfo.isProvisional || !this._pagePromise)
resume();
else
this._pagePromise.then(resume);
if (this._wkPage)
this._wkPage.onProvisionalLoadStarted(session);
if (targetInfo.isPaused)
this._resumeTarget(targetInfo.targetId);
} else if (this._pagePromise) {
assert(!this._pagePausedOnStart);
// This is the first time page target is created, will resume
// after finishing intialization.
this._pagePausedOnStart = !!targetInfo.isPaused;
} else if (targetInfo.isPaused) {
this._resumeTarget(targetInfo.targetId);
}
}

private _resumeTarget(targetId: string) {
this._pageProxySession.send('Target.resume', { targetId }).catch(debugError);
}

private _onTargetDestroyed(event: Protocol.Target.targetDestroyedPayload) {
const { targetId, crashed } = event;
const session = this._sessions.get(targetId);
Expand Down

0 comments on commit 460527d

Please sign in to comment.