diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/Services.kt b/app/src/common/shared/org/mozilla/vrbrowser/browser/Services.kt index 2d459de5b0..a215b5f9df 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/Services.kt +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/Services.kt @@ -28,6 +28,8 @@ import org.mozilla.geckoview.AllowOrDeny import org.mozilla.geckoview.GeckoResult import org.mozilla.geckoview.GeckoSession import org.mozilla.vrbrowser.R +import org.mozilla.vrbrowser.browser.engine.EngineProvider +import org.mozilla.vrbrowser.browser.engine.GeckoViewFetchClient import org.mozilla.vrbrowser.browser.engine.SessionStore class Services(context: Context, places: Places): GeckoSession.NavigationDelegate { @@ -44,7 +46,7 @@ class Services(context: Context, places: Places): GeckoSession.NavigationDelegat // This makes bookmarks storage accessible to background sync workers. init { RustLog.enable() - RustHttpConfig.setClient(lazy { HttpURLConnectionClient() }) + RustHttpConfig.setClient(lazy { EngineProvider.createClient(context) }) // Make sure we get logs out of our android-components. Log.addSink(AndroidLogSink()) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/EngineProvider.kt b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/EngineProvider.kt new file mode 100644 index 0000000000..552099f68a --- /dev/null +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/EngineProvider.kt @@ -0,0 +1,68 @@ +/* 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.vrbrowser.browser.engine + +import android.content.Context +import mozilla.components.concept.fetch.Client +import org.mozilla.geckoview.ContentBlocking +import org.mozilla.geckoview.GeckoRuntime +import org.mozilla.geckoview.GeckoRuntimeSettings +import org.mozilla.geckoview.WebExtension +import org.mozilla.vrbrowser.BuildConfig +import org.mozilla.vrbrowser.browser.SettingsStore +import org.mozilla.vrbrowser.crashreporting.CrashReporterService + +object EngineProvider { + + private val WEB_EXTENSIONS = arrayOf("webcompat_vimeo", "webcompat_youtube") + + private var runtime: GeckoRuntime? = null + + @Synchronized + fun getOrCreateRuntime(context: Context): GeckoRuntime { + if (runtime == null) { + val builder = GeckoRuntimeSettings.Builder() + + builder.crashHandler(CrashReporterService::class.java) + builder.contentBlocking(ContentBlocking.Settings.Builder() + .antiTracking(ContentBlocking.AntiTracking.AD or ContentBlocking.AntiTracking.SOCIAL or ContentBlocking.AntiTracking.ANALYTIC) + .build()) + builder.consoleOutput(SettingsStore.getInstance(context).isConsoleLogsEnabled) + builder.displayDensityOverride(SettingsStore.getInstance(context).displayDensity) + builder.remoteDebuggingEnabled(SettingsStore.getInstance(context).isRemoteDebuggingEnabled) + builder.displayDpiOverride(SettingsStore.getInstance(context).displayDpi) + builder.screenSizeOverride(SettingsStore.getInstance(context).maxWindowWidth, + SettingsStore.getInstance(context).maxWindowHeight) + builder.autoplayDefault(if (SettingsStore.getInstance(context).isAutoplayEnabled) GeckoRuntimeSettings.AUTOPLAY_DEFAULT_ALLOWED else GeckoRuntimeSettings.AUTOPLAY_DEFAULT_BLOCKED) + + if (SettingsStore.getInstance(context).transparentBorderWidth > 0) { + builder.useMaxScreenDepth(true) + } + + if (BuildConfig.DEBUG) { + builder.arguments(arrayOf("-purgecaches")) + builder.debugLogging(true) + builder.aboutConfigEnabled(true) + } else { + builder.debugLogging(SettingsStore.getInstance(context).isDebugLogginEnabled) + } + + runtime = GeckoRuntime.create(context, builder.build()) + for (extension in WEB_EXTENSIONS) { + val path = "resource://android/assets/web_extensions/$extension/" + runtime!!.registerWebExtension(WebExtension(path)) + } + + + } + + return runtime!! + } + + fun createClient(context: Context): Client { + val runtime = getOrCreateRuntime(context) + return GeckoViewFetchClient(context, runtime) + } +} diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java index 4bcca26f99..e66c6aabea 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java @@ -7,30 +7,20 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import org.mozilla.geckoview.ContentBlocking; import org.mozilla.geckoview.GeckoRuntime; import org.mozilla.geckoview.GeckoRuntimeSettings; import org.mozilla.geckoview.GeckoSession; -import org.mozilla.geckoview.WebExtension; -import org.mozilla.vrbrowser.BuildConfig; import org.mozilla.vrbrowser.VRBrowserApplication; import org.mozilla.vrbrowser.browser.BookmarksStore; import org.mozilla.vrbrowser.browser.HistoryStore; import org.mozilla.vrbrowser.browser.PermissionDelegate; import org.mozilla.vrbrowser.browser.Services; -import org.mozilla.vrbrowser.browser.SettingsStore; -import org.mozilla.vrbrowser.crashreporting.CrashReporterService; import java.util.ArrayList; import java.util.List; public class SessionStore implements GeckoSession.PermissionDelegate { - private static final String[] WEB_EXTENSIONS = new String[] { - "webcompat_vimeo", - "webcompat_youtube" - }; - private static SessionStore mInstance; public static SessionStore get() { @@ -56,48 +46,10 @@ private SessionStore() { public void setContext(Context context, Bundle aExtras) { mContext = context; - if (mRuntime == null) { - // FIXME: Once GeckoView has a prefs API - SessionUtils.vrPrefsWorkAround(context, aExtras); - - GeckoRuntimeSettings.Builder runtimeSettingsBuilder = new GeckoRuntimeSettings.Builder(); - runtimeSettingsBuilder.crashHandler(CrashReporterService.class); - runtimeSettingsBuilder.contentBlocking((new ContentBlocking.Settings.Builder() - .antiTracking(ContentBlocking.AntiTracking.AD | ContentBlocking.AntiTracking.SOCIAL| ContentBlocking.AntiTracking.ANALYTIC)) - .build()); - runtimeSettingsBuilder.consoleOutput(SettingsStore.getInstance(context).isConsoleLogsEnabled()); - runtimeSettingsBuilder.displayDensityOverride(SettingsStore.getInstance(context).getDisplayDensity()); - runtimeSettingsBuilder.remoteDebuggingEnabled(SettingsStore.getInstance(context).isRemoteDebuggingEnabled()); - runtimeSettingsBuilder.displayDpiOverride(SettingsStore.getInstance(context).getDisplayDpi()); - runtimeSettingsBuilder.screenSizeOverride(SettingsStore.getInstance(context).getMaxWindowWidth(), - SettingsStore.getInstance(context).getMaxWindowHeight()); - runtimeSettingsBuilder.autoplayDefault(SettingsStore.getInstance(mContext).isAutoplayEnabled() ? GeckoRuntimeSettings.AUTOPLAY_DEFAULT_ALLOWED : GeckoRuntimeSettings.AUTOPLAY_DEFAULT_BLOCKED); - - if (SettingsStore.getInstance(context).getTransparentBorderWidth() > 0) { - runtimeSettingsBuilder.useMaxScreenDepth(true); - } - - if (BuildConfig.DEBUG) { - runtimeSettingsBuilder.arguments(new String[] { "-purgecaches" }); - runtimeSettingsBuilder.debugLogging(true); - runtimeSettingsBuilder.aboutConfigEnabled(true); - } else { - runtimeSettingsBuilder.debugLogging(SettingsStore.getInstance(context).isDebugLogginEnabled()); - } - - mRuntime = GeckoRuntime.create(context, runtimeSettingsBuilder.build()); - for (String extension: WEB_EXTENSIONS) { - String path = "resource://android/assets/web_extensions/" + extension + "/"; - mRuntime.registerWebExtension(new WebExtension(path)); - } - - } else { - mRuntime.attachTo(context); - } - } + mRuntime = EngineProvider.INSTANCE.getOrCreateRuntime(context); - public GeckoRuntime getRuntime() { - return mRuntime; + // FIXME: Once GeckoView has a prefs API + SessionUtils.vrPrefsWorkAround(context, aExtras); } public void initializeServices() {