| 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 |
|
| 5 |
use crate::storage::{ClientRemoteTabs, RemoteTab, TabsStorage}; |
| 6 |
use crate::{ApiResult, PendingCommand, RemoteCommand}; |
| 7 |
use std::path::Path; |
| 8 |
use std::sync::{Arc, Mutex}; |
| 9 |
|
| 10 |
pub struct TabsStore { |
| 11 |
pub storage: Mutex<TabsStorage>, |
| 12 |
} |
| 13 |
|
| 14 |
impl TabsStore { |
| 15 |
pub fn new(db_path: impl AsRef<Path>) -> Self { |
| 16 |
Self { |
| 17 |
storage: Mutex::new(TabsStorage::new(db_path)), |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
pub fn new_with_mem_path(db_path: &str) -> Self { |
| 22 |
Self { |
| 23 |
storage: Mutex::new(TabsStorage::new_with_mem_path(db_path)), |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
// Closes connection to the tabs DB, this is named slightly |
| 28 |
// different since Kotlin implements AutoClosable and doesn't |
| 29 |
// want us using close |
| 30 |
pub fn close_connection(&self) { |
| 31 |
self.storage.lock().unwrap().close() |
| 32 |
} |
| 33 |
|
| 34 |
pub fn set_local_tabs(&self, local_state: Vec<RemoteTab>) { |
| 35 |
self.storage.lock().unwrap().update_local_state(local_state); |
| 36 |
} |
| 37 |
|
| 38 |
// like remote_tabs, but serves the uniffi layer |
| 39 |
pub fn get_all(&self) -> Vec<ClientRemoteTabs> { |
| 40 |
self.remote_tabs().unwrap_or_default() |
| 41 |
} |
| 42 |
|
| 43 |
pub fn remote_tabs(&self) -> Option<Vec<ClientRemoteTabs>> { |
| 44 |
self.storage.lock().unwrap().get_remote_tabs() |
| 45 |
} |
| 46 |
|
| 47 |
pub fn new_remote_command_store(self: Arc<Self>) -> Arc<RemoteCommandStore> { |
| 48 |
Arc::new(RemoteCommandStore { |
| 49 |
store: Arc::clone(&self), |
| 50 |
}) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
pub struct RemoteCommandStore { |
| 55 |
// it's a shame we can't hold a TabsStorage. |
| 56 |
store: Arc<TabsStore>, |
| 57 |
} |
| 58 |
|
| 59 |
impl RemoteCommandStore { |
| 60 |
// Info about remote tab commands. |
| 61 |
// We record a local timestamp and a state of "pending". The app must arrange to deliver and |
| 62 |
// mark then as "sent". Thus it also serves as a persistent queue of commands to send while |
| 63 |
// handling unreliable delivery. |
| 64 |
|
| 65 |
// Commands here will influence what TabsStore::remote_tabs() returns for the device in an |
| 66 |
// attempt the pretend the command has remotely executed and succeeded before it actually has. |
| 67 |
// The policies for when we should stop pretending the command has executed is up to the app via |
| 68 |
// removing the command. |
| 69 |
#[error_support::handle_error(crate::Error)] |
| 70 |
pub fn add_remote_command(&self, device_id: &str, command: &RemoteCommand) -> ApiResult<bool> { |
| 71 |
self.store |
| 72 |
.storage |
| 73 |
.lock() |
| 74 |
.unwrap() |
| 75 |
.add_remote_tab_command(device_id, command) |
| 76 |
} |
| 77 |
|
| 78 |
#[error_support::handle_error(crate::Error)] |
| 79 |
pub fn add_remote_command_at( |
| 80 |
&self, |
| 81 |
device_id: &str, |
| 82 |
command: &RemoteCommand, |
| 83 |
when: types::Timestamp, |
| 84 |
) -> ApiResult<bool> { |
| 85 |
self.store |
| 86 |
.storage |
| 87 |
.lock() |
| 88 |
.unwrap() |
| 89 |
.add_remote_tab_command_at(device_id, command, when) |
| 90 |
} |
| 91 |
|
| 92 |
// Remove all information about a command. |
| 93 |
#[error_support::handle_error(crate::Error)] |
| 94 |
pub fn remove_remote_command( |
| 95 |
&self, |
| 96 |
device_id: &str, |
| 97 |
command: &RemoteCommand, |
| 98 |
) -> ApiResult<bool> { |
| 99 |
self.store |
| 100 |
.storage |
| 101 |
.lock() |
| 102 |
.unwrap() |
| 103 |
.remove_remote_tab_command(device_id, command) |
| 104 |
} |
| 105 |
|
| 106 |
#[error_support::handle_error(crate::Error)] |
| 107 |
pub fn get_unsent_commands(&self) -> ApiResult<Vec<PendingCommand>> { |
| 108 |
self.store.storage.lock().unwrap().get_unsent_commands() |
| 109 |
} |
| 110 |
|
| 111 |
#[error_support::handle_error(crate::Error)] |
| 112 |
pub fn set_pending_command_sent(&self, command: &PendingCommand) -> ApiResult<bool> { |
| 113 |
self.store |
| 114 |
.storage |
| 115 |
.lock() |
| 116 |
.unwrap() |
| 117 |
.set_pending_command_sent(command) |
| 118 |
} |
| 119 |
} |