Commit 50959d99 authored by Michael Comella's avatar Michael Comella Committed by Michael Comella
Browse files

For #13959: add marker when StrictMode is suppressed.

parent 3b5d6d58
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -10,12 +10,14 @@
package org.mozilla.fenix

import android.os.Build
import android.os.Looper
import android.os.StrictMode
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import mozilla.components.support.ktx.android.os.resetAfter
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.perf.Performance

private const val MANUFACTURE_HUAWEI: String = "HUAWEI"
@@ -24,9 +26,19 @@ private const val MANUFACTURE_ONE_PLUS: String = "OnePlus"
/**
 * Manages strict mode settings for the application.
 */
class StrictModeManager(config: Config) {
class StrictModeManager(
    config: Config,

    // Ideally, we'd pass in a more specific value but there is a circular dependency: StrictMode
    // is passed into Core but we'd need to pass in Core here. Instead, we take components and later
    // fetch the value we need from it.
    //
    // Public to be accessible by inline functions.
    val components: Components
) {

    val logger = Performance.logger // public to be accessible by inline functions.
    val mainLooper = Looper.getMainLooper() // public to be accessible by inline functions.

    // This is public so it can be used by inline functions.
    val isEnabledByBuildConfig = config.channel.isDebug
@@ -111,6 +123,9 @@ class StrictModeManager(config: Config) {
            // because it'd increase complexity.
            suppressionCount += 1
            logger.warn("StrictMode violation suppressed: #$suppressionCount")
            if (Thread.currentThread() == mainLooper.thread) { // markers only supported on main thread.
                components.core.engine.profiler?.addMarker("StrictMode.suppression", "Count: $suppressionCount")
            }

            policy.resetAfter(functionBlock)
        } else {
+1 −1
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ class Components(private val context: Context) {
    val performance by lazy { PerformanceComponent() }
    val push by lazy { Push(context, analytics.crashReporter) }
    val wifiConnectionMonitor by lazy { WifiConnectionMonitor(context as Application) }
    val strictMode by lazy { StrictModeManager(Config) }
    val strictMode by lazy { StrictModeManager(Config, this) }

    val settings by lazy { Settings(context) }

+5 −2
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner

@RunWith(FenixRobolectricTestRunner::class)
@@ -36,13 +37,15 @@ class StrictModeManagerTest {
        MockKAnnotations.init(this)
        mockkStatic(StrictMode::class)

        val components: Components = mockk(relaxed = true)

        // These tests log a warning that mockk couldn't set the backing field of Config.channel
        // but it doesn't seem to impact their correctness so I'm ignoring it.
        val debugConfig: Config = mockk { every { channel } returns ReleaseChannel.Debug }
        debugManager = StrictModeManager(debugConfig)
        debugManager = StrictModeManager(debugConfig, components)

        val releaseConfig: Config = mockk { every { channel } returns ReleaseChannel.Release }
        releaseManager = StrictModeManager(releaseConfig)
        releaseManager = StrictModeManager(releaseConfig, components)
    }

    @After