From 6ac2509131ab191594765be9e461f87688fdfb15 Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Mon, 26 Aug 2019 14:48:35 +0200 Subject: [PATCH] [fenix] Move creation of GeckoRuntime to flavor-specific source set. Since we are now able to build against GeckoView Nightly and GeckoView Beta, we should create the GeckoRuntime from a flavor-specific source set. Creating the runtime is not covered by the AC abstraction and so API changes in GeckoView Nightly can break the build and leaves us with no option to fix it from a shared code base. Separating the creation of GeckoRuntime allows us to adapt individually and also to configure the runtimes differently. --- .../org/mozilla/fenix/engine/GeckoProvider.kt | 46 +++++++++++++++++++ .../org/mozilla/fenix/engine/GeckoProvider.kt | 46 +++++++++++++++++++ .../browser/BrowserPerformanceTestActivity.kt | 4 +- .../java/org/mozilla/fenix/components/Core.kt | 33 ++----------- .../org/mozilla/fenix/components/TestCore.kt | 2 - 5 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 app/src/geckoBeta/java/org/mozilla/fenix/engine/GeckoProvider.kt create mode 100644 app/src/geckoNightly/java/org/mozilla/fenix/engine/GeckoProvider.kt diff --git a/app/src/geckoBeta/java/org/mozilla/fenix/engine/GeckoProvider.kt b/app/src/geckoBeta/java/org/mozilla/fenix/engine/GeckoProvider.kt new file mode 100644 index 0000000000..8df3d3669a --- /dev/null +++ b/app/src/geckoBeta/java/org/mozilla/fenix/engine/GeckoProvider.kt @@ -0,0 +1,46 @@ +/* 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/. */ + +import android.content.Context +import android.os.Bundle +import mozilla.components.lib.crash.handler.CrashHandlerService +import org.mozilla.fenix.utils.Settings +import org.mozilla.geckoview.GeckoRuntime +import org.mozilla.geckoview.GeckoRuntimeSettings + +object GeckoProvider { + var testConfig: Bundle? = null + private var runtime: GeckoRuntime? = null + + @Synchronized + fun getOrCreateRuntime(context: Context): GeckoRuntime { + if (runtime == null) { + runtime = createRuntime(context) + } + + return runtime!! + } + + private fun createRuntime(context: Context): GeckoRuntime { + val builder = GeckoRuntimeSettings.Builder() + + testConfig?.let { + builder.extras(it) + .remoteDebuggingEnabled(true) + } + + val runtimeSettings = builder + .crashHandler(CrashHandlerService::class.java) + .useContentProcessHint(true) + .build() + + if (!Settings.getInstance(context).shouldUseAutoSize) { + runtimeSettings.automaticFontSizeAdjustment = false + val fontSize = Settings.getInstance(context).fontSizeFactor + runtimeSettings.fontSizeFactor = fontSize + } + + return GeckoRuntime.create(context, runtimeSettings) + } +} diff --git a/app/src/geckoNightly/java/org/mozilla/fenix/engine/GeckoProvider.kt b/app/src/geckoNightly/java/org/mozilla/fenix/engine/GeckoProvider.kt new file mode 100644 index 0000000000..8df3d3669a --- /dev/null +++ b/app/src/geckoNightly/java/org/mozilla/fenix/engine/GeckoProvider.kt @@ -0,0 +1,46 @@ +/* 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/. */ + +import android.content.Context +import android.os.Bundle +import mozilla.components.lib.crash.handler.CrashHandlerService +import org.mozilla.fenix.utils.Settings +import org.mozilla.geckoview.GeckoRuntime +import org.mozilla.geckoview.GeckoRuntimeSettings + +object GeckoProvider { + var testConfig: Bundle? = null + private var runtime: GeckoRuntime? = null + + @Synchronized + fun getOrCreateRuntime(context: Context): GeckoRuntime { + if (runtime == null) { + runtime = createRuntime(context) + } + + return runtime!! + } + + private fun createRuntime(context: Context): GeckoRuntime { + val builder = GeckoRuntimeSettings.Builder() + + testConfig?.let { + builder.extras(it) + .remoteDebuggingEnabled(true) + } + + val runtimeSettings = builder + .crashHandler(CrashHandlerService::class.java) + .useContentProcessHint(true) + .build() + + if (!Settings.getInstance(context).shouldUseAutoSize) { + runtimeSettings.automaticFontSizeAdjustment = false + val fontSize = Settings.getInstance(context).fontSizeFactor + runtimeSettings.fontSizeFactor = fontSize + } + + return GeckoRuntime.create(context, runtimeSettings) + } +} diff --git a/app/src/main/java/org/mozilla/fenix/browser/BrowserPerformanceTestActivity.kt b/app/src/main/java/org/mozilla/fenix/browser/BrowserPerformanceTestActivity.kt index e0cc79c8c2..a497ba9712 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BrowserPerformanceTestActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BrowserPerformanceTestActivity.kt @@ -4,12 +4,12 @@ package org.mozilla.fenix.browser +import GeckoProvider import android.app.Activity import android.content.Intent import android.os.Bundle import mozilla.components.support.utils.SafeIntent import org.mozilla.fenix.IntentReceiverActivity -import org.mozilla.fenix.ext.components /** * This activity is used for performance testing with Raptor/tp6: @@ -20,7 +20,7 @@ class BrowserPerformanceTestActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - components.core.testConfig = SafeIntent(intent).extras + GeckoProvider.testConfig = SafeIntent(intent).extras val intent = Intent(intent) diff --git a/app/src/main/java/org/mozilla/fenix/components/Core.kt b/app/src/main/java/org/mozilla/fenix/components/Core.kt index ebb10b1eb5..94f6a9b382 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Core.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Core.kt @@ -4,9 +4,9 @@ package org.mozilla.fenix.components +import GeckoProvider import android.content.Context import android.content.res.Configuration -import android.os.Bundle import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -27,14 +27,11 @@ import mozilla.components.feature.media.MediaFeature import mozilla.components.feature.media.RecordingDevicesNotificationFeature import mozilla.components.feature.media.state.MediaStateMachine import mozilla.components.feature.session.HistoryDelegate -import mozilla.components.lib.crash.handler.CrashHandlerService import org.mozilla.fenix.AppRequestInterceptor import org.mozilla.fenix.FeatureFlags import org.mozilla.fenix.ext.components import org.mozilla.fenix.test.Mockable import org.mozilla.fenix.utils.Settings -import org.mozilla.geckoview.GeckoRuntime -import org.mozilla.geckoview.GeckoRuntimeSettings import java.util.concurrent.TimeUnit /** @@ -43,30 +40,6 @@ import java.util.concurrent.TimeUnit @Mockable class Core(private val context: Context) { - protected val runtime by lazy { - val builder = GeckoRuntimeSettings.Builder() - - testConfig?.let { - builder.extras(it) - .remoteDebuggingEnabled(true) - } - - val runtimeSettings = builder - .crashHandler(CrashHandlerService::class.java) - .useContentProcessHint(true) - .build() - - if (!Settings.getInstance(context).shouldUseAutoSize) { - runtimeSettings.automaticFontSizeAdjustment = false - val fontSize = Settings.getInstance(context).fontSizeFactor - runtimeSettings.fontSizeFactor = fontSize - } - - GeckoRuntime.create(context, runtimeSettings) - } - - var testConfig: Bundle? = null - /** * The browser engine component initialized based on the build * configuration (see build variants). @@ -83,14 +56,14 @@ class Core(private val context: Context) { suspendMediaWhenInactive = !FeatureFlags.mediaIntegration ) - GeckoEngine(context, defaultSettings, runtime) + GeckoEngine(context, defaultSettings, GeckoProvider.getOrCreateRuntime(context)) } /** * [Client] implementation to be used for code depending on `concept-fetch`` */ val client: Client by lazy { - GeckoViewFetchClient(context, runtime) + GeckoViewFetchClient(context, GeckoProvider.getOrCreateRuntime(context)) } val sessionStorage: SessionStorage by lazy { diff --git a/app/src/test/java/org/mozilla/fenix/components/TestCore.kt b/app/src/test/java/org/mozilla/fenix/components/TestCore.kt index 7af72f7bb6..f9af81f7c7 100644 --- a/app/src/test/java/org/mozilla/fenix/components/TestCore.kt +++ b/app/src/test/java/org/mozilla/fenix/components/TestCore.kt @@ -9,12 +9,10 @@ import io.mockk.mockk import kotlinx.coroutines.ObsoleteCoroutinesApi import mozilla.components.browser.engine.gecko.GeckoEngine import mozilla.components.browser.session.SessionManager -import org.mozilla.geckoview.GeckoRuntime @ObsoleteCoroutinesApi class TestCore(private val context: Context) : Core(context) { - override val runtime = mockk(relaxed = true) override val engine = mockk(relaxed = true) override val sessionManager = SessionManager(engine) }