2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-03-09 10:18:02 -04:00
|
|
|
import * as permissionsOps from "./ops/permissions.ts";
|
2019-03-04 11:04:19 -05:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
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
|
|
|
export type PermissionState = "granted" | "denied" | "prompt";
|
2019-03-04 11:04:19 -05:00
|
|
|
|
2020-04-28 10:43:58 -04:00
|
|
|
export interface RunPermissionDescriptor {
|
2019-10-27 11:22:53 -04:00
|
|
|
name: "run";
|
|
|
|
}
|
2020-04-28 10:43:58 -04:00
|
|
|
|
|
|
|
export interface ReadPermissionDescriptor {
|
|
|
|
name: "read";
|
2019-10-27 11:22:53 -04:00
|
|
|
path?: string;
|
|
|
|
}
|
2020-04-28 10:43:58 -04:00
|
|
|
|
|
|
|
export interface WritePermissionDescriptor {
|
|
|
|
name: "write";
|
|
|
|
path?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface NetPermissionDescriptor {
|
2019-10-27 11:22:53 -04:00
|
|
|
name: "net";
|
|
|
|
url?: string;
|
|
|
|
}
|
2020-04-28 10:43:58 -04:00
|
|
|
|
|
|
|
export interface EnvPermissionDescriptor {
|
2019-10-27 11:22:53 -04:00
|
|
|
name: "env";
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
2020-04-28 10:43:58 -04:00
|
|
|
|
|
|
|
export interface PluginPermissionDescriptor {
|
2019-12-05 15:30:20 -05:00
|
|
|
name: "plugin";
|
|
|
|
}
|
2020-04-28 10:43:58 -04:00
|
|
|
|
|
|
|
export interface HrtimePermissionDescriptor {
|
2019-10-27 11:22:53 -04:00
|
|
|
name: "hrtime";
|
|
|
|
}
|
2020-04-28 10:43:58 -04:00
|
|
|
|
|
|
|
export type PermissionDescriptor =
|
2019-10-27 11:22:53 -04:00
|
|
|
| RunPermissionDescriptor
|
2020-04-28 10:43:58 -04:00
|
|
|
| ReadPermissionDescriptor
|
|
|
|
| WritePermissionDescriptor
|
2019-10-27 11:22:53 -04:00
|
|
|
| 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
|
|
|
export class PermissionStatus {
|
|
|
|
constructor(public state: PermissionState) {}
|
|
|
|
// TODO(kt3k): implement onchange handler
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Permissions {
|
2020-03-20 09:38:34 -04:00
|
|
|
query(desc: PermissionDescriptor): Promise<PermissionStatus> {
|
2020-03-09 10:18:02 -04:00
|
|
|
const state = permissionsOps.query(desc);
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve(new PermissionStatus(state));
|
2019-10-27 11:22:53 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
revoke(desc: PermissionDescriptor): Promise<PermissionStatus> {
|
2020-03-09 10:18:02 -04:00
|
|
|
const state = permissionsOps.revoke(desc);
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve(new PermissionStatus(state));
|
2019-10-27 11:22:53 -04:00
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
request(desc: PermissionDescriptor): Promise<PermissionStatus> {
|
2020-03-09 10:18:02 -04:00
|
|
|
const state = permissionsOps.request(desc);
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve(new PermissionStatus(state));
|
2019-11-11 10:33:29 -05:00
|
|
|
}
|
2019-03-04 11:04:19 -05:00
|
|
|
}
|
2019-10-27 11:22:53 -04:00
|
|
|
|
|
|
|
export const permissions = new Permissions();
|