Commit 59c50941 authored by MozLando's avatar MozLando
Browse files

Merge #7632

7632: Closes #7626: Add download log entries r=csadilek a=Amejia481

To help to debug this https://github.com/mozilla-mobile/fenix/issues/12297

 edge cases issue and other future bugs, we need to add some log entries to be able determine the source of an issues.
Co-authored-by: default avatarArturo Mejia <arturomejiamarmol@gmail.com>
parents e7b3c77a 839084c4
......@@ -62,6 +62,7 @@ import mozilla.components.feature.downloads.facts.emitNotificationPauseFact
import mozilla.components.feature.downloads.facts.emitNotificationCancelFact
import mozilla.components.feature.downloads.facts.emitNotificationTryAgainFact
import mozilla.components.feature.downloads.facts.emitNotificationOpenFact
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.ktx.kotlin.sanitizeURL
import mozilla.components.support.utils.DownloadUtils
import java.io.File
......@@ -92,6 +93,7 @@ abstract class AbstractFetchDownloadService : Service() {
internal val context: Context get() = this
@VisibleForTesting
internal var compatForegroundNotificationId: Int = COMPAT_DEFAULT_FOREGROUND_ID
private val logger = Logger("AbstractFetchDownloadService")
internal var downloadJobs = mutableMapOf<Long, DownloadJobState>()
......@@ -165,6 +167,7 @@ abstract class AbstractFetchDownloadService : Service() {
setDownloadJobStatus(currentDownloadJobState, PAUSED)
currentDownloadJobState.job?.cancel()
emitNotificationPauseFact()
logger.debug("ACTION_PAUSE for ${currentDownloadJobState.state.url}")
}
ACTION_RESUME -> {
......@@ -175,6 +178,7 @@ abstract class AbstractFetchDownloadService : Service() {
}
emitNotificationResumeFact()
logger.debug("ACTION_RESUME for ${currentDownloadJobState.state.url}")
}
ACTION_CANCEL -> {
......@@ -190,6 +194,7 @@ abstract class AbstractFetchDownloadService : Service() {
removeDownloadJob(currentDownloadJobState)
emitNotificationCancelFact()
logger.debug("ACTION_CANCEL for ${currentDownloadJobState.state.url}")
}
ACTION_TRY_AGAIN -> {
......@@ -202,9 +207,13 @@ abstract class AbstractFetchDownloadService : Service() {
}
emitNotificationTryAgainFact()
logger.debug("ACTION_TRY_AGAIN for ${currentDownloadJobState.state.url}")
}
ACTION_DISMISS -> removeDownloadJob(currentDownloadJobState)
ACTION_DISMISS -> {
removeDownloadJob(currentDownloadJobState)
logger.debug("ACTION_DISMISS for ${currentDownloadJobState.state.url}")
}
ACTION_OPEN -> {
if (!openFile(
......@@ -220,9 +229,11 @@ abstract class AbstractFetchDownloadService : Service() {
)
Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_SHORT).show()
logger.debug("ACTION_OPEN errorMessage for ${currentDownloadJobState.state.url} ")
}
emitNotificationOpenFact()
logger.debug("ACTION_OPEN for ${currentDownloadJobState.state.url}")
}
}
}
......@@ -356,9 +367,11 @@ abstract class AbstractFetchDownloadService : Service() {
@Suppress("TooGenericExceptionCaught")
internal fun startDownloadJob(currentDownloadJobState: DownloadJobState) {
logger.debug("Starting download for ${currentDownloadJobState.state.url} ")
try {
performDownload(currentDownloadJobState)
} catch (e: Exception) {
logger.error("Unable to complete download for ${currentDownloadJobState.state.url} marked as FAILED", e)
setDownloadJobStatus(currentDownloadJobState, FAILED)
}
}
......@@ -554,6 +567,7 @@ abstract class AbstractFetchDownloadService : Service() {
val request = Request(download.url.sanitizeURL(), headers = headers)
val response = httpClient.fetch(request)
logger.debug("Fetching download for ${currentDownloadJobState.state.url} ")
// If we are resuming a download and the response does not contain a CONTENT_RANGE
// we cannot be sure that the request will properly be handled
......@@ -562,6 +576,7 @@ abstract class AbstractFetchDownloadService : Service() {
// We experienced a problem trying to fetch the file, send a failure notification
currentDownloadJobState.currentBytesCopied = 0
setDownloadJobStatus(currentDownloadJobState, FAILED)
logger.debug("Unable to fetching Download for ${currentDownloadJobState.state.url} status FAILED")
return
}
......@@ -584,6 +599,7 @@ abstract class AbstractFetchDownloadService : Service() {
if (getDownloadJobStatus(download) == DownloadJobStatus.ACTIVE &&
download.currentBytesCopied < download.state.contentLength ?: 0) {
setDownloadJobStatus(download, FAILED)
logger.error("verifyDownload for ${download.state.url} FAILED")
} else if (getDownloadJobStatus(download) == DownloadJobStatus.ACTIVE) {
setDownloadJobStatus(download, COMPLETED)
/**
......@@ -595,11 +611,14 @@ abstract class AbstractFetchDownloadService : Service() {
val newState = download.state.copy(contentLength = download.currentBytesCopied)
updateDownloadState(newState)
}
logger.debug("verifyDownload for ${download.state.url} ${download.status}")
}
}
private fun copyInChunks(downloadJobState: DownloadJobState, inStream: InputStream, outStream: OutputStream) {
val data = ByteArray(CHUNK_SIZE)
logger.debug("starting copyInChunks ${downloadJobState.state.url}" +
" currentBytesCopied ${downloadJobState.currentBytesCopied}")
// To ensure that we copy all files (even ones that don't have fileSize, we must NOT check < fileSize
while (getDownloadJobStatus(downloadJobState) == DownloadJobStatus.ACTIVE) {
......@@ -612,6 +631,8 @@ abstract class AbstractFetchDownloadService : Service() {
outStream.write(data, 0, bytesRead)
}
logger.debug("Finishing copyInChunks ${downloadJobState.state.url} " +
"currentBytesCopied ${downloadJobState.currentBytesCopied}")
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment