Unverified Commit 74948cb3 authored by Sachin's avatar Sachin Committed by GitHub
Browse files

for #7225, Changed strictMode thread penalty to dialog on startup and back to...

for #7225, Changed strictMode  thread penalty to dialog on startup and back to logs after startup is done. (#10831)

for #7225, refactored and cleanup the branch.

for #7225, change strict mode policy only on main process.

for #7225, setting thread policy inside a seperate thread to keep it from getting overridden in activities.

for #7225 removed Handler().postAtFrontOfQueue as a solution due to unknown side effects. moved the enableStrictMode function to be static so we can reuse it.

for #7225 lint check

for #7225 created strict mode manager and moved enabledStrictMode function inside it.

for #7225 removed penalty death on network

for #7225 added allow disk access on thread for already existing violation


strict mode running in main process to see if it passes the gitlab check, will revert it if it doesnt

allowed diskread for super.onCreate for home activity

added comments for disk violation oncreate homeactivity

added fragment manager inside strictmode manager

allowed disk read for onboarding

allowed disk read for cachedTopSites
parent 04a436d3
......@@ -4,17 +4,20 @@
package org.mozilla.fenix
import android.os.StrictMode
import androidx.preference.PreferenceManager
import leakcanary.AppWatcher
import leakcanary.LeakCanary
import mozilla.components.support.ktx.android.os.resetAfter
import org.mozilla.fenix.ext.getPreferenceKey
class DebugFenixApplication : FenixApplication() {
override fun setupLeakCanary() {
val isEnabled = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(getPreferenceKey(R.string.pref_key_leakcanary), true)
val isEnabled = StrictMode.allowThreadDiskReads().resetAfter {
PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(getPreferenceKey(R.string.pref_key_leakcanary), true)
}
updateLeakCanaryState(isEnabled)
}
......
......@@ -49,6 +49,8 @@ import org.mozilla.fenix.session.PerformanceActivityLifecycleCallbacks
import org.mozilla.fenix.session.VisibilityLifecycleCallback
import org.mozilla.fenix.utils.BrowsersCache
import org.mozilla.fenix.utils.Settings
import mozilla.components.support.ktx.android.os.resetAfter
import org.mozilla.fenix.StrictModeManager.enableStrictMode
/**
*The main application class for Fenix. Records data to measure initialization performance.
......@@ -124,12 +126,13 @@ open class FenixApplication : LocaleAwareApplication() {
val megazordSetup = setupMegazord()
setDayNightTheme()
enableStrictMode()
enableStrictMode(true)
warmBrowsersCache()
// Make sure the engine is initialized and ready to use.
components.core.engine.warmUp()
StrictMode.allowThreadDiskReads().resetAfter {
components.core.engine.warmUp()
}
initializeWebExtensionSupport()
// Just to make sure it is impossible for any application-services pieces
......@@ -336,28 +339,6 @@ open class FenixApplication : LocaleAwareApplication() {
}
}
private fun enableStrictMode() {
if (Config.channel.isDebug) {
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build()
)
var builder = StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.detectLeakedRegistrationObjects()
.detectActivityLeaks()
.detectFileUriExposure()
.penaltyLog()
if (SDK_INT >= Build.VERSION_CODES.O) builder =
builder.detectContentUriWithoutPermission()
if (SDK_INT >= Build.VERSION_CODES.P) builder = builder.detectNonSdkApiUsage()
StrictMode.setVmPolicy(builder.build())
}
}
private fun initializeWebExtensionSupport() {
try {
GlobalAddonDependencyProvider.initialize(
......
......@@ -7,6 +7,7 @@ package org.mozilla.fenix
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.StrictMode
import android.util.AttributeSet
import android.view.View
import android.view.WindowManager
......@@ -39,6 +40,7 @@ import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.support.base.feature.UserInteractionHandler
import mozilla.components.support.ktx.android.arch.lifecycle.addObservers
import mozilla.components.support.ktx.android.content.share
import mozilla.components.support.ktx.android.os.resetAfter
import mozilla.components.support.ktx.kotlin.isUrl
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
import mozilla.components.support.locale.LocaleAwareAppCompatActivity
......@@ -122,7 +124,11 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
}
final override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
StrictModeManager.changeStrictModePolicies(supportFragmentManager)
// There is disk read violations on some devices such as samsung and pixel for android 9/10
StrictMode.allowThreadDiskReads().resetAfter {
super.onCreate(savedInstanceState)
}
components.publicSuffixList.prefetch()
......@@ -428,6 +434,12 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
browsingModeManager.mode = sessionMode
}
override fun attachBaseContext(base: Context) {
StrictMode.allowThreadDiskReads().resetAfter {
super.attachBaseContext(base)
}
}
protected open fun createBrowsingModeManager(initialMode: BrowsingMode): BrowsingModeManager {
return DefaultBrowsingModeManager(initialMode) { newMode ->
themeManager.currentTheme = newMode
......
/* 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
import android.os.Build
import android.os.StrictMode
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
/**
* Manages strict mode settings for the application.
*/
object StrictModeManager {
/***
* Enables strict mode for debug purposes. meant to be run only in the main process.
* @param setPenaltyDialog boolean value to decide setting the dialog box as a penalty.
*/
fun enableStrictMode(setPenaltyDialog: Boolean) {
if (Config.channel.isDebug) {
val threadPolicy = StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
if (setPenaltyDialog) {
threadPolicy.penaltyDialog()
}
StrictMode.setThreadPolicy(threadPolicy.build())
var builder = StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.detectLeakedRegistrationObjects()
.detectActivityLeaks()
.detectFileUriExposure()
.penaltyLog()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) builder =
builder.detectContentUriWithoutPermission()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (setPenaltyDialog) {
builder.permitNonSdkApiUsage()
} else {
builder.detectNonSdkApiUsage()
}
}
StrictMode.setVmPolicy(builder.build())
}
}
/**
* Revert strict mode to disable penalty dialog. Tied to fragment lifecycle since strict mode
* needs to switch to penalty logs. Using the fragment life cycle allows decoupling from any
* specific fragment.
*/
fun changeStrictModePolicies(fragmentManager: FragmentManager) {
fragmentManager.registerFragmentLifecycleCallbacks(object :
FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
enableStrictMode(false)
fm.unregisterFragmentLifecycleCallbacks(this)
}
}, false)
}
}
......@@ -5,6 +5,7 @@
package org.mozilla.fenix.components
import android.content.Context
import android.os.StrictMode
import androidx.annotation.GuardedBy
import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.CoroutineScope
......@@ -17,6 +18,7 @@ import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.lib.crash.CrashReporter
import mozilla.components.service.fxa.manager.FxaAccountManager
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.ktx.android.os.resetAfter
import kotlin.coroutines.CoroutineContext
/**
......@@ -75,7 +77,9 @@ class AccountAbnormalities(
private val logger = Logger("AccountAbnormalities")
private val prefs = context.getSharedPreferences(PREF_FXA_ABNORMALITIES, Context.MODE_PRIVATE)
private val prefs = StrictMode.allowThreadDiskReads().resetAfter {
context.getSharedPreferences(PREF_FXA_ABNORMALITIES, Context.MODE_PRIVATE)
}
/**
* Once [accountManager] is initialized, queries it to detect abnormal account states.
......
......@@ -5,6 +5,7 @@
package org.mozilla.fenix.components
import android.content.Context
import android.os.StrictMode
import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import mozilla.components.browser.session.Session
......@@ -14,6 +15,7 @@ import mozilla.components.feature.tab.collections.TabCollection
import mozilla.components.feature.tab.collections.TabCollectionStorage
import mozilla.components.support.base.observer.Observable
import mozilla.components.support.base.observer.ObserverRegistry
import mozilla.components.support.ktx.android.os.resetAfter
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.toShortUrl
import org.mozilla.fenix.home.sessioncontrol.viewholders.CollectionViewHolder
......@@ -49,7 +51,9 @@ class TabCollectionStorage(
var cachedTabCollections = listOf<TabCollection>()
private val collectionStorage by lazy {
TabCollectionStorage(context, sessionManager)
StrictMode.allowThreadDiskReads().resetAfter {
TabCollectionStorage(context, sessionManager)
}
}
fun createCollection(title: String, sessions: List<Session>) {
......
......@@ -10,6 +10,7 @@ import android.content.DialogInterface
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.StrictMode
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
......@@ -59,6 +60,7 @@ import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.feature.tab.collections.TabCollection
import mozilla.components.feature.top.sites.TopSite
import mozilla.components.lib.state.ext.consumeFrom
import mozilla.components.support.ktx.android.os.resetAfter
import mozilla.components.support.ktx.android.util.dpToPx
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
......@@ -146,8 +148,10 @@ class HomeFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
postponeEnterTransition()
if (!onboarding.userHasBeenOnboarded()) {
requireComponents.analytics.metrics.track(Event.OpenedAppFirstRun)
StrictMode.allowThreadDiskReads().resetAfter {
if (!onboarding.userHasBeenOnboarded()) {
requireComponents.analytics.metrics.track(Event.OpenedAppFirstRun)
}
}
}
......@@ -172,7 +176,9 @@ class HomeFragment : Fragment() {
collections = requireComponents.core.tabCollectionStorage.cachedTabCollections,
expandedCollections = emptySet(),
mode = currentMode.getCurrentMode(),
topSites = requireComponents.core.topSiteStorage.cachedTopSites,
topSites = StrictMode.allowThreadDiskReads().resetAfter {
requireComponents.core.topSiteStorage.cachedTopSites
},
tip = FenixTipManager(listOf(MigrationTipProvider(requireContext()))).getTip()
)
)
......
......@@ -5,6 +5,8 @@ package org.mozilla.fenix.whatsnew
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import android.content.Context
import android.os.StrictMode
import mozilla.components.support.ktx.android.os.resetAfter
// This file is a modified port from Focus Android
......@@ -68,7 +70,9 @@ class WhatsNew private constructor(private val storage: WhatsNewStorage) {
fun shouldHighlightWhatsNew(context: Context): Boolean {
return shouldHighlightWhatsNew(
ContextWhatsNewVersion(context),
SharedPreferenceWhatsNewStorage(context)
StrictMode.allowThreadDiskReads().resetAfter {
SharedPreferenceWhatsNewStorage(context)
}
)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment