Handle that Glean might be uninitalized when an upload task is requested

If Glean is uninitalized a `Done` task is returned, instructing the
uploader to not request further upload tasks and finish.
Uploads might now be delayed until the next ping is submitted (or the app restarted),
but that was kinda already the case if this crashed anyway.
This commit is contained in:
Jan-Erik Rediger 2022-07-28 14:35:42 +02:00 коммит произвёл Jan-Erik Rediger
Родитель b48898e1db
Коммит 73745a1df5
3 изменённых файлов: 21 добавлений и 2 удалений

Просмотреть файл

@ -53,6 +53,9 @@ pub fn setup_glean(glean: Glean) -> Result<()> {
Ok(())
}
/// Execute `f` passing the global Glean object.
///
/// Panics if the global Glean object has not been set.
pub fn with_glean<F, R>(f: F) -> R
where
F: FnOnce(&Glean) -> R,
@ -62,6 +65,9 @@ where
f(&lock)
}
/// Execute `f` passing the global Glean object mutable.
///
/// Panics if the global Glean object has not been set.
pub fn with_glean_mut<F, R>(f: F) -> R
where
F: FnOnce(&mut Glean) -> R,
@ -71,6 +77,19 @@ where
f(&mut lock)
}
/// Execute `f` passing the global Glean object if it has been set.
///
/// Returns `None` if the global Glean object has not been set.
/// Returns `Some(T)` otherwise.
pub fn with_opt_glean<F, R>(f: F) -> Option<R>
where
F: FnOnce(&Glean) -> R,
{
let glean = global_glean()?;
let lock = glean.lock().unwrap();
Some(f(&lock))
}
/// The object holding meta information about a Glean instance.
///
/// ## Example

Просмотреть файл

@ -847,7 +847,7 @@ pub fn glean_test_destroy_glean(clear_stores: bool) {
/// Get the next upload task
pub fn glean_get_upload_task() -> PingUploadTask {
core::with_glean(|glean| glean.get_upload_task())
core::with_opt_glean(|glean| glean.get_upload_task()).unwrap_or_else(PingUploadTask::done)
}
/// Processes the response from an attempt to upload a ping.

Просмотреть файл

@ -174,7 +174,7 @@ impl PingUploadTask {
matches!(self, PingUploadTask::Wait { .. })
}
fn done() -> Self {
pub(crate) fn done() -> Self {
PingUploadTask::Done { unused: 0 }
}
}