From b4d1ac0d638296fb3317b8b9f327fae10b38df18 Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Wed, 13 Feb 2019 14:19:20 +0100 Subject: [PATCH] Closes #472: Integrate feature-session-bundling component. This will take care of saving and restoring the (`SessionManager`) state. --- app/build.gradle | 5 +++- .../java/org/mozilla/fenix/components/Core.kt | 29 ++++++++++++++++++- buildSrc/src/main/java/Dependencies.kt | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 35ac3ea7c..203b12f4f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -133,9 +133,12 @@ dependencies { implementation Deps.mozilla_feature_session implementation Deps.mozilla_feature_toolbar implementation Deps.mozilla_feature_tabs + implementation Deps.mozilla_feature_findinpage + implementation Deps.mozilla_feature_session_bundling + implementation Deps.mozilla_service_fretboard implementation Deps.mozilla_service_glean - implementation Deps.mozilla_feature_findinpage + implementation Deps.mozilla_support_ktx implementation Deps.mozilla_ui_colors 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 cc9c18f42..5bd5109bf 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Core.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Core.kt @@ -7,6 +7,10 @@ package org.mozilla.fenix.components import android.content.Context import android.content.SharedPreferences import android.preference.PreferenceManager +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.async +import kotlinx.coroutines.launch import mozilla.components.browser.engine.gecko.GeckoEngine import mozilla.components.browser.session.SessionManager import mozilla.components.browser.session.storage.SessionStorage @@ -15,10 +19,12 @@ import mozilla.components.concept.engine.DefaultSettings import mozilla.components.concept.engine.Engine import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy import mozilla.components.feature.session.HistoryDelegate +import mozilla.components.feature.session.bundling.SessionBundleStorage import mozilla.components.lib.crash.handler.CrashHandlerService import org.mozilla.fenix.AppRequestInterceptor import org.mozilla.geckoview.GeckoRuntime import org.mozilla.geckoview.GeckoRuntimeSettings +import java.util.concurrent.TimeUnit /** * Component group for all core browser functionality. @@ -49,6 +55,10 @@ class Core(private val context: Context) { GeckoEngine(context, defaultSettings, runtime) } + val sessionStorage by lazy { + SessionBundleStorage(context, bundleLifetime = Pair(1, TimeUnit.HOURS)) + } + /** * The session manager component provides access to a centralized registry of * all browser sessions (i.e. tabs). It is initialized here to persist and restore @@ -56,7 +66,24 @@ class Core(private val context: Context) { * case all sessions/tabs are closed. */ val sessionManager by lazy { - SessionManager(engine) + SessionManager(engine).also { sessionManager -> + // Restore a previous, still active bundle. + GlobalScope.launch(Dispatchers.Main) { + val snapshot = async(Dispatchers.IO) { + sessionStorage.restore()?.restoreSnapshot(engine) + } + + // There's an active bundle with a snapshot: Feed it into the SessionManager. + snapshot.await()?.let { sessionManager.restore(it) } + + // 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(sessionManager) + .periodicallyInForeground(interval = 30, unit = TimeUnit.SECONDS) + .whenGoingToBackground() + .whenSessionsChange() + } + } } /** diff --git a/buildSrc/src/main/java/Dependencies.kt b/buildSrc/src/main/java/Dependencies.kt index fdcfc275a..2378c34e7 100644 --- a/buildSrc/src/main/java/Dependencies.kt +++ b/buildSrc/src/main/java/Dependencies.kt @@ -76,6 +76,7 @@ object Deps { const val mozilla_feature_prompts = "org.mozilla.components:feature-prompts:${Versions.mozilla_android_components}" const val mozilla_feature_toolbar = "org.mozilla.components:feature-toolbar:${Versions.mozilla_android_components}" const val mozilla_feature_findinpage = "org.mozilla.components:feature-findinpage:${Versions.mozilla_android_components}" + const val mozilla_feature_session_bundling = "org.mozilla.components:feature-session-bundling:${Versions.mozilla_android_components}" const val mozilla_service_fretboard = "org.mozilla.components:service-fretboard:${Versions.mozilla_android_components}" const val mozilla_service_glean = "org.mozilla.components:service-glean:${Versions.mozilla_android_components}"