From 19a07cdf1d7207ff4be4e1eb109a41e668048d17 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 14 Mar 2025 12:51:33 +0200 Subject: [PATCH] remove direct memory access --- src/app.rs | 6 +++--- src/app/browser.rs | 4 ++-- src/app/browser/window.rs | 4 ++-- src/app/browser/window/header.rs | 6 +++--- src/app/browser/window/header/bar.rs | 6 +++--- src/app/browser/window/header/bar/menu.rs | 6 +++--- src/app/browser/window/tab.rs | 6 +++--- src/app/browser/window/tab/item.rs | 4 ++-- src/app/browser/window/tab/item/client.rs | 8 ++++---- src/app/browser/window/tab/item/page.rs | 6 +++--- .../browser/window/tab/item/page/navigation.rs | 4 ++-- .../window/tab/item/page/navigation/bookmark.rs | 6 +++--- .../window/tab/item/page/navigation/request.rs | 10 +++++----- .../page/navigation/request/identity/common.rs | 11 +++++++---- .../navigation/request/identity/common/form.rs | 6 +++--- .../request/identity/common/form/drop.rs | 4 ++-- .../request/identity/common/form/exit.rs | 6 +++--- .../request/identity/common/form/list.rs | 4 ++-- .../request/identity/common/form/list/item.rs | 6 +++--- .../identity/common/form/list/item/is_active.rs | 4 ++-- .../request/identity/common/form/save.rs | 6 +++--- .../identity/common/form/save/certificate.rs | 4 ++-- .../tab/item/page/navigation/request/search.rs | 5 +++-- .../item/page/navigation/request/search/form.rs | 4 ++-- .../page/navigation/request/search/form/drop.rs | 6 +++--- .../page/navigation/request/search/form/list.rs | 4 ++-- .../item/page/navigation/request/suggestion.rs | 17 +++++++---------- src/profile/bookmark.rs | 10 +++++----- src/profile/history.rs | 10 +++++----- src/profile/identity/auth/memory.rs | 14 +++++++------- src/profile/identity/memory.rs | 12 ++++++------ src/profile/search/memory.rs | 14 +++++++------- 32 files changed, 112 insertions(+), 111 deletions(-) diff --git a/src/app.rs b/src/app.rs index c4a6a9cb..7f2dd3e9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,12 +10,12 @@ use gtk::{ prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt}, }; use sqlite::Transaction; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; const APPLICATION_ID: &str = "io.github.yggverse.Yoda"; pub struct App { - profile: Rc, + profile: Arc, application: Application, } @@ -30,7 +30,7 @@ impl App { .build(); // Init components - let profile = Rc::new(profile); + let profile = Arc::new(profile); let browser = Rc::new(Browser::build(&profile)); // Prevent startup warning @TODO diff --git a/src/app/browser.rs b/src/app/browser.rs index df5f3c76..de813393 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -18,7 +18,7 @@ use gtk::{ FileLauncher, }; use sqlite::Transaction; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub struct Browser { pub action: Rc, @@ -30,7 +30,7 @@ impl Browser { // Constructors /// Build new `Self` - pub fn build(profile: &Rc) -> Browser { + pub fn build(profile: &Arc) -> Browser { // Init components let action = Rc::new(Action::new()); let window = Rc::new(Window::build(profile, &action)); diff --git a/src/app/browser/window.rs b/src/app/browser/window.rs index ecedac68..985a1a81 100644 --- a/src/app/browser/window.rs +++ b/src/app/browser/window.rs @@ -11,7 +11,7 @@ use anyhow::Result; use gtk::{prelude::BoxExt, Box, Orientation}; use header::Header; use sqlite::Transaction; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; use tab::Tab; pub struct Window { @@ -24,7 +24,7 @@ impl Window { // Constructors /// Build new `Self` - pub fn build(profile: &Rc, browser_action: &Rc) -> Self { + pub fn build(profile: &Arc, browser_action: &Rc) -> Self { // Init local actions let action = Rc::new(Action::new()); diff --git a/src/app/browser/window/header.rs b/src/app/browser/window/header.rs index 24e33a2b..d495ef9f 100644 --- a/src/app/browser/window/header.rs +++ b/src/app/browser/window/header.rs @@ -4,12 +4,12 @@ use super::{Action as WindowAction, BrowserAction, Profile}; use adw::{TabView, ToolbarView}; use bar::Bar; use gtk::Box; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub trait Header { fn header( action: (&Rc, &Rc), - profile: &Rc, + profile: &Arc, tab_view: &TabView, ) -> Self; } @@ -20,7 +20,7 @@ impl Header for ToolbarView { /// Build new `Self` fn header( (browser_action, window_action): (&Rc, &Rc), - profile: &Rc, + profile: &Arc, tab_view: &TabView, ) -> Self { let toolbar_view = ToolbarView::builder().build(); diff --git a/src/app/browser/window/header/bar.rs b/src/app/browser/window/header/bar.rs index 4addca71..6badb1a4 100644 --- a/src/app/browser/window/header/bar.rs +++ b/src/app/browser/window/header/bar.rs @@ -9,12 +9,12 @@ use tab::Tab; use super::{BrowserAction, Profile, WindowAction}; use adw::{TabBar, TabView}; use gtk::{prelude::BoxExt, Box, MenuButton, Orientation}; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub trait Bar { fn bar( action: (&Rc, &Rc), - profile: &Rc, + profile: &Arc, view: &TabView, ) -> Self; } @@ -25,7 +25,7 @@ impl Bar for Box { /// Build new `Self` fn bar( (browser_action, window_action): (&Rc, &Rc), - profile: &Rc, + profile: &Arc, view: &TabView, ) -> Self { let g_box = Box::builder() diff --git a/src/app/browser/window/header/bar/menu.rs b/src/app/browser/window/header/bar/menu.rs index 2c9ff877..a14b7af3 100644 --- a/src/app/browser/window/header/bar/menu.rs +++ b/src/app/browser/window/header/bar/menu.rs @@ -6,13 +6,13 @@ use gtk::{ Align, MenuButton, }; use indexmap::IndexMap; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; // Config options const LABEL_MAX_LENGTH: usize = 28; pub trait Menu { - fn menu(action: (&Rc, &Rc), profile: &Rc) -> Self; + fn menu(action: (&Rc, &Rc), profile: &Arc) -> Self; } #[rustfmt::skip] // @TODO template builder? @@ -22,7 +22,7 @@ impl Menu for MenuButton { /// Build new `Self` fn menu( (browser_action, window_action): (&Rc, &Rc), - profile: &Rc, + profile: &Arc, ) -> Self { // Main let main = gio::Menu::new(); diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index a41aed68..41431af6 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -13,13 +13,13 @@ pub use item::Item; use menu::Menu; use sourceview::prelude::IsA; use sqlite::Transaction; -use std::{cell::RefCell, collections::HashMap, rc::Rc}; +use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc}; // Main pub struct Tab { browser_action: Rc, window_action: Rc, - profile: Rc, + profile: Arc, index: Rc>>>, pub action: Rc, pub tab_view: TabView, @@ -30,7 +30,7 @@ impl Tab { /// Build new `Self` pub fn build( - profile: &Rc, + profile: &Arc, (browser_action, window_action): (&Rc, &Rc), ) -> Self { let action = Rc::new(Action::new()); diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index 3dd04793..8d8c2cba 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -15,7 +15,7 @@ use gtk::{ }; use page::Page; use sqlite::Transaction; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub struct Item { // Multi-protocol handler @@ -32,7 +32,7 @@ impl Item { /// Build new `Self` pub fn build( (tab_page, target_child): (&TabPage, &Box), - profile: &Rc, + profile: &Arc, (browser_action, window_action, tab_action): ( &Rc, &Rc, diff --git a/src/app/browser/window/tab/item/client.rs b/src/app/browser/window/tab/item/client.rs index a9a4ca64..11094e15 100644 --- a/src/app/browser/window/tab/item/client.rs +++ b/src/app/browser/window/tab/item/client.rs @@ -9,21 +9,21 @@ use gtk::{ glib::{Uri, UriFlags}, prelude::{ActionExt, CancellableExt}, }; -use std::{cell::Cell, rc::Rc}; +use std::{cell::Cell, rc::Rc, sync::Arc}; /// Multi-protocol client API for tab `Item` pub struct Client { cancellable: Cell, driver: Rc, page: Rc, - profile: Rc, + profile: Arc, } impl Client { // Constructors /// Create new `Self` - pub fn init(profile: &Rc, page: &Rc) -> Self { + pub fn init(profile: &Arc, page: &Rc) -> Self { Self { cancellable: Cell::new(Cancellable::new()), driver: Rc::new(Driver::build(page)), @@ -108,7 +108,7 @@ impl Client { /// Create request using async DNS resolver (slow method) /// * return suggestion [Uri](https://docs.gtk.org/glib/struct.Uri.html) on failure (to handle as redirect) fn lookup( - profile: &Rc, + profile: &Arc, query: &str, cancellable: Cancellable, callback: impl FnOnce(Rc, Cancellable, Result) + 'static, diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index bf4dc36d..c2b2e29a 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -12,10 +12,10 @@ use input::Input; use navigation::Navigation; use search::Search; use sqlite::Transaction; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub struct Page { - pub profile: Rc, + pub profile: Arc, // Actions pub browser_action: Rc, pub item_action: Rc, @@ -38,7 +38,7 @@ impl Page { // Constructors pub fn build( - profile: &Rc, + profile: &Arc, (browser_action, window_action, tab_action, item_action): ( &Rc, &Rc, diff --git a/src/app/browser/window/tab/item/page/navigation.rs b/src/app/browser/window/tab/item/page/navigation.rs index 8503845f..d3355186 100644 --- a/src/app/browser/window/tab/item/page/navigation.rs +++ b/src/app/browser/window/tab/item/page/navigation.rs @@ -18,7 +18,7 @@ use home::Home; use reload::Reload; use request::Request; use sqlite::Transaction; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; const MARGIN: i32 = 6; const SPACING: i32 = 6; @@ -30,7 +30,7 @@ pub struct Navigation { impl Navigation { pub fn build( - profile: &Rc, + profile: &Arc, (window_action, tab_action, item_action): ( &Rc, &Rc, diff --git a/src/app/browser/window/tab/item/page/navigation/bookmark.rs b/src/app/browser/window/tab/item/page/navigation/bookmark.rs index 86c56842..1c87ba75 100644 --- a/src/app/browser/window/tab/item/page/navigation/bookmark.rs +++ b/src/app/browser/window/tab/item/page/navigation/bookmark.rs @@ -3,18 +3,18 @@ use gtk::{ prelude::{ActionExt, ButtonExt, EditableExt, WidgetExt}, Button, Entry, }; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; const ICON_NAME: (&str, &str) = ("non-starred-symbolic", "starred-symbolic"); const TOOLTIP_TEXT: (&str, &str) = ("Add Bookmark", "Remove Bookmark"); pub trait Bookmark { - fn bookmark(action: &Rc, profile: &Rc, request: &Entry) -> Self; + fn bookmark(action: &Rc, profile: &Arc, request: &Entry) -> Self; fn update(&self, profile: &Profile, request: &Entry); } impl Bookmark for Button { - fn bookmark(action: &Rc, profile: &Rc, request: &Entry) -> Self { + fn bookmark(action: &Rc, profile: &Arc, request: &Entry) -> Self { let button = Button::builder() .action_name(format!( "{}.{}", diff --git a/src/app/browser/window/tab/item/page/navigation/request.rs b/src/app/browser/window/tab/item/page/navigation/request.rs index 587be7db..73d19d0b 100644 --- a/src/app/browser/window/tab/item/page/navigation/request.rs +++ b/src/app/browser/window/tab/item/page/navigation/request.rs @@ -14,7 +14,7 @@ use gtk::{ }; use primary_icon::PrimaryIcon; use sqlite::Transaction; -use std::{cell::Cell, rc::Rc}; +use std::{cell::Cell, rc::Rc, sync::Arc}; use suggestion::Suggestion; const PREFIX_DOWNLOAD: &str = "download:"; @@ -23,14 +23,14 @@ const PREFIX_SOURCE: &str = "source:"; pub struct Request { pub entry: Entry, suggestion: Rc, - profile: Rc, + profile: Arc, } impl Request { // Constructors /// Build new `Self` - pub fn build(item_action: &Rc, profile: &Rc) -> Self { + pub fn build(item_action: &Rc, profile: &Arc) -> Self { // Init main widget let entry = Entry::builder() .placeholder_text("URL or search term...") @@ -327,7 +327,7 @@ fn update_secondary_icon(entry: &Entry) { } /// Present Identity [AlertDialog](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.AlertDialog.html) for `Self` -fn show_identity_dialog(entry: &Entry, profile: &Rc) { +fn show_identity_dialog(entry: &Entry, profile: &Arc) { // connect identity traits use identity::{Common, Unsupported}; if let Some(uri) = uri(entry) { @@ -353,7 +353,7 @@ fn show_identity_dialog(entry: &Entry, profile: &Rc) { } /// Present Search providers [AlertDialog](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.AlertDialog.html) for `Self` -fn show_search_dialog(entry: &Entry, profile: &Rc) { +fn show_search_dialog(entry: &Entry, profile: &Arc) { use search::Search; AlertDialog::search(profile).present(Some(entry)) } diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common.rs index ae5e4936..7ab51604 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common.rs @@ -5,13 +5,16 @@ use crate::Profile; use action::Action as WidgetAction; use adw::AlertDialog; use gtk::{glib::Uri, prelude::EditableExt}; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; // Select options pub trait Common { - fn common(profile: &Rc, request: &Uri, callback: &Rc) - -> Self; + fn common( + profile: &Arc, + request: &Uri, + callback: &Rc, + ) -> Self; } impl Common for AlertDialog { @@ -19,7 +22,7 @@ impl Common for AlertDialog { /// Create new `Self` fn common( - profile: &Rc, + profile: &Arc, request: &Uri, callback: &Rc, ) -> Self { diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs index 74225ceb..04aa6dc4 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs @@ -19,7 +19,7 @@ use gtk::{ prelude::{BoxExt, WidgetExt}, Box, Button, Entry, Orientation, }; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub struct Form { // pub action_widget: Rc, @@ -30,14 +30,14 @@ pub struct Form { pub name: Entry, pub save: Rc, pub g_box: Box, - profile: Rc, + profile: Arc, } impl Form { // Constructors /// Create new `Self` - pub fn build(widget_action: &Rc, profile: &Rc, request: &Uri) -> Self { + pub fn build(widget_action: &Rc, profile: &Arc, request: &Uri) -> Self { // Init components let list = Rc::new(List::build(widget_action, profile, request)); let file = Rc::new(File::build(widget_action)); diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/drop.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/drop.rs index 14f1021c..85fd2283 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/drop.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/drop.rs @@ -9,7 +9,7 @@ use gtk::{ prelude::{ButtonExt, WidgetExt}, Button, }; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; // Defaults @@ -30,7 +30,7 @@ impl Drop { // Constructors /// Create new `Self` - pub fn build(profile: &Rc, list: &Rc) -> Self { + pub fn build(profile: &Arc, list: &Rc) -> Self { // Init main widget let button = Button::builder() .label(LABEL) diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs index 897d3eee..b7511327 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs @@ -8,12 +8,12 @@ use gtk::{ prelude::{ButtonExt, WidgetExt}, Button, }; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub trait Exit { fn exit( widget_action: &Rc, - profile: &Rc, + profile: &Arc, list: &Rc, request: &Uri, ) -> Self; @@ -25,7 +25,7 @@ impl Exit for Button { /// Create new `Self` fn exit( widget_action: &Rc, - profile: &Rc, + profile: &Arc, list: &Rc, request: &Uri, ) -> Self { diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list.rs index 3561a188..48443d14 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list.rs @@ -1,5 +1,5 @@ pub mod item; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; use item::Item; @@ -25,7 +25,7 @@ impl List { // Constructors /// Create new `Self` - pub fn build(widget_action: &Rc, profile: &Rc, request: &Uri) -> Self { + pub fn build(widget_action: &Rc, profile: &Arc, request: &Uri) -> Self { // Init dropdown items let guest_session = Item::new_guest_session(); let generate_pem = Item::new_generate_pem(); diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item.rs index de6e40b9..4b314b8d 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item.rs @@ -14,7 +14,7 @@ use gtk::{ gio::TlsCertificate, glib::{self, Object}, }; -use std::rc::Rc; +use std::sync::Arc; glib::wrapper! { pub struct Item(ObjectSubclass); @@ -54,7 +54,7 @@ impl Item { } pub fn new_profile_identity_id( - profile: &Rc, + profile: &Arc, profile_identity_id: i64, auth_url: &str, ) -> Result { @@ -96,7 +96,7 @@ impl Item { // Actions /// Update properties for `Self` for given `Profile` and `auth_url` - pub fn update(&self, profile: &Rc, auth_url: &str) -> Result<(), Error> { + pub fn update(&self, profile: &Arc, auth_url: &str) -> Result<(), Error> { // Update item depending on value type match self.value_enum() { Value::ProfileIdentityId(profile_identity_id) => { diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/is_active.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/is_active.rs index 8a4a6bea..a3901463 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/is_active.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/is_active.rs @@ -1,8 +1,8 @@ use crate::profile::Profile; -use std::rc::Rc; +use std::sync::Arc; pub fn new_for_profile_identity_id( - profile: &Rc, + profile: &Arc, profile_identity_id: i64, auth_url: &str, ) -> bool { diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save.rs index 89140c60..f636efca 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save.rs @@ -9,7 +9,7 @@ use gtk::{ prelude::{ButtonExt, FileExt, OutputStreamExtManual, WidgetExt}, Button, FileDialog, FileFilter, Window, }; -use std::{path::MAIN_SEPARATOR, rc::Rc}; +use std::{path::MAIN_SEPARATOR, rc::Rc, sync::Arc}; const LABEL: &str = "Export"; const TOOLTIP_TEXT: &str = "Export selected identity to file"; @@ -24,7 +24,7 @@ impl Save { // Constructors /// Create new `Self` - pub fn build(profile: &Rc, list: &Rc) -> Self { + pub fn build(profile: &Arc, list: &Rc) -> Self { // Init main widget let button = Button::builder() .label(LABEL) @@ -46,7 +46,7 @@ impl Save { button.set_sensitive(false); // Create PEM file based on option ID selected - match Certificate::build(profile.clone(), profile_identity_id) { + match Certificate::build(&profile, profile_identity_id) { Ok(certificate) => { // Init file filters related with PEM extension let filters = ListStore::new::(); diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save/certificate.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save/certificate.rs index bc9be18e..5aa42366 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save/certificate.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save/certificate.rs @@ -2,7 +2,7 @@ use anyhow::{bail, Result}; use crate::profile::Profile; use gtk::{gio::TlsCertificate, prelude::TlsCertificateExt}; -use std::rc::Rc; +use std::sync::Arc; /// Certificate details holder for export to file action pub struct Certificate { @@ -14,7 +14,7 @@ impl Certificate { // Constructors /// Create new `Self` - pub fn build(profile: Rc, profile_identity_id: i64) -> Result { + pub fn build(profile: &Arc, profile_identity_id: i64) -> Result { let record = profile.identity.database.record(profile_identity_id)?; match record { Some(identity) => Ok(Self { diff --git a/src/app/browser/window/tab/item/page/navigation/request/search.rs b/src/app/browser/window/tab/item/page/navigation/request/search.rs index 7bb4d16c..953e7031 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/search.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/search.rs @@ -10,6 +10,7 @@ use form::{list::item::Value, list::Item, Form, Query}; use gtk::prelude::{EditableExt, WidgetExt}; use sourceview::prelude::CastNone; use std::rc::Rc; +use std::sync::Arc; // Response variants const RESPONSE_APPLY: (&str, &str) = ("apply", "Apply"); @@ -17,14 +18,14 @@ const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel"); // const RESPONSE_MANAGE: (&str, &str) = ("manage", "Manage"); pub trait Search { - fn search(profile: &Rc) -> Self; + fn search(profile: &Arc) -> Self; } impl Search for AlertDialog { // Constructors /// Create new `Self` - fn search(profile: &Rc) -> Self { + fn search(profile: &Arc) -> Self { // Init child container let form = Rc::new(Form::build(profile)); diff --git a/src/app/browser/window/tab/item/page/navigation/request/search/form.rs b/src/app/browser/window/tab/item/page/navigation/request/search/form.rs index 7b62df07..fd6b53cf 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/search/form.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/search/form.rs @@ -7,7 +7,7 @@ use drop::Drop; use gtk::{prelude::BoxExt, Box, Button, Entry, Orientation}; use list::List; pub use query::Query; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub struct Form { pub drop: Button, @@ -20,7 +20,7 @@ impl Form { // Constructors /// Create new `Self` - pub fn build(profile: &Rc) -> Self { + pub fn build(profile: &Arc) -> Self { // Init components let list = Rc::new(List::build(profile)); let query = Entry::query(); diff --git a/src/app/browser/window/tab/item/page/navigation/request/search/form/drop.rs b/src/app/browser/window/tab/item/page/navigation/request/search/form/drop.rs index b3195efa..dc597c89 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/search/form/drop.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/search/form/drop.rs @@ -9,17 +9,17 @@ use gtk::{ prelude::{ButtonExt, WidgetExt}, Button, }; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; pub trait Drop { - fn drop(profile: &Rc, list: &Rc) -> Self; + fn drop(profile: &Arc, list: &Rc) -> Self; } impl Drop for Button { // Constructors /// Create new `Self` - fn drop(profile: &Rc, list: &Rc) -> Self { + fn drop(profile: &Arc, list: &Rc) -> Self { // Defaults const LABEL: &str = "Delete"; diff --git a/src/app/browser/window/tab/item/page/navigation/request/search/form/list.rs b/src/app/browser/window/tab/item/page/navigation/request/search/form/list.rs index 7a15bf67..d39433b5 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/search/form/list.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/search/form/list.rs @@ -10,7 +10,7 @@ use gtk::{ Align, Box, DropDown, Label, ListItem, Orientation, SignalListItemFactory, }; pub use item::Item; -use std::rc::Rc; +use std::sync::Arc; pub struct List { pub dropdown: DropDown, @@ -21,7 +21,7 @@ impl List { // Constructors /// Create new `Self` - pub fn build(profile: &Rc) -> Self { + pub fn build(profile: &Arc) -> Self { // Init dropdown items let new_search_provider = Item::add(); diff --git a/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs b/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs index e966d9fb..a9a763be 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/suggestion.rs @@ -17,14 +17,14 @@ use gtk::{ }; pub use item::Item; use sourceview::prelude::ListModelExt; -use std::{cell::RefCell, rc::Rc}; +use std::{cell::RefCell, rc::Rc, sync::Arc}; pub struct Suggestion { list_store: ListStore, list_view: ListView, single_selection: SingleSelection, request: Entry, - profile: Rc, + profile: Arc, popover: Popover, pub signal_handler_id: Rc>>, } @@ -33,7 +33,7 @@ impl Suggestion { // Constructors /// Create new `Self` - pub fn build(profile: &Rc, request: &Entry) -> Self { + pub fn build(profile: &Arc, request: &Entry) -> Self { let signal_handler_id = Rc::new(RefCell::new(None)); let list_store = ListStore::new::(); let single_selection = { @@ -162,15 +162,12 @@ impl Suggestion { let query = self.request.text(); let popover = self.popover.clone(); let list_store = self.list_store.clone(); - - let history = self.profile.history.memory.clone(); // @TODO - let bookmark = self.profile.bookmark.memory.clone(); // @TODO + let profile = self.profile.clone(); gtk::glib::spawn_future_local(async move { let list_items = gtk::gio::spawn_blocking(move || { - let result = history - .read() - .unwrap() + let result = profile + .history .contains_request(&query, limit) .into_iter() .sorted_by(|a, b| Ord::cmp(&b.opened.count, &a.opened.count)); @@ -184,7 +181,7 @@ impl Suggestion { list_items.push(( title, subtitle, - bookmark.read().unwrap().is_match_request(&item.request), + profile.bookmark.is_match_request(&item.request), item.request, )) } diff --git a/src/profile/bookmark.rs b/src/profile/bookmark.rs index 845216ec..3f0ce0b2 100644 --- a/src/profile/bookmark.rs +++ b/src/profile/bookmark.rs @@ -10,12 +10,12 @@ use memory::Memory; use r2d2::Pool; use r2d2_sqlite::SqliteConnectionManager; use sqlite::Transaction; -use std::sync::{Arc, RwLock}; +use std::sync::RwLock; pub struct Bookmark { - database: Database, // permanent storage - pub memory: Arc>, // fast search index -} // @TODO close memory member, Arc entire profile instead + database: Database, // permanent storage + memory: RwLock, // fast search index +} impl Bookmark { // Constructors @@ -24,7 +24,7 @@ impl Bookmark { pub fn build(database_pool: &Pool, profile_id: i64) -> Result { // Init children components let database = Database::new(database_pool, profile_id); - let memory = Arc::new(RwLock::new(Memory::new())); + let memory = RwLock::new(Memory::new()); // Build initial index { diff --git a/src/profile/history.rs b/src/profile/history.rs index 0d7cacf0..6ff01056 100644 --- a/src/profile/history.rs +++ b/src/profile/history.rs @@ -10,12 +10,12 @@ use memory::Memory; use r2d2::Pool; use r2d2_sqlite::SqliteConnectionManager; use sqlite::Transaction; -use std::sync::{Arc, RwLock}; +use std::sync::RwLock; pub struct History { - database: Database, // permanent storage - pub memory: Arc>, // fast search index -} // @TODO close memory member, Arc entire profile instead + database: Database, // permanent storage + memory: RwLock, // fast search index +} impl History { // Constructors @@ -24,7 +24,7 @@ impl History { pub fn build(database_pool: &Pool, profile_id: i64) -> Result { // Init children components let database = Database::build(database_pool, profile_id); - let memory = Arc::new(RwLock::new(Memory::new())); + let memory = RwLock::new(Memory::new()); for item in database.records(None, None)? { memory.write().unwrap().add(item) diff --git a/src/profile/identity/auth/memory.rs b/src/profile/identity/auth/memory.rs index ba07b85e..70a0304d 100644 --- a/src/profile/identity/auth/memory.rs +++ b/src/profile/identity/auth/memory.rs @@ -2,11 +2,11 @@ pub mod auth; use anyhow::{bail, Result}; pub use auth::Auth; -use std::{cell::RefCell, collections::HashMap}; +use std::{collections::HashMap, sync::RwLock}; /// Reduce disk usage by cache Auth index in memory pub struct Memory { - index: RefCell>, + index: RwLock>, } impl Default for Memory { @@ -21,7 +21,7 @@ impl Memory { /// Create new `Self` pub fn new() -> Self { Self { - index: RefCell::new(HashMap::new()), + index: RwLock::new(HashMap::new()), } } @@ -31,7 +31,7 @@ impl Memory { /// * validate record with same key does not exist yet pub fn add(&self, scope: String, profile_identity_id: i64) -> Result<()> { // Borrow shared index access - let mut index = self.index.borrow_mut(); + let mut index = self.index.write().unwrap(); // Prevent existing key overwrite if index.contains_key(&scope) { @@ -47,7 +47,7 @@ impl Memory { /// Cleanup index pub fn clear(&self) -> Result<()> { - let mut index = self.index.borrow_mut(); + let mut index = self.index.write().unwrap(); index.clear(); if index.is_empty() { Ok(()) @@ -62,7 +62,7 @@ impl Memory { pub fn match_scope(&self, scope: &str) -> Option { let mut result = Vec::new(); - for (value, &profile_identity_id) in self.index.borrow().iter() { + for (value, &profile_identity_id) in self.index.read().unwrap().iter() { if scope.starts_with(value) { result.push(Auth { profile_identity_id, @@ -83,7 +83,7 @@ impl Memory { /// * see also parent `is_match_request` pub fn total(&self, profile_identity_id: i64) -> usize { let mut total = 0; - for (_, _profile_identity_id) in self.index.borrow().iter() { + for (_, _profile_identity_id) in self.index.read().unwrap().iter() { if *_profile_identity_id == profile_identity_id { total += 1 } diff --git a/src/profile/identity/memory.rs b/src/profile/identity/memory.rs index 041f76d7..990e4e4c 100644 --- a/src/profile/identity/memory.rs +++ b/src/profile/identity/memory.rs @@ -1,9 +1,9 @@ use anyhow::{bail, Result}; -use std::{cell::RefCell, collections::HashMap}; +use std::{collections::HashMap, sync::RwLock}; /// Reduce disk usage by cache index in memory pub struct Memory { - index: RefCell>, + index: RwLock>, } impl Default for Memory { @@ -18,7 +18,7 @@ impl Memory { /// Create new `Self` pub fn new() -> Self { Self { - index: RefCell::new(HashMap::new()), + index: RwLock::new(HashMap::new()), } } @@ -28,7 +28,7 @@ impl Memory { /// * validate record with same key does not exist yet pub fn add(&self, profile_identity_id: i64, pem: String) -> Result<()> { // Borrow shared index access - let mut index = self.index.borrow_mut(); + let mut index = self.index.write().unwrap(); // Prevent existing key overwrite if index.contains_key(&profile_identity_id) { @@ -44,7 +44,7 @@ impl Memory { /// Get `pem` clone by `id` from memory index pub fn get(&self, id: i64) -> Result { - match self.index.borrow().get(&id) { + match self.index.read().unwrap().get(&id) { Some(pem) => Ok(pem.clone()), None => bail!("Record `{id}` not found in memory index"), } @@ -52,7 +52,7 @@ impl Memory { /// Cleanup index pub fn clear(&self) -> Result<()> { - let mut index = self.index.borrow_mut(); + let mut index = self.index.write().unwrap(); index.clear(); if index.is_empty() { Ok(()) diff --git a/src/profile/search/memory.rs b/src/profile/search/memory.rs index 71da28ac..66d7af53 100644 --- a/src/profile/search/memory.rs +++ b/src/profile/search/memory.rs @@ -1,9 +1,9 @@ use super::database::Row; -use std::cell::RefCell; +use std::sync::RwLock; /// Reduce disk usage by cache Bookmarks index in memory pub struct Memory { - index: RefCell>, + index: RwLock>, } impl Memory { @@ -12,7 +12,7 @@ impl Memory { /// Create new `Self` pub fn init() -> Self { Self { - index: RefCell::new(Vec::new()), + index: RwLock::new(Vec::new()), } } @@ -20,7 +20,7 @@ impl Memory { /// Add new record pub fn push(&self, id: i64, query: String, is_default: bool) { - self.index.borrow_mut().push(Row { + self.index.write().unwrap().push(Row { id, query, is_default, @@ -29,19 +29,19 @@ impl Memory { /// Clear all records pub fn clear(&self) { - self.index.borrow_mut().clear() + self.index.write().unwrap().clear() } // Getters /// Get all records pub fn records(&self) -> Vec { - self.index.borrow().clone() + self.index.read().unwrap().clone() } /// Get all records pub fn default(&self) -> Option { - for record in self.index.borrow().iter() { + for record in self.index.read().unwrap().iter() { if record.is_default { return Some(record.clone()); }