Skip to content

Commit

Permalink
server: Add Lock implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
  • Loading branch information
warusadura committed Nov 19, 2024
1 parent de29404 commit 73676bd
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
31 changes: 27 additions & 4 deletions server/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use oo7::{
portal::Keyring,
};
use tokio::sync::{Mutex, RwLock};
use zbus::{interface, object_server::SignalEmitter, zvariant};
use zbus::{interface, object_server::SignalEmitter, proxy::Defaults, zvariant};
use zvariant::{ObjectPath, OwnedObjectPath};

use crate::{error::Error, item, service_manager::ServiceManager};
use crate::{error::Error, item, service_manager::ServiceManager, Service};

#[derive(Debug, Clone)]
pub struct Collection {
Expand Down Expand Up @@ -130,9 +130,9 @@ impl Collection {
) -> zbus::Result<()>;

#[zbus(signal, name = "ItemChanged")]
async fn item_changed(
pub async fn item_changed(
signal_emitter: &SignalEmitter<'_>,
item: OwnedObjectPath,
item: &OwnedObjectPath,
) -> zbus::Result<()>;
}

Expand Down Expand Up @@ -205,6 +205,29 @@ impl Collection {
None
}

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

for item in items.iter() {
item.set_locked(locked).await?;
}

let manager = self.manager.lock().await;

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

let service_path = oo7::dbus::api::Service::PATH.as_ref().unwrap();
let signal_emitter = manager.signal_emitter(service_path)?;
Service::collection_changed(&signal_emitter, &self.path).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
20 changes: 19 additions & 1 deletion server/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use oo7::{
use tokio::sync::Mutex;
use zbus::zvariant::{ObjectPath, OwnedObjectPath};

use crate::service_manager::ServiceManager;
use crate::{collection::Collection, service_manager::ServiceManager};

#[derive(Debug, Clone)]
pub struct Item {
Expand All @@ -21,6 +21,7 @@ pub struct Item {
inner: Arc<Mutex<oo7::portal::Item>>,
// Other attributes
manager: Arc<Mutex<ServiceManager>>,
collection_path: OwnedObjectPath,
path: OwnedObjectPath,
}

Expand Down Expand Up @@ -160,6 +161,7 @@ impl Item {
Self {
locked: Arc::new(AtomicBool::new(locked)),
inner: Arc::new(Mutex::new(item)),
collection_path: collection_path.clone(),
path: OwnedObjectPath::try_from(format!("{}/{}", collection_path, item_index)).unwrap(),
manager,
}
Expand All @@ -168,4 +170,20 @@ impl Item {
pub fn path(&self) -> &OwnedObjectPath {
&self.path
}

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

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

let signal_emitter = manager.signal_emitter(&self.collection_path)?;
Collection::item_changed(&signal_emitter, &self.path).await?;

Ok(())
}
}
29 changes: 25 additions & 4 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,30 @@ impl Service {
#[zbus(out_args("locked", "prompt"))]
pub async fn lock(
&mut self,
_objects: Vec<OwnedObjectPath>,
objects: Vec<OwnedObjectPath>,
) -> 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.set_locked(true).await?;
locked.push(object.clone());
tracing::info!("Collection: {} is locked.", object);
break;
} else if let Some(item) = collection.item_from_path(object).await {
item.set_locked(true).await?;
locked.push(object.clone());
tracing::info!("Item: {} is locked.", object);
break;
}
tracing::info!("Object: {} does not exist.", object);
}
}

Ok((locked, prompt))
}

#[zbus(out_args("secrets"))]
Expand Down Expand Up @@ -231,9 +252,9 @@ impl Service {
) -> zbus::Result<()>;

#[zbus(signal, name = "CollectionChanged")]
async fn collection_changed(
pub async fn collection_changed(
signal_emitter: &SignalEmitter<'_>,
collection: OwnedObjectPath,
collection: &OwnedObjectPath,
) -> zbus::Result<()>;
}

Expand Down
15 changes: 14 additions & 1 deletion server/src/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{collections::HashMap, sync::Arc};

use zbus::zvariant::OwnedObjectPath;
use zbus::zvariant::{ObjectPath, OwnedObjectPath};

use crate::session::Session;

Expand All @@ -25,6 +25,19 @@ impl ServiceManager {
self.connection.object_server()
}

pub fn signal_emitter<'a, P>(
&self,
path: P,
) -> Result<zbus::object_server::SignalEmitter<'a>, oo7::dbus::ServiceError>
where
P: TryInto<ObjectPath<'a>>,
P::Error: Into<zbus::Error>,
{
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 73676bd

Please sign in to comment.