Skip to content

Commit 4d5c321

Browse files
committed
feat(permission): update
1 parent 07cecc3 commit 4d5c321

File tree

5 files changed

+236
-295
lines changed

5 files changed

+236
-295
lines changed

cli/flags.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub struct DenoFlags {
5454
pub allow_env: bool,
5555
pub allow_run: bool,
5656
pub allow_hrtime: bool,
57-
pub no_prompts: bool,
5857
pub no_fetch: bool,
5958
pub seed: Option<u64>,
6059
pub v8_flags: Option<Vec<String>>,
@@ -118,11 +117,6 @@ fn add_run_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
118117
.long("allow-all")
119118
.help("Allow all permissions"),
120119
)
121-
.arg(
122-
Arg::with_name("no-prompt")
123-
.long("no-prompt")
124-
.help("Do not use prompts"),
125-
)
126120
.arg(
127121
Arg::with_name("no-fetch")
128122
.long("no-fetch")
@@ -707,9 +701,6 @@ fn parse_run_args(mut flags: DenoFlags, matches: &ArgMatches) -> DenoFlags {
707701
flags.allow_write = true;
708702
flags.allow_hrtime = true;
709703
}
710-
if matches.is_present("no-prompt") {
711-
flags.no_prompts = true;
712-
}
713704
if matches.is_present("no-fetch") {
714705
flags.no_fetch = true;
715706
}

cli/js/permissions.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
22
import * as dispatch from "./dispatch.ts";
33
import { sendSync } from "./dispatch_json.ts";
4-
import * as eventTarget from "./event_target.ts";
54

65
/** Permissions as granted by the caller
76
* See: https://w3c.github.io/permissions/#permission-registry
@@ -21,47 +20,43 @@ export type PermissionState = "granted" | "denied" | "prompt";
2120
/** See: https://w3c.github.io/permissions/#permission-descriptor */
2221
interface PermissionDescriptor {
2322
name: PermissionName;
23+
url?: string;
24+
path?: string;
2425
}
2526

2627
export class Permissions {
2728
/**
2829
*/
2930
async query(desc: PermissionDescriptor): Promise<PermissionStatus> {
30-
const { state } = await sendSync(dispatch.OP_QUERY_PERMISSION, {
31-
name: desc.name
31+
const { state } = sendSync(dispatch.OP_QUERY_PERMISSION, {
32+
...desc
3233
});
33-
return new PermissionStatus(desc, state);
34+
return new PermissionStatus(state);
3435
}
3536

3637
/**
3738
*/
3839
async revoke(desc: PermissionDescriptor): Promise<PermissionStatus> {
39-
const { state } = await sendSync(dispatch.OP_REVOKE_PERMISSION, {
40-
name: desc.name
40+
const { state } = sendSync(dispatch.OP_REVOKE_PERMISSION, {
41+
...desc
4142
});
42-
return new PermissionStatus(desc, state);
43+
return new PermissionStatus(state);
4344
}
4445

4546
/**
4647
*/
4748
async request(desc: PermissionDescriptor): Promise<PermissionStatus> {
48-
const { state } = await sendSync(dispatch.OP_REQUEST_PERMISSION, {
49-
name: desc.name
49+
const { state } = sendSync(dispatch.OP_REQUEST_PERMISSION, {
50+
...desc
5051
});
51-
return new PermissionStatus(desc, state);
52+
return new PermissionStatus(state);
5253
}
5354
}
5455

5556
export const permissions = new Permissions();
5657

5758
/** https://w3c.github.io/permissions/#permissionstatus */
58-
export class PermissionStatus extends eventTarget.EventTarget {
59-
private _desc: PermissionDescriptor;
60-
state: PermissionState;
61-
constructor(desc: PermissionDescriptor, state: PermissionState) {
62-
super();
63-
this._desc = desc;
64-
this.state = state;
65-
}
59+
export class PermissionStatus {
60+
constructor(public state: PermissionState) {}
6661
// TODO(kt3k): implement onchange handler
6762
}

cli/ops/permissions.rs

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
22
use super::dispatch_json::{Deserialize, JsonOp, Value};
33
use crate::ops::json_op;
4+
use crate::permissions;
45
use crate::state::ThreadSafeState;
56
use deno::*;
67

@@ -19,37 +20,40 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
1920
);
2021
}
2122

22-
pub fn op_permissions(
23-
state: &ThreadSafeState,
24-
_args: Value,
25-
_zero_copy: Option<PinnedBuf>,
26-
) -> Result<JsonOp, ErrBox> {
27-
Ok(JsonOp::Sync(json!({
28-
"run": state.permissions.allows_run(),
29-
"read": state.permissions.allows_read(),
30-
"write": state.permissions.allows_write(),
31-
"net": state.permissions.allows_net(),
32-
"env": state.permissions.allows_env(),
33-
"hrtime": state.permissions.allows_hrtime(),
34-
})))
35-
}
36-
3723
#[derive(Deserialize)]
3824
struct PermissionArgs {
3925
name: String,
4026
url: Option<String>,
4127
path: Option<String>,
4228
}
4329

30+
fn get_current_permission(
31+
state: &ThreadSafeState,
32+
args: &PermissionArgs,
33+
) -> Result<permissions::PermissionAccessorState, ErrBox> {
34+
Ok(
35+
state
36+
.permissions
37+
.get_permission_state(&args.name, &args.path, &args.url)?,
38+
)
39+
}
40+
41+
fn permission_state_to_json_op(
42+
state: permissions::PermissionAccessorState,
43+
) -> JsonOp {
44+
JsonOp::Sync(json!({
45+
"state": state.to_string()
46+
}))
47+
}
48+
4449
pub fn op_query_permission(
4550
state: &ThreadSafeState,
4651
args: Value,
4752
_zero_copy: Option<PinnedBuf>,
4853
) -> Result<JsonOp, ErrBox> {
4954
let args: PermissionArgs = serde_json::from_value(args)?;
50-
Ok(JsonOp::Sync(json!({
51-
"state": state.permissions.get_permission_string(args.name)?
52-
})))
55+
let perm = get_current_permission(state, &args)?;
56+
Ok(permission_state_to_json_op(perm))
5357
}
5458

5559
pub fn op_request_permission(
@@ -58,19 +62,26 @@ pub fn op_request_permission(
5862
_zero_copy: Option<PinnedBuf>,
5963
) -> Result<JsonOp, ErrBox> {
6064
let args: PermissionArgs = serde_json::from_value(value)?;
65+
let path = args.path.as_ref();
66+
let url = args.url.as_ref();
6167
let name = args.name.as_ref();
68+
let perm = get_current_permission(state, &args)?;
69+
if perm != permissions::PermissionAccessorState::Ask {
70+
return Ok(permission_state_to_json_op(perm));
71+
}
72+
6273
match name {
6374
"run" => state.permissions.request_run(),
64-
"read" => state.permissions.request_read(args.path.unwrap().as_ref()),
65-
"write" => state.permissions.request_write(args.path.unwrap().as_ref()),
66-
"net" => state.permissions.request_net(args.url.unwrap().as_ref()),
75+
"read" => state.permissions.request_read(path.unwrap().as_ref()),
76+
"write" => state.permissions.request_write(path.unwrap().as_ref()),
77+
"net" => state.permissions.request_net(url.unwrap().as_ref()),
6778
"env" => state.permissions.request_env(),
6879
"hrtime" => state.permissions.request_hrtime(),
6980
_ => Ok(()),
7081
}?;
71-
Ok(JsonOp::Sync(json!({
72-
"state": state.permissions.get_permission_string(args.name)?
73-
})))
82+
83+
let perm1 = get_current_permission(state, &args)?;
84+
Ok(permission_state_to_json_op(perm1))
7485
}
7586

7687
pub fn op_revoke_permission(
@@ -86,9 +97,9 @@ pub fn op_revoke_permission(
8697
"net" => state.permissions.revoke_net(),
8798
"env" => state.permissions.revoke_env(),
8899
"hrtime" => state.permissions.revoke_hrtime(),
89-
_ => Ok(()),
90-
}?;
91-
Ok(JsonOp::Sync(json!({
92-
"state": state.permissions.get_permission_string(args.name)?
93-
})))
100+
_ => {}
101+
};
102+
103+
let perm = get_current_permission(state, &args)?;
104+
Ok(permission_state_to_json_op(perm))
94105
}

0 commit comments

Comments
 (0)