1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { GeckoViewActorChild } from "resource://gre/modules/GeckoViewActorChild.sys.mjs";
9 ChromeUtils.defineESModuleGetters(lazy, {
10 E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs",
13 const PERM_ACCESS_FINE_LOCATION = "android.permission.ACCESS_FINE_LOCATION";
15 export class GeckoViewPermissionChild extends GeckoViewActorChild {
16 getMediaPermission(aPermission) {
17 return this.eventDispatcher.sendRequestForResult({
18 type: "GeckoView:MediaPermission",
23 addCameraPermission() {
24 return this.sendQuery("AddCameraPermission");
27 getAppPermissions(aPermissions) {
28 return this.sendQuery("GetAppPermissions", aPermissions);
31 mediaRecordingStatusChanged(aDevices) {
32 return this.eventDispatcher.sendRequest({
33 type: "GeckoView:MediaRecordingStatusChanged",
38 async promptPermission(aRequest) {
39 // Only allow exactly one permission request here.
40 const types = aRequest.types.QueryInterface(Ci.nsIArray);
41 if (types.length !== 1) {
42 return { allow: false };
45 const perm = types.queryElementAt(0, Ci.nsIContentPermissionType);
47 perm.type === "desktop-notification" &&
48 !aRequest.hasValidTransientUserGestureActivation &&
49 Services.prefs.getBoolPref(
50 "dom.webnotifications.requireuserinteraction",
54 // We need user interaction and don't have it.
55 return { allow: false };
59 perm.type === "storage-access"
61 : aRequest.topLevelPrincipal;
65 allowOrDeny = await this.eventDispatcher.sendRequestForResult({
66 type: "GeckoView:ContentPermission",
67 uri: principal.URI.displaySpec,
68 thirdPartyOrigin: aRequest.principal.origin,
69 principal: lazy.E10SUtils.serializePrincipal(principal),
71 value: perm.capability,
72 contextId: principal.originAttributes.geckoViewSessionContextId ?? null,
73 privateMode: principal.privateBrowsingId != 0,
76 if (allowOrDeny === Services.perms.ALLOW_ACTION) {
77 // Ask for app permission after asking for content permission.
78 if (perm.type === "geolocation") {
79 const granted = await this.getAppPermissions([
80 PERM_ACCESS_FINE_LOCATION,
83 ? Services.perms.ALLOW_ACTION
84 : Services.perms.DENY_ACTION;
88 console.error("Permission error: " + error);
89 allowOrDeny = Services.perms.DENY_ACTION;
92 // Manually release the target request here to facilitate garbage collection.
95 const allow = allowOrDeny === Services.perms.ALLOW_ACTION;
97 // The storage access code adds itself to the perm manager; no need for us to do it.
98 if (perm.type === "storage-access") {
100 return { allow, permission: { "storage-access": "allow" } };
105 Services.perms.addFromPrincipal(
109 Services.perms.EXPIRE_NEVER
116 const { debug, warn } = GeckoViewPermissionChild.initLogging(
117 "GeckoViewPermissionChild"