1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterController.kt

88 lines
2.8 KiB
Kotlin
Raw Normal View History

2019-09-13 19:06:03 +02:00
/* 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/. */
package org.mozilla.fenix.crashes
import androidx.navigation.NavController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
2019-09-13 19:06:03 +02:00
import mozilla.components.browser.session.Session
import mozilla.components.lib.crash.Crash
import org.mozilla.fenix.R
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.utils.Settings
class CrashReporterController(
private val crash: Crash,
private val session: Session?,
private val navController: NavController,
private val components: Components,
private val settings: Settings
) {
init {
components.analytics.metrics.track(Event.CrashReporterOpened)
}
/**
* Closes the crash reporter fragment and tries to recover the session.
*
* @param sendCrash If true, submit a crash report.
* @return Job if report is submitted through an IO thread, null otherwise
2019-09-13 19:06:03 +02:00
*/
fun handleCloseAndRestore(sendCrash: Boolean): Job? {
val job = submitReportIfNecessary(sendCrash)
2019-09-13 19:06:03 +02:00
components.useCases.sessionUseCases.crashRecovery.invoke()
navController.popBackStack()
return job
2019-09-13 19:06:03 +02:00
}
/**
* Closes the crash reporter fragment and the tab.
*
* @param sendCrash If true, submit a crash report.
* @return Job if report is submitted through an IO thread, null otherwise
2019-09-13 19:06:03 +02:00
*/
fun handleCloseAndRemove(sendCrash: Boolean): Job? {
session ?: return null
val job = submitReportIfNecessary(sendCrash)
2019-09-13 19:06:03 +02:00
components.useCases.tabsUseCases.removeTab(session)
components.useCases.sessionUseCases.crashRecovery.invoke()
2019-09-13 19:06:03 +02:00
navController.nav(
R.id.crashReporterFragment,
CrashReporterFragmentDirections.actionGlobalHome()
2019-09-13 19:06:03 +02:00
)
return job
2019-09-13 19:06:03 +02:00
}
/**
* Submits the crash report if the "Send crash" checkbox was checked and the setting is enabled.
*
* @param sendCrash If true, submit a crash report.
* @return Job if report is submitted through an IO thread, null otherwise
2019-09-13 19:06:03 +02:00
*/
private fun submitReportIfNecessary(sendCrash: Boolean): Job? {
var job: Job? = null
2019-09-13 19:06:03 +02:00
val didSubmitReport = if (sendCrash && settings.isCrashReportingEnabled) {
job = GlobalScope.launch(Dispatchers.IO) {
components.analytics.crashReporter.submitReport(crash)
}
2019-09-13 19:06:03 +02:00
true
} else {
false
}
components.analytics.metrics.track(Event.CrashReporterClosed(didSubmitReport))
return job
2019-09-13 19:06:03 +02:00
}
}