Skip to content

Commit 78b44ed

Browse files
authored
fix: report new window downloads (#2019)
1 parent 5993a6a commit 78b44ed

File tree

9 files changed

+48
-12
lines changed

9 files changed

+48
-12
lines changed

browsers.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
{
88
"name": "firefox",
9-
"revision": "1087"
9+
"revision": "1088"
1010
},
1111
{
1212
"name": "webkit",

packages/playwright-firefox/browsers.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"browsers": [
33
{
44
"name": "firefox",
5-
"revision": "1087"
5+
"revision": "1088"
66
}
77
]
88
}

packages/playwright/browsers.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
{
88
"name": "firefox",
9-
"revision": "1087"
9+
"revision": "1088"
1010
},
1111
{
1212
"name": "webkit",

src/chromium/crPage.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class CRPage implements PageDelegate {
4949
readonly rawMouse: RawMouseImpl;
5050
readonly rawKeyboard: RawKeyboardImpl;
5151
readonly _targetId: string;
52-
private readonly _opener: CRPage | null;
52+
readonly _opener: CRPage | null;
5353
private readonly _pdf: CRPDF;
5454
private readonly _coverage: CRCoverage;
5555
readonly _browserContext: CRBrowserContext;
@@ -654,7 +654,18 @@ class FrameSession {
654654
}
655655

656656
_onDownloadWillBegin(payload: Protocol.Page.downloadWillBeginPayload) {
657-
this._crPage._browserContext._browser._downloadCreated(this._page, payload.guid, payload.url);
657+
let originPage = this._crPage._initializedPage;
658+
// If it's a new window download, report it on the opener page.
659+
if (!originPage) {
660+
// Resume the page creation with an error. The page will automatically close right
661+
// after the download begins.
662+
this._firstNonInitialNavigationCommittedReject(new Error('Starting new page download'));
663+
if (this._crPage._opener)
664+
originPage = this._crPage._opener._initializedPage;
665+
}
666+
if (!originPage)
667+
return;
668+
this._crPage._browserContext._browser._downloadCreated(originPage, payload.guid, payload.url);
658669
}
659670

660671
_onDownloadProgress(payload: Protocol.Page.downloadProgressPayload) {

src/firefox/ffBrowser.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,18 @@ export class FFBrowser extends BrowserBase {
153153
assert(ffPage);
154154
if (!ffPage)
155155
return;
156-
this._downloadCreated(ffPage._page, payload.uuid, payload.url);
156+
let originPage = ffPage._initializedPage;
157+
// If it's a new window download, report it on the opener page.
158+
if (!originPage) {
159+
// Resume the page creation with an error. The page will automatically close right
160+
// after the download begins.
161+
ffPage._pageCallback(new Error('Starting new page download'));
162+
if (ffPage._opener)
163+
originPage = ffPage._opener._initializedPage;
164+
}
165+
if (!originPage)
166+
return;
167+
this._downloadCreated(originPage, payload.uuid, payload.url);
157168
}
158169

159170
_onDownloadFinished(payload: Protocol.Browser.downloadFinishedPayload) {

src/firefox/ffPage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export class FFPage implements PageDelegate {
4545
readonly _networkManager: FFNetworkManager;
4646
readonly _browserContext: FFBrowserContext;
4747
private _pagePromise: Promise<Page | Error>;
48-
private _pageCallback: (pageOrError: Page | Error) => void = () => {};
48+
_pageCallback: (pageOrError: Page | Error) => void = () => {};
4949
_initializedPage: Page | null = null;
50-
private readonly _opener: FFPage | null;
50+
readonly _opener: FFPage | null;
5151
private readonly _contextIdToContext: Map<string, dom.FrameExecutionContext>;
5252
private _eventListeners: RegisteredListener[];
5353
private _workers = new Map<string, { frameId: string, session: FFSession }>();

src/webkit/wkBrowser.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,18 @@ export class WKBrowser extends BrowserBase {
107107
// here by simulating cancelled provisional load which matches downloads from network.
108108
frameManager.provisionalLoadFailed(frame, '', 'Download is starting');
109109
}
110-
this._downloadCreated(page._page, payload.uuid, payload.url);
110+
let originPage = page._initializedPage;
111+
// If it's a new window download, report it on the opener page.
112+
if (!originPage) {
113+
// Resume the page creation with an error. The page will automatically close right
114+
// after the download begins.
115+
page._firstNonInitialNavigationCommittedReject(new Error('Starting new page download'));
116+
if (page._opener)
117+
originPage = page._opener._initializedPage;
118+
}
119+
if (!originPage)
120+
return;
121+
this._downloadCreated(originPage, payload.uuid, payload.url);
111122
}
112123

113124
_onDownloadFinished(payload: Protocol.Playwright.downloadFinishedPayload) {

src/webkit/wkPage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class WKPage implements PageDelegate {
5353
private readonly _pagePromise: Promise<Page | Error>;
5454
private _pagePromiseCallback: (page: Page | Error) => void = () => {};
5555
private readonly _pageProxySession: WKSession;
56-
private readonly _opener: WKPage | null;
56+
readonly _opener: WKPage | null;
5757
private readonly _requestIdToRequest = new Map<string, WKInterceptableRequest>();
5858
private readonly _workers: WKWorkers;
5959
private readonly _contextIdToContext: Map<number, dom.FrameExecutionContext>;
@@ -65,7 +65,7 @@ export class WKPage implements PageDelegate {
6565
_initializedPage: Page | null = null;
6666
private _firstNonInitialNavigationCommittedPromise: Promise<void>;
6767
private _firstNonInitialNavigationCommittedFulfill = () => {};
68-
private _firstNonInitialNavigationCommittedReject = (e: Error) => {};
68+
_firstNonInitialNavigationCommittedReject = (e: Error) => {};
6969
private _lastConsoleMessage: { derivedType: string, text: string, handles: JSHandle[]; count: number, location: ConsoleMessageLocation; } | null = null;
7070

7171
constructor(browserContext: WKBrowserContext, pageProxySession: WKSession, opener: WKPage | null) {

test/download.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ describe('Download', function() {
117117
expect(fs.readFileSync(path).toString()).toBe('Hello world');
118118
await page.close();
119119
});
120-
it.fail(CHROMIUM || WEBKIT || FFOX)('should report new window downloads', async({browser, server}) => {
120+
it('should report new window downloads', async({browser, server}) => {
121+
// TODO: - the test fails in headful Chromium as the popup page gets closed along
122+
// with the session before download completed event arrives.
123+
// - WebKit doesn't close the popup page
121124
const page = await browser.newPage({ acceptDownloads: true });
122125
await page.setContent(`<a target=_blank href="${server.PREFIX}/download">download</a>`);
123126
const [ download ] = await Promise.all([

0 commit comments

Comments
 (0)