Skip to content

Commit 460527d

Browse files
yury-sdgozman
authored andcommitted
fix(webkit): do not poll readyState if target is paused before first navigation (#721)
1 parent 9d34f28 commit 460527d

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/webkit/wkPage.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ export class WKPage implements PageDelegate {
8484
this._workers.setSession(session);
8585
}
8686

87-
async initialize(session: WKSession) {
87+
async initialize(session: WKSession, pagePausedOnStart: boolean) {
8888
this._setSession(session);
8989
await Promise.all([
9090
this._initializePageProxySession(),
91-
this._initializeSession(this._session, ({frameTree}) => this._handleFrameTree(frameTree)),
91+
this._initializeSession(this._session, ({frameTree}) => this._handleFrameTree(frameTree, pagePausedOnStart)),
9292
]);
9393
}
9494

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

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

241-
frame._utilityContext().then(async context => {
242-
const readyState = await context.evaluate(() => document.readyState).catch(e => 'loading');
243-
if (frame.isDetached())
244-
return;
245-
if (readyState === 'interactive' || readyState === 'complete')
246-
this._page._frameManager.frameLifecycleEvent(frame._id, 'domcontentloaded');
247-
if (readyState === 'complete')
248-
this._page._frameManager.frameLifecycleEvent(frame._id, 'load');
249-
});
241+
if (!pagePausedOnStart) {
242+
frame._utilityContext().then(async context => {
243+
const readyState = await context.evaluate(() => document.readyState).catch(e => 'loading');
244+
if (frame.isDetached())
245+
return;
246+
if (readyState === 'interactive' || readyState === 'complete')
247+
this._page._frameManager.frameLifecycleEvent(frame._id, 'domcontentloaded');
248+
if (readyState === 'complete')
249+
this._page._frameManager.frameLifecycleEvent(frame._id, 'load');
250+
});
251+
}
250252

251253
if (!frameTree.childFrames)
252254
return;
253255
for (const child of frameTree.childFrames)
254-
this._handleFrameTree(child);
256+
this._handleFrameTree(child, pagePausedOnStart);
255257
}
256258

257259
_onFrameAttached(frameId: string, parentFrameId: string | null): frames.Frame {

src/webkit/wkPageProxy.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class WKPageProxy {
3232
private _wkPage: WKPage | null = null;
3333
private readonly _firstTargetPromise: Promise<void>;
3434
private _firstTargetCallback?: () => void;
35+
private _pagePausedOnStart: boolean = false;
3536
private readonly _sessions = new Map<string, WKSession>();
3637
private readonly _eventListeners: RegisteredListener[];
3738

@@ -121,7 +122,11 @@ export class WKPageProxy {
121122
}
122123
assert(session, 'One non-provisional target session must exist');
123124
this._wkPage = new WKPage(this._browserContext, this._pageProxySession);
124-
await this._wkPage.initialize(session!);
125+
await this._wkPage.initialize(session!, this._pagePausedOnStart);
126+
if (this._pagePausedOnStart) {
127+
this._resumeTarget(session!.sessionId);
128+
this._pagePausedOnStart = false;
129+
}
125130
return this._wkPage._page;
126131
}
127132

@@ -140,19 +145,26 @@ export class WKPageProxy {
140145
this._firstTargetCallback();
141146
this._firstTargetCallback = undefined;
142147
}
143-
if (targetInfo.isProvisional)
148+
if (targetInfo.isProvisional) {
144149
(session as any)[isPovisionalSymbol] = true;
145-
if (targetInfo.isProvisional && this._wkPage)
146-
this._wkPage.onProvisionalLoadStarted(session);
147-
if (targetInfo.isPaused) {
148-
const resume = () => this._pageProxySession.send('Target.resume', { targetId: targetInfo.targetId }).catch(debugError);
149-
if (targetInfo.isProvisional || !this._pagePromise)
150-
resume();
151-
else
152-
this._pagePromise.then(resume);
150+
if (this._wkPage)
151+
this._wkPage.onProvisionalLoadStarted(session);
152+
if (targetInfo.isPaused)
153+
this._resumeTarget(targetInfo.targetId);
154+
} else if (this._pagePromise) {
155+
assert(!this._pagePausedOnStart);
156+
// This is the first time page target is created, will resume
157+
// after finishing intialization.
158+
this._pagePausedOnStart = !!targetInfo.isPaused;
159+
} else if (targetInfo.isPaused) {
160+
this._resumeTarget(targetInfo.targetId);
153161
}
154162
}
155163

164+
private _resumeTarget(targetId: string) {
165+
this._pageProxySession.send('Target.resume', { targetId }).catch(debugError);
166+
}
167+
156168
private _onTargetDestroyed(event: Protocol.Target.targetDestroyedPayload) {
157169
const { targetId, crashed } = event;
158170
const session = this._sessions.get(targetId);

0 commit comments

Comments
 (0)