diff --git a/CHANGELOG.md b/CHANGELOG.md index de25238fc01..49bb8561410 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ parses them directly with `clap::Parser::parse`. ([#7784](https://github.com/moz ## ⚠️ Breaking Changes ⚠️ +### Sync Manager +- `DeviceSettings.enabled_engines` is removed, replaced by `SyncParams.enabled_engines`. + ### Firefox Accounts - Removed the deprecated `FxaRustAuthState` struct and the `get_auth_state()` method. Changed `FxaState::Authenticating::initial_state` to store a `FxaState` value. @@ -51,6 +54,8 @@ parses them directly with `clap::Parser::parse`. ([#7784](https://github.com/moz ### Sync Manager - `EngineChoicesModel.Device` implements per-device engine choices. It's for testing only for now. +- `SyncResult.account_engine_choices` reports the account-wide engine choices in `meta/global`, so apps using per-device engine choices can adopt them. +- `SyncParams.enabled_engines` declares all the engines this device has chosen to sync. With `SyncEngineSelection.All`, only those engines are synced. ## ⚠️ Breaking Changes ⚠️ diff --git a/components/sync15/src/client/status.rs b/components/sync15/src/client/status.rs index a34bed5d87e..30ebcc4d02b 100644 --- a/components/sync15/src/client/status.rs +++ b/components/sync15/src/client/status.rs @@ -2,6 +2,7 @@ * 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::AccountEngineChoices; use crate::error::{Error, ErrorResponse}; use crate::telemetry::SyncTelemetryPing; use std::collections::HashMap; @@ -70,6 +71,10 @@ pub struct SyncResult { /// The set of declined engines, if we know them. pub declined: Option>, + /// The account-wide engine choices in `meta/global`, including one we wrote + /// in this sync. `None` if we didn't get as far as knowing them. + pub account_engine_choices: Option, + /// The result of the sync. pub result: Result<(), Error>, diff --git a/components/sync15/src/client/sync_multiple.rs b/components/sync15/src/client/sync_multiple.rs index 6b26816b76e..4b48148d0e5 100644 --- a/components/sync15/src/client/sync_multiple.rs +++ b/components/sync15/src/client/sync_multiple.rs @@ -14,7 +14,7 @@ use crate::clients_engine::{self, CLIENTS_TTL_REFRESH, CommandProcessor}; use crate::engine::{EngineSyncAssociation, SyncEngine}; use crate::error::{Error, debug, info, trace, warn}; use crate::telemetry; -use crate::{EngineChoicesModel, KeyBundle}; +use crate::{AccountEngineChoices, EngineChoicesModel, KeyBundle}; use interrupt_support::Interruptee; use std::collections::HashMap; use std::result; @@ -132,6 +132,7 @@ pub fn sync_multiple_with_command_processor( service_status: ServiceStatus::OtherError, result: Ok(()), declined: None, + account_engine_choices: None, next_sync_after: None, engine_results: HashMap::with_capacity(engines.len()), telemetry: telemetry::SyncTelemetryPing::new(), @@ -441,6 +442,14 @@ impl SyncMultipleDriver<'_, '_, '_, '_> { } Ok(state) => state, }; + // If we wrote meta/global, the state machine fetched it again, so this + // is what we wrote. + let mut enabled: Vec = state.global.engines.keys().cloned().collect(); + enabled.sort(); + self.result.account_engine_choices = Some(AccountEngineChoices { + enabled, + declined: state.global.declined.clone(), + }); self.result.telemetry.uid(client_info.client.hashed_uid()?); // As for client_info, put None back now so we start from scratch on error. self.mem_cached_state.last_global_state = None; diff --git a/components/sync15/src/clients_engine/engine.rs b/components/sync15/src/clients_engine/engine.rs index 3eaf13df415..dd43b8c28f3 100644 --- a/components/sync15/src/clients_engine/engine.rs +++ b/components/sync15/src/clients_engine/engine.rs @@ -410,7 +410,6 @@ mod tests { fxa_device_id: "deviceAAAAAA".into(), device_name: "Laptop".into(), device_type: DeviceType::Desktop, - enabled_engines: vec![], }, outgoing_commands: [ Command::Wipe("bookmarks".into()), @@ -565,7 +564,6 @@ mod tests { fxa_device_id: "deviceAAAAAA".into(), device_name: "Laptop".into(), device_type: DeviceType::Desktop, - enabled_engines: vec![], }, outgoing_commands: [].iter().cloned().collect(), }; @@ -632,7 +630,6 @@ mod tests { fxa_device_id: "deviceAAAAAA".into(), device_name: "Laptop".into(), device_type: DeviceType::Desktop, - enabled_engines: vec![], }, outgoing_commands: [].iter().cloned().collect(), }; @@ -702,7 +699,6 @@ mod tests { fxa_device_id: "deviceAAAAAA".into(), device_name: "Laptop".into(), device_type: DeviceType::Desktop, - enabled_engines: vec![], }, outgoing_commands: HashSet::new(), }; diff --git a/components/sync15/src/clients_engine/mod.rs b/components/sync15/src/clients_engine/mod.rs index fc373ee38f5..7346712dc77 100644 --- a/components/sync15/src/clients_engine/mod.rs +++ b/components/sync15/src/clients_engine/mod.rs @@ -80,8 +80,6 @@ pub struct Settings { pub device_name: String, /// The type of this client: mobile, tablet, desktop, or other. pub device_type: DeviceType, - /// The engines this client syncs. Not yet published. - pub enabled_engines: Vec, } #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] diff --git a/components/sync15/src/engine_choices.rs b/components/sync15/src/engine_choices.rs index 18081766670..d0c84b591ff 100644 --- a/components/sync15/src/engine_choices.rs +++ b/components/sync15/src/engine_choices.rs @@ -41,3 +41,14 @@ impl EngineChoicesModel { } } } + +/// The account-wide engine choices, as recorded in `meta/global`. Maintained by +/// clients using `EngineChoicesModel::Account`, but reported with either model, +/// so apps using per-device choices can adopt them. +#[derive(Debug, Clone, Default, PartialEq, Eq, uniffi::Record)] +pub struct AccountEngineChoices { + /// The engines in `meta/global`'s engines. + pub enabled: Vec, + /// The engines in `meta/global`'s declined. + pub declined: Vec, +} diff --git a/components/sync15/src/lib.rs b/components/sync15/src/lib.rs index 79549bf6f83..94d08ccdf6c 100644 --- a/components/sync15/src/lib.rs +++ b/components/sync15/src/lib.rs @@ -32,7 +32,7 @@ pub mod telemetry; pub use crate::client_types::{ClientData, RemoteClient}; pub use crate::device_type::DeviceType; -pub use crate::engine_choices::EngineChoicesModel; +pub use crate::engine_choices::{AccountEngineChoices, EngineChoicesModel}; pub use crate::error::{Error, Result}; #[cfg(feature = "crypto")] pub use enc_payload::EncryptedPayload; diff --git a/components/sync_manager/src/manager.rs b/components/sync_manager/src/manager.rs index 40383a78b0a..78b31e5ca6a 100644 --- a/components/sync_manager/src/manager.rs +++ b/components/sync_manager/src/manager.rs @@ -57,7 +57,8 @@ impl SyncManager { breadcrumb!("SyncManager::sync is already in progress, bailing out early"); return Err(SyncManagerError::Busy); }; - let engines = self.calc_engines_to_sync(¶ms.engines)?; + let engines = + self.calc_engines_to_sync(¶ms.engines, params.enabled_engines.as_deref())?; let next_sync_after = state.as_ref().and_then(|mcs| mcs.get_next_sync_after()); let result = if !backoff_in_effect(next_sync_after, ¶ms) { info!("No backoff in effect (or we decided to ignore it), starting sync"); @@ -72,6 +73,7 @@ impl SyncManager { successful: Default::default(), failures: Default::default(), declined: None, + account_engine_choices: None, next_sync_allowed_at: next_sync_after, persisted_state: params.persisted_state.unwrap_or_default(), // It would be nice to record telemetry here. @@ -158,7 +160,6 @@ impl SyncManager { fxa_device_id: params.device_settings.fxa_device_id, device_name: params.device_settings.name, device_type: params.device_settings.kind, - enabled_engines: params.device_settings.enabled_engines, }; let c = SyncClient::new(settings); let result = sync_multiple_with_command_processor( @@ -200,6 +201,7 @@ impl SyncManager { successful, failures, declined: result.declined, + account_engine_choices: result.account_engine_choices, next_sync_allowed_at: result.next_sync_after, persisted_state: disk_cached_state.unwrap_or_default(), telemetry_json: Some(telemetry_json), @@ -213,6 +215,7 @@ impl SyncManager { fn calc_engines_to_sync( &self, selection: &SyncEngineSelection, + enabled_engines: Option<&[String]>, ) -> Result>> { // BTreeMap to ensure we sync the engines in priority order. let mut engine_map: BTreeMap<_, _> = self.iter_registered_engines().collect(); @@ -239,6 +242,20 @@ impl SyncManager { } // Filter engines based on the selection engine_map.retain(|engine_id, _| selected_engine_ids.contains(engine_id)) + } else if let Some(enabled_engines) = enabled_engines { + // "All" means all the engines this device has chosen. It's only a + // declaration, so names we don't know aren't an error. + let enabled_engine_ids: HashSet = enabled_engines + .iter() + .filter_map(|name| match Self::get_engine_id(name) { + Ok(id) => Some(id), + Err(_) => { + warn!("Ignoring unknown enabled engine {:?}", name); + None + } + }) + .collect(); + engine_map.retain(|engine_id, _| enabled_engine_ids.contains(engine_id)) } Ok(engine_map.into_values().collect()) } @@ -333,6 +350,7 @@ mod test { reason: SyncReason::Scheduled, engines: SyncEngineSelection::All, engine_choices_model: EngineChoicesModel::default(), + enabled_engines: None, local_encryption_keys: HashMap::new(), auth_info: SyncAuthInfo { kid: "kid".to_string(), @@ -345,7 +363,6 @@ mod test { fxa_device_id: "device-id".to_string(), name: "Test Device".to_string(), kind: sync15::DeviceType::Mobile, - enabled_engines: vec![], }, } } diff --git a/components/sync_manager/src/types.rs b/components/sync_manager/src/types.rs index 29b98adc9e0..edd2c573e9c 100644 --- a/components/sync_manager/src/types.rs +++ b/components/sync_manager/src/types.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use std::time::SystemTime; -use sync15::{DeviceType, EngineChoicesModel}; +use sync15::{AccountEngineChoices, DeviceType, EngineChoicesModel}; #[derive(Debug, uniffi::Record)] pub struct SyncParams { @@ -14,6 +14,12 @@ pub struct SyncParams { pub engines: SyncEngineSelection, /// How the user's choice of engines to sync is modelled. pub engine_choices_model: EngineChoicesModel, + /// All the engines this device has chosen to sync, which may be more than + /// `engines` asks for in this sync. A declaration of the device's choices, + /// not used to decide what to sync, except that `SyncEngineSelection::All` + /// means these engines when specified. + #[uniffi(default)] + pub enabled_engines: Option>, /// Keys to encrypt/decrypt data from local database files. These are /// separate from the key we use to encrypt the sync payload as a whole. pub local_encryption_keys: HashMap, @@ -59,10 +65,6 @@ pub struct DeviceSettings { pub fxa_device_id: String, pub name: String, pub kind: DeviceType, - /// The engines this device syncs, to be published for other devices on the - /// account to see. Not yet published. - #[uniffi(default)] - pub enabled_engines: Vec, } #[derive(Debug, uniffi::Record)] @@ -86,6 +88,12 @@ pub struct SyncResult { /// choices are local without remote updates. `None` means "no change", not /// "nothing declined". pub declined: Option>, + /// The account-wide engine choices in `meta/global`, reported with either + /// `EngineChoicesModel` variant. Will also return the value this sync wrote + /// to `meta/global` if any. + /// Apps using `EngineChoicesModel::Device` may use these to seed their own + /// choices. `None` if the sync didn't get far enough to know them. + pub account_engine_choices: Option, /// Earliest time that the next sync should happen at pub next_sync_allowed_at: Option, /// JSON string encoding a `SyncTelemetryPing` object diff --git a/testing/sync-test/src/auth.rs b/testing/sync-test/src/auth.rs index a85ac8b2183..7877ae5dabf 100644 --- a/testing/sync-test/src/auth.rs +++ b/testing/sync-test/src/auth.rs @@ -41,6 +41,8 @@ pub struct TestClient { /// How this client models engine choices. Change it to simulate an old /// (per-account) or new (per-device) client, or a client flipping between them. pub engine_choices_model: EngineChoicesModel, + /// The engines this client declares it has chosen to sync, if any. + pub enabled_engines: Option>, } impl TestClient { @@ -96,12 +98,13 @@ impl TestClient { sync_manager: SyncManager::new(), persisted_state: None, engine_choices_model: EngineChoicesModel::default(), + enabled_engines: None, }) } fn sync_params( &mut self, - engines: &[String], + engines: SyncEngineSelection, local_encryption_keys: HashMap, ) -> Result { // ensure all our engines are registered. @@ -111,10 +114,9 @@ impl TestClient { let sync_info = self.cli.sync_info()?.expect("CliFxa must have SYNC_SCOPE"); Ok(SyncParams { reason: SyncReason::User, - engines: SyncEngineSelection::Some { - engines: engines.to_vec(), - }, + engines, engine_choices_model: self.engine_choices_model.clone(), + enabled_engines: self.enabled_engines.clone(), local_encryption_keys, auth_info: sync_info.auth_info, persisted_state: self.persisted_state.take(), @@ -122,7 +124,6 @@ impl TestClient { fxa_device_id: self.device.id.clone(), name: self.device.display_name.clone(), kind: self.device.device_type, - enabled_engines: vec![], }, }) } @@ -131,6 +132,23 @@ impl TestClient { &mut self, engines: &[String], local_encryption_keys: HashMap, + ) -> Result { + let engines = SyncEngineSelection::Some { + engines: engines.to_vec(), + }; + self.sync_selection(engines, local_encryption_keys) + } + + /// Syncs with `SyncEngineSelection::All`, so the engines synced are + /// those in `enabled_engines` if it's specified, or all registered engines. + pub fn sync_all(&mut self) -> Result { + self.sync_selection(SyncEngineSelection::All, HashMap::new()) + } + + fn sync_selection( + &mut self, + engines: SyncEngineSelection, + local_encryption_keys: HashMap, ) -> Result { let params = self.sync_params(engines, local_encryption_keys)?; let result = self.sync_manager.sync(params)?; @@ -155,6 +173,9 @@ impl TestClient { engines: &[String], local_encryption_keys: HashMap, ) -> Result> { + let engines = SyncEngineSelection::Some { + engines: engines.to_vec(), + }; let params = self.sync_params(engines, local_encryption_keys)?; let result = self.sync_manager.sync(params)?; // Syncs initiated with this function should fail otherwise `sync` should be used. diff --git a/testing/sync-test/src/engine_choices.rs b/testing/sync-test/src/engine_choices.rs index 72e1198d20c..18e329c6d04 100644 --- a/testing/sync-test/src/engine_choices.rs +++ b/testing/sync-test/src/engine_choices.rs @@ -13,6 +13,7 @@ use crate::auth::TestClient; use crate::testing::TestGroup; use serde_json::Value; use std::collections::{HashMap, HashSet}; +use sync15::AccountEngineChoices; use sync15::EngineChoicesModel::{self, Device}; const ENGINES: [&str; 2] = ["tabs", "passwords"]; @@ -39,6 +40,33 @@ fn sync_failing(c: &mut TestClient, model: EngineChoicesModel) -> HashSet AccountEngineChoices { + c.engine_choices_model = model; + let result = c.sync(&engines(), HashMap::new()).expect("should sync"); + result + .account_engine_choices + .expect("should report the account's choices") +} + +// The account's engine choices in a meta/global record, as they're reported. +fn choices_in(global: &Value) -> AccountEngineChoices { + let mut enabled: Vec = global["engines"] + .as_object() + .expect("should have engines") + .keys() + .cloned() + .collect(); + enabled.sort(); + let declined = global["declined"] + .as_array() + .expect("should have declined") + .iter() + .map(|e| e.as_str().expect("should be a string").to_string()) + .collect(); + AccountEngineChoices { enabled, declined } +} + fn meta_global(c: &TestClient) -> Value { c.server_meta_global().expect("should fetch meta/global") } @@ -79,8 +107,11 @@ fn test_per_device_fresh_start(c0: &mut TestClient, c1: &mut TestClient) { sync(c1, set_tabs(false)); c0.fully_wipe_server().expect("should wipe"); - sync(c0, Device); + let choices = reported_choices(c0, Device); let global = meta_global(c0); + // What it reports is what it wrote. + assert_eq!(choices, choices_in(&global)); + assert!(choices.enabled.contains(&"tabs".to_string())); let tabs_id = sync_id(&global, "tabs"); assert!(tabs_id.is_some()); assert_eq!(sync_id(&meta_syncids(c0), "tabs"), tabs_id); @@ -97,9 +128,11 @@ fn test_per_device_fresh_start_keeps_account_choices(c0: &mut TestClient, c1: &m sync(c0, Device); c0.fully_wipe_server().expect("should wipe"); - sync(c0, Device); + let choices = reported_choices(c0, Device); let global = meta_global(c0); + assert_eq!(choices, choices_in(&global)); + assert!(choices.declined.contains(&"tabs".to_string())); assert!(sync_id(&global, "tabs").is_none()); assert!(global["declined"] .as_array() @@ -246,6 +279,44 @@ fn test_per_device_leaves_newer_sync_ids(c0: &mut TestClient, c1: &mut TestClien assert_eq!(meta_syncids(c0), sync_ids); } +// Either kind of client reports the account's choices, which a per-device +// client might adopt as its own. +fn test_account_choices_reported(c0: &mut TestClient, c1: &mut TestClient) { + // c1 is a per-account client on which the user disabled tabs. + sync(c1, set_tabs(false)); + let global = meta_global(c1); + + for (c, model) in [ + (&mut *c1, EngineChoicesModel::default()), + (&mut *c0, Device), + ] { + let choices = reported_choices(c, model); + assert_eq!(choices, choices_in(&global)); + assert!(choices.declined.contains(&"tabs".to_string())); + assert!(!choices.enabled.contains(&"tabs".to_string())); + } +} + +// The engines a client declares it syncs are what `SyncEngineSelection::All` +// means, ignoring any we don't know. They don't limit an explicit selection. +fn test_enabled_engines(c0: &mut TestClient, _c1: &mut TestClient) { + c0.engine_choices_model = Device; + c0.enabled_engines = Some(vec!["tabs".to_string(), "unknown".to_string()]); + let synced: HashSet = c0 + .sync_all() + .expect("should sync") + .successful + .into_iter() + .collect(); + // Syncing addresses or credit cards, which are also registered, would + // fail without their local encryption keys. + assert_eq!(synced, HashSet::from(["tabs".to_string()])); + + c0.enabled_engines = Some(vec![]); + assert_eq!(sync(c0, Device), HashSet::from_iter(engines())); + c0.enabled_engines = None; +} + pub fn get_test_group() -> TestGroup { TestGroup::new( "engine_choices", @@ -273,6 +344,11 @@ pub fn get_test_group() -> TestGroup { "test_per_device_leaves_newer_sync_ids", test_per_device_leaves_newer_sync_ids, ), + ( + "test_account_choices_reported", + test_account_choices_reported, + ), + ("test_enabled_engines", test_enabled_engines), ], ) }