Skip to content

Commit

Permalink
Implement a skeleton sync15::BridgedEngine for extension storage.
Browse files Browse the repository at this point in the history
Currently, the only supported method is `wipe`, with all other
methods returning `NotImplemented`. We can wire this up to Mark's
syncing implementation in #2892. This also implements enough of the
glue to where it can be vendored and used in Desktop.
  • Loading branch information
linabutler committed May 1, 2020
1 parent d20321c commit 43aaf11
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/webext-storage/Cargo.toml
Expand Up @@ -19,6 +19,7 @@ serde = "1"
serde_json = "1"
serde_derive = "1"
sql-support = { path = "../support/sql" }
sync15-traits = {path = "../support/sync15-traits"}
sync-guid = { path = "../support/guid", features = ["rusqlite_support", "random"] }
url = { version = "2.1", features = ["serde"] }

Expand Down
3 changes: 3 additions & 0 deletions components/webext-storage/src/error.rs
Expand Up @@ -52,6 +52,9 @@ pub enum ErrorKind {

#[fail(display = "Database version {} is not supported", _0)]
UnsupportedDatabaseVersion(i64),

#[fail(display = "This operation isn't implemented yet")]
NotImplemented,
}

error_support::define_error! {
Expand Down
1 change: 1 addition & 0 deletions components/webext-storage/src/lib.rs
Expand Up @@ -10,6 +10,7 @@ mod db;
pub mod error;
mod schema;
pub mod store;
mod sync;

// This is what we roughly expect the "bridge" used by desktop to do.
// It's primarily here to avoid dead-code warnings (but I don't want to disable
Expand Down
6 changes: 6 additions & 0 deletions components/webext-storage/src/store.rs
Expand Up @@ -5,6 +5,7 @@
use crate::api::{self, StorageChanges};
use crate::db::StorageDb;
use crate::error::*;
use crate::sync;
use std::path::Path;
use std::result;

Expand Down Expand Up @@ -108,6 +109,11 @@ impl Store {
Ok(())
}

/// Returns a bridged sync engine for Desktop for this store.
pub fn bridged_engine(&self) -> sync::BridgedEngine<'_> {
sync::BridgedEngine::new(&self.db)
}

/// Closes the store and its database connection. See the docs for
/// `StorageDb::close` for more details on when this can fail.
pub fn close(self) -> result::Result<(), (Store, Error)> {
Expand Down
75 changes: 75 additions & 0 deletions components/webext-storage/src/sync/bridge.rs
@@ -0,0 +1,75 @@
/* 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 sync15_traits::{self, ApplyResults};

use crate::api;
use crate::db::StorageDb;
use crate::error::{Error, ErrorKind, Result};

/// A bridged engine implements all the methods needed to make the
/// `storage.sync` store work with Desktop's Sync implementation.
/// Conceptually, it's similar to `sync15_traits::Store`, which we
/// should eventually rename and unify with this trait (#2841).
pub struct BridgedEngine<'a> {
db: &'a StorageDb,
}

impl<'a> BridgedEngine<'a> {
/// Creates a bridged engine for syncing.
pub fn new(db: &'a StorageDb) -> Self {
BridgedEngine { db }
}
}

impl<'a> sync15_traits::BridgedEngine for BridgedEngine<'a> {
type Error = Error;

fn last_sync(&self) -> Result<i64> {
Err(ErrorKind::NotImplemented.into())
}

fn set_last_sync(&self, _last_sync_millis: i64) -> Result<()> {
Err(ErrorKind::NotImplemented.into())
}

fn sync_id(&self) -> Result<Option<String>> {
Err(ErrorKind::NotImplemented.into())
}

fn reset_sync_id(&self) -> Result<String> {
Err(ErrorKind::NotImplemented.into())
}

fn ensure_current_sync_id(&self, _new_sync_id: &str) -> Result<String> {
Err(ErrorKind::NotImplemented.into())
}

fn store_incoming(&self, _incoming_cleartexts: &[String]) -> Result<()> {
Err(ErrorKind::NotImplemented.into())
}

fn apply(&self) -> Result<ApplyResults> {
Err(ErrorKind::NotImplemented.into())
}

fn set_uploaded(&self, _server_modified_millis: i64, _ids: &[String]) -> Result<()> {
Err(ErrorKind::NotImplemented.into())
}

fn sync_finished(&self) -> Result<()> {
Err(ErrorKind::NotImplemented.into())
}

fn reset(&self) -> Result<()> {
Err(ErrorKind::NotImplemented.into())
}

fn wipe(&self) -> Result<()> {
let tx = self.db.unchecked_transaction()?;
api::wipe_all(&tx)?;
tx.commit()?;
Ok(())
}
}
7 changes: 7 additions & 0 deletions components/webext-storage/src/sync/mod.rs
@@ -0,0 +1,7 @@
/* 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/. */

mod bridge;

pub use bridge::BridgedEngine;

0 comments on commit 43aaf11

Please sign in to comment.