Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
For #3074 - Send crash reports through the crash reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
boek committed Nov 9, 2018
1 parent d75d70c commit 8154a5b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ dependencies {
}
implementation "org.mozilla.components:support-ktx:$mozilla_components_version"
implementation "org.mozilla.components:support-utils:$mozilla_components_version"
implementation "org.mozilla.components:lib-crash:$mozilla_components_version"

implementation 'com.squareup.okhttp3:okhttp:3.11.0'

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/mozilla/focus/FocusApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import mozilla.components.service.fretboard.storage.flatfile.FlatFileExperimentS
import org.mozilla.focus.locale.LocaleAwareApplication
import org.mozilla.focus.session.NotificationSessionObserver
import org.mozilla.focus.session.VisibilityLifeCycleCallback
import org.mozilla.focus.telemetry.SentryWrapper
import org.mozilla.focus.telemetry.CrashReporterWrapper
import org.mozilla.focus.telemetry.TelemetrySessionObserver
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.utils.AdjustHelper
Expand Down Expand Up @@ -52,7 +52,7 @@ class FocusApplication : LocaleAwareApplication() {
override fun onCreate() {
super.onCreate()

SentryWrapper.init(this)
CrashReporterWrapper.init(this)
StethoWrapper.init(this)

PreferenceManager.setDefaultValues(this, R.xml.settings, false)
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/org/mozilla/focus/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import org.mozilla.focus.session.removeAndCloseAllSessions
import org.mozilla.focus.session.ui.SessionsSheetFragment
import org.mozilla.focus.settings.ExperimentsSettingsFragment
import org.mozilla.focus.shortcut.HomeScreen
import org.mozilla.focus.telemetry.SentryWrapper
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.utils.AppConstants
import org.mozilla.focus.utils.ExperimentsSyncService
Expand Down Expand Up @@ -60,10 +59,6 @@ open class MainActivity : LocaleAwareAppCompatActivity() {
}
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
SentryWrapper.init(this)
}

initViewModel()

if (Settings.getInstance(this).shouldUseSecureMode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package org.mozilla.focus.telemetry

import android.content.Context
import io.sentry.Sentry
import io.sentry.android.AndroidSentryClientFactory
import android.os.Build
import mozilla.components.lib.crash.Crash
import mozilla.components.lib.crash.CrashReporter
import mozilla.components.lib.crash.service.SentryService
import org.mozilla.focus.BuildConfig
import org.mozilla.focus.locale.LocaleManager
import java.util.Locale
Expand All @@ -19,39 +21,38 @@ import java.util.Locale
* builds, add a .sentry_token file and replace the [TelemetryWrapper.isTelemetryEnabled]
* value with true (upload is disabled by default in dev builds).
*/
object SentryWrapper {
object CrashReporterWrapper {
private const val TAG_BUILD_FLAVOR: String = "build_flavor"
private const val TAG_BUILD_TYPE: String = "build_type"
private const val TAG_LOCALE_LANG_TAG: String = "locale_lang_tag"
private var crashReporter: CrashReporter? = null

fun init(context: Context) {
onIsEnabledChanged(context, TelemetryWrapper.isTelemetryEnabled(context))
addTags(context)
}

internal fun onIsEnabledChanged(context: Context, isEnabled: Boolean) {
// The BuildConfig value is populated from a file at compile time.
// If the file did not exist, the value will be null.
//
// If you provide a null DSN to Sentry, it will disable upload and buffering to disk:
// https://github.com/getsentry/sentry-java/issues/574#issuecomment-378298484
//
// In the current implementation, each time `init` is called, it will overwrite the
// stored client and DSN, thus calling it with a null DSN will have the affect of
// disabling the client: https://github.com/getsentry/sentry-java/issues/574#issuecomment-378406105
val sentryDsn = if (isEnabled) BuildConfig.SENTRY_TOKEN else null
Sentry.init(sentryDsn, AndroidSentryClientFactory(context.applicationContext))
val supportedBuild = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
if (!supportedBuild || BuildConfig.SENTRY_TOKEN.isEmpty()) return

val sentryDsn= BuildConfig.SENTRY_TOKEN
crashReporter = CrashReporter(services = listOf(
SentryService(
context,
sentryDsn,
tags = createTags(context))
)).install(context)

onIsEnabledChanged(context)
}

private fun addTags(context: Context) {
Sentry.getContext().addTag(TAG_BUILD_FLAVOR, BuildConfig.FLAVOR)
Sentry.getContext().addTag(TAG_BUILD_TYPE, BuildConfig.BUILD_TYPE)
Sentry.getContext().addTag(
TAG_LOCALE_LANG_TAG,
getLocaleTag(context)
)
fun onIsEnabledChanged(context: Context, isEnabled: Boolean = TelemetryWrapper.isTelemetryEnabled(context)) {
crashReporter?.enabled = isEnabled
}

private fun createTags(context: Context) = mapOf(
TAG_BUILD_FLAVOR to BuildConfig.FLAVOR,
TAG_BUILD_TYPE to BuildConfig.BUILD_TYPE,
TAG_LOCALE_LANG_TAG to getLocaleTag(context))

private fun getLocaleTag(context: Context): String {
val currentLocale = LocaleManager.getInstance().getCurrentLocale(context)
return if (currentLocale != null) {
Expand All @@ -62,6 +63,7 @@ object SentryWrapper {
}

fun captureGeckoCrash() {
Sentry.capture("GeckoSession crashes, opening new session")
// crashReporter?.submitReport(Crash())
// .capture("GeckoSession crashes, opening new session")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import kotlinx.coroutines.experimental.launch
import mozilla.components.browser.errorpages.ErrorPages
import mozilla.components.browser.errorpages.ErrorType
import mozilla.components.browser.session.Session
import mozilla.components.lib.crash.handler.CrashHandlerService
import mozilla.components.support.ktx.android.util.Base64
import mozilla.components.support.utils.ThreadUtils
import org.json.JSONException
Expand All @@ -31,7 +32,7 @@ import org.mozilla.focus.browser.LocalizedContent
import org.mozilla.focus.ext.savedWebViewState
import org.mozilla.focus.gecko.GeckoViewPrompt
import org.mozilla.focus.gecko.NestedGeckoView
import org.mozilla.focus.telemetry.SentryWrapper
import org.mozilla.focus.telemetry.CrashReporterWrapper
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.utils.AppConstants
import org.mozilla.focus.utils.FileUtils
Expand Down Expand Up @@ -89,6 +90,8 @@ class GeckoWebViewProvider : IWebViewProvider {
runtimeSettingsBuilder.useContentProcessHint(true)
runtimeSettingsBuilder.blockMalware(Settings.getInstance(context).shouldUseSafeBrowsing())
runtimeSettingsBuilder.blockPhishing(Settings.getInstance(context).shouldUseSafeBrowsing())
runtimeSettingsBuilder.crashHandler(CrashHandlerService::class.java)

geckoRuntime =
GeckoRuntime.create(context.applicationContext, runtimeSettingsBuilder.build())
}
Expand Down Expand Up @@ -372,7 +375,7 @@ class GeckoWebViewProvider : IWebViewProvider {

override fun onCrash(session: GeckoSession) {
Log.i(TAG, "Crashed, opening new session")
SentryWrapper.captureGeckoCrash()
CrashReporterWrapper.captureGeckoCrash()
geckoSession.close()
geckoSession = createGeckoSession()
applySettingsAndSetDelegates()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Context
import android.util.AttributeSet
import org.mozilla.focus.R
import org.mozilla.focus.settings.LearnMoreSwitchPreference
import org.mozilla.focus.telemetry.CrashReporterWrapper
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.utils.SupportUtils
import org.mozilla.telemetry.TelemetryHolder
Expand All @@ -29,6 +30,7 @@ internal class TelemetrySwitchPreference(context: Context?, attrs: AttributeSet?
TelemetryHolder.get()
.configuration
.setUploadEnabled(isChecked).isCollectionEnabled = isChecked
CrashReporterWrapper.onIsEnabledChanged(context)
}

override fun getDescription(): String? {
Expand Down

0 comments on commit 8154a5b

Please sign in to comment.