Fix multiple permission dialogs displaying for the same permission check (#2849)
This commit is contained in:
@@ -175,4 +175,17 @@ describe('main/PermissionsManager', () => {
|
|||||||
expect(permissionsManager.writeToFile).toHaveBeenCalled();
|
expect(permissionsManager.writeToFile).toHaveBeenCalled();
|
||||||
expect(cb).toHaveBeenCalledWith(false);
|
expect(cb).toHaveBeenCalledWith(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should only pop dialog once upon multiple permission checks', async () => {
|
||||||
|
const permissionsManager = new PermissionsManager('anyfile.json');
|
||||||
|
permissionsManager.writeToFile = jest.fn();
|
||||||
|
const cb = jest.fn();
|
||||||
|
dialog.showMessageBox.mockReturnValue(Promise.resolve({response: 0}));
|
||||||
|
await Promise.all([
|
||||||
|
permissionsManager.handlePermissionRequest({id: 2}, 'notifications', cb, {securityOrigin: 'http://anyurl.com'}),
|
||||||
|
permissionsManager.handlePermissionRequest({id: 2}, 'notifications', cb, {securityOrigin: 'http://anyurl.com'}),
|
||||||
|
permissionsManager.handlePermissionRequest({id: 2}, 'notifications', cb, {securityOrigin: 'http://anyurl.com'}),
|
||||||
|
]);
|
||||||
|
expect(dialog.showMessageBox).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -50,6 +50,14 @@ type Permissions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export class PermissionsManager extends JsonFileManager<Permissions> {
|
export class PermissionsManager extends JsonFileManager<Permissions> {
|
||||||
|
private inflightPermissionChecks: Set<string>;
|
||||||
|
|
||||||
|
constructor(file: string) {
|
||||||
|
super(file);
|
||||||
|
|
||||||
|
this.inflightPermissionChecks = new Set();
|
||||||
|
}
|
||||||
|
|
||||||
handlePermissionRequest = async (
|
handlePermissionRequest = async (
|
||||||
webContents: WebContents,
|
webContents: WebContents,
|
||||||
permission: string,
|
permission: string,
|
||||||
@@ -120,6 +128,13 @@ export class PermissionsManager extends JsonFileManager<Permissions> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure we don't pop multiple dialogs for the same permission check
|
||||||
|
const permissionKey = `${parsedURL.origin}:${permission}`;
|
||||||
|
if (this.inflightPermissionChecks.has(permissionKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.inflightPermissionChecks.add(permissionKey);
|
||||||
|
|
||||||
// Show the dialog to ask the user
|
// Show the dialog to ask the user
|
||||||
const {response} = await dialog.showMessageBox(mainWindow, {
|
const {response} = await dialog.showMessageBox(mainWindow, {
|
||||||
title: localizeMessage('main.permissionsManager.checkPermission.dialog.title', 'Permission Requested'),
|
title: localizeMessage('main.permissionsManager.checkPermission.dialog.title', 'Permission Requested'),
|
||||||
@@ -144,6 +159,8 @@ export class PermissionsManager extends JsonFileManager<Permissions> {
|
|||||||
};
|
};
|
||||||
this.writeToFile();
|
this.writeToFile();
|
||||||
|
|
||||||
|
this.inflightPermissionChecks.delete(permissionKey);
|
||||||
|
|
||||||
if (response > 0) {
|
if (response > 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user