Skip to content

Commit dc84511

Browse files
committed
fix(permissions): manage permissions on the proxy level in webkit
1 parent bae56ea commit dc84511

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"playwright": {
1111
"chromium_revision": "751710",
1212
"firefox_revision": "1043",
13-
"webkit_revision": "1180"
13+
"webkit_revision": "1182"
1414
},
1515
"scripts": {
1616
"ctest": "cross-env BROWSER=chromium node test/test.js",

src/browserContext.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export abstract class BrowserContextBase extends platform.EventEmitter implement
7070
_closed = false;
7171
private readonly _closePromise: Promise<Error>;
7272
private _closePromiseFulfill: ((error: Error) => void) | undefined;
73-
private _permissions = new Map<string, string[]>();
73+
readonly _permissions = new Map<string, string[]>();
7474

7575
constructor(options: BrowserContextOptions) {
7676
super();

src/webkit/wkBrowser.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -263,20 +263,13 @@ export class WKBrowserContext extends BrowserContextBase {
263263
}
264264

265265
async _doGrantPermissions(origin: string, permissions: string[]) {
266-
const webPermissionToProtocol = new Map<string, string>([
267-
['geolocation', 'geolocation'],
268-
]);
269-
const filtered = permissions.map(permission => {
270-
const protocolPermission = webPermissionToProtocol.get(permission);
271-
if (!protocolPermission)
272-
throw new Error('Unknown permission: ' + permission);
273-
return protocolPermission;
274-
});
275-
await this._browser._browserSession.send('Playwright.grantPermissions', { origin, browserContextId: this._browserContextId, permissions: filtered });
266+
for (const page of this.pages())
267+
await (page._delegate as WKPage)._grantPermissions(origin, permissions);
276268
}
277269

278270
async _doClearPermissions() {
279-
await this._browser._browserSession.send('Playwright.resetPermissions', { browserContextId: this._browserContextId });
271+
for (const page of this.pages())
272+
await (page._delegate as WKPage)._clearPermissions();
280273
}
281274

282275
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {

src/webkit/wkPage.ts

+21
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ export class WKPage implements PageDelegate {
9898
if (this._page._state.viewportSize || contextOptions.viewport)
9999
promises.push(this._updateViewport());
100100
promises.push(this.updateHttpCredentials());
101+
if (this._browserContext._permissions.size) {
102+
for (const [key, value] of this._browserContext._permissions)
103+
this._grantPermissions(key, value);
104+
}
101105
await Promise.all(promises);
102106
}
103107

@@ -820,6 +824,23 @@ export class WKPage implements PageDelegate {
820824
request.request._setFailureText(event.errorText);
821825
this._page._frameManager.requestFailed(request.request, event.errorText.includes('cancelled'));
822826
}
827+
828+
async _grantPermissions(origin: string, permissions: string[]) {
829+
const webPermissionToProtocol = new Map<string, string>([
830+
['geolocation', 'geolocation'],
831+
]);
832+
const filtered = permissions.map(permission => {
833+
const protocolPermission = webPermissionToProtocol.get(permission);
834+
if (!protocolPermission)
835+
throw new Error('Unknown permission: ' + permission);
836+
return protocolPermission;
837+
});
838+
await this._pageProxySession.send('Emulation.grantPermissions', { origin, permissions: filtered });
839+
}
840+
841+
async _clearPermissions() {
842+
await this._pageProxySession.send('Emulation.resetPermissions', {});
843+
}
823844
}
824845

825846
function toRemoteObject(handle: dom.ElementHandle): Protocol.Runtime.RemoteObject {

test/geolocation.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,16 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
8787
});
8888
await context.close();
8989
});
90+
it('should use context options for popup', async({page, context, server}) => {
91+
await context.grantPermissions(['geolocation']);
92+
await context.setGeolocation({ longitude: 10, latitude: 10 });
93+
const [popup] = await Promise.all([
94+
page.waitForEvent('popup'),
95+
page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/geolocation.html'),
96+
]);
97+
const geolocation = await popup.evaluate(() => window.geolocationPromise);
98+
expect(geolocation).toEqual({ longitude: 10, latitude: 10 });
99+
});
100+
90101
});
91102
};

0 commit comments

Comments
 (0)