tabs/
store.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use 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    // Closes connection to the tabs DB, this is named slightly
29    // different since Kotlin implements AutoClosable and doesn't
30    // want us using close
31    pub fn close_connection(&self) {
32        self.storage.lock().unwrap().close()
33    }
34
35    /// For when we have no concept of tab groups or windows.
36    /// Deprecated for desktop, hopefully one day deprecated on mobile.
37    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    // like remote_tabs, but serves the uniffi layer
50    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    // it's a shame we can't hold a TabsStorage.
67    store: Arc<TabsStore>,
68}
69
70impl RemoteCommandStore {
71    // Info about remote tab commands.
72    // We record a local timestamp and a state of "pending". The app must arrange to deliver and
73    // mark then as "sent". Thus it also serves as a persistent queue of commands to send while
74    // handling unreliable delivery.
75
76    // Commands here will influence what TabsStore::remote_tabs() returns for the device in an
77    // attempt the pretend the command has remotely executed and succeeded before it actually has.
78    // The policies for when we should stop pretending the command has executed is up to the app via
79    // removing the command.
80    #[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    // Remove all information about a command.
104    #[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}