Commit a83717dd authored by Sebastian Kaspari's avatar Sebastian Kaspari
Browse files

Run migration in background service.

In order to avoid a half done migration we are moving the migration to a background service (that is
running in the "foreground").

This is the Fenix part of:
https://github.com/mozilla-mobile/android-components/issues/4879
parent 5268db15
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
    <application
        android:name="org.mozilla.fenix.MigratingFenixApplication"
        tools:replace="android:name">
        <service android:name="org.mozilla.fenix.MigrationService" />
    </application>
</manifest>
+18 −15
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

package org.mozilla.fenix

import android.content.Context
import kotlinx.coroutines.runBlocking
import mozilla.components.support.migration.FennecMigrator

@@ -11,12 +12,25 @@ import mozilla.components.support.migration.FennecMigrator
 * An application class which knows how to migrate Fennec data.
 */
class MigratingFenixApplication : FenixApplication() {
    val migrator by lazy {
        FennecMigrator.Builder(this, this.components.analytics.crashReporter)
            .migrateOpenTabs(this.components.core.sessionManager)
            .migrateHistory(this.components.core.historyStorage)
            .migrateBookmarks(this.components.core.bookmarksStorage)
            .migrateLogins(
                this.components.core.passwordsStorage.store,
                this.components.core.passwordsEncryptionKey
            )
            .migrateFxa(this.components.backgroundServices.accountManager)
            .build()
    }

    override fun setupInMainProcessOnly() {
        migrateGeckoBlocking()

        super.setupInMainProcessOnly()

        migrateDataAsynchronously()
        migrator.startMigrationServiceIfNeeded(MigrationService::class.java)
    }

    private fun migrateGeckoBlocking() {
@@ -28,19 +42,8 @@ class MigratingFenixApplication : FenixApplication() {
            migrator.migrateAsync().await()
        }
    }

    private fun migrateDataAsynchronously() {
        val migrator = FennecMigrator.Builder(this, this.components.analytics.crashReporter)
            .migrateOpenTabs(this.components.core.sessionManager)
            .migrateHistory(this.components.core.historyStorage)
            .migrateBookmarks(this.components.core.bookmarksStorage)
            .migrateLogins(
                this.components.core.passwordsStorage.store,
                this.components.core.passwordsEncryptionKey
            )
            .migrateFxa(this.components.backgroundServices.accountManager)
            .build()

        migrator.migrateAsync()
}

fun Context.getMigratorFromApplication(): FennecMigrator {
    return (applicationContext as MigratingFenixApplication).migrator
}
+14 −0
Original line number Diff line number Diff line
/* 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 mozilla.components.support.migration.AbstractMigrationService

/**
 * Background service for running the migration from legacy Firefox for Android (Fennec).
 */
class MigrationService : AbstractMigrationService() {
    override val migrator by lazy { getMigratorFromApplication() }
}