Skip to content

Commit

Permalink
refactor: rename jni functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Nov 21, 2024
1 parent ec66742 commit 8d3ff08
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import io.embrace.android.embracesdk.internal.ndk.EmbraceNdkService
import io.embrace.android.embracesdk.internal.ndk.EmbraceNdkServiceRepository
import io.embrace.android.embracesdk.internal.ndk.NativeCrashDataSourceImpl
import io.embrace.android.embracesdk.internal.ndk.NativeCrashService
import io.embrace.android.embracesdk.internal.ndk.NdkDelegateImpl
import io.embrace.android.embracesdk.internal.ndk.NdkService
import io.embrace.android.embracesdk.internal.ndk.jni.JniDelegateImpl
import io.embrace.android.embracesdk.internal.worker.Worker

internal class NativeFeatureModuleImpl(
Expand Down Expand Up @@ -39,7 +39,7 @@ internal class NativeFeatureModuleImpl(
nativeCoreModule.sharedObjectLoader,
initModule.logger,
embraceNdkServiceRepository,
NdkDelegateImpl(),
JniDelegateImpl(),
workerThreadModule.backgroundWorker(Worker.Background.IoRegWorker),
payloadSourceModule.deviceArchitecture,
initModule.jsonSerializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.embrace.android.embracesdk.internal.config.ConfigService
import io.embrace.android.embracesdk.internal.crash.CrashFileMarkerImpl
import io.embrace.android.embracesdk.internal.logging.EmbLogger
import io.embrace.android.embracesdk.internal.logging.InternalErrorType
import io.embrace.android.embracesdk.internal.ndk.jni.JniDelegate
import io.embrace.android.embracesdk.internal.payload.AppFramework
import io.embrace.android.embracesdk.internal.payload.NativeCrashData
import io.embrace.android.embracesdk.internal.payload.NativeCrashDataError
Expand Down Expand Up @@ -47,7 +48,7 @@ internal class EmbraceNdkService(
private val sharedObjectLoader: SharedObjectLoader,
private val logger: EmbLogger,
private val repository: NdkServiceRepository,
private val delegate: NdkServiceDelegate.NdkDelegate,
private val delegate: JniDelegate,
private val backgroundWorker: BackgroundWorker,
private val deviceArchitecture: DeviceArchitecture,
private val serializer: PlatformSerializer,
Expand Down Expand Up @@ -81,7 +82,7 @@ internal class EmbraceNdkService(

override fun updateSessionId(newSessionId: String) {
if (sharedObjectLoader.loaded.get()) {
delegate._updateSessionId(newSessionId)
delegate.updateSessionId(newSessionId)
}
}

Expand Down Expand Up @@ -126,12 +127,12 @@ internal class EmbraceNdkService(

private fun checkSignalHandlersOverwritten() {
if (configService.autoDataCaptureBehavior.is3rdPartySigHandlerDetectionEnabled()) {
val culprit = delegate._checkForOverwrittenHandlers()
val culprit = delegate.checkForOverwrittenHandlers()

Check warning on line 130 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt#L130

Added line #L130 was not covered by tests
if (culprit != null) {
if (shouldIgnoreOverriddenHandler(culprit)) {
return
}
delegate._reinstallSignalHandlers()
delegate.reinstallSignalHandlers()

Check warning on line 135 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt#L135

Added line #L135 was not covered by tests
}
}
}
Expand Down Expand Up @@ -167,7 +168,7 @@ internal class EmbraceNdkService(
val nativeCrashId: String = unityCrashId ?: Uuid.getEmbUuid()
val is32bit = deviceArchitecture.is32BitDevice
Systrace.traceSynchronous("native-install-handlers") {
delegate._installSignalHandlers(
delegate.installSignalHandlers(
reportBasePath,
markerFilePath,
sessionIdProvider(),
Expand All @@ -189,7 +190,7 @@ internal class EmbraceNdkService(
private fun getNativeCrashErrors(errorFile: File?): List<NativeCrashDataError?>? {
if (errorFile != null) {
val absolutePath = errorFile.absolutePath
val errorsRaw = delegate._getErrors(absolutePath)
val errorsRaw = delegate.getErrors(absolutePath)

Check warning on line 193 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt#L193

Added line #L193 was not covered by tests
if (errorsRaw != null) {
runCatching {
val type = TypeUtils.typedList(NativeCrashDataError::class)
Expand Down Expand Up @@ -230,7 +231,7 @@ internal class EmbraceNdkService(
for (crashFile in matchingFiles) {
try {
val path = crashFile.path
delegate._getCrashReport(path)?.let { crashRaw ->
delegate.getCrashReport(path)?.let { crashRaw ->
val nativeCrash = serializer.fromJson(crashRaw, NativeCrashData::class.java)
val errorFile = repository.errorFileForCrash(crashFile)?.apply {
getNativeCrashErrors(this).let { errors ->
Expand Down Expand Up @@ -302,7 +303,7 @@ internal class EmbraceNdkService(

private fun updateAppState(newAppState: String) {
if (sharedObjectLoader.loaded.get()) {
delegate._updateAppState(newAppState)
delegate.updateAppState(newAppState)
}
}

Expand All @@ -316,7 +317,7 @@ internal class EmbraceNdkService(
if (json.length >= EMB_DEVICE_META_DATA_SIZE) {
json = serializeMetadata(src.copy(sessionProperties = null))
}
delegate._updateMetaData(json)
delegate.updateMetaData(json)
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.embrace.android.embracesdk.internal.ndk.jni

internal interface JniDelegate {
fun installSignalHandlers(
reportPath: String?,
markerFilePath: String?,
sessionId: String?,
appState: String?,
reportId: String?,
apiLevel: Int,
is32bit: Boolean,
devLogging: Boolean,
)
fun updateMetaData(metadata: String?)
fun updateSessionId(sessionId: String?)
fun updateAppState(appState: String?)
fun getCrashReport(path: String?): String?
fun getErrors(path: String?): String?
fun checkForOverwrittenHandlers(): String?
fun reinstallSignalHandlers(): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.embrace.android.embracesdk.internal.ndk.jni

class JniDelegateImpl : JniDelegate {
external override fun installSignalHandlers(
reportPath: String?,
markerFilePath: String?,
sessionId: String?,
appState: String?,
reportId: String?,
apiLevel: Int,
is32bit: Boolean,
devLogging: Boolean,
)

external override fun updateMetaData(metadata: String?)
external override fun updateSessionId(sessionId: String?)
external override fun updateAppState(appState: String?)
external override fun getCrashReport(path: String?): String?
external override fun getErrors(path: String?): String?
external override fun checkForOverwrittenHandlers(): String?
external override fun reinstallSignalHandlers(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import io.embrace.android.embracesdk.internal.capture.metadata.MetadataService
import io.embrace.android.embracesdk.internal.crash.CrashFileMarkerImpl
import io.embrace.android.embracesdk.internal.logging.EmbLogger
import io.embrace.android.embracesdk.internal.logging.EmbLoggerImpl
import io.embrace.android.embracesdk.internal.ndk.jni.JniDelegate
import io.embrace.android.embracesdk.internal.payload.AppFramework
import io.embrace.android.embracesdk.internal.payload.NativeCrashMetadata
import io.embrace.android.embracesdk.internal.serialization.EmbraceSerializer
Expand Down Expand Up @@ -79,7 +80,7 @@ internal class EmbraceNdkServiceTest {
private lateinit var sessionPropertiesService: FakeSessionPropertiesService
private lateinit var sharedObjectLoader: FakeSharedObjectLoader
private lateinit var logger: EmbLogger
private lateinit var delegate: NdkServiceDelegate.NdkDelegate
private lateinit var delegate: JniDelegate
private lateinit var repository: FakeNdkServiceRepository
private lateinit var resources: Resources
private lateinit var blockableExecutorService: BlockableExecutorService
Expand Down Expand Up @@ -177,14 +178,14 @@ internal class EmbraceNdkServiceTest {
fun `test updateSessionId where installSignals was executed and isInstalled true`() {
initializeService()
embraceNdkService.updateSessionId("sessionId")
verify(exactly = 1) { delegate._updateSessionId("sessionId") }
verify(exactly = 1) { delegate.updateSessionId("sessionId") }
}

@Test
fun `test onBackground runs _updateAppState when _updateMetaData was executed and isInstalled true`() {
initializeService()
embraceNdkService.onBackground(0L)
verify(exactly = 1) { delegate._updateAppState("background") }
verify(exactly = 1) { delegate.updateAppState("background") }
}

@Test
Expand All @@ -200,7 +201,7 @@ internal class EmbraceNdkServiceTest {
)

val expected = serializer.toJson(newDeviceMetaData)
verify { delegate._updateMetaData(expected) }
verify { delegate.updateMetaData(expected) }
}

@Test
Expand All @@ -216,7 +217,7 @@ internal class EmbraceNdkServiceTest {
val markerFilePath =
storageManager.filesDirectory.absolutePath + "/" + CrashFileMarkerImpl.CRASH_MARKER_FILE_NAME
verify(exactly = 1) {
delegate._installSignalHandlers(
delegate.installSignalHandlers(
reportBasePath,
markerFilePath,
"null",
Expand All @@ -237,7 +238,7 @@ internal class EmbraceNdkServiceTest {
)
)

verify(exactly = 1) { delegate._updateMetaData(newDeviceMetaData) }
verify(exactly = 1) { delegate.updateMetaData(newDeviceMetaData) }
assertEquals(embraceNdkService.unityCrashId, Uuid.getEmbUuid())
}

Expand All @@ -253,7 +254,7 @@ internal class EmbraceNdkServiceTest {
storageManager.filesDirectory.absolutePath + "/" + CrashFileMarkerImpl.CRASH_MARKER_FILE_NAME

verifyOrder {
delegate._installSignalHandlers(
delegate.installSignalHandlers(
reportBasePath,
markerFilePath,
"null",
Expand All @@ -263,7 +264,7 @@ internal class EmbraceNdkServiceTest {
deviceArchitecture.is32BitDevice,
false
)
delegate._updateMetaData(any())
delegate.updateMetaData(any())
}
}

Expand All @@ -289,22 +290,22 @@ internal class EmbraceNdkServiceTest {
)

val expected = serializer.toJson(newDeviceMetaData)
verify { delegate._updateMetaData(expected) }
verify { delegate.updateMetaData(expected) }
}

@Test
fun `test onForeground runs _updateAppState when _updateMetaData was executed and isInstalled true`() {
initializeService()
embraceNdkService.onForeground(true, 10)
verify(exactly = 1) { delegate._updateAppState("foreground") }
verify(exactly = 1) { delegate.updateAppState("foreground") }
}

@Test
fun `test getLatestNativeCrash does nothing if there are no matchingFiles`() {
initializeService()
val result = embraceNdkService.getLatestNativeCrash()
assertNull(result)
verify(exactly = 0) { delegate._getCrashReport(any()) }
verify(exactly = 0) { delegate.getCrashReport(any()) }
}

@Test
Expand Down Expand Up @@ -337,7 +338,7 @@ internal class EmbraceNdkServiceTest {
" }\n" +
" ]\n" +
"}"
every { delegate._getCrashReport(any()) } returns json
every { delegate.getCrashReport(any()) } returns json

initializeService()
val crashData = embraceNdkService.getLatestNativeCrash()
Expand All @@ -349,7 +350,7 @@ internal class EmbraceNdkServiceTest {
repository.addCrashFiles(
nativeCrashFile = File.createTempFile("test", "crash-test")
)
every { delegate._getCrashReport(any()) } returns getNativeCrashRaw()
every { delegate.getCrashReport(any()) } returns getNativeCrashRaw()

configService.appFramework = AppFramework.UNITY

Expand Down Expand Up @@ -390,7 +391,7 @@ internal class EmbraceNdkServiceTest {

@Test
fun `getNativeCrashes returns all the crashes in the repository and doesn't invoke delete`() {
every { delegate._getCrashReport(any()) } returns getNativeCrashRaw()
every { delegate.getCrashReport(any()) } returns getNativeCrashRaw()
initializeService()
repository.addCrashFiles(File.createTempFile("file1", "tmp"))
repository.addCrashFiles(File.createTempFile("file2", "temp"))
Expand All @@ -400,7 +401,7 @@ internal class EmbraceNdkServiceTest {

@Test
fun `getLatestNativeCrash returns only one crash even if there are many and deletes them all`() {
every { delegate._getCrashReport(any()) } returns getNativeCrashRaw()
every { delegate.getCrashReport(any()) } returns getNativeCrashRaw()
initializeService()
repository.addCrashFiles(File.createTempFile("file1", "tmp"))
repository.addCrashFiles(File.createTempFile("file2", "temp"))
Expand All @@ -410,7 +411,7 @@ internal class EmbraceNdkServiceTest {

private fun assertNativeSignalHandlerInstalled() {
verify(exactly = 1) {
delegate._installSignalHandlers(
delegate.installSignalHandlers(
any(),
any(),
any(),
Expand Down
2 changes: 1 addition & 1 deletion embrace-android-sdk/embrace-proguard.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

## Keep classes with JNI calls to native code
-keep class io.embrace.android.embracesdk.internal.anr.ndk.NativeThreadSamplerNdkDelegate { *; }
-keep class io.embrace.android.embracesdk.internal.ndk.NdkDelegateImpl { *; }
-keep class io.embrace.android.embracesdk.internal.ndk.jni.JniDelegateImpl { *; }

## OpenTelemetry Java SDK
-keep class io.opentelemetry.api.trace.StatusCode { *; }
Expand Down
Loading

0 comments on commit 8d3ff08

Please sign in to comment.