This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[components] For mozilla-mobile/android-components#3439 - Add "report…
…" action to crash notification
- Loading branch information
1 parent
08af642
commit 58310fd
Showing
5 changed files
with
211 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ts/lib/crash/src/main/java/mozilla/components/lib/crash/service/SendCrashReportService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* 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 mozilla.components.lib.crash.service | ||
|
||
import android.app.Service | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.os.IBinder | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import mozilla.components.lib.crash.Crash | ||
import mozilla.components.lib.crash.CrashReporter | ||
import mozilla.components.lib.crash.R | ||
import mozilla.components.lib.crash.notification.CrashNotification | ||
import mozilla.components.lib.crash.notification.NOTIFICATION_TAG | ||
import mozilla.components.support.base.ids.NotificationIds | ||
import mozilla.components.support.base.ids.cancel | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
|
||
class SendCrashReportService : Service() { | ||
private val crashReporter: CrashReporter by lazy { CrashReporter.requireInstance } | ||
private val logger by lazy { CrashReporter | ||
.requireInstance | ||
.logger | ||
} | ||
|
||
private var reporterCoroutineContext: CoroutineContext = EmptyCoroutineContext | ||
|
||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val channel = CrashNotification.ensureChannelExists(this) | ||
val notification = NotificationCompat.Builder(this, channel) | ||
.setContentTitle(getString(R.string.mozac_lib_send_crash_report_in_progress, | ||
crashReporter.promptConfiguration.organizationName)) | ||
.setSmallIcon(R.drawable.mozac_lib_crash_notification) | ||
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | ||
.setCategory(NotificationCompat.CATEGORY_ERROR) | ||
.setAutoCancel(true) | ||
.setProgress(0, 0, true) | ||
.build() | ||
|
||
val notificationId = NotificationIds.getIdForTag(this, NOTIFICATION_TAG) | ||
startForeground(notificationId, notification) | ||
} | ||
|
||
intent.extras?.let { extras -> | ||
val crash = Crash.NativeCodeCrash.fromBundle(extras) | ||
NotificationManagerCompat.from(this).cancel(this, NOTIFICATION_TAG) | ||
|
||
sendCrashReport(crash) { | ||
stopSelf() | ||
} | ||
} ?: logger.error("Received intent with null extras") | ||
|
||
return START_NOT_STICKY | ||
} | ||
|
||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
internal fun sendCrashReport(crash: Crash, then: () -> Unit) { | ||
GlobalScope.launch(reporterCoroutineContext) { | ||
crashReporter.submitReport(crash) | ||
|
||
withContext(Dispatchers.Main) { | ||
then() | ||
} | ||
} | ||
} | ||
|
||
override fun onBind(intent: Intent): IBinder? { | ||
// We don't provide binding, so return null | ||
return null | ||
} | ||
|
||
companion object { | ||
fun createReportIntent(context: Context, crash: Crash): Intent { | ||
val intent = Intent(context, SendCrashReportService::class.java) | ||
crash.fillIn(intent) | ||
|
||
return intent | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ib/crash/src/test/java/mozilla/components/lib/crash/service/SendCrashReportServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* 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 mozilla.components.lib.crash.service | ||
|
||
import android.content.ComponentName | ||
import android.content.Intent | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.components.lib.crash.Crash | ||
import mozilla.components.lib.crash.CrashReporter | ||
import mozilla.components.support.test.any | ||
import mozilla.components.support.test.eq | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.spy | ||
import org.mockito.Mockito.verify | ||
import org.robolectric.Robolectric | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class SendCrashReportServiceTest { | ||
private var service: SendCrashReportService? = null | ||
|
||
@Before | ||
fun setUp() { | ||
service = spy(Robolectric.setupService(SendCrashReportService::class.java)) | ||
service?.startService(Intent()) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
service?.stopService(Intent()) | ||
CrashReporter.reset() | ||
} | ||
|
||
@Test | ||
fun `CrashRHandlerService will forward same crash to crash reporter`() { | ||
spy(CrashReporter( | ||
shouldPrompt = CrashReporter.Prompt.ALWAYS, | ||
services = listOf(object : CrashReporterService { | ||
override fun report(crash: Crash.UncaughtExceptionCrash) { | ||
} | ||
|
||
override fun report(crash: Crash.NativeCodeCrash) { | ||
} | ||
})) | ||
).install(testContext) | ||
val originalCrash = Crash.NativeCodeCrash( | ||
"/data/data/org.mozilla.samples.browser/files/mozilla/Crash Reports/pending/3ba5f665-8422-dc8e-a88e-fc65c081d304.dmp", | ||
true, | ||
"/data/data/org.mozilla.samples.browser/files/mozilla/Crash Reports/pending/3ba5f665-8422-dc8e-a88e-fc65c081d304.extra", | ||
false | ||
) | ||
|
||
val intent = Intent("org.mozilla.gecko.ACTION_CRASHED") | ||
intent.component = ComponentName( | ||
"org.mozilla.samples.browser", | ||
"mozilla.components.lib.crash.handler.CrashHandlerService" | ||
) | ||
intent.putExtra( | ||
"minidumpPath", | ||
"/data/data/org.mozilla.samples.browser/files/mozilla/Crash Reports/pending/3ba5f665-8422-dc8e-a88e-fc65c081d304.dmp" | ||
) | ||
intent.putExtra("fatal", false) | ||
intent.putExtra( | ||
"extrasPath", | ||
"/data/data/org.mozilla.samples.browser/files/mozilla/Crash Reports/pending/3ba5f665-8422-dc8e-a88e-fc65c081d304.extra" | ||
) | ||
intent.putExtra("minidumpSuccess", true) | ||
originalCrash.fillIn(intent) | ||
|
||
service?.onStartCommand(intent, 0, 0) | ||
verify(service)?.sendCrashReport(eq(originalCrash), any()) | ||
} | ||
} |