1use crate::storage::{ClientRemoteTabs, LocalTabsInfo, RemoteTab, TabsStorage};
6use crate::{ApiResult, PendingCommand, RemoteCommand};
7use std::collections::HashMap;
8use std::path::Path;
9use std::sync::{Arc, Mutex};
10
11pub struct TabsStore {
12 pub storage: Mutex<TabsStorage>,
13}
14
15impl TabsStore {
16 pub fn new(db_path: impl AsRef<Path>) -> Self {
17 Self {
18 storage: Mutex::new(TabsStorage::new(db_path)),
19 }
20 }
21
22 pub fn new_with_mem_path(db_path: &str) -> Self {
23 Self {
24 storage: Mutex::new(TabsStorage::new_with_mem_path(db_path)),
25 }
26 }
27
28 pub fn close_connection(&self) {
32 self.storage.lock().unwrap().close()
33 }
34
35 pub fn set_local_tabs(&self, local_state: Vec<RemoteTab>) {
38 self.set_local_tabs_info(LocalTabsInfo {
39 tabs: local_state,
40 tab_groups: HashMap::new(),
41 windows: HashMap::new(),
42 })
43 }
44
45 pub fn set_local_tabs_info(&self, info: LocalTabsInfo) {
46 self.storage.lock().unwrap().update_local_state(info);
47 }
48
49 pub fn get_all(&self) -> Vec<ClientRemoteTabs> {
51 self.remote_tabs().unwrap_or_default()
52 }
53
54 pub fn remote_tabs(&self) -> Option<Vec<ClientRemoteTabs>> {
55 self.storage.lock().unwrap().get_remote_tabs()
56 }
57
58 pub fn new_remote_command_store(self: Arc<Self>) -> Arc<RemoteCommandStore> {
59 Arc::new(RemoteCommandStore {
60 store: Arc::clone(&self),
61 })
62 }
63}
64
65pub struct RemoteCommandStore {
66 store: Arc<TabsStore>,
68}
69
70impl RemoteCommandStore {
71 #[error_support::handle_error(crate::Error)]
81 pub fn add_remote_command(&self, device_id: &str, command: &RemoteCommand) -> ApiResult<bool> {
82 self.store
83 .storage
84 .lock()
85 .unwrap()
86 .add_remote_tab_command(device_id, command)
87 }
88
89 #[error_support::handle_error(crate::Error)]
90 pub fn add_remote_command_at(
91 &self,
92 device_id: &str,
93 command: &RemoteCommand,
94 when: types::Timestamp,
95 ) -> ApiResult<bool> {
96 self.store
97 .storage
98 .lock()
99 .unwrap()
100 .add_remote_tab_command_at(device_id, command, when)
101 }
102
103 #[error_support::handle_error(crate::Error)]
105 pub fn remove_remote_command(
106 &self,
107 device_id: &str,
108 command: &RemoteCommand,
109 ) -> ApiResult<bool> {
110 self.store
111 .storage
112 .lock()
113 .unwrap()
114 .remove_remote_tab_command(device_id, command)
115 }
116
117 #[error_support::handle_error(crate::Error)]
118 pub fn get_unsent_commands(&self) -> ApiResult<Vec<PendingCommand>> {
119 self.store.storage.lock().unwrap().get_unsent_commands()
120 }
121
122 #[error_support::handle_error(crate::Error)]
123 pub fn set_pending_command_sent(&self, command: &PendingCommand) -> ApiResult<bool> {
124 self.store
125 .storage
126 .lock()
127 .unwrap()
128 .set_pending_command_sent(command)
129 }
130}