summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tabs/src/sync/full_sync.rs
blob: defd4b473b7bc9bf61cde4194a97e62bf272d669 (plain)
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
/* 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::{sync::engine::TabsSyncImpl, ApiResult, Result, TabsEngine, TabsStore};
use error_support::handle_error;
use interrupt_support::NeverInterrupts;
use std::sync::Arc;
use sync15::client::{sync_multiple, MemoryCachedState, Sync15StorageClientInit};
use sync15::engine::EngineSyncAssociation;
use sync15::KeyBundle;

impl TabsStore {
    pub fn reset(self: Arc<Self>) -> ApiResult<()> {
        handle_error! {
            let mut sync_impl = TabsSyncImpl::new(Arc::clone(&self));
            sync_impl.reset(EngineSyncAssociation::Disconnected)?;
            Ok(())
        }
    }

    /// A convenience wrapper around sync_multiple.
    pub fn sync(
        self: Arc<Self>,
        key_id: String,
        access_token: String,
        sync_key: String,
        tokenserver_url: String,
        local_id: String,
    ) -> ApiResult<String> {
        handle_error! {
            let mut mem_cached_state = MemoryCachedState::default();
            let engine = TabsEngine::new(Arc::clone(&self));

            // Since we are syncing without the sync manager, there's no
            // command processor, therefore no clients engine, and in
            // consequence `TabsStore::prepare_for_sync` is never called
            // which means our `local_id` will never be set.
            // Do it here.
            engine.sync_impl.lock().unwrap().local_id = local_id;

            let storage_init = &Sync15StorageClientInit {
                key_id,
                access_token,
                tokenserver_url: url::Url::parse(tokenserver_url.as_str())?,
            };
            let root_sync_key = &KeyBundle::from_ksync_base64(sync_key.as_str())?;

            let mut result = sync_multiple(
                &[&engine],
                &mut None,
                &mut mem_cached_state,
                storage_init,
                root_sync_key,
                &NeverInterrupts,
                None,
            );

            // for b/w compat reasons, we do some dances with the result.
            // XXX - note that this means telemetry isn't going to be reported back
            // to the app - we need to check with lockwise about whether they really
            // need these failures to be reported or whether we can loosen this.
            if let Err(e) = result.result {
                return Err(e.into());
            }
            match result.engine_results.remove("tabs") {
                None | Some(Ok(())) => Ok(serde_json::to_string(&result.telemetry)?),
                Some(Err(e)) => Err(e.into()),
            }
        }
    }
}