Skip to content

Commit

Permalink
closes mozilla-mobile#11894: sync devices on authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewTighe authored and mergify[bot] committed Mar 21, 2022
1 parent e44b1d9 commit a5f7201
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ internal class DefaultPresenter(
}

override fun onAuthenticated(account: OAuthAccount, authType: AuthType) {
CoroutineScope(Dispatchers.Main).launch { controller.refreshSyncedTabs() }
CoroutineScope(Dispatchers.Main).launch {
controller.syncAccount()
controller.refreshSyncedTabs()
}
}

override fun onAuthenticationProblems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@ import mozilla.components.browser.storage.sync.Tab
import mozilla.components.browser.storage.sync.TabEntry
import mozilla.components.concept.sync.Device
import mozilla.components.lib.state.ext.flowScoped
import mozilla.components.service.fxa.SyncEngine
import mozilla.components.service.fxa.manager.FxaAccountManager
import mozilla.components.service.fxa.manager.ext.withConstellation
import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged

/**
* A storage that listens to the [BrowserStore] changes to synchronize the local tabs state
* with [RemoteTabsStorage].
* with [RemoteTabsStorage] and then synchronize with [accountManager].
*
* @param accountManager Account manager used to retrieve synced tabs.
* @param store Browser store to observe for state changes.
* @param tabsStorage Storage layer for tabs to sync.
* @param debounceMillis Length to debounce rapid changes for storing.
* @param onStoreComplete Callback for action to take after tabs are stored.
* Note that this will be debounced by [debounceMillis].
* @param debounceMillis Length to debounce rapid changes for storing and syncing.
*/
class SyncedTabsStorage(
private val accountManager: FxaAccountManager,
private val store: BrowserStore,
private val tabsStorage: RemoteTabsStorage = RemoteTabsStorage(),
private val debounceMillis: Long = 1000L,
private val onStoreComplete: () -> Unit = {}
) : SyncedTabsProvider {
private var scope: CoroutineScope? = null

Expand All @@ -63,7 +62,10 @@ class SyncedTabsStorage(
.debounce(debounceMillis)
.collect { tabs ->
tabsStorage.store(tabs)
onStoreComplete()
accountManager.syncNow(
reason = SyncReason.User,
customEngineSubset = listOf(SyncEngine.Tabs)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class DefaultPresenterTest {
presenter.accountObserver.onAuthenticated(mock(), mock())
shadowOf(getMainLooper()).idle()

verify(controller).syncAccount()
verify(controller).refreshSyncedTabs()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import mozilla.components.concept.sync.Device
import mozilla.components.concept.sync.DeviceConstellation
import mozilla.components.concept.sync.DeviceType
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.service.fxa.SyncEngine
import mozilla.components.service.fxa.manager.FxaAccountManager
import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.support.test.any
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.mock
Expand Down Expand Up @@ -72,13 +74,11 @@ class SyncedTabsStorageTest {

@Test
fun `listens to browser store changes, stores state changes, and calls onStoreComplete`() = runBlockingTest {
val onStoreComplete: () -> Unit = mock()
val feature = SyncedTabsStorage(
accountManager,
store,
tabsStorage,
debounceMillis = 0,
onStoreComplete = onStoreComplete
)
feature.start()

Expand All @@ -92,7 +92,9 @@ class SyncedTabsStorageTest {
// Private tab is absent.
)
)
verify(onStoreComplete, times(2)).invoke()
verify(accountManager, times(2)).syncNow(
SyncReason.User, false, listOf(SyncEngine.Tabs)
)
}

@Test
Expand Down

0 comments on commit a5f7201

Please sign in to comment.