1
0
Fork 0

Post: Clean up 'push' initialization code

master
Grisha Kruglov 2019-09-11 14:50:54 -07:00 committed by Grisha Kruglov
parent d67fb3eed2
commit 8a9340fd9d
3 changed files with 20 additions and 26 deletions

View File

@ -191,16 +191,14 @@ open class FenixApplication : Application() {
// 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.
if (components.backgroundServices.pushConfig != null) {
Logger.info("Push configuration found; initializing autopush..")
val push = components.backgroundServices.push
components.backgroundServices.push?.let { autoPushFeature ->
Logger.info("AutoPushFeature is configured, initializing it...")
// Install the AutoPush singleton to receive messages.
PushProcessor.install(push)
PushProcessor.install(autoPushFeature)
// Initialize the service. This could potentially be done in a coroutine in the future.
push.initialize()
autoPushFeature.initialize()
}
}

View File

@ -9,9 +9,6 @@ import android.os.Build
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE
import androidx.lifecycle.ProcessLifecycleOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.browser.storage.sync.PlacesBookmarksStorage
import mozilla.components.browser.storage.sync.PlacesHistoryStorage
import mozilla.components.concept.push.Bus
@ -89,10 +86,9 @@ class BackgroundServices(
SyncConfig(setOf(SyncEngine.HISTORY, SyncEngine.BOOKMARKS), syncPeriodInMinutes = 240L) // four hours
}
val pushConfig by lazy { makePushConfig() }
private val pushService by lazy { FirebasePush() }
val push by lazy { makePush() }
val push by lazy { makePushConfig()?.let { makePush(it) } }
init {
// Make the "history" and "bookmark" stores accessible to workers spawned by the sync manager.
@ -115,16 +111,16 @@ class BackgroundServices(
context.components.analytics.metrics
)
private val pushAccountObserver = PushAccountObserver(push)
private val pushAccountObserver by lazy { push?.let { PushAccountObserver(it) } }
val accountManager = makeAccountManager(context, serverConfig, deviceConfig, syncConfig)
@VisibleForTesting(otherwise = PRIVATE)
fun makePush(): AutoPushFeature {
fun makePush(pushConfig: PushConfig): AutoPushFeature {
return AutoPushFeature(
context = context,
service = pushService,
config = pushConfig!!
config = pushConfig
)
}
@ -160,38 +156,38 @@ class BackgroundServices(
// This is a good example of an information leak at the API level.
// See https://github.com/mozilla-mobile/android-components/issues/3732
setOf("https://identity.mozilla.com/apps/oldsync")
).also {
).also { accountManager ->
// TODO this needs to change once we have a SyncManager
context.settings.fxaHasSyncedItems = syncConfig?.supportedEngines?.isNotEmpty() ?: false
it.registerForDeviceEvents(deviceEventObserver, ProcessLifecycleOwner.get(), false)
accountManager.registerForDeviceEvents(deviceEventObserver, ProcessLifecycleOwner.get(), false)
// Register a telemetry account observer to keep track of FxA auth metrics.
it.register(telemetryAccountObserver)
accountManager.register(telemetryAccountObserver)
// Enable push if we have the config.
if (pushConfig != null) {
// Enable push if it's configured.
push?.let { autoPushFeature ->
// Register the push account observer so we know how to update our push subscriptions.
it.register(pushAccountObserver)
accountManager.register(pushAccountObserver!!)
val logger = Logger("AutoPushFeature")
// Notify observers for Services' messages.
push.registerForPushMessages(
autoPushFeature.registerForPushMessages(
PushType.Services,
object : Bus.Observer<PushType, String> {
override fun onEvent(type: PushType, message: String) {
it.authenticatedAccount()?.deviceConstellation()
accountManager.authenticatedAccount()?.deviceConstellation()
?.processRawEventAsync(message)
}
})
// Notify observers for subscription changes.
push.registerForSubscriptions(object : PushSubscriptionObserver {
autoPushFeature.registerForSubscriptions(object : PushSubscriptionObserver {
override fun onSubscriptionAvailable(subscription: AutoPushSubscription) {
// Update for only the services subscription.
if (subscription.type == PushType.Services) {
logger.info("New push subscription received for FxA")
it.authenticatedAccount()?.deviceConstellation()
accountManager.authenticatedAccount()?.deviceConstellation()
?.setDevicePushSubscriptionAsync(
DevicePushSubscription(
endpoint = subscription.endpoint,
@ -203,7 +199,7 @@ class BackgroundServices(
}
})
}
CoroutineScope(Dispatchers.Main).launch { it.initAsync().await() }
accountManager.initAsync()
}
/**

View File

@ -42,7 +42,7 @@ class BackgroundServicesTest {
) = mockk<FxaAccountManager>(relaxed = true)
override fun makePushConfig() = mockk<PushConfig>(relaxed = true)
override fun makePush() = mockk<AutoPushFeature>(relaxed = true)
override fun makePush(pushConfig: PushConfig) = mockk<AutoPushFeature>(relaxed = true)
}
@Test