feat: add log-server (ring buffer) and permission-manager (grant/revoke/check) system servers
This commit is contained in:
82
crates/servers/permission-manager/src/server.rs
Normal file
82
crates/servers/permission-manager/src/server.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use server_framework::Server;
|
||||
|
||||
use crate::protocol::{CapabilityKind, PermissionRequest, PermissionResponse};
|
||||
|
||||
const MAX_PROCESSES: usize = 64;
|
||||
const NUM_KINDS: usize = 9;
|
||||
|
||||
pub struct PermissionManager {
|
||||
grants: [[bool; NUM_KINDS]; MAX_PROCESSES],
|
||||
}
|
||||
|
||||
impl PermissionManager {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
grants: [[false; NUM_KINDS]; MAX_PROCESSES],
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
let idx = process as usize;
|
||||
if idx >= MAX_PROCESSES {
|
||||
return 0;
|
||||
}
|
||||
let mut bits = 0u16;
|
||||
for i in 0..NUM_KINDS {
|
||||
if self.grants[idx][i] {
|
||||
bits |= 1 << i;
|
||||
}
|
||||
}
|
||||
bits
|
||||
}
|
||||
}
|
||||
|
||||
impl Server for PermissionManager {
|
||||
type Request = PermissionRequest;
|
||||
type Response = PermissionResponse;
|
||||
|
||||
fn handle(&mut self, request: PermissionRequest) -> PermissionResponse {
|
||||
match request {
|
||||
PermissionRequest::RequestCapability { process, kind } => {
|
||||
let idx = process as usize;
|
||||
if idx >= MAX_PROCESSES {
|
||||
return PermissionResponse::Denied;
|
||||
}
|
||||
self.grants[idx][Self::kind_index(kind)] = true;
|
||||
PermissionResponse::Granted
|
||||
}
|
||||
PermissionRequest::RevokeCapability { process, kind } => {
|
||||
let idx = process as usize;
|
||||
if idx >= MAX_PROCESSES {
|
||||
return PermissionResponse::Denied;
|
||||
}
|
||||
self.grants[idx][Self::kind_index(kind)] = false;
|
||||
PermissionResponse::Revoked
|
||||
}
|
||||
PermissionRequest::CheckCapability { process, kind } => {
|
||||
let idx = process as usize;
|
||||
if idx >= MAX_PROCESSES {
|
||||
return PermissionResponse::HasCapability(false);
|
||||
}
|
||||
PermissionResponse::HasCapability(self.grants[idx][Self::kind_index(kind)])
|
||||
}
|
||||
PermissionRequest::ListGranted { process } => {
|
||||
PermissionResponse::GrantedList(self.granted_bits(process))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user