diff --git a/components/browser/state/src/main/java/mozilla/components/browser/state/reducer/ContentStateReducer.kt b/components/browser/state/src/main/java/mozilla/components/browser/state/reducer/ContentStateReducer.kt index e3eff2d73bd..5f9a4b8779f 100644 --- a/components/browser/state/src/main/java/mozilla/components/browser/state/reducer/ContentStateReducer.kt +++ b/components/browser/state/src/main/java/mozilla/components/browser/state/reducer/ContentStateReducer.kt @@ -61,7 +61,7 @@ internal object ContentStateReducer { it.copy(thumbnail = action.thumbnail) } is ContentAction.UpdateDownloadAction -> updateContentState(state, action.sessionId) { - it.copy(download = action.download) + it.copy(download = action.download.copy(sessionId = action.sessionId)) } is ContentAction.ConsumeDownloadAction -> updateContentState(state, action.sessionId) { if (it.download != null && it.download.id == action.downloadId) { diff --git a/components/browser/state/src/main/java/mozilla/components/browser/state/state/content/DownloadState.kt b/components/browser/state/src/main/java/mozilla/components/browser/state/state/content/DownloadState.kt index 4f2331b43bf..b324963673d 100644 --- a/components/browser/state/src/main/java/mozilla/components/browser/state/state/content/DownloadState.kt +++ b/components/browser/state/src/main/java/mozilla/components/browser/state/state/content/DownloadState.kt @@ -22,6 +22,8 @@ import kotlin.random.Random * @property referrerUrl The site that linked to this download. * @property skipConfirmation Whether or not the confirmation dialog should be shown before the download begins. * @property id The unique identifier of this download. + * @property sessionId Identifier of the session that spawned the download. + * @ */ @Suppress("Deprecation") @Parcelize @@ -34,7 +36,8 @@ data class DownloadState( val destinationDirectory: String = Environment.DIRECTORY_DOWNLOADS, val referrerUrl: String? = null, val skipConfirmation: Boolean = false, - val id: Long = Random.nextLong() + val id: Long = Random.nextLong(), + val sessionId: String? = null ) : Parcelable { val filePath: String get() = Environment.getExternalStoragePublicDirectory(destinationDirectory).path + "/" + fileName diff --git a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/DownloadsFeature.kt b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/DownloadsFeature.kt index e934bf534b7..9109837bbb2 100644 --- a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/DownloadsFeature.kt +++ b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/DownloadsFeature.kt @@ -93,9 +93,8 @@ class DownloadsFeature( flow.mapNotNull { state -> state.findTabOrCustomTabOrSelectedTab(tabId) } .ifChanged { it.content.download } .collect { state -> - val download = state.content.download - if (download != null) { - processDownload(state, download) + state.content.download?.let { downloadState -> + processDownload(state, downloadState) } } } diff --git a/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/DownloadsFeatureTest.kt b/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/DownloadsFeatureTest.kt index 9871c0c6999..9807dd459a7 100644 --- a/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/DownloadsFeatureTest.kt +++ b/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/DownloadsFeatureTest.kt @@ -115,8 +115,9 @@ class DownloadsFeatureTest { feature.start() verify(fragmentManager, never()).beginTransaction() + val download = DownloadState(url = "https://www.mozilla.org", sessionId = "test-tab") - store.dispatch(ContentAction.UpdateDownloadAction("test-tab", download = mock())) + store.dispatch(ContentAction.UpdateDownloadAction("test-tab", download)) .joinBlocking() testDispatcher.advanceUntilIdle() @@ -166,7 +167,7 @@ class DownloadsFeatureTest { verify(downloadManager, never()).download(any(), anyString()) - val download = DownloadState(url = "https://www.mozilla.org") + val download = DownloadState(url = "https://www.mozilla.org", sessionId = "test-tab") store.dispatch(ContentAction.UpdateDownloadAction("test-tab", download)) .joinBlocking() @@ -200,7 +201,8 @@ class DownloadsFeatureTest { val download = DownloadState( url = "https://www.mozilla.org", - skipConfirmation = true + skipConfirmation = true, + sessionId = "test-tab" ) store.dispatch(ContentAction.UpdateDownloadAction("test-tab", download)) @@ -218,7 +220,8 @@ class DownloadsFeatureTest { fun `When starting the feature will reattach to already existing dialog`() { grantPermissions() - store.dispatch(ContentAction.UpdateDownloadAction("test-tab", download = mock())) + val download = DownloadState(url = "https://www.mozilla.org", sessionId = "test-tab") + store.dispatch(ContentAction.UpdateDownloadAction("test-tab", download)) .joinBlocking() val dialogFragment: DownloadDialogFragment = mock() @@ -364,7 +367,7 @@ class DownloadsFeatureTest { verify(downloadManager, never()).download(any(), anyString()) verify(feature, never()).showDownloadNotSupportedError() - val download = DownloadState(url = "https://www.mozilla.org") + val download = DownloadState(url = "https://www.mozilla.org", sessionId = "test-tab") store.dispatch(ContentAction.UpdateDownloadAction("test-tab", download)) .joinBlocking()