Skip to content

Commit

Permalink
server: Add Lock implementation
Browse files Browse the repository at this point in the history
This change adds Secret Service method Lock implementation.

Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
  • Loading branch information
warusadura committed Nov 3, 2024
1 parent eef3b5d commit 13e500c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
25 changes: 24 additions & 1 deletion server/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Collection {
#[zbus(signal, name = "ItemChanged")]
async fn item_changed(
signal_emitter: &SignalEmitter<'_>,
item: OwnedObjectPath,
item: &OwnedObjectPath,
) -> zbus::Result<()>;
}

Expand Down Expand Up @@ -157,6 +157,29 @@ impl Collection {
self.alias.lock().await.clone()
}

pub async fn set_locked(&self, locked: bool) -> Result<(), ServiceError> {
let items = self.items.lock().await;

for item in items.iter() {
if !item.is_locked().await {
item.set_locked(true).await?;
}
let manager = self.manager.lock().await;
let signal_emitter = manager.signal_emitter(self.path.clone())?;
Self::item_changed(&signal_emitter, item.path()).await?;
}

if !self.is_locked().await {
self.locked
.store(locked, std::sync::atomic::Ordering::Relaxed);
let manager = self.manager.lock().await;
let signal_emitter = manager.signal_emitter(self.path.clone())?;
self.locked_changed(&signal_emitter).await?;
}

Ok(())
}

pub async fn dispatch_items(&self) -> Result<(), Error> {
let keyring_items = self.keyring.items().await;
let mut items = self.items.lock().await;
Expand Down
15 changes: 13 additions & 2 deletions server/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Item {
locked: Arc<AtomicBool>,
inner: Arc<Mutex<oo7::portal::Item>>,
// Other attributes
_manager: Arc<Mutex<ServiceManager>>,
manager: Arc<Mutex<ServiceManager>>,
path: OwnedObjectPath,
}

Expand Down Expand Up @@ -94,11 +94,22 @@ impl Item {
locked: Arc::new(AtomicBool::new(locked)),
inner: Arc::new(Mutex::new(item)),
path: OwnedObjectPath::try_from(format!("{}/{}", collection_path, item_index)).unwrap(),
_manager: manager,
manager,
}
}

pub fn path(&self) -> &OwnedObjectPath {
&self.path
}

pub async fn set_locked(&self, locked: bool) -> Result<(), ServiceError> {
let manager = self.manager.lock().await;
let signal_emitter = manager.signal_emitter(self.path.clone())?;

self.locked
.store(locked, std::sync::atomic::Ordering::Relaxed);
self.locked_changed(&signal_emitter).await?;

Ok(())
}
}
24 changes: 21 additions & 3 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,27 @@ impl Service {
#[zbus(out_args("locked", "prompt"))]
pub async fn lock(
&mut self,
_objects: Vec<OwnedObjectPath>,
objects: Vec<OwnedObjectPath>,
#[zbus(signal_emitter)] signal_emitter: SignalEmitter<'_>,
) -> Result<(Vec<OwnedObjectPath>, ObjectPath), ServiceError> {
todo!()
let mut locked: Vec<OwnedObjectPath> = Vec::new();
let prompt = ObjectPath::default();
let collections = self.collections.lock().await;

for object in &objects {
for collection in collections.iter() {
if object == collection.path() || collection.items().await.contains(object) {
collection.set_locked(true).await?;
locked.push(object.clone());
Self::collection_changed(&signal_emitter, collection.path()).await?;
tracing::info!("Object: {} is locked.", object);
break;
}
tracing::info!("Object: {} does not exist.", object);
}
}

Ok((locked, prompt))
}

#[zbus(out_args("secrets"))]
Expand Down Expand Up @@ -182,7 +200,7 @@ impl Service {
#[zbus(signal, name = "CollectionChanged")]
async fn collection_changed(
signal_emitter: &SignalEmitter<'_>,
collection: OwnedObjectPath,
collection: &OwnedObjectPath,
) -> zbus::Result<()>;
}

Expand Down
9 changes: 9 additions & 0 deletions server/src/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ impl ServiceManager {
self.connection.object_server()
}

pub fn signal_emitter(
&self,
path: OwnedObjectPath,
) -> Result<zbus::object_server::SignalEmitter, oo7::dbus::ServiceError> {
let signal_emitter = zbus::object_server::SignalEmitter::new(&self.connection, path)?;

Ok(signal_emitter)
}

pub fn n_sessions(&self) -> usize {
self.sessions.len()
}
Expand Down

0 comments on commit 13e500c

Please sign in to comment.