2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import { sendSync } from "./dispatch_json.ts";
|
2019-03-04 11:04:19 -05:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
/** Permissions as granted by the caller
|
|
|
|
* See: https://w3c.github.io/permissions/#permission-registry
|
|
|
|
*/
|
|
|
|
export type PermissionName =
|
|
|
|
| "read"
|
|
|
|
| "write"
|
|
|
|
| "net"
|
|
|
|
| "env"
|
|
|
|
| "run"
|
2019-12-05 15:30:20 -05:00
|
|
|
| "plugin"
|
2019-10-27 11:22:53 -04:00
|
|
|
| "hrtime";
|
|
|
|
// NOTE: Keep in sync with cli/permissions.rs
|
2019-03-04 11:04:19 -05:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
/** https://w3c.github.io/permissions/#status-of-a-permission */
|
|
|
|
export type PermissionState = "granted" | "denied" | "prompt";
|
2019-03-04 11:04:19 -05:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
interface RunPermissionDescriptor {
|
|
|
|
name: "run";
|
|
|
|
}
|
|
|
|
interface ReadWritePermissionDescriptor {
|
|
|
|
name: "read" | "write";
|
|
|
|
path?: string;
|
|
|
|
}
|
|
|
|
interface NetPermissionDescriptor {
|
|
|
|
name: "net";
|
|
|
|
url?: string;
|
|
|
|
}
|
|
|
|
interface EnvPermissionDescriptor {
|
|
|
|
name: "env";
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
2019-12-05 15:30:20 -05:00
|
|
|
interface PluginPermissionDescriptor {
|
|
|
|
name: "plugin";
|
|
|
|
}
|
2019-10-27 11:22:53 -04:00
|
|
|
interface HrtimePermissionDescriptor {
|
|
|
|
name: "hrtime";
|
|
|
|
}
|
|
|
|
/** See: https://w3c.github.io/permissions/#permission-descriptor */
|
|
|
|
type PermissionDescriptor =
|
|
|
|
| RunPermissionDescriptor
|
|
|
|
| ReadWritePermissionDescriptor
|
|
|
|
| NetPermissionDescriptor
|
|
|
|
| EnvPermissionDescriptor
|
2019-12-05 15:30:20 -05:00
|
|
|
| PluginPermissionDescriptor
|
2019-10-27 11:22:53 -04:00
|
|
|
| HrtimePermissionDescriptor;
|
2019-03-09 12:30:38 -05:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
/** https://w3c.github.io/permissions/#permissionstatus */
|
|
|
|
export class PermissionStatus {
|
|
|
|
constructor(public state: PermissionState) {}
|
|
|
|
// TODO(kt3k): implement onchange handler
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Permissions {
|
|
|
|
/** Queries the permission.
|
|
|
|
* const status = await Deno.permissions.query({ name: "read", path: "/etc" });
|
|
|
|
* if (status.state === "granted") {
|
|
|
|
* file = await Deno.readFile("/etc/passwd");
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
async query(desc: PermissionDescriptor): Promise<PermissionStatus> {
|
2020-02-25 09:14:27 -05:00
|
|
|
const { state } = sendSync("op_query_permission", desc);
|
2019-10-27 11:22:53 -04:00
|
|
|
return new PermissionStatus(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Revokes the permission.
|
|
|
|
* const status = await Deno.permissions.revoke({ name: "run" });
|
|
|
|
* assert(status.state !== "granted")
|
|
|
|
*/
|
|
|
|
async revoke(desc: PermissionDescriptor): Promise<PermissionStatus> {
|
2020-02-25 09:14:27 -05:00
|
|
|
const { state } = sendSync("op_revoke_permission", desc);
|
2019-10-27 11:22:53 -04:00
|
|
|
return new PermissionStatus(state);
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
|
|
/** Requests the permission.
|
|
|
|
* const status = await Deno.permissions.request({ name: "env" });
|
|
|
|
* if (status.state === "granted") {
|
|
|
|
* console.log(Deno.homeDir());
|
|
|
|
* } else {
|
|
|
|
* console.log("'env' permission is denied.");
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
async request(desc: PermissionDescriptor): Promise<PermissionStatus> {
|
2020-02-25 09:14:27 -05:00
|
|
|
const { state } = sendSync("op_request_permission", desc);
|
2019-11-11 10:33:29 -05:00
|
|
|
return new PermissionStatus(state);
|
|
|
|
}
|
2019-03-04 11:04:19 -05:00
|
|
|
}
|
2019-10-27 11:22:53 -04:00
|
|
|
|
|
|
|
export const permissions = new Permissions();
|