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(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> {
|
||||
private inflightPermissionChecks: Set<string>;
|
||||
|
||||
constructor(file: string) {
|
||||
super(file);
|
||||
|
||||
this.inflightPermissionChecks = new Set();
|
||||
}
|
||||
|
||||
handlePermissionRequest = async (
|
||||
webContents: WebContents,
|
||||
permission: string,
|
||||
@@ -120,6 +128,13 @@ export class PermissionsManager extends JsonFileManager<Permissions> {
|
||||
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
|
||||
const {response} = await dialog.showMessageBox(mainWindow, {
|
||||
title: localizeMessage('main.permissionsManager.checkPermission.dialog.title', 'Permission Requested'),
|
||||
@@ -144,6 +159,8 @@ export class PermissionsManager extends JsonFileManager<Permissions> {
|
||||
};
|
||||
this.writeToFile();
|
||||
|
||||
this.inflightPermissionChecks.delete(permissionKey);
|
||||
|
||||
if (response > 0) {
|
||||
return false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user