2019-02-15 23:44:43 +01:00
|
|
|
/* 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.components
|
|
|
|
|
|
|
|
import android.content.Context
|
2019-05-23 22:13:27 +02:00
|
|
|
import androidx.lifecycle.ProcessLifecycleOwner
|
2019-03-13 19:23:00 +01:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
2019-04-26 23:20:13 +02:00
|
|
|
import mozilla.components.browser.storage.sync.PlacesBookmarksStorage
|
2019-03-13 19:23:00 +01:00
|
|
|
import mozilla.components.browser.storage.sync.PlacesHistoryStorage
|
2019-05-23 22:13:27 +02:00
|
|
|
import mozilla.components.concept.sync.DeviceCapability
|
|
|
|
import mozilla.components.concept.sync.DeviceEvent
|
|
|
|
import mozilla.components.concept.sync.DeviceEventsObserver
|
2019-05-08 18:04:06 +02:00
|
|
|
import mozilla.components.concept.sync.DeviceType
|
2019-03-13 19:23:00 +01:00
|
|
|
import mozilla.components.feature.sync.BackgroundSyncManager
|
|
|
|
import mozilla.components.feature.sync.GlobalSyncableStoreProvider
|
2019-02-15 23:44:43 +01:00
|
|
|
import mozilla.components.service.fxa.Config
|
2019-05-08 18:04:06 +02:00
|
|
|
import mozilla.components.service.fxa.manager.DeviceTuple
|
|
|
|
import mozilla.components.service.fxa.manager.FxaAccountManager
|
2019-05-29 00:27:44 +02:00
|
|
|
import mozilla.components.support.base.log.logger.Logger
|
2019-05-24 00:31:49 +02:00
|
|
|
import org.mozilla.fenix.R
|
2019-05-07 23:36:37 +02:00
|
|
|
import org.mozilla.fenix.test.Mockable
|
2019-02-15 23:44:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component group for background services. These are the components that need to be accessed from within a
|
|
|
|
* background worker.
|
|
|
|
*/
|
2019-05-07 23:36:37 +02:00
|
|
|
@Mockable
|
2019-02-15 23:44:43 +01:00
|
|
|
class BackgroundServices(
|
2019-03-13 19:23:00 +01:00
|
|
|
context: Context,
|
2019-04-26 23:20:13 +02:00
|
|
|
historyStorage: PlacesHistoryStorage,
|
2019-05-23 22:13:27 +02:00
|
|
|
bookmarkStorage: PlacesBookmarksStorage,
|
|
|
|
notificationManager: NotificationManager
|
2019-02-15 23:44:43 +01:00
|
|
|
) {
|
|
|
|
companion object {
|
|
|
|
const val CLIENT_ID = "a2270f727f45f648"
|
|
|
|
const val REDIRECT_URL = "https://accounts.firefox.com/oauth/success/$CLIENT_ID"
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is slightly messy - here we need to know the union of all "scopes"
|
|
|
|
// needed by components which rely on FxA integration. If this list
|
|
|
|
// grows too far we probably want to find a way to determine the set
|
|
|
|
// at runtime.
|
|
|
|
private val scopes: Array<String> = arrayOf("profile", "https://identity.mozilla.com/apps/oldsync")
|
|
|
|
private val config = Config.release(CLIENT_ID, REDIRECT_URL)
|
|
|
|
|
2019-03-13 19:23:00 +01:00
|
|
|
init {
|
2019-04-26 23:20:13 +02:00
|
|
|
// Make the "history" and "bookmark" stores accessible to workers spawned by the sync manager.
|
2019-03-13 19:23:00 +01:00
|
|
|
GlobalSyncableStoreProvider.configureStore("history" to historyStorage)
|
2019-04-26 23:20:13 +02:00
|
|
|
GlobalSyncableStoreProvider.configureStore("bookmarks" to bookmarkStorage)
|
2019-03-13 19:23:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
val syncManager = BackgroundSyncManager("https://identity.mozilla.com/apps/oldsync").also {
|
|
|
|
it.addStore("history")
|
2019-04-26 23:20:13 +02:00
|
|
|
it.addStore("bookmarks")
|
2019-03-13 19:23:00 +01:00
|
|
|
}
|
|
|
|
|
2019-05-23 22:13:27 +02:00
|
|
|
private val deviceEventObserver = object : DeviceEventsObserver {
|
2019-05-29 00:27:44 +02:00
|
|
|
private val logger = Logger("DeviceEventsObserver")
|
2019-05-23 22:13:27 +02:00
|
|
|
override fun onEvents(events: List<DeviceEvent>) {
|
2019-05-29 00:27:44 +02:00
|
|
|
logger.info("Received ${events.size} device event(s)")
|
2019-05-23 22:13:27 +02:00
|
|
|
events.filter { it is DeviceEvent.TabReceived }.forEach {
|
|
|
|
notificationManager.showReceivedTabs(it as DeviceEvent.TabReceived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 18:04:06 +02:00
|
|
|
val accountManager = FxaAccountManager(
|
|
|
|
context,
|
|
|
|
config,
|
|
|
|
scopes,
|
2019-05-24 00:31:49 +02:00
|
|
|
DeviceTuple(context.getString(R.string.app_name), DeviceType.MOBILE, listOf(DeviceCapability.SEND_TAB)),
|
2019-05-08 18:04:06 +02:00
|
|
|
syncManager
|
|
|
|
).also {
|
2019-05-23 22:13:27 +02:00
|
|
|
it.registerForDeviceEvents(deviceEventObserver, ProcessLifecycleOwner.get(), true)
|
2019-03-13 19:23:00 +01:00
|
|
|
CoroutineScope(Dispatchers.Main).launch { it.initAsync().await() }
|
|
|
|
}
|
2019-02-15 23:44:43 +01:00
|
|
|
}
|