2019-01-24 21:07:52 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 18:38:15 +00:00
|
|
|
* 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/. */
|
2019-01-24 21:07:52 +00:00
|
|
|
|
2019-01-23 21:39:53 +00:00
|
|
|
package org.mozilla.fenix
|
|
|
|
|
2019-02-13 15:08:35 +00:00
|
|
|
import android.annotation.SuppressLint
|
2019-06-23 17:13:52 +00:00
|
|
|
import android.os.Build
|
|
|
|
import android.os.Build.VERSION.SDK_INT
|
2019-07-12 05:23:28 +00:00
|
|
|
import android.os.StrictMode
|
2020-07-28 23:26:18 +00:00
|
|
|
import android.util.Log.INFO
|
2019-11-22 13:18:50 +00:00
|
|
|
import androidx.annotation.CallSuper
|
2021-03-12 13:21:34 +00:00
|
|
|
import androidx.annotation.VisibleForTesting
|
2019-04-02 00:53:37 +00:00
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
2019-08-28 19:55:24 +00:00
|
|
|
import androidx.core.content.getSystemService
|
2020-07-28 23:26:18 +00:00
|
|
|
import androidx.work.Configuration.Builder
|
2020-08-04 18:18:52 +00:00
|
|
|
import androidx.work.Configuration.Provider
|
2019-05-02 15:59:13 +00:00
|
|
|
import kotlinx.coroutines.Deferred
|
2019-01-29 19:20:29 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
2020-01-04 02:15:35 +00:00
|
|
|
import kotlinx.coroutines.async
|
|
|
|
import kotlinx.coroutines.launch
|
2019-07-17 22:09:47 +00:00
|
|
|
import mozilla.appservices.Megazord
|
2020-08-19 18:10:20 +00:00
|
|
|
import mozilla.components.browser.state.action.SystemAction
|
2021-01-14 18:21:44 +00:00
|
|
|
import mozilla.components.browser.state.selector.selectedTab
|
2020-11-30 10:38:42 +00:00
|
|
|
import mozilla.components.concept.base.crash.Breadcrumb
|
2021-03-12 13:21:34 +00:00
|
|
|
import mozilla.components.concept.engine.webextension.WebExtension
|
|
|
|
import mozilla.components.concept.engine.webextension.isUnsupported
|
2019-07-19 20:05:17 +00:00
|
|
|
import mozilla.components.concept.push.PushProcessor
|
2021-03-12 13:21:34 +00:00
|
|
|
import mozilla.components.feature.addons.migration.DefaultSupportedAddonsChecker
|
2020-02-25 17:31:05 +00:00
|
|
|
import mozilla.components.feature.addons.update.GlobalAddonDependencyProvider
|
2020-06-03 18:18:44 +00:00
|
|
|
import mozilla.components.lib.crash.CrashReporter
|
2020-01-15 23:31:51 +00:00
|
|
|
import mozilla.components.service.glean.Glean
|
|
|
|
import mozilla.components.service.glean.config.Configuration
|
|
|
|
import mozilla.components.service.glean.net.ConceptFetchHttpUploader
|
2021-02-02 01:48:54 +00:00
|
|
|
import mozilla.components.support.base.facts.register
|
2019-01-29 19:20:29 +00:00
|
|
|
import mozilla.components.support.base.log.Log
|
|
|
|
import mozilla.components.support.base.log.logger.Logger
|
2019-03-14 13:27:54 +00:00
|
|
|
import mozilla.components.support.ktx.android.content.isMainProcess
|
|
|
|
import mozilla.components.support.ktx.android.content.runOnlyInMainProcess
|
2020-01-04 02:15:35 +00:00
|
|
|
import mozilla.components.support.locale.LocaleAwareApplication
|
2019-07-17 22:09:47 +00:00
|
|
|
import mozilla.components.support.rusthttp.RustHttpConfig
|
2019-02-15 21:19:14 +00:00
|
|
|
import mozilla.components.support.rustlog.RustLog
|
2020-03-26 00:36:31 +00:00
|
|
|
import mozilla.components.support.utils.logElapsedTime
|
2020-02-04 06:41:52 +00:00
|
|
|
import mozilla.components.support.webextensions.WebExtensionSupport
|
2021-03-04 16:22:56 +00:00
|
|
|
import org.mozilla.fenix.GleanMetrics.GleanBuildInfo
|
2021-03-09 11:13:14 +00:00
|
|
|
import org.mozilla.fenix.GleanMetrics.Metrics
|
2021-02-11 23:19:26 +00:00
|
|
|
import org.mozilla.fenix.GleanMetrics.PerfStartup
|
2019-01-23 21:39:53 +00:00
|
|
|
import org.mozilla.fenix.components.Components
|
2020-02-08 05:53:37 +00:00
|
|
|
import org.mozilla.fenix.components.metrics.MetricServiceType
|
2021-03-04 19:28:35 +00:00
|
|
|
import org.mozilla.fenix.components.metrics.SecurePrefsTelemetry
|
2021-03-17 17:35:50 +00:00
|
|
|
import org.mozilla.fenix.ext.measureNoInline
|
2019-09-10 17:11:58 +00:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2021-03-22 22:33:57 +00:00
|
|
|
import org.mozilla.fenix.perf.AppStartReasonProvider
|
2021-02-02 01:48:54 +00:00
|
|
|
import org.mozilla.fenix.perf.ProfilerMarkerFactProcessor
|
2020-04-07 21:41:57 +00:00
|
|
|
import org.mozilla.fenix.perf.StartupTimeline
|
2020-12-18 14:34:33 +00:00
|
|
|
import org.mozilla.fenix.perf.StorageStatsMetrics
|
2020-11-02 17:49:17 +00:00
|
|
|
import org.mozilla.fenix.perf.runBlockingIncrement
|
2020-02-28 22:09:06 +00:00
|
|
|
import org.mozilla.fenix.push.PushFxaIntegration
|
2020-03-09 19:38:00 +00:00
|
|
|
import org.mozilla.fenix.push.WebPushEngineIntegration
|
2020-01-23 08:24:48 +00:00
|
|
|
import org.mozilla.fenix.session.PerformanceActivityLifecycleCallbacks
|
2019-08-28 19:55:24 +00:00
|
|
|
import org.mozilla.fenix.session.VisibilityLifecycleCallback
|
2020-01-21 02:19:10 +00:00
|
|
|
import org.mozilla.fenix.utils.BrowsersCache
|
2020-12-18 14:34:33 +00:00
|
|
|
import java.util.concurrent.TimeUnit
|
2019-01-23 21:39:53 +00:00
|
|
|
|
2020-05-12 16:22:05 +00:00
|
|
|
/**
|
|
|
|
*The main application class for Fenix. Records data to measure initialization performance.
|
|
|
|
* Installs [CrashReporter], initializes [Glean] in fenix builds and setup Megazord in the main process.
|
|
|
|
*/
|
2020-06-03 18:18:44 +00:00
|
|
|
@Suppress("Registered", "TooManyFunctions", "LargeClass")
|
2020-07-28 23:26:18 +00:00
|
|
|
open class FenixApplication : LocaleAwareApplication(), Provider {
|
2020-04-07 21:41:57 +00:00
|
|
|
init {
|
|
|
|
recordOnInit() // DO NOT MOVE ANYTHING ABOVE HERE: the timing of this measurement is critical.
|
|
|
|
}
|
|
|
|
|
2020-01-15 23:31:51 +00:00
|
|
|
private val logger = Logger("FenixApplication")
|
2019-01-29 19:20:29 +00:00
|
|
|
|
2019-05-07 21:36:37 +00:00
|
|
|
open val components by lazy { Components(this) }
|
2019-01-29 16:42:10 +00:00
|
|
|
|
2019-08-28 19:55:24 +00:00
|
|
|
var visibilityLifecycleCallback: VisibilityLifecycleCallback? = null
|
|
|
|
private set
|
|
|
|
|
2019-01-29 16:42:10 +00:00
|
|
|
override fun onCreate() {
|
2021-03-17 17:35:50 +00:00
|
|
|
// We use start/stop instead of measure so we don't measure outside the main process.
|
|
|
|
val completeMethodDurationTimerId = PerfStartup.applicationOnCreate.start() // DO NOT MOVE ANYTHING ABOVE HERE.
|
|
|
|
val subsectionThroughGleanTimerId = PerfStartup.appOnCreateToGleanInit.start()
|
|
|
|
|
2019-01-29 16:42:10 +00:00
|
|
|
super.onCreate()
|
2019-05-29 19:44:03 +00:00
|
|
|
|
2019-11-22 13:18:50 +00:00
|
|
|
setupInAllProcesses()
|
2019-01-29 16:42:10 +00:00
|
|
|
|
2019-03-14 13:27:54 +00:00
|
|
|
if (!isMainProcess()) {
|
2019-02-26 16:24:14 +00:00
|
|
|
// If this is not the main process then do not continue with the initialization here. Everything that
|
|
|
|
// follows only needs to be done in our app's main process and should not be done in other processes like
|
|
|
|
// a GeckoView child process or the crash handling process. Most importantly we never want to end up in a
|
2019-07-09 16:29:44 +00:00
|
|
|
// situation where we create a GeckoRuntime from the Gecko child process.
|
2019-02-26 16:24:14 +00:00
|
|
|
return
|
2019-02-13 15:08:35 +00:00
|
|
|
}
|
2019-02-26 16:24:14 +00:00
|
|
|
|
2020-01-23 16:06:04 +00:00
|
|
|
if (Config.channel.isFenix) {
|
|
|
|
// We need to always initialize Glean and do it early here.
|
|
|
|
// Note that we are only initializing Glean here for "fenix" builds. "fennec" builds
|
|
|
|
// will initialize in MigratingFenixApplication because we first need to migrate the
|
|
|
|
// user's choice from Fennec.
|
|
|
|
initializeGlean()
|
2020-01-22 12:37:52 +00:00
|
|
|
}
|
2020-01-23 16:06:04 +00:00
|
|
|
|
2021-03-17 17:35:50 +00:00
|
|
|
PerfStartup.appOnCreateToGleanInit.stopAndAccumulate(subsectionThroughGleanTimerId)
|
|
|
|
|
2020-01-23 16:06:04 +00:00
|
|
|
setupInMainProcessOnly()
|
2021-02-11 23:19:26 +00:00
|
|
|
|
2021-03-17 17:35:50 +00:00
|
|
|
// DO NOT MOVE ANYTHING BELOW THIS stop CALL.
|
|
|
|
PerfStartup.applicationOnCreate.stopAndAccumulate(completeMethodDurationTimerId)
|
2020-01-23 16:06:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 18:18:52 +00:00
|
|
|
protected open fun initializeGlean() {
|
2020-01-23 16:06:04 +00:00
|
|
|
val telemetryEnabled = settings().isTelemetryEnabled
|
|
|
|
|
|
|
|
logger.debug("Initializing Glean (uploadEnabled=$telemetryEnabled, isFennec=${Config.channel.isFennec})")
|
|
|
|
|
2020-01-15 23:31:51 +00:00
|
|
|
Glean.initialize(
|
|
|
|
applicationContext = this,
|
|
|
|
configuration = Configuration(
|
|
|
|
channel = BuildConfig.BUILD_TYPE,
|
|
|
|
httpClient = ConceptFetchHttpUploader(
|
|
|
|
lazy(LazyThreadSafetyMode.NONE) { components.core.client }
|
|
|
|
)),
|
2021-03-04 16:22:56 +00:00
|
|
|
uploadEnabled = telemetryEnabled,
|
|
|
|
buildInfo = GleanBuildInfo.buildInfo
|
2020-01-15 23:31:51 +00:00
|
|
|
)
|
2021-03-09 11:13:14 +00:00
|
|
|
|
|
|
|
// Set this early to guarantee it's in every ping from here on.
|
|
|
|
Metrics.distributionId.set(
|
|
|
|
when (Config.channel.isMozillaOnline) {
|
|
|
|
true -> "MozillaOnline"
|
|
|
|
false -> "Mozilla"
|
|
|
|
}
|
|
|
|
)
|
2019-11-22 13:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@CallSuper
|
|
|
|
open fun setupInAllProcesses() {
|
|
|
|
setupCrashReporting()
|
|
|
|
|
|
|
|
// We want the log messages of all builds to go to Android logcat
|
2020-10-13 13:17:58 +00:00
|
|
|
Log.addSink(FenixLogSink(logsDebug = Config.channel.isDebug))
|
2019-11-22 13:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@CallSuper
|
|
|
|
open fun setupInMainProcessOnly() {
|
2021-03-17 17:35:50 +00:00
|
|
|
PerfStartup.appOnCreateToMegazordInit.measureNoInline {
|
|
|
|
ProfilerMarkerFactProcessor.create { components.core.engine.profiler }.register()
|
|
|
|
|
|
|
|
run {
|
|
|
|
// Attention: Do not invoke any code from a-s in this scope.
|
|
|
|
val megazordSetup = setupMegazord()
|
|
|
|
|
|
|
|
setDayNightTheme()
|
|
|
|
components.strictMode.enableStrictMode(true)
|
|
|
|
warmBrowsersCache()
|
|
|
|
|
|
|
|
// Make sure the engine is initialized and ready to use.
|
|
|
|
components.strictMode.resetAfter(StrictMode.allowThreadDiskReads()) {
|
|
|
|
components.core.engine.warmUp()
|
|
|
|
}
|
|
|
|
initializeWebExtensionSupport()
|
|
|
|
restoreBrowserState()
|
|
|
|
restoreDownloads()
|
2021-03-23 18:16:30 +00:00
|
|
|
restoreLocale()
|
2021-03-17 17:35:50 +00:00
|
|
|
|
|
|
|
// Just to make sure it is impossible for any application-services pieces
|
|
|
|
// to invoke parts of itself that require complete megazord initialization
|
|
|
|
// before that process completes, we wait here, if necessary.
|
|
|
|
if (!megazordSetup.isCompleted) {
|
|
|
|
runBlockingIncrement { megazordSetup.await() }
|
|
|
|
}
|
2019-11-23 20:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-01 14:20:45 +00:00
|
|
|
|
2021-03-17 17:35:50 +00:00
|
|
|
PerfStartup.appOnCreateToSetupInMain.measureNoInline {
|
|
|
|
setupLeakCanary()
|
|
|
|
startMetricsIfEnabled()
|
|
|
|
setupPush()
|
2019-08-28 19:55:24 +00:00
|
|
|
|
2021-03-17 17:35:50 +00:00
|
|
|
visibilityLifecycleCallback = VisibilityLifecycleCallback(getSystemService())
|
|
|
|
registerActivityLifecycleCallbacks(visibilityLifecycleCallback)
|
2019-08-28 19:55:24 +00:00
|
|
|
|
2021-03-17 17:35:50 +00:00
|
|
|
// Storage maintenance disabled, for now, as it was interfering with background migrations.
|
|
|
|
// See https://github.com/mozilla-mobile/fenix/issues/7227 for context.
|
|
|
|
// if ((System.currentTimeMillis() - settings().lastPlacesStorageMaintenance) > ONE_DAY_MILLIS) {
|
|
|
|
// runStorageMaintenance()
|
|
|
|
// }
|
2020-01-23 08:24:48 +00:00
|
|
|
|
2021-03-22 22:33:57 +00:00
|
|
|
components.appStartReasonProvider.registerInAppOnCreate(this)
|
2021-03-17 17:35:50 +00:00
|
|
|
initVisualCompletenessQueueAndQueueTasks()
|
2020-08-27 06:14:13 +00:00
|
|
|
|
2021-03-17 17:35:50 +00:00
|
|
|
components.appStartupTelemetry.onFenixApplicationOnCreate()
|
|
|
|
}
|
2020-07-21 20:36:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 14:34:33 +00:00
|
|
|
private fun restoreBrowserState() = GlobalScope.launch(Dispatchers.Main) {
|
|
|
|
val store = components.core.store
|
|
|
|
val sessionStorage = components.core.sessionStorage
|
|
|
|
|
2021-01-07 19:40:32 +00:00
|
|
|
components.useCases.tabsUseCases.restore(sessionStorage, settings().getTabTimeout())
|
2020-12-18 14:34:33 +00:00
|
|
|
|
|
|
|
// Now that we have restored our previous state (if there's one) let's setup auto saving the state while
|
|
|
|
// the app is used.
|
|
|
|
sessionStorage.autoSave(store)
|
|
|
|
.periodicallyInForeground(interval = 30, unit = TimeUnit.SECONDS)
|
|
|
|
.whenGoingToBackground()
|
|
|
|
.whenSessionsChange()
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:43:06 +00:00
|
|
|
private fun restoreDownloads() {
|
2020-09-02 03:04:31 +00:00
|
|
|
components.useCases.downloadUseCases.restoreDownloads()
|
2020-08-21 21:43:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 18:16:30 +00:00
|
|
|
private fun restoreLocale() {
|
|
|
|
components.useCases.localeUseCases.restore()
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:36:06 +00:00
|
|
|
private fun initVisualCompletenessQueueAndQueueTasks() {
|
2020-08-17 22:05:06 +00:00
|
|
|
val queue = components.performance.visualCompletenessQueue.queue
|
2020-07-21 20:41:16 +00:00
|
|
|
|
|
|
|
fun initQueue() {
|
2020-08-17 22:05:06 +00:00
|
|
|
registerActivityLifecycleCallbacks(PerformanceActivityLifecycleCallbacks(queue))
|
2020-07-21 20:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun queueInitStorageAndServices() {
|
2020-08-17 22:05:06 +00:00
|
|
|
components.performance.visualCompletenessQueue.queue.runIfReadyOrQueue {
|
2020-07-21 20:41:16 +00:00
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
|
|
|
logger.info("Running post-visual completeness tasks...")
|
|
|
|
logElapsedTime(logger, "Storage initialization") {
|
|
|
|
components.core.historyStorage.warmUp()
|
|
|
|
components.core.bookmarksStorage.warmUp()
|
|
|
|
components.core.passwordsStorage.warmUp()
|
|
|
|
}
|
2021-03-04 19:28:35 +00:00
|
|
|
|
|
|
|
SecurePrefsTelemetry(this@FenixApplication, components.analytics.experiments).startTests()
|
2020-03-26 00:36:31 +00:00
|
|
|
}
|
2020-07-21 20:41:16 +00:00
|
|
|
// Account manager initialization needs to happen on the main thread.
|
|
|
|
GlobalScope.launch(Dispatchers.Main) {
|
|
|
|
logElapsedTime(logger, "Kicking-off account manager") {
|
|
|
|
components.backgroundServices.accountManager
|
|
|
|
}
|
2020-03-27 06:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-19 19:24:23 +00:00
|
|
|
}
|
2020-07-21 20:41:16 +00:00
|
|
|
|
2020-07-21 22:28:02 +00:00
|
|
|
fun queueMetrics() {
|
|
|
|
if (SDK_INT >= Build.VERSION_CODES.O) { // required by StorageStatsMetrics.
|
2020-08-17 22:05:06 +00:00
|
|
|
queue.runIfReadyOrQueue {
|
2020-07-21 22:28:02 +00:00
|
|
|
// Because it may be slow to capture the storage stats, it might be preferred to
|
|
|
|
// create a WorkManager task for this metric, however, I ran out of
|
|
|
|
// implementation time and WorkManager is harder to test.
|
|
|
|
StorageStatsMetrics.report(this.applicationContext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 20:31:31 +00:00
|
|
|
fun queueReviewPrompt() {
|
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
|
|
|
components.reviewPromptController.trackApplicationLaunch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:41:16 +00:00
|
|
|
initQueue()
|
|
|
|
|
|
|
|
// We init these items in the visual completeness queue to avoid them initing in the critical
|
|
|
|
// startup path, before the UI finishes drawing (i.e. visual completeness).
|
|
|
|
queueInitStorageAndServices()
|
2020-07-21 22:28:02 +00:00
|
|
|
queueMetrics()
|
2020-08-26 20:31:31 +00:00
|
|
|
queueReviewPrompt()
|
2019-09-24 17:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 20:33:46 +00:00
|
|
|
private fun startMetricsIfEnabled() {
|
|
|
|
if (settings().isTelemetryEnabled) {
|
|
|
|
components.analytics.metrics.start(MetricServiceType.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings().isMarketingTelemetryEnabled) {
|
|
|
|
components.analytics.metrics.start(MetricServiceType.Marketing)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 23:13:45 +00:00
|
|
|
// See https://github.com/mozilla-mobile/fenix/issues/7227 for context.
|
|
|
|
// To re-enable this, we need to do so in a way that won't interfere with any startup operations
|
|
|
|
// which acquire reserved+ sqlite lock. Currently, Fennec migrations need to write to storage
|
|
|
|
// on startup, and since they run in a background service we can't simply order these operations.
|
2019-09-24 17:50:11 +00:00
|
|
|
private fun runStorageMaintenance() {
|
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
|
|
|
// Bookmarks and history storage sit on top of the same db file so we only need to
|
|
|
|
// run maintenance on one - arbitrarily using bookmarks.
|
|
|
|
components.core.bookmarksStorage.runMaintenance()
|
|
|
|
}
|
2019-09-27 15:57:39 +00:00
|
|
|
settings().lastPlacesStorageMaintenance = System.currentTimeMillis()
|
2019-01-29 19:20:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 15:08:35 +00:00
|
|
|
protected open fun setupLeakCanary() {
|
|
|
|
// no-op, LeakCanary is disabled by default
|
|
|
|
}
|
|
|
|
|
2019-09-27 07:16:32 +00:00
|
|
|
open fun updateLeakCanaryState(isEnabled: Boolean) {
|
2019-02-13 15:08:35 +00:00
|
|
|
// no-op, LeakCanary is disabled by default
|
|
|
|
}
|
|
|
|
|
2019-08-27 20:12:29 +00:00
|
|
|
private fun setupPush() {
|
2019-08-27 21:15:38 +00:00
|
|
|
// Sets the PushFeature as the singleton instance for push messages to go to.
|
|
|
|
// We need the push feature setup here to deliver messages in the case where the service
|
|
|
|
// starts up the app first.
|
2020-02-28 22:09:06 +00:00
|
|
|
components.push.feature?.let {
|
2019-09-11 21:50:54 +00:00
|
|
|
Logger.info("AutoPushFeature is configured, initializing it...")
|
2019-08-27 21:15:38 +00:00
|
|
|
|
|
|
|
// Install the AutoPush singleton to receive messages.
|
2020-02-28 22:09:06 +00:00
|
|
|
PushProcessor.install(it)
|
|
|
|
|
2020-06-15 22:48:12 +00:00
|
|
|
WebPushEngineIntegration(components.core.engine, it).start()
|
2020-03-09 19:38:00 +00:00
|
|
|
|
2020-02-28 22:09:06 +00:00
|
|
|
// Perform a one-time initialization of the account manager if a message is received.
|
|
|
|
PushFxaIntegration(it, lazy { components.backgroundServices.accountManager }).launch()
|
2019-08-27 21:15:38 +00:00
|
|
|
|
|
|
|
// Initialize the service. This could potentially be done in a coroutine in the future.
|
2020-02-28 22:09:06 +00:00
|
|
|
it.initialize()
|
2019-08-27 21:15:38 +00:00
|
|
|
}
|
2019-08-27 20:12:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:42:10 +00:00
|
|
|
private fun setupCrashReporting() {
|
|
|
|
components
|
|
|
|
.analytics
|
|
|
|
.crashReporter
|
|
|
|
.install(this)
|
|
|
|
}
|
2019-02-15 21:19:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate Megazord sequence! Megazord Battle Mode!
|
|
|
|
*
|
2020-02-25 21:09:02 +00:00
|
|
|
* The application-services combined libraries are known as the "megazord". We use the default `full`
|
|
|
|
* megazord - it contains everything that fenix needs, and (currently) nothing more.
|
|
|
|
*
|
|
|
|
* Documentation on what megazords are, and why they're needed:
|
2019-07-17 22:09:47 +00:00
|
|
|
* - https://github.com/mozilla/application-services/blob/master/docs/design/megazords.md
|
|
|
|
* - https://mozilla.github.io/application-services/docs/applications/consuming-megazord-libraries.html
|
2019-02-15 21:19:14 +00:00
|
|
|
*/
|
2019-11-23 20:35:17 +00:00
|
|
|
private fun setupMegazord(): Deferred<Unit> {
|
|
|
|
// Note: Megazord.init() must be called as soon as possible ...
|
2019-07-17 22:09:47 +00:00
|
|
|
Megazord.init()
|
2019-11-23 20:35:17 +00:00
|
|
|
|
|
|
|
return GlobalScope.async(Dispatchers.IO) {
|
|
|
|
// ... but RustHttpConfig.setClient() and RustLog.enable() can be called later.
|
|
|
|
RustHttpConfig.setClient(lazy { components.core.client })
|
2020-06-05 01:09:42 +00:00
|
|
|
RustLog.enable(components.analytics.crashReporter)
|
2021-02-05 18:54:47 +00:00
|
|
|
// We want to ensure Nimbus is initialized as early as possible so we can
|
|
|
|
// experiment on features close to startup.
|
|
|
|
// But we need viaduct (the RustHttp client) to be ready before we do.
|
|
|
|
components.analytics.experiments.initialize()
|
2019-11-23 20:35:17 +00:00
|
|
|
}
|
2019-02-15 21:19:14 +00:00
|
|
|
}
|
2019-02-26 16:24:14 +00:00
|
|
|
|
2019-03-14 13:27:54 +00:00
|
|
|
override fun onTrimMemory(level: Int) {
|
|
|
|
super.onTrimMemory(level)
|
2020-03-05 13:05:27 +00:00
|
|
|
|
2020-11-30 10:38:42 +00:00
|
|
|
// Additional logging and breadcrumb to debug memory issues:
|
|
|
|
// https://github.com/mozilla-mobile/fenix/issues/12731
|
|
|
|
|
|
|
|
logger.info("onTrimMemory(), level=$level, main=${isMainProcess()}")
|
|
|
|
|
|
|
|
components.analytics.crashReporter.recordCrashBreadcrumb(Breadcrumb(
|
|
|
|
category = "Memory",
|
|
|
|
message = "onTrimMemory()",
|
|
|
|
data = mapOf(
|
|
|
|
"level" to level.toString(),
|
|
|
|
"main" to isMainProcess().toString()
|
|
|
|
),
|
|
|
|
level = Breadcrumb.Level.INFO
|
|
|
|
))
|
|
|
|
|
2019-03-14 13:27:54 +00:00
|
|
|
runOnlyInMainProcess {
|
2020-03-05 13:05:27 +00:00
|
|
|
components.core.icons.onTrimMemory(level)
|
2020-08-19 18:10:20 +00:00
|
|
|
components.core.store.dispatch(SystemAction.LowMemoryAction(level))
|
2019-02-26 16:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 00:53:37 +00:00
|
|
|
|
2019-04-04 14:44:03 +00:00
|
|
|
@SuppressLint("WrongConstant")
|
|
|
|
// Suppressing erroneous lint warning about using MODE_NIGHT_AUTO_BATTERY, a likely library bug
|
2019-04-02 00:53:37 +00:00
|
|
|
private fun setDayNightTheme() {
|
2019-09-24 17:33:46 +00:00
|
|
|
val settings = this.settings()
|
2019-04-02 00:53:37 +00:00
|
|
|
when {
|
2019-06-23 17:13:52 +00:00
|
|
|
settings.shouldUseLightTheme -> {
|
2019-04-02 00:53:37 +00:00
|
|
|
AppCompatDelegate.setDefaultNightMode(
|
|
|
|
AppCompatDelegate.MODE_NIGHT_NO
|
|
|
|
)
|
|
|
|
}
|
2019-06-23 17:13:52 +00:00
|
|
|
settings.shouldUseDarkTheme -> {
|
2019-04-02 00:53:37 +00:00
|
|
|
AppCompatDelegate.setDefaultNightMode(
|
|
|
|
AppCompatDelegate.MODE_NIGHT_YES
|
|
|
|
)
|
|
|
|
}
|
2019-06-23 17:13:52 +00:00
|
|
|
SDK_INT < Build.VERSION_CODES.P && settings.shouldUseAutoBatteryTheme -> {
|
2019-04-02 00:53:37 +00:00
|
|
|
AppCompatDelegate.setDefaultNightMode(
|
|
|
|
AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
|
|
|
|
)
|
|
|
|
}
|
2019-06-23 17:13:52 +00:00
|
|
|
SDK_INT >= Build.VERSION_CODES.P && settings.shouldFollowDeviceTheme -> {
|
2019-04-02 00:53:37 +00:00
|
|
|
AppCompatDelegate.setDefaultNightMode(
|
|
|
|
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// First run of app no default set, set the default to Follow System for 28+ and Normal Mode otherwise
|
|
|
|
else -> {
|
2019-06-23 17:13:52 +00:00
|
|
|
if (SDK_INT >= Build.VERSION_CODES.P) {
|
2019-04-02 00:53:37 +00:00
|
|
|
AppCompatDelegate.setDefaultNightMode(
|
|
|
|
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
|
|
|
)
|
2019-08-20 17:47:55 +00:00
|
|
|
settings.shouldFollowDeviceTheme = true
|
2019-04-02 00:53:37 +00:00
|
|
|
} else {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(
|
|
|
|
AppCompatDelegate.MODE_NIGHT_NO
|
|
|
|
)
|
2019-08-20 17:47:55 +00:00
|
|
|
settings.shouldUseLightTheme = true
|
2019-04-02 00:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 05:23:28 +00:00
|
|
|
|
2020-01-21 02:19:10 +00:00
|
|
|
private fun warmBrowsersCache() {
|
|
|
|
// We avoid blocking the main thread for BrowsersCache on startup by loading it on
|
|
|
|
// background thread.
|
|
|
|
GlobalScope.launch(Dispatchers.Default) {
|
|
|
|
BrowsersCache.all(this@FenixApplication)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 06:41:52 +00:00
|
|
|
private fun initializeWebExtensionSupport() {
|
|
|
|
try {
|
2020-02-25 17:31:05 +00:00
|
|
|
GlobalAddonDependencyProvider.initialize(
|
|
|
|
components.addonManager,
|
2020-02-27 14:42:13 +00:00
|
|
|
components.addonUpdater,
|
|
|
|
onCrash = { exception ->
|
|
|
|
components.analytics.crashReporter.submitCaughtException(exception)
|
|
|
|
}
|
2020-02-25 17:31:05 +00:00
|
|
|
)
|
2020-02-04 06:41:52 +00:00
|
|
|
WebExtensionSupport.initialize(
|
|
|
|
components.core.engine,
|
|
|
|
components.core.store,
|
|
|
|
onNewTabOverride = {
|
|
|
|
_, engineSession, url ->
|
2020-02-23 17:57:04 +00:00
|
|
|
val shouldCreatePrivateSession =
|
2021-01-14 18:21:44 +00:00
|
|
|
components.core.store.state.selectedTab?.content?.private
|
2020-07-21 17:47:10 +00:00
|
|
|
?: components.settings.openLinksInAPrivateTab
|
2020-02-23 17:57:04 +00:00
|
|
|
|
2021-02-04 21:31:06 +00:00
|
|
|
if (shouldCreatePrivateSession) {
|
|
|
|
components.useCases.tabsUseCases.addPrivateTab(
|
|
|
|
url = url,
|
|
|
|
selectTab = true,
|
|
|
|
engineSession = engineSession
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
components.useCases.tabsUseCases.addTab(
|
|
|
|
url = url,
|
|
|
|
selectTab = true,
|
|
|
|
engineSession = engineSession
|
|
|
|
)
|
|
|
|
}
|
2020-02-04 06:41:52 +00:00
|
|
|
},
|
|
|
|
onCloseTabOverride = {
|
2020-04-09 15:21:32 +00:00
|
|
|
_, sessionId -> components.useCases.tabsUseCases.removeTab(sessionId)
|
2020-02-04 06:41:52 +00:00
|
|
|
},
|
|
|
|
onSelectTabOverride = {
|
2021-01-18 19:41:44 +00:00
|
|
|
_, sessionId -> components.useCases.tabsUseCases.selectTab(sessionId)
|
2020-02-28 20:37:08 +00:00
|
|
|
},
|
|
|
|
onExtensionsLoaded = { extensions ->
|
|
|
|
components.addonUpdater.registerForFutureUpdates(extensions)
|
2021-03-12 13:21:34 +00:00
|
|
|
subscribeForNewAddonsIfNeeded(components.supportedAddonsChecker, extensions)
|
2020-03-02 18:46:37 +00:00
|
|
|
},
|
|
|
|
onUpdatePermissionRequest = components.addonUpdater::onUpdatePermissionRequest
|
2020-02-04 06:41:52 +00:00
|
|
|
)
|
|
|
|
} catch (e: UnsupportedOperationException) {
|
|
|
|
Logger.error("Failed to initialize web extension support", e)
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 21:41:57 +00:00
|
|
|
|
2021-03-12 13:21:34 +00:00
|
|
|
@VisibleForTesting
|
|
|
|
internal fun subscribeForNewAddonsIfNeeded(
|
|
|
|
checker: DefaultSupportedAddonsChecker,
|
|
|
|
installedExtensions: List<WebExtension>
|
|
|
|
) {
|
|
|
|
val hasUnsupportedAddons = installedExtensions.any { it.isUnsupported() }
|
|
|
|
if (hasUnsupportedAddons) {
|
|
|
|
checker.registerForChecks()
|
|
|
|
} else {
|
|
|
|
// As checks are a persistent subscriptions, we have to make sure
|
|
|
|
// we remove any previous subscriptions.
|
|
|
|
checker.unregisterForChecks()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 21:41:57 +00:00
|
|
|
protected fun recordOnInit() {
|
|
|
|
// This gets called by more than one process. Ideally we'd only run this in the main process
|
|
|
|
// but the code to check which process we're in crashes because the Context isn't valid yet.
|
2020-04-15 18:14:53 +00:00
|
|
|
//
|
|
|
|
// This method is not covered by our internal crash reporting: be very careful when modifying it.
|
|
|
|
StartupTimeline.onApplicationInit() // DO NOT MOVE ANYTHING ABOVE HERE: the timing is critical.
|
2020-04-07 21:41:57 +00:00
|
|
|
}
|
2020-04-21 04:43:53 +00:00
|
|
|
|
|
|
|
override fun onConfigurationChanged(config: android.content.res.Configuration) {
|
|
|
|
// Workaround for androidx appcompat issue where follow system day/night mode config changes
|
|
|
|
// are not triggered when also using createConfigurationContext like we do in LocaleManager
|
|
|
|
// https://issuetracker.google.com/issues/143570309#comment3
|
|
|
|
applicationContext.resources.configuration.uiMode = config.uiMode
|
2020-06-21 21:54:49 +00:00
|
|
|
|
2020-10-27 20:43:57 +00:00
|
|
|
if (isMainProcess()) {
|
|
|
|
// We can only do this on the main process as resetAfter will access components.core, which
|
|
|
|
// will initialize the engine and create an additional GeckoRuntime from the Gecko
|
|
|
|
// child process, causing a crash.
|
|
|
|
|
|
|
|
// There's a strict mode violation in A-Cs LocaleAwareApplication which
|
|
|
|
// reads from shared prefs: https://github.com/mozilla-mobile/android-components/issues/8816
|
|
|
|
components.strictMode.resetAfter(StrictMode.allowThreadDiskReads()) {
|
|
|
|
super.onConfigurationChanged(config)
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-21 21:54:49 +00:00
|
|
|
super.onConfigurationChanged(config)
|
|
|
|
}
|
2020-04-21 04:43:53 +00:00
|
|
|
}
|
2020-05-28 16:37:45 +00:00
|
|
|
|
2020-07-28 23:26:18 +00:00
|
|
|
override fun getWorkManagerConfiguration() = Builder().setMinimumLoggingLevel(INFO).build()
|
2019-02-26 16:24:14 +00:00
|
|
|
}
|