Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ⚠️

Expand Down
5 changes: 5 additions & 0 deletions components/sync15/src/client/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +71,10 @@ pub struct SyncResult {
/// The set of declined engines, if we know them.
pub declined: Option<Vec<String>>,

/// 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<AccountEngineChoices>,

/// The result of the sync.
pub result: Result<(), Error>,

Expand Down
11 changes: 10 additions & 1 deletion components/sync15/src/client/sync_multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<String> = 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;
Expand Down
4 changes: 0 additions & 4 deletions components/sync15/src/clients_engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -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(),
};
Expand Down
2 changes: 0 additions & 2 deletions components/sync15/src/clients_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
11 changes: 11 additions & 0 deletions components/sync15/src/engine_choices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// The engines in `meta/global`'s declined.
pub declined: Vec<String>,
}
2 changes: 1 addition & 1 deletion components/sync15/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 20 additions & 3 deletions components/sync_manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params.engines)?;
let engines =
self.calc_engines_to_sync(&params.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, &params) {
info!("No backoff in effect (or we decided to ignore it), starting sync");
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand All @@ -213,6 +215,7 @@ impl SyncManager {
fn calc_engines_to_sync(
&self,
selection: &SyncEngineSelection,
enabled_engines: Option<&[String]>,
) -> Result<Vec<Box<dyn SyncEngine>>> {
// BTreeMap to ensure we sync the engines in priority order.
let mut engine_map: BTreeMap<_, _> = self.iter_registered_engines().collect();
Expand All @@ -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<SyncEngineId> = 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())
}
Expand Down Expand Up @@ -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(),
Expand All @@ -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![],
},
}
}
Expand Down
18 changes: 13 additions & 5 deletions components/sync_manager/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Vec<String>>,
/// 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<String, String>,
Expand Down Expand Up @@ -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<String>,
}

#[derive(Debug, uniffi::Record)]
Expand All @@ -86,6 +88,12 @@ pub struct SyncResult {
/// choices are local without remote updates. `None` means "no change", not
/// "nothing declined".
pub declined: Option<Vec<String>>,
/// 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<AccountEngineChoices>,
/// Earliest time that the next sync should happen at
pub next_sync_allowed_at: Option<SystemTime>,
/// JSON string encoding a `SyncTelemetryPing` object
Expand Down
31 changes: 26 additions & 5 deletions testing/sync-test/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,
}

impl TestClient {
Expand Down Expand Up @@ -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<String, String>,
) -> Result<SyncParams> {
// ensure all our engines are registered.
Expand All @@ -111,18 +114,16 @@ 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(),
device_settings: DeviceSettings {
fxa_device_id: self.device.id.clone(),
name: self.device.display_name.clone(),
kind: self.device.device_type,
enabled_engines: vec![],
},
})
}
Expand All @@ -131,6 +132,23 @@ impl TestClient {
&mut self,
engines: &[String],
local_encryption_keys: HashMap<String, String>,
) -> Result<SyncResult> {
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<SyncResult> {
self.sync_selection(SyncEngineSelection::All, HashMap::new())
}

fn sync_selection(
&mut self,
engines: SyncEngineSelection,
local_encryption_keys: HashMap<String, String>,
) -> Result<SyncResult> {
let params = self.sync_params(engines, local_encryption_keys)?;
let result = self.sync_manager.sync(params)?;
Expand All @@ -155,6 +173,9 @@ impl TestClient {
engines: &[String],
local_encryption_keys: HashMap<String, String>,
) -> Result<HashMap<String, String, RandomState>> {
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.
Expand Down
Loading