Commit e1946c92 authored by Tommy Steimel's avatar Tommy Steimel Committed by Commit Bot

Revert "Move DownloadManagerBridge to chrome/browser/download"

This reverts commit fdf71331.

Reason for revert: compile failure:https://ci.chromium.org/p/chrome/builders/ci/android_arm64-builder-perf/129178

Original change's description:
> Move DownloadManagerBridge to chrome/browser/download
> 
> Part of the modulization effort.
> 
> Bug: 1013759
> Change-Id: I3a250fb293925ebe3c98fc068003b32568112ff9
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1902072
> Reviewed-by: Shakti Sahu <shaktisahu@chromium.org>
> Reviewed-by: David Trainor <dtrainor@chromium.org>
> Commit-Queue: Min Qin <qinmin@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#715066}

TBR=dtrainor@chromium.org,qinmin@chromium.org,shaktisahu@chromium.org

Change-Id: I8751d6528d8b800a409657b3fdbd9a8c7897e0ae
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 1013759
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1915219Reviewed-by: default avatarTommy Steimel <steimel@chromium.org>
Commit-Queue: Tommy Steimel <steimel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715097}
parent d0fdd823
......@@ -2593,6 +2593,7 @@ generate_jni("chrome_jni_headers") {
"java/src/org/chromium/chrome/browser/download/DownloadController.java",
"java/src/org/chromium/chrome/browser/download/DownloadItem.java",
"java/src/org/chromium/chrome/browser/download/DownloadLocationDialogBridge.java",
"java/src/org/chromium/chrome/browser/download/DownloadManagerBridge.java",
"java/src/org/chromium/chrome/browser/download/DownloadManagerService.java",
"java/src/org/chromium/chrome/browser/download/DownloadMediaData.java",
"java/src/org/chromium/chrome/browser/download/DownloadMediaParserBridge.java",
......
......@@ -487,6 +487,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/download/DownloadItem.java",
"java/src/org/chromium/chrome/browser/download/DownloadLocationCustomView.java",
"java/src/org/chromium/chrome/browser/download/DownloadLocationDialogBridge.java",
"java/src/org/chromium/chrome/browser/download/DownloadManagerBridge.java",
"java/src/org/chromium/chrome/browser/download/DownloadManagerService.java",
"java/src/org/chromium/chrome/browser/download/DownloadMediaData.java",
"java/src/org/chromium/chrome/browser/download/DownloadMediaParserBridge.java",
......
......@@ -335,7 +335,7 @@ public class DownloadBroadcastManager extends Service {
intent, DownloadNotificationService.EXTRA_DOWNLOAD_FILE_PATH);
if (ContentUriUtils.isContentUri(downloadFilePath)) {
// On Q+, content URI is being used and there is no download ID.
openDownloadWithId(context, intent, DownloadConstants.INVALID_DOWNLOAD_ID, contentId);
openDownloadWithId(context, intent, DownloadItem.INVALID_DOWNLOAD_ID, contentId);
} else {
long[] ids =
intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
......@@ -360,8 +360,7 @@ public class DownloadBroadcastManager extends Service {
* Called to open a particular download item with the given ID.
* @param context Context of the receiver.
* @param intent Intent from the notification.
* @param id ID from the Android DownloadManager, or DownloadConstants.INVALID_DOWNLOAD_ID on
* Q+.
* @param id ID from the Android DownloadManager, or DownloadItem.INVALID_DOWNLOAD_ID on Q+.
* @param contentId Content ID of the download.
*/
private void openDownloadWithId(Context context, Intent intent, long id, ContentId contentId) {
......
......@@ -391,7 +391,7 @@ public class DownloadInfoBarController implements OfflineContentProvider.Observe
return false;
}
if (MimeUtils.canAutoOpenMimeType(offlineItem.mimeType)) {
if (DownloadUtils.shouldAutoOpenDownload(offlineItem.mimeType, true)) {
return false;
}
......
......@@ -21,10 +21,12 @@ import org.chromium.components.offline_items_collection.OfflineItemState;
* DownloadManager must be queried for the correct status.
*/
public class DownloadItem {
static final long INVALID_DOWNLOAD_ID = -1L;
private final ContentId mContentId = new ContentId();
private boolean mUseAndroidDownloadManager;
private DownloadInfo mDownloadInfo;
private long mDownloadId = DownloadConstants.INVALID_DOWNLOAD_ID;
private long mDownloadId = INVALID_DOWNLOAD_ID;
private long mStartTime;
private long mEndTime;
private boolean mHasBeenExternallyRemoved;
......
......@@ -13,6 +13,7 @@ import android.os.Build;
import android.os.Environment;
import android.support.v4.app.NotificationManagerCompat;
import android.text.TextUtils;
import android.util.Pair;
import org.chromium.base.Callback;
import org.chromium.base.ContentUriUtils;
......@@ -99,7 +100,7 @@ public class DownloadManagerBridge {
NotificationManagerCompat.from(getContext());
boolean useSystemNotification = !notificationManager.areNotificationsEnabled();
long downloadId = getDownloadIdForDownloadGuid(downloadGuid);
if (downloadId != DownloadConstants.INVALID_DOWNLOAD_ID) return downloadId;
if (downloadId != DownloadItem.INVALID_DOWNLOAD_ID) return downloadId;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Class<?> c = manager.getClass();
......@@ -108,7 +109,7 @@ public class DownloadManagerBridge {
String.class, long.class, boolean.class, Uri.class, Uri.class};
Method method = c.getMethod("addCompletedDownload", args);
// OriginalUri has to be null or non-empty http(s) scheme.
Uri originalUri = UriUtils.parseOriginalUrl(originalUrl);
Uri originalUri = DownloadUtils.parseOriginalUrl(originalUrl);
Uri refererUri = TextUtils.isEmpty(referer) ? null : Uri.parse(referer);
downloadId = (Long) method.invoke(manager, fileName, description, true, mimeType,
filePath, fileSizeBytes, useSystemNotification, originalUri, refererUri);
......@@ -188,10 +189,10 @@ public class DownloadManagerBridge {
(DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
Cursor c = manager.query(new DownloadManager.Query().setFilterById(downloadId));
if (c == null) {
result.downloadStatus = DownloadStatus.CANCELLED;
result.downloadStatus = DownloadManagerService.DownloadStatus.CANCELLED;
return result;
}
result.downloadStatus = DownloadStatus.IN_PROGRESS;
result.downloadStatus = DownloadManagerService.DownloadStatus.IN_PROGRESS;
if (c.moveToNext()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
result.downloadStatus = getDownloadStatus(status);
......@@ -204,7 +205,7 @@ public class DownloadManagerBridge {
result.bytesTotal =
c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
} else {
result.downloadStatus = DownloadStatus.CANCELLED;
result.downloadStatus = DownloadManagerService.DownloadStatus.CANCELLED;
}
c.close();
......@@ -276,7 +277,8 @@ public class DownloadManagerBridge {
/**
* This function is meant to be called as the last step of a download. It will add the download
* to the android's DownloadManager if the download is not a content URI.
* to the android's DownloadManager and determine if the download can be resolved to any
* activity so that it can be auto-opened.
*/
@CalledByNative
private static void addCompletedDownload(String fileName, String description,
......@@ -284,19 +286,25 @@ public class DownloadManagerBridge {
String referrer, String downloadGuid, long callbackId) {
final String mimeType =
MimeUtils.remapGenericMimeType(originalMimeType, originalUrl, fileName);
AsyncTask<Long> task = new AsyncTask<Long>() {
AsyncTask<Pair<Long, Boolean>> task = new AsyncTask<Pair<Long, Boolean>>() {
@Override
protected Long doInBackground() {
protected Pair<Long, Boolean> doInBackground() {
long downloadId = ContentUriUtils.isContentUri(filePath)
? DownloadConstants.INVALID_DOWNLOAD_ID
? DownloadItem.INVALID_DOWNLOAD_ID
: addCompletedDownload(fileName, description, mimeType, filePath,
fileSizeBytes, originalUrl, referrer, downloadGuid);
return downloadId;
boolean success = ContentUriUtils.isContentUri(filePath)
|| downloadId != DownloadItem.INVALID_DOWNLOAD_ID;
boolean canResolve = success
&& DownloadManagerService.canResolveDownload(
filePath, mimeType, downloadId);
return Pair.create(downloadId, canResolve);
}
@Override
protected void onPostExecute(Long downloadId) {
DownloadManagerBridgeJni.get().onAddCompletedDownloadDone(callbackId, downloadId);
protected void onPostExecute(Pair<Long, Boolean> result) {
DownloadManagerBridgeJni.get().onAddCompletedDownloadDone(
callbackId, result.first, result.second);
}
};
try {
......@@ -305,18 +313,18 @@ public class DownloadManagerBridge {
// Reaching thread limit, update will be reschduled for the next run.
Log.e(TAG, "Thread limit reached, reschedule notification update later.");
DownloadManagerBridgeJni.get().onAddCompletedDownloadDone(
callbackId, DownloadConstants.INVALID_DOWNLOAD_ID);
callbackId, DownloadItem.INVALID_DOWNLOAD_ID, false);
}
}
private static int getDownloadStatus(int downloadManagerStatus) {
switch (downloadManagerStatus) {
case DownloadManager.STATUS_SUCCESSFUL:
return DownloadStatus.COMPLETE;
return DownloadManagerService.DownloadStatus.COMPLETE;
case DownloadManager.STATUS_FAILED:
return DownloadStatus.FAILED;
return DownloadManagerService.DownloadStatus.FAILED;
default:
return DownloadStatus.IN_PROGRESS;
return DownloadManagerService.DownloadStatus.IN_PROGRESS;
}
}
......@@ -451,6 +459,6 @@ public class DownloadManagerBridge {
@NativeMethods
interface Natives {
void onAddCompletedDownloadDone(long callbackId, long downloadId);
void onAddCompletedDownloadDone(long callbackId, long downloadId, boolean canResolve);
}
}
......@@ -19,6 +19,7 @@ import android.provider.MediaStore.MediaColumns;
import android.text.TextUtils;
import android.util.Pair;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
......@@ -67,6 +68,8 @@ import org.chromium.net.RegistrationPolicyAlwaysRegister;
import org.chromium.ui.widget.Toast;
import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
......@@ -86,6 +89,18 @@ import java.util.concurrent.RejectedExecutionException;
public class DownloadManagerService implements DownloadController.DownloadNotificationService,
NetworkChangeNotifierAutoDetect.Observer,
DownloadServiceDelegate, ProfileManager.Observer {
// Download status.
@IntDef({DownloadStatus.IN_PROGRESS, DownloadStatus.COMPLETE, DownloadStatus.FAILED,
DownloadStatus.CANCELLED, DownloadStatus.INTERRUPTED})
@Retention(RetentionPolicy.SOURCE)
public @interface DownloadStatus {
int IN_PROGRESS = 0;
int COMPLETE = 1;
int FAILED = 2;
int CANCELLED = 3;
int INTERRUPTED = 4;
}
private static final String TAG = "DownloadService";
private static final String DOWNLOAD_DIRECTORY = "Download";
private static final String DOWNLOAD_UMA_ENTRY = "DownloadUmaEntry";
......@@ -586,7 +601,7 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
info.getFileName(), info.getDescription(), info.getMimeType(),
info.getFilePath(), info.getBytesReceived(), info.getOriginalUrl(),
info.getReferrer(), info.getDownloadGuid());
success = systemDownloadId != DownloadConstants.INVALID_DOWNLOAD_ID;
success = systemDownloadId != DownloadItem.INVALID_DOWNLOAD_ID;
if (success) item.setSystemDownloadId(systemDownloadId);
}
boolean canResolve = success
......@@ -808,7 +823,8 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
downloadItem.setSystemDownloadId(response.downloadId);
if (!response.result) {
onDownloadFailed(downloadItem, response.failureReason);
recordDownloadCompletionStats(true, DownloadStatus.FAILED, 0, 0, 0, 0);
recordDownloadCompletionStats(
true, DownloadManagerService.DownloadStatus.FAILED, 0, 0, 0, 0);
return;
}
......@@ -822,7 +838,7 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
boolean isSupportedMimeType, String originalUrl, String referrer,
@Nullable String mimeType) {
assert !ThreadUtils.runningOnUiThread();
if (downloadId == DownloadConstants.INVALID_DOWNLOAD_ID) {
if (downloadId == DownloadItem.INVALID_DOWNLOAD_ID) {
if (!ContentUriUtils.isContentUri(filePath)) return null;
return getLaunchIntentFromDownloadUri(
filePath, isSupportedMimeType, originalUrl, referrer, mimeType);
......@@ -1233,8 +1249,9 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
public void onSuccessNotificationShown(
DownloadInfo info, boolean canResolve, int notificationId, long systemDownloadId) {
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.DOWNLOAD_OFFLINE_CONTENT_PROVIDER)) {
if (canResolve && MimeUtils.canAutoOpenMimeType(info.getMimeType())
&& info.hasUserGesture()) {
if (canResolve
&& DownloadUtils.shouldAutoOpenDownload(
info.getMimeType(), info.hasUserGesture())) {
DownloadItem item = new DownloadItem(false, info);
item.setSystemDownloadId(systemDownloadId);
handleAutoOpenAfterDownload(item);
......@@ -1372,8 +1389,9 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
@Override
protected void onPostExecute(Boolean canResolve) {
if (MimeUtils.canAutoOpenMimeType(result.mimeType)
&& item.getDownloadInfo().hasUserGesture() && canResolve) {
if (DownloadUtils.shouldAutoOpenDownload(
result.mimeType, item.getDownloadInfo().hasUserGesture())
&& canResolve) {
handleAutoOpenAfterDownload(item);
} else {
getInfoBarController(item.getDownloadInfo().isOffTheRecord())
......@@ -1803,8 +1821,9 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
boolean isSupportedMimeType = isSupportedMimeType(info.getMimeType());
boolean canResolve = MimeUtils.isOMADownloadDescription(info.getMimeType())
|| canResolveDownloadItem(downloadItem, isSupportedMimeType);
return canResolve && MimeUtils.canAutoOpenMimeType(info.getMimeType())
&& info.hasUserGesture();
return canResolve
&& DownloadUtils.shouldAutoOpenDownload(
info.getMimeType(), info.hasUserGesture());
}
@Override
protected void onPostExecute(Boolean result) {
......
......@@ -80,8 +80,11 @@ import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
......@@ -104,6 +107,13 @@ public class DownloadUtils {
private static final int[] BYTES_STRINGS = {
R.string.download_ui_kb, R.string.download_ui_mb, R.string.download_ui_gb};
// Set will be more expensive to initialize, so use an ArrayList here.
private static final List<String> MIME_TYPES_TO_OPEN = new ArrayList<String>(Arrays.asList(
MimeUtils.OMA_DOWNLOAD_DESCRIPTOR_MIME, "application/pdf", "application/x-x509-ca-cert",
"application/x-x509-user-cert", "application/x-x509-server-cert",
"application/x-pkcs12", "application/application/x-pem-file", "application/pkix-cert",
"application/x-wifi-config"));
private static final String TAG = "download";
private static final String DEFAULT_MIME_TYPE = "*/*";
......@@ -379,6 +389,21 @@ public class DownloadUtils {
return uri != null ? uri.toString() : new String();
}
/**
* Determines if the download should be immediately opened after
* downloading.
*
* @param mimeType The mime type of the download.
* @param hasUserGesture Whether the download is associated with an user gesture.
* @return true if the downloaded content should be opened, or false otherwise.
*/
@VisibleForTesting
@CalledByNative
public static boolean shouldAutoOpenDownload(String mimeType, boolean hasUserGesture) {
return hasUserGesture && MIME_TYPES_TO_OPEN.contains(mimeType);
}
/**
* Utility method to open an {@link OfflineItem}, which can be a chrome download, offline page.
* Falls back to open download home.
......@@ -989,6 +1014,25 @@ public class DownloadUtils {
return primaryPath == null ? false : path.contains(primaryPath);
}
/**
* Parses an originating URL string and returns a valid Uri that can be inserted into
* DownloadProvider. The returned Uri has to be null or non-empty http(s) scheme.
* @param originalUrl String representation of the originating URL.
* @return A valid Uri that can be accepted by DownloadProvider.
*/
public static Uri parseOriginalUrl(String originalUrl) {
Uri originalUri = TextUtils.isEmpty(originalUrl) ? null : Uri.parse(originalUrl);
if (originalUri != null) {
String scheme = originalUri.normalizeScheme().getScheme();
if (scheme == null
|| (!scheme.equals(UrlConstants.HTTPS_SCHEME)
&& !scheme.equals(UrlConstants.HTTP_SCHEME))) {
originalUri = null;
}
}
return originalUri;
}
/**
* @return The status of prompt for download pref, defined by {@link DownloadPromptStatus}.
*/
......
......@@ -290,7 +290,7 @@ public class OMADownloadHandler extends BroadcastReceiver {
OMAInfo omaInfo = null;
final DownloadManager manager =
(DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
boolean isContentUri = (mDownloadId == DownloadConstants.INVALID_DOWNLOAD_ID)
boolean isContentUri = (mDownloadId == DownloadItem.INVALID_DOWNLOAD_ID)
&& ContentUriUtils.isContentUri(mDownloadInfo.getFilePath());
try {
ParcelFileDescriptor fd = null;
......@@ -336,14 +336,14 @@ public class OMADownloadHandler extends BroadcastReceiver {
// Send notification if required attributes are missing.
if (omaInfo.getTypes().isEmpty() || getSize(omaInfo) <= 0
|| omaInfo.isValueEmpty(OMA_OBJECT_URI)) {
sendNotification(omaInfo, mDownloadInfo, DownloadConstants.INVALID_DOWNLOAD_ID,
sendNotification(omaInfo, mDownloadInfo, DownloadItem.INVALID_DOWNLOAD_ID,
DOWNLOAD_STATUS_INVALID_DESCRIPTOR);
return;
}
// Check version. Null version are treated as 1.0.
String version = omaInfo.getValue(OMA_DD_VERSION);
if (version != null && !version.startsWith("1.")) {
sendNotification(omaInfo, mDownloadInfo, DownloadConstants.INVALID_DOWNLOAD_ID,
sendNotification(omaInfo, mDownloadInfo, DownloadItem.INVALID_DOWNLOAD_ID,
DOWNLOAD_STATUS_INVALID_DDVERSION);
return;
}
......@@ -369,8 +369,8 @@ public class OMADownloadHandler extends BroadcastReceiver {
String action = intent.getAction();
if (!DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) return;
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, DownloadConstants.INVALID_DOWNLOAD_ID);
if (downloadId == DownloadConstants.INVALID_DOWNLOAD_ID) return;
DownloadManager.EXTRA_DOWNLOAD_ID, DownloadItem.INVALID_DOWNLOAD_ID);
if (downloadId == DownloadItem.INVALID_DOWNLOAD_ID) return;
boolean isPendingOMADownload = isPendingOMADownload(downloadId);
boolean isInOMASharedPrefs = isDownloadIdInOMASharedPrefs(downloadId);
if (isPendingOMADownload || isInOMASharedPrefs) {
......@@ -517,7 +517,8 @@ public class OMADownloadHandler extends BroadcastReceiver {
if (which == AlertDialog.BUTTON_POSITIVE) {
downloadOMAContent(downloadId, downloadInfo, omaInfo);
} else {
sendNotification(omaInfo, downloadInfo, DownloadConstants.INVALID_DOWNLOAD_ID,
sendNotification(omaInfo, downloadInfo,
DownloadItem.INVALID_DOWNLOAD_ID,
DOWNLOAD_STATUS_USER_CANCELLED);
}
};
......@@ -548,7 +549,7 @@ public class OMADownloadHandler extends BroadcastReceiver {
DialogInterface.OnClickListener clickListener = (dialog, which) -> {
if (which == AlertDialog.BUTTON_POSITIVE) {
sendInstallNotificationAndNextStep(omaInfo, downloadInfo,
DownloadConstants.INVALID_DOWNLOAD_ID, statusMessage);
DownloadItem.INVALID_DOWNLOAD_ID, statusMessage);
}
};
new AlertDialog
......@@ -852,11 +853,11 @@ public class OMADownloadHandler extends BroadcastReceiver {
@Override
protected void onPostExecute(Boolean canResolve) {
if (result.downloadStatus == DownloadStatus.COMPLETE) {
if (result.downloadStatus == DownloadManagerService.DownloadStatus.COMPLETE) {
onDownloadCompleted(item.getDownloadInfo(), downloadId, installNotifyURI);
removeOMADownloadFromSharedPrefs(downloadId);
showDownloadOnInfoBar(item, result.downloadStatus);
} else if (result.downloadStatus == DownloadStatus.FAILED) {
} else if (result.downloadStatus == DownloadManagerService.DownloadStatus.FAILED) {
onDownloadFailed(item.getDownloadInfo(), downloadId, result.failureReason,
installNotifyURI);
removeOMADownloadFromSharedPrefs(downloadId);
......@@ -874,9 +875,9 @@ public class OMADownloadHandler extends BroadcastReceiver {
if (infobarController == null) return;
OfflineItem offlineItem = DownloadItem.createOfflineItem(downloadItem);
offlineItem.id.namespace = LegacyHelpers.LEGACY_ANDROID_DOWNLOAD_NAMESPACE;
if (downloadStatus == DownloadStatus.COMPLETE) {
if (downloadStatus == DownloadManagerService.DownloadStatus.COMPLETE) {
offlineItem.state = OfflineItemState.COMPLETE;
} else if (downloadStatus == DownloadStatus.FAILED) {
} else if (downloadStatus == DownloadManagerService.DownloadStatus.FAILED) {
offlineItem.state = OfflineItemState.FAILED;
}
......@@ -1049,7 +1050,7 @@ public class OMADownloadHandler extends BroadcastReceiver {
protected void onPostExecute(Boolean success) {
if (success) {
showNextUrlDialog(mOMAInfo);
} else if (mDownloadId != DownloadConstants.INVALID_DOWNLOAD_ID) {
} else if (mDownloadId != DownloadItem.INVALID_DOWNLOAD_ID) {
// Remove the downloaded content.
((DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE))
.remove(mDownloadId);
......
......@@ -8,6 +8,7 @@ import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.test.filters.MediumTest;
import android.support.test.filters.SmallTest;
import android.util.Log;
import android.util.Pair;
......@@ -484,4 +485,23 @@ public class DownloadManagerServiceTest {
mService.onConnectionTypeChanged(ConnectionType.CONNECTION_2G);
Assert.assertEquals(resumableIdCount, mService.mAutoResumableDownloadIds.size());
}
/**
* Test to make sure {@link DownloadUtils#shouldAutoOpenDownload}
* returns the right result for varying MIME types and Content-Dispositions.
*/
@Test
@SmallTest
@Feature({"Download"})
public void testshouldAutoOpenDownload() {
// Should not open any download type MIME types.
Assert.assertFalse(DownloadUtils.shouldAutoOpenDownload("application/download", true));
Assert.assertFalse(DownloadUtils.shouldAutoOpenDownload("application/x-download", true));
Assert.assertFalse(DownloadUtils.shouldAutoOpenDownload("application/octet-stream", true));
Assert.assertTrue(DownloadUtils.shouldAutoOpenDownload("application/pdf", true));
Assert.assertFalse(DownloadUtils.shouldAutoOpenDownload("application/pdf", false));
Assert.assertFalse(DownloadUtils.shouldAutoOpenDownload("application/x-download", true));
Assert.assertFalse(DownloadUtils.shouldAutoOpenDownload("application/x-download", true));
Assert.assertFalse(DownloadUtils.shouldAutoOpenDownload("application/x-download", true));
}
}
......@@ -291,12 +291,12 @@ public class OMADownloadHandlerTest {
4, true);
DownloadQueryResultVerifier verifier =
new DownloadQueryResultVerifier(DownloadStatus.COMPLETE);
new DownloadQueryResultVerifier(DownloadManagerService.DownloadStatus.COMPLETE);
DownloadManagerBridge.queryDownloadResult(downloadId1, verifier);
waitForQueryCompletion(verifier);
manager.remove(downloadId1);
verifier = new DownloadQueryResultVerifier(DownloadStatus.CANCELLED);
verifier = new DownloadQueryResultVerifier(DownloadManagerService.DownloadStatus.CANCELLED);
DownloadManagerBridge.queryDownloadResult(downloadId1, verifier);
waitForQueryCompletion(verifier);
}
......
......@@ -7,17 +7,13 @@ import("//build/config/android/rules.gni")
android_library("java") {
java_files = [
"java/src/org/chromium/chrome/browser/download/DirectoryOption.java",
"java/src/org/chromium/chrome/browser/download/DownloadConstants.java",
"java/src/org/chromium/chrome/browser/download/DownloadFilter.java",
"java/src/org/chromium/chrome/browser/download/DownloadDirectoryProvider.java",
"java/src/org/chromium/chrome/browser/download/DownloadFileProvider.java",
"java/src/org/chromium/chrome/browser/download/DownloadFilter.java",
"java/src/org/chromium/chrome/browser/download/DownloadInfo.java",
"java/src/org/chromium/chrome/browser/download/DownloadManagerBridge.java",
"java/src/org/chromium/chrome/browser/download/DownloadStatus.java",
"java/src/org/chromium/chrome/browser/download/MediaStoreHelper.java",
"java/src/org/chromium/chrome/browser/download/DownloadStartupUtils.java",
"java/src/org/chromium/chrome/browser/download/MimeUtils.java",
"java/src/org/chromium/chrome/browser/download/UriUtils.java",
]
deps = [
......@@ -36,7 +32,6 @@ android_library("java") {
generate_jni("jni_headers") {
sources = [
"java/src/org/chromium/chrome/browser/download/DownloadInfo.java",
"java/src/org/chromium/chrome/browser/download/DownloadManagerBridge.java",
"java/src/org/chromium/chrome/browser/download/DownloadStartupUtils.java",
"java/src/org/chromium/chrome/browser/download/MimeUtils.java",
]
......
......@@ -10,7 +10,7 @@
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/feature_list.h"
#include "chrome/browser/download/android/jni_headers/DownloadManagerBridge_jni.h"
#include "chrome/android/chrome_jni_headers/DownloadManagerBridge_jni.h"
#include "components/download/public/common/download_features.h"
#include "url/gurl.h"
......@@ -22,13 +22,14 @@ using download::DownloadItem;
static void JNI_DownloadManagerBridge_OnAddCompletedDownloadDone(
JNIEnv* env,
jlong callback_id,
jlong download_id) {
jlong download_id,
jboolean can_resolve) {
DCHECK(callback_id);
// Convert java long long int to c++ pointer, take ownership.
std::unique_ptr<AddCompletedDownloadCallback> cb(
reinterpret_cast<AddCompletedDownloadCallback*>(callback_id));
std::move(*cb).Run(download_id);
std::move(*cb).Run(download_id, can_resolve);
}
void DownloadManagerBridge::AddCompletedDownload(
......
......@@ -9,7 +9,7 @@
#include "components/download/public/common/download_item.h"
using DownloadItem = download::DownloadItem;
using AddCompletedDownloadCallback = base::OnceCallback<void(int64_t)>;
using AddCompletedDownloadCallback = base::OnceCallback<void(int64_t, bool)>;
// This class pairs with DownloadManagerBridge on Java side, that handles all
// the android DownloadManager related functionalities. Both classes have only
......
......@@ -110,9 +110,9 @@ std::string DownloadUtils::RemapGenericMimeType(const std::string& mime_type,
// static
bool DownloadUtils::ShouldAutoOpenDownload(download::DownloadItem* item) {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_MimeUtils_canAutoOpenMimeType(
env, ConvertUTF8ToJavaString(env, item->GetMimeType())) &&
IsDownloadUserInitiated(item);
return Java_DownloadUtils_shouldAutoOpenDownload(
env, ConvertUTF8ToJavaString(env, item->GetMimeType()),
IsDownloadUserInitiated(item));
}
// static
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.download;
/**
* Constants that are used in download.
*/
public class DownloadConstants { static final long INVALID_DOWNLOAD_ID = -1L; }
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.download;
import androidx.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Status of a download item.
*/
@IntDef({DownloadStatus.IN_PROGRESS, DownloadStatus.COMPLETE, DownloadStatus.FAILED,
DownloadStatus.CANCELLED, DownloadStatus.INTERRUPTED})
@Retention(RetentionPolicy.SOURCE)
public @interface DownloadStatus {
int IN_PROGRESS = 0;
int COMPLETE = 1;
int FAILED = 2;
int CANCELLED = 3;
int INTERRUPTED = 4;
}
\ No newline at end of file
......@@ -9,10 +9,8 @@ import android.webkit.MimeTypeMap;
import org.chromium.base.annotations.CalledByNative;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* Utility class for MIME type related operations.
......@@ -31,14 +29,6 @@ public class MimeUtils {
private static final HashSet<String> GENERIC_MIME_TYPES = new HashSet<String>(Arrays.asList(
"text/plain", "application/octet-stream", "binary/octet-stream", "octet/stream",
"application/download", "application/force-download", "application/unknown"));
// Set will be more expensive to initialize, so use an ArrayList here.
private static final List<String> MIME_TYPES_TO_OPEN = new ArrayList<String>(Arrays.asList(
MimeUtils.OMA_DOWNLOAD_DESCRIPTOR_MIME, "application/pdf", "application/x-x509-ca-cert",
"application/x-x509-user-cert", "application/x-x509-server-cert",
"application/x-pkcs12", "application/application/x-pem-file", "application/pkix-cert",
"application/x-wifi-config"));
/**
* If the given MIME type is null, or one of the "generic" types (text/plain
* or application/octet-stream) map it to a type that Android can deal with.
......@@ -92,16 +82,4 @@ public class MimeUtils {
public static boolean isOMADownloadDescription(String mimeType) {
return OMA_DOWNLOAD_DESCRIPTOR_MIME.equalsIgnoreCase(mimeType);
}
/**
* Determines if the download should be immediately opened after
* downloading.
*
* @param mimeType The mime type of the download.
* @return true if the downloaded content should be opened, or false otherwise.
*/
@CalledByNative
public static boolean canAutoOpenMimeType(String mimeType) {
return MIME_TYPES_TO_OPEN.contains(mimeType);
}
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.download;
import android.net.Uri;
import android.text.TextUtils;
import org.chromium.chrome.browser.util.UrlConstants;
/**
* Utility class for Uri related operations.
*/
public class UriUtils {
/**
* Parses an originating URL string and returns a valid Uri that can be inserted into
* DownloadManager. The returned Uri has to be null or non-empty http(s) scheme.
* @param originalUrl String representation of the originating URL.
* @return A valid Uri that can be accepted by DownloadManager.
*/
public static Uri parseOriginalUrl(String originalUrl) {
Uri originalUri = TextUtils.isEmpty(originalUrl) ? null : Uri.parse(originalUrl);
if (originalUri != null) {
String scheme = originalUri.normalizeScheme().getScheme();
if (scheme == null
|| (!scheme.equals(UrlConstants.HTTPS_SCHEME)
&& !scheme.equals(UrlConstants.HTTP_SCHEME))) {
originalUri = null;
}
}
return originalUri;
}
}
......@@ -27,22 +27,4 @@ public class MimeUtilsTest {
Assert.assertEquals("txt", MimeUtils.getFileExtension("http://file.ext", "file.txt"));
Assert.assertEquals("txt", MimeUtils.getFileExtension("http://file.ext", "file name.txt"));
}
/**
* Test to make sure {@link DownloadUtils#shouldAutoOpenDownload}
* returns the right result for varying MIME types and Content-Dispositions.
*/
@Test
@SmallTest
@Feature({"Download"})
public void testCanAutoOpenMimeType() {
// Should not open any download type MIME types.
Assert.assertFalse(MimeUtils.canAutoOpenMimeType("application/download"));
Assert.assertFalse(MimeUtils.canAutoOpenMimeType("application/x-download"));
Assert.assertFalse(MimeUtils.canAutoOpenMimeType("application/octet-stream"));
Assert.assertTrue(MimeUtils.canAutoOpenMimeType("application/pdf"));
Assert.assertTrue(MimeUtils.canAutoOpenMimeType("application/x-x509-server-cert"));
Assert.assertTrue(MimeUtils.canAutoOpenMimeType("application/x-wifi-config"));
Assert.assertTrue(MimeUtils.canAutoOpenMimeType("application/pkix-cert"));
}
}
......@@ -492,15 +492,18 @@ void DownloadOfflineContentProvider::AddCompletedDownload(DownloadItem* item) {
DownloadManagerBridge::AddCompletedDownload(
item,
base::BindOnce(&DownloadOfflineContentProvider::AddCompletedDownloadDone,
weak_ptr_factory_.GetWeakPtr(), item->GetGuid()));
weak_ptr_factory_.GetWeakPtr(), item));
#endif
}
void DownloadOfflineContentProvider::AddCompletedDownloadDone(
const std::string& download_guid,
int64_t system_download_id) {
DownloadItem* item,
int64_t system_download_id,
bool can_resolve) {
#if defined(OS_ANDROID)
DownloadItem* item = GetDownload(download_guid);
if (!can_resolve)
return;
if (DownloadUtils::IsOmaDownloadDescription(item->GetMimeType())) {
DownloadManagerService::GetInstance()->HandleOMADownload(
item, system_download_id);
......
......@@ -116,8 +116,9 @@ class DownloadOfflineContentProvider
VisualsCallback callback,
const SkBitmap& bitmap);
void AddCompletedDownload(DownloadItem* item);
void AddCompletedDownloadDone(const std::string& download_guid,
int64_t system_download_id);
void AddCompletedDownloadDone(DownloadItem* item,
int64_t system_download_id,
bool can_resolve);
void OnRenameDownloadCallbackDone(RenameCallback callback,
DownloadItem* item,
DownloadItem::DownloadRenameResult result);
......
Markdown is supported
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