Skip to content

Commit

Permalink
feat(downloads): subsribe to download events in Browser domain instea…
Browse files Browse the repository at this point in the history
…d of Page
  • Loading branch information
yury-s committed Apr 5, 2021
1 parent e3cf675 commit 04ff79c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
28 changes: 27 additions & 1 deletion src/server/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export class CRBrowser extends Browser {
if ((options as any).__testHookOnConnectToBrowser)
await (options as any).__testHookOnConnectToBrowser();

const version = await session.send('Browser.getVersion');
const [version] = await Promise.all([
session.send('Browser.getVersion'),
session.send('Browser.enable'),
]);
browser._isMac = version.userAgent.includes('Macintosh');
browser._version = version.product.substring(version.product.indexOf('/') + 1);
if (!options.persistent) {
Expand All @@ -77,6 +80,8 @@ export class CRBrowser extends Browser {
this._connection.on(ConnectionEvents.Disconnected, () => this._didClose());
this._session.on('Target.attachedToTarget', this._onAttachedToTarget.bind(this));
this._session.on('Target.detachedFromTarget', this._onDetachedFromTarget.bind(this));
this._session.on('Browser.downloadWillBegin', this._onDownloadWillBegin.bind(this));
this._session.on('Browser.downloadProgress', this._onDownloadProgress.bind(this));
}

async newContext(options: types.BrowserContextOptions): Promise<BrowserContext> {
Expand Down Expand Up @@ -180,6 +185,27 @@ export class CRBrowser extends Browser {
}
}

_onDownloadWillBegin(payload: any) {
const page = this._crPages.get(payload.pageId);
assert(page, 'Download started in unknown page: ' + JSON.stringify(payload));
page.willBeginDownload();

let originPage = page._initializedPage;
// If it's a new window download, report it on the opener page.
if (!originPage && page._opener)
originPage = page._opener._initializedPage;
if (!originPage)
return;
this._downloadCreated(originPage, payload.guid, payload.url, payload.suggestedFilename);
}

_onDownloadProgress(payload: any) {
if (payload.state === 'completed')
this._downloadFinished(payload.guid, '');
if (payload.state === 'canceled')
this._downloadFinished(payload.guid, 'canceled');
}

async _closePage(crPage: CRPage) {
await this._session.send('Target.closeTarget', { targetId: crPage._targetId });
}
Expand Down
20 changes: 5 additions & 15 deletions src/server/chromium/crPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export class CRPage implements PageDelegate {
return this._sessionForFrame(frame);
}

willBeginDownload() {
this._mainFrameSession._willBeginDownload();
}

async pageOrError(): Promise<Page | Error> {
return this._pagePromise;
}
Expand Down Expand Up @@ -400,8 +404,6 @@ class FrameSession {
private _addBrowserListeners() {
this._eventListeners.push(...[
helper.addEventListener(this._client, 'Inspector.targetCrashed', event => this._onTargetCrashed()),
helper.addEventListener(this._client, 'Page.downloadWillBegin', event => this._onDownloadWillBegin(event)),
helper.addEventListener(this._client, 'Page.downloadProgress', event => this._onDownloadProgress(event)),
helper.addEventListener(this._client, 'Page.screencastFrame', event => this._onScreencastFrame(event)),
helper.addEventListener(this._client, 'Page.windowOpen', event => this._onWindowOpen(event)),
]);
Expand Down Expand Up @@ -799,26 +801,14 @@ class FrameSession {
await this._page._onFileChooserOpened(handle);
}

_onDownloadWillBegin(payload: Protocol.Page.downloadWillBeginPayload) {
_willBeginDownload() {
let originPage = this._crPage._initializedPage;
// If it's a new window download, report it on the opener page.
if (!originPage) {
// Resume the page creation with an error. The page will automatically close right
// after the download begins.
this._firstNonInitialNavigationCommittedReject(new Error('Starting new page download'));
if (this._crPage._opener)
originPage = this._crPage._opener._initializedPage;
}
if (!originPage)
return;
this._crPage._browserContext._browser._downloadCreated(originPage, payload.guid, payload.url, payload.suggestedFilename);
}

_onDownloadProgress(payload: Protocol.Page.downloadProgressPayload) {
if (payload.state === 'completed')
this._crPage._browserContext._browser._downloadFinished(payload.guid, '');
if (payload.state === 'canceled')
this._crPage._browserContext._browser._downloadFinished(payload.guid, 'canceled');
}

_onScreencastFrame(payload: Protocol.Page.screencastFramePayload) {
Expand Down
4 changes: 2 additions & 2 deletions tests/download.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ it.describe('download event', () => {
await page.close();
});

it('should report new window downloads', async ({browser, server, browserName, headful}) => {
it.fixme(browserName === 'chromium' && headful);
it('should report new window downloads', async ({browser, server, browserName, headful, browserChannel}) => {
it.fixme(browserName === 'chromium' && headful && !!browserChannel, 'Disabled until upstream fix hits stable');

// TODO: - the test fails in headful Chromium as the popup page gets closed along
// with the session before download completed event arrives.
Expand Down

0 comments on commit 04ff79c

Please sign in to comment.