Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #8567: Prevent crashes when trying to add to the system databases
Browse files Browse the repository at this point in the history
(cherry picked from commit c7eefe7)

# Conflicts:
#	docs/changelog.md
Amejia481 authored and mergify-bot committed Feb 23, 2022
1 parent fcf85d7 commit 3a3a5fd
Showing 3 changed files with 90 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -451,7 +451,6 @@ abstract class AbstractFetchDownloadService : Service() {
?: throw IllegalStateException("A fileName for a download is required")
val file = File(download.filePath)
// addCompletedDownload can't handle any non http(s) urls
val url = if (!download.isScheme(listOf("http", "https"))) null else download.url.toUri()
scope.launch {
addCompletedDownload(
title = fileName,
@@ -462,8 +461,7 @@ abstract class AbstractFetchDownloadService : Service() {
length = download.contentLength ?: file.length(),
// Only show notifications if our channel is blocked
showNotification = !DownloadNotification.isChannelEnabled(context),
uri = url,
referer = download.referrerUrl?.toUri()
download
)
}
}
@@ -479,21 +477,25 @@ abstract class AbstractFetchDownloadService : Service() {
path: String,
length: Long,
showNotification: Boolean,
uri: Uri?,
referer: Uri?
download: DownloadState
) {
context.addCompletedDownload(
title = title,
description = description,
isMediaScannerScannable = isMediaScannerScannable,
mimeType = mimeType,
path = path,
length = length,
// Only show notifications if our channel is blocked
showNotification = showNotification,
uri = uri,
referer = referer
)
try {
val url = if (!download.isScheme(listOf("http", "https"))) null else download.url.toUri()
context.addCompletedDownload(
title = title,
description = description,
isMediaScannerScannable = isMediaScannerScannable,
mimeType = mimeType,
path = path,
length = length,
// Only show notifications if our channel is blocked
showNotification = showNotification,
uri = url,
referer = download.referrerUrl?.toUri()
)
} catch (e: IllegalArgumentException) {
logger.error("Unable add the download to the system database", e)
}
}

@VisibleForTesting
Original file line number Diff line number Diff line change
@@ -68,6 +68,7 @@ import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@@ -1470,8 +1471,7 @@ class AbstractFetchDownloadServiceTest {
path = any(),
length = anyLong(),
showNotification = anyBoolean(),
uri = any(),
referer = any()
download = any()
)
}

@@ -1499,8 +1499,7 @@ class AbstractFetchDownloadServiceTest {
path = any(),
length = anyLong(),
showNotification = anyBoolean(),
uri = any(),
referer = any()
download = any()
)
doReturn(true).`when`(service).shouldUseScopedStorage()

@@ -1514,8 +1513,7 @@ class AbstractFetchDownloadServiceTest {
path = any(),
length = anyLong(),
showNotification = anyBoolean(),
uri = any(),
referer = any()
download = any()
)
}

@@ -1580,6 +1578,41 @@ class AbstractFetchDownloadServiceTest {
verify(downloadManager).addCompletedDownload(anyString(), anyString(), anyBoolean(), anyString(), anyString(), anyLong(), anyBoolean(), isNull(), any())
}

@Test
@Config(sdk = [Build.VERSION_CODES.P], shadows = [ShadowFileProvider::class])
@Suppress("Deprecation")
fun `GIVEN a download that throws an exception WHEN adding to the system database THEN handle the exception`() =
runBlockingTest {
val download = DownloadState(
url = "url",
fileName = "example.apk",
destinationDirectory = folder.root.path
)

val service = spy(object : AbstractFetchDownloadService() {
override val httpClient = client
override val store = browserStore
})

val spyContext = spy(testContext)
val downloadManager: DownloadManager = mock()

doReturn(spyContext).`when`(service).context
doReturn(downloadManager).`when`(spyContext).getSystemService<DownloadManager>()

doAnswer { throw IllegalArgumentException() }.`when`(downloadManager)
.addCompletedDownload(
anyString(), anyString(), anyBoolean(), anyString(),
anyString(), anyLong(), anyBoolean(), isNull(), any()
)

try {
service.addToDownloadSystemDatabaseCompat(download, this)
} catch (e: IOException) {
fail()
}
}

@Test
@Config(sdk = [Build.VERSION_CODES.P], shadows = [ShadowFileProvider::class])
@Suppress("Deprecation")
32 changes: 32 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -12,7 +12,39 @@ permalink: /changelog/
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/main/.config.yml)

* **support-utils**
<<<<<<< HEAD
* 🌟️️ **Add a `PendingUtils.defaultFlags` property to ease setting PendingIntent mutability as required for Android 31+.
=======
* 🌟️️ **Added new Browsers constant for Fennec `Browsers.FIREFOX_FENNEC_NIGHTLY`.
* ⚠️ **This is a breaking change**: `Browsers.FIREFOX_NIGHTLY` now points to `org.mozilla.fenix`, for fennec nightly use `Browsers.FIREFOX_FENNEC_NIGHTLY` [#11682](https://github.com/mozilla-mobile/android-components/pull/11682).
* **feature-downloads**:
* 🚒 Bug fixed [issue #8567](https://github.com/mozilla-mobile/android-components/issues/8567) - Prevent crashes when trying to add to the system databases.

* **concept-engine**
* 🌟️️ Add `EngineSessionStateStorage`, describing a storage of `EngineSessionState` instances.

* **browser-session-storage**
* 🌟️️ Add `FileEngineSessionStateStorage`, an implementation of `EngineSessionStateStorage` for persisting engine state outside of the regular RecoverableBrowserState flow.

* **browser-state**
* ⚠️ **This is a breaking change**: Shape of `RecoverableTab` changed. There's now a tab-state wrapper called `TabState`; use it when `engineSessionState` isn't necessary right away.

* **feature-recentlyclosed**
* 🌟️️ Add `RecentlyClosedTabsStorage`, which knows how to write/read recently closed tabs.

* **feature-tabs**
* ⚠️ **This is a breaking change**: `RestoreUseCase` implementation responsible for restoring `RecoverableTab` instances now takes a `TabState` and a `EngineSessionStateStorage` instead (and will read/rehydrate an EngineSessionState prior to restoring).

# 98.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v97.0.0...v98.0.0)
* [Milestone](https://github.com/mozilla-mobile/android-components/milestone/145?closed=1)
* [Dependencies](https://github.com/mozilla-mobile/android-components/v98.0.0/main/buildSrc/src/main/java/Dependencies.kt)
* [Gecko](https://github.com/mozilla-mobile/android-components/v98.0.0/main/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/v98.0.0/main/.config.yml)

* **support-utils**
* 🌟️️ **Add a `PendingUtils.defaultFlags`** property to ease setting PendingIntent mutability as required for Android 31+.
>>>>>>> c7eefe7d6c (Closes #8567: Prevent crashes when trying to add to the system databases)
* **feature-prompts**:
* More prompts are dismissable.

1 comment on commit 3a3a5fd

@firefoxci-taskcluster
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh! Looks like an error! Details

Failed to fetch task artifact public/github/customCheckRunText.md for GitHub integration.
Make sure the artifact exists on the worker or other location.

Please sign in to comment.