tabs/
store.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::storage::{ClientRemoteTabs, RemoteTab, TabsStorage};
use crate::{ApiResult, PendingCommand, RemoteCommand};
use std::path::Path;
use std::sync::{Arc, Mutex};

pub struct TabsStore {
    pub storage: Mutex<TabsStorage>,
}

impl TabsStore {
    pub fn new(db_path: impl AsRef<Path>) -> Self {
        Self {
            storage: Mutex::new(TabsStorage::new(db_path)),
        }
    }

    pub fn new_with_mem_path(db_path: &str) -> Self {
        Self {
            storage: Mutex::new(TabsStorage::new_with_mem_path(db_path)),
        }
    }

    // Closes connection to the tabs DB, this is named slightly
    // different since Kotlin implements AutoClosable and doesn't
    // want us using close
    pub fn close_connection(&self) {
        self.storage.lock().unwrap().close()
    }

    pub fn set_local_tabs(&self, local_state: Vec<RemoteTab>) {
        self.storage.lock().unwrap().update_local_state(local_state);
    }

    // like remote_tabs, but serves the uniffi layer
    pub fn get_all(&self) -> Vec<ClientRemoteTabs> {
        self.remote_tabs().unwrap_or_default()
    }

    pub fn remote_tabs(&self) -> Option<Vec<ClientRemoteTabs>> {
        self.storage.lock().unwrap().get_remote_tabs()
    }

    pub fn new_remote_command_store(self: Arc<Self>) -> Arc<RemoteCommandStore> {
        Arc::new(RemoteCommandStore {
            store: Arc::clone(&self),
        })
    }
}

pub struct RemoteCommandStore {
    // it's a shame we can't hold a TabsStorage.
    store: Arc<TabsStore>,
}

impl RemoteCommandStore {
    // Info about remote tab commands.
    // We record a local timestamp and a state of "pending". The app must arrange to deliver and
    // mark then as "sent". Thus it also serves as a persistent queue of commands to send while
    // handling unreliable delivery.

    // Commands here will influence what TabsStore::remote_tabs() returns for the device in an
    // attempt the pretend the command has remotely executed and succeeded before it actually has.
    // The policies for when we should stop pretending the command has executed is up to the app via
    // removing the command.
    #[error_support::handle_error(crate::Error)]
    pub fn add_remote_command(&self, device_id: &str, command: &RemoteCommand) -> ApiResult<bool> {
        self.store
            .storage
            .lock()
            .unwrap()
            .add_remote_tab_command(device_id, command)
    }

    #[error_support::handle_error(crate::Error)]
    pub fn add_remote_command_at(
        &self,
        device_id: &str,
        command: &RemoteCommand,
        when: types::Timestamp,
    ) -> ApiResult<bool> {
        self.store
            .storage
            .lock()
            .unwrap()
            .add_remote_tab_command_at(device_id, command, when)
    }

    // Remove all information about a command.
    #[error_support::handle_error(crate::Error)]
    pub fn remove_remote_command(
        &self,
        device_id: &str,
        command: &RemoteCommand,
    ) -> ApiResult<bool> {
        self.store
            .storage
            .lock()
            .unwrap()
            .remove_remote_tab_command(device_id, command)
    }

    #[error_support::handle_error(crate::Error)]
    pub fn get_unsent_commands(&self) -> ApiResult<Vec<PendingCommand>> {
        self.store.storage.lock().unwrap().get_unsent_commands()
    }

    #[error_support::handle_error(crate::Error)]
    pub fn set_pending_command_sent(&self, command: &PendingCommand) -> ApiResult<bool> {
        self.store
            .storage
            .lock()
            .unwrap()
            .set_pending_command_sent(command)
    }
}