Skip to content

Commit

Permalink
feat: perm-req
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Nov 8, 2019
1 parent adab69b commit 82d03c8
Show file tree
Hide file tree
Showing 4 changed files with 384 additions and 64 deletions.
1 change: 1 addition & 0 deletions cli/js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export let OP_GLOBAL_TIMER: number;
export let OP_NOW: number;
export let OP_QUERY_PERMISSION: number;
export let OP_REVOKE_PERMISSION: number;
export let OP_REQUEST_PERMISSION: number;
export let OP_CREATE_WORKER: number;
export let OP_HOST_GET_WORKER_CLOSED: number;
export let OP_HOST_POST_MESSAGE: number;
Expand Down
13 changes: 13 additions & 0 deletions cli/js/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ export class Permissions {
const { state } = sendSync(dispatch.OP_REVOKE_PERMISSION, desc);
return new PermissionStatus(state);
}

/** Requests the permission.
* const status = await Deno.permissions.request({ name: "env" });
* if (status.state === "granted") {
* home = Deno.homeDir();
* } else {
* console.log("'env' permission is denied.");
* }
*/
async request(desc: PermissionDescriptor): Promise<PermissionStatus> {
const { state } = sendSync(dispatch.OP_REQUEST_PERMISSION, desc);
return new PermissionStatus(state);
}
}

export const permissions = new Permissions();
33 changes: 33 additions & 0 deletions cli/ops/permissions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::deno_error::type_error;
use crate::ops::json_op;
use crate::state::ThreadSafeState;
use deno::*;
Expand All @@ -13,6 +14,10 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
"revoke_permission",
s.core_op(json_op(s.stateful_op(op_revoke_permission))),
);
i.register_op(
"request_permission",
s.core_op(json_op(s.stateful_op(op_request_permission))),
);
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -58,3 +63,31 @@ pub fn op_revoke_permission(
)?;
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
}

pub fn op_request_permission(
state: &ThreadSafeState,
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: PermissionArgs = serde_json::from_value(args)?;
let perm = match args.name.as_ref() {
"run" => Ok(state.permissions.request_run()),
"read" => Ok(
state
.permissions
.request_read(&args.path.as_ref().map(String::as_str)),
),
"write" => Ok(
state
.permissions
.request_write(&args.path.as_ref().map(String::as_str)),
),
"net" => state
.permissions
.request_net(&args.url.as_ref().map(String::as_str)),
"env" => Ok(state.permissions.request_env()),
"hrtime" => Ok(state.permissions.request_hrtime()),
n => Err(type_error(format!("No such permission name: {}", n))),
}?;
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
}
Loading

0 comments on commit 82d03c8

Please sign in to comment.