refactor(permission-manager): deduplicate kind mapping into CapabilityKind::index/from_index
This commit is contained in:
@@ -13,6 +13,37 @@ pub enum CapabilityKind {
|
|||||||
Bluetooth,
|
Bluetooth,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CapabilityKind {
|
||||||
|
pub fn index(self) -> usize {
|
||||||
|
match self {
|
||||||
|
Self::Display => 0,
|
||||||
|
Self::Input => 1,
|
||||||
|
Self::AudioPlayback => 2,
|
||||||
|
Self::AudioCapture => 3,
|
||||||
|
Self::Camera => 4,
|
||||||
|
Self::Storage => 5,
|
||||||
|
Self::Network => 6,
|
||||||
|
Self::Gps => 7,
|
||||||
|
Self::Bluetooth => 8,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_index(v: usize) -> Option<Self> {
|
||||||
|
match v {
|
||||||
|
0 => Some(Self::Display),
|
||||||
|
1 => Some(Self::Input),
|
||||||
|
2 => Some(Self::AudioPlayback),
|
||||||
|
3 => Some(Self::AudioCapture),
|
||||||
|
4 => Some(Self::Camera),
|
||||||
|
5 => Some(Self::Storage),
|
||||||
|
6 => Some(Self::Network),
|
||||||
|
7 => Some(Self::Gps),
|
||||||
|
8 => Some(Self::Bluetooth),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum PermissionRequest {
|
pub enum PermissionRequest {
|
||||||
RequestCapability { process: u16, kind: CapabilityKind },
|
RequestCapability { process: u16, kind: CapabilityKind },
|
||||||
@@ -30,46 +61,18 @@ pub enum PermissionResponse {
|
|||||||
GrantedList(u16),
|
GrantedList(u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_kind(kind: CapabilityKind) -> usize {
|
|
||||||
match kind {
|
|
||||||
CapabilityKind::Display => 0,
|
|
||||||
CapabilityKind::Input => 1,
|
|
||||||
CapabilityKind::AudioPlayback => 2,
|
|
||||||
CapabilityKind::AudioCapture => 3,
|
|
||||||
CapabilityKind::Camera => 4,
|
|
||||||
CapabilityKind::Storage => 5,
|
|
||||||
CapabilityKind::Network => 6,
|
|
||||||
CapabilityKind::Gps => 7,
|
|
||||||
CapabilityKind::Bluetooth => 8,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decode_kind(v: usize) -> Option<CapabilityKind> {
|
|
||||||
match v {
|
|
||||||
0 => Some(CapabilityKind::Display),
|
|
||||||
1 => Some(CapabilityKind::Input),
|
|
||||||
2 => Some(CapabilityKind::AudioPlayback),
|
|
||||||
3 => Some(CapabilityKind::AudioCapture),
|
|
||||||
4 => Some(CapabilityKind::Camera),
|
|
||||||
5 => Some(CapabilityKind::Storage),
|
|
||||||
6 => Some(CapabilityKind::Network),
|
|
||||||
7 => Some(CapabilityKind::Gps),
|
|
||||||
8 => Some(CapabilityKind::Bluetooth),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encode for PermissionRequest {
|
impl Encode for PermissionRequest {
|
||||||
fn encode(&self) -> [usize; 4] {
|
fn encode(&self) -> [usize; 4] {
|
||||||
match self {
|
match self {
|
||||||
PermissionRequest::RequestCapability { process, kind } => {
|
PermissionRequest::RequestCapability { process, kind } => {
|
||||||
[0, *process as usize, encode_kind(*kind), 0]
|
[0, *process as usize, kind.index(), 0]
|
||||||
}
|
}
|
||||||
PermissionRequest::RevokeCapability { process, kind } => {
|
PermissionRequest::RevokeCapability { process, kind } => {
|
||||||
[1, *process as usize, encode_kind(*kind), 0]
|
[1, *process as usize, kind.index(), 0]
|
||||||
}
|
}
|
||||||
PermissionRequest::CheckCapability { process, kind } => {
|
PermissionRequest::CheckCapability { process, kind } => {
|
||||||
[2, *process as usize, encode_kind(*kind), 0]
|
[2, *process as usize, kind.index(), 0]
|
||||||
}
|
}
|
||||||
PermissionRequest::ListGranted { process } => {
|
PermissionRequest::ListGranted { process } => {
|
||||||
[3, *process as usize, 0, 0]
|
[3, *process as usize, 0, 0]
|
||||||
@@ -83,15 +86,15 @@ impl Decode for PermissionRequest {
|
|||||||
match words[0] {
|
match words[0] {
|
||||||
0 => Some(PermissionRequest::RequestCapability {
|
0 => Some(PermissionRequest::RequestCapability {
|
||||||
process: words[1] as u16,
|
process: words[1] as u16,
|
||||||
kind: decode_kind(words[2])?,
|
kind: CapabilityKind::from_index(words[2])?,
|
||||||
}),
|
}),
|
||||||
1 => Some(PermissionRequest::RevokeCapability {
|
1 => Some(PermissionRequest::RevokeCapability {
|
||||||
process: words[1] as u16,
|
process: words[1] as u16,
|
||||||
kind: decode_kind(words[2])?,
|
kind: CapabilityKind::from_index(words[2])?,
|
||||||
}),
|
}),
|
||||||
2 => Some(PermissionRequest::CheckCapability {
|
2 => Some(PermissionRequest::CheckCapability {
|
||||||
process: words[1] as u16,
|
process: words[1] as u16,
|
||||||
kind: decode_kind(words[2])?,
|
kind: CapabilityKind::from_index(words[2])?,
|
||||||
}),
|
}),
|
||||||
3 => Some(PermissionRequest::ListGranted {
|
3 => Some(PermissionRequest::ListGranted {
|
||||||
process: words[1] as u16,
|
process: words[1] as u16,
|
||||||
|
|||||||
@@ -16,20 +16,6 @@ impl PermissionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kind_index(kind: CapabilityKind) -> usize {
|
|
||||||
match kind {
|
|
||||||
CapabilityKind::Display => 0,
|
|
||||||
CapabilityKind::Input => 1,
|
|
||||||
CapabilityKind::AudioPlayback => 2,
|
|
||||||
CapabilityKind::AudioCapture => 3,
|
|
||||||
CapabilityKind::Camera => 4,
|
|
||||||
CapabilityKind::Storage => 5,
|
|
||||||
CapabilityKind::Network => 6,
|
|
||||||
CapabilityKind::Gps => 7,
|
|
||||||
CapabilityKind::Bluetooth => 8,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn granted_bits(&self, process: u16) -> u16 {
|
fn granted_bits(&self, process: u16) -> u16 {
|
||||||
let idx = process as usize;
|
let idx = process as usize;
|
||||||
if idx >= MAX_PROCESSES {
|
if idx >= MAX_PROCESSES {
|
||||||
@@ -56,7 +42,7 @@ impl Server for PermissionManager {
|
|||||||
if idx >= MAX_PROCESSES {
|
if idx >= MAX_PROCESSES {
|
||||||
return PermissionResponse::Denied;
|
return PermissionResponse::Denied;
|
||||||
}
|
}
|
||||||
self.grants[idx][Self::kind_index(kind)] = true;
|
self.grants[idx][kind.index()] = true;
|
||||||
PermissionResponse::Granted
|
PermissionResponse::Granted
|
||||||
}
|
}
|
||||||
PermissionRequest::RevokeCapability { process, kind } => {
|
PermissionRequest::RevokeCapability { process, kind } => {
|
||||||
@@ -64,7 +50,7 @@ impl Server for PermissionManager {
|
|||||||
if idx >= MAX_PROCESSES {
|
if idx >= MAX_PROCESSES {
|
||||||
return PermissionResponse::Denied;
|
return PermissionResponse::Denied;
|
||||||
}
|
}
|
||||||
self.grants[idx][Self::kind_index(kind)] = false;
|
self.grants[idx][kind.index()] = false;
|
||||||
PermissionResponse::Revoked
|
PermissionResponse::Revoked
|
||||||
}
|
}
|
||||||
PermissionRequest::CheckCapability { process, kind } => {
|
PermissionRequest::CheckCapability { process, kind } => {
|
||||||
@@ -72,7 +58,7 @@ impl Server for PermissionManager {
|
|||||||
if idx >= MAX_PROCESSES {
|
if idx >= MAX_PROCESSES {
|
||||||
return PermissionResponse::HasCapability(false);
|
return PermissionResponse::HasCapability(false);
|
||||||
}
|
}
|
||||||
PermissionResponse::HasCapability(self.grants[idx][Self::kind_index(kind)])
|
PermissionResponse::HasCapability(self.grants[idx][kind.index()])
|
||||||
}
|
}
|
||||||
PermissionRequest::ListGranted { process } => {
|
PermissionRequest::ListGranted { process } => {
|
||||||
PermissionResponse::GrantedList(self.granted_bits(process))
|
PermissionResponse::GrantedList(self.granted_bits(process))
|
||||||
|
|||||||
Reference in New Issue
Block a user