diff --git a/library/include/updater.h b/library/include/updater.h index 7dfec93..7adb9c7 100644 --- a/library/include/updater.h +++ b/library/include/updater.h @@ -55,6 +55,11 @@ SHOREBIRD_EXPORT bool shorebird_init(const struct AppParameters *c_params, const char *c_yaml); +/** + * Returns if the app should run the updater automatically on launch. + */ +SHOREBIRD_EXPORT bool shorebird_should_auto_update(void); + /** * The currently running patch number, or 0 if the release has not been * patched. diff --git a/library/src/c_api.rs b/library/src/c_api.rs index 70aea17..6b31d2a 100644 --- a/library/src/c_api.rs +++ b/library/src/c_api.rs @@ -109,6 +109,16 @@ pub extern "C" fn shorebird_init( ) } +/// Returns if the app should run the updater automatically on launch. +#[no_mangle] +pub extern "C" fn shorebird_should_auto_update() -> bool { + log_on_error( + || updater::should_auto_update(), + "fetching update behavior", + true, + ) +} + /// The currently running patch number, or 0 if the release has not been /// patched. #[no_mangle] @@ -336,6 +346,25 @@ mod test { free_parameters(c_params); } + #[serial] + #[test] + fn yaml_parsing() { + testing_reset_config(); + let tmp_dir = TempDir::new("example").unwrap(); + let c_params = parameters(&tmp_dir, "/dir/lib/arm64/libapp.so"); + let c_yaml = c_string( + " + app_id: foo + channel: bar + base_url: baz + auto_update: false", + ); + assert_eq!(shorebird_init(&c_params, c_yaml), true); + free_c_string(c_yaml); + free_parameters(c_params); + assert_eq!(shorebird_should_auto_update(), false); + } + #[serial] #[test] fn empty_state_no_update() { diff --git a/library/src/cache.rs b/library/src/cache.rs index 8451ca9..3643309 100644 --- a/library/src/cache.rs +++ b/library/src/cache.rs @@ -88,6 +88,8 @@ impl UpdaterState { self.failed_patches.iter().any(|v| v == &patch_number) } + // TODO(eseidel): Should return Result instead of logging, the c_api + // layer can log if desired. pub fn mark_patch_as_bad(&mut self, patch_number: usize) { if self.is_known_good_patch(patch_number) { warn!("Tried to report failed launch for a known good patch. Ignoring."); @@ -102,6 +104,8 @@ impl UpdaterState { self.failed_patches.push(patch_number); } + // TODO(eseidel): Should return Result instead of logging, the c_api + // layer can log if desired. pub fn mark_patch_as_good(&mut self, patch_number: usize) { if self.is_known_bad_patch(patch_number) { warn!("Tried to report successful launch for a known bad patch. Ignoring."); diff --git a/library/src/config.rs b/library/src/config.rs index 6a30b23..daa057c 100644 --- a/library/src/config.rs +++ b/library/src/config.rs @@ -73,6 +73,7 @@ where pub struct UpdateConfig { pub cache_dir: PathBuf, pub download_dir: PathBuf, + pub auto_update: bool, pub channel: String, pub app_id: String, pub release_version: String, @@ -102,6 +103,7 @@ pub fn set_config( .as_deref() .unwrap_or(DEFAULT_CHANNEL) .to_owned(), + auto_update: yaml.auto_update.unwrap_or(true), app_id: yaml.app_id.to_string(), release_version: app_config.release_version.to_string(), libapp_path, diff --git a/library/src/updater.rs b/library/src/updater.rs index 95709cc..0cfa8c7 100644 --- a/library/src/updater.rs +++ b/library/src/updater.rs @@ -117,6 +117,10 @@ pub fn init(app_config: AppConfig, yaml: &str) -> Result<(), UpdateError> { .map_err(|err| UpdateError::InvalidState(err.to_string())) } +pub fn should_auto_update() -> anyhow::Result { + with_config(|config| Ok(config.auto_update)) +} + fn check_for_update_internal() -> anyhow::Result { with_config(|config| { // Load UpdaterState from disk diff --git a/library/src/yaml.rs b/library/src/yaml.rs index c00fbaa..df23747 100644 --- a/library/src/yaml.rs +++ b/library/src/yaml.rs @@ -10,6 +10,8 @@ pub struct YamlConfig { pub channel: Option, /// Update URL. Defaults to the default update URL if not set. pub base_url: Option, + /// Update behavior. Defaults to true if not set. + pub auto_update: Option, } impl YamlConfig {