| Index: chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java
|
| index d03d98fb8ab7aff8fb84a599da3c6782847c23bc..72a03a8c6e5fa99dfb1506c75fcd8db12e81c98f 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java
|
| @@ -18,6 +18,7 @@ import android.webkit.MimeTypeMap;
|
| import android.webkit.URLUtil;
|
| import android.widget.Toast;
|
|
|
| +import org.chromium.base.CalledByNative;
|
| import org.chromium.base.VisibleForTesting;
|
| import org.chromium.chrome.R;
|
| import org.chromium.chrome.browser.Tab;
|
| @@ -307,11 +308,90 @@ public class ChromeDownloadDelegate
|
| }
|
|
|
| /**
|
| + * Launch an info bar if the file name already exists for the download.
|
| + * @param info The information of the file we are about to download.
|
| + * @return Whether an info bar has been launched or not.
|
| + */
|
| + private boolean launchInfoBarIfFileExists(final DownloadInfo info) {
|
| + // Checks if file exists.
|
| + final String fileName = info.getFileName();
|
| + File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
| + if (!dir.mkdir() && !dir.isDirectory()) return false;
|
| + String dirName = dir.getName();
|
| + final File file = new File(dir, info.getFileName());
|
| + String fullDirPath = file.getParent();
|
| + if (!file.exists()) return false;
|
| + if (TextUtils.isEmpty(fileName) || TextUtils.isEmpty(dirName)
|
| + || TextUtils.isEmpty(fullDirPath)) {
|
| + return false;
|
| + }
|
| +
|
| + nativeLaunchDownloadOverwriteInfoBar(this, mTab, info.getFileName(), dirName, fullDirPath,
|
| + info.getUrl(), info.getUserAgent(), info.getContentDisposition(),
|
| + info.getMimeType(), info.getCookie(), info.getReferer(), info.hasUserGesture(),
|
| + info.getContentLength(), info.isGETRequest());
|
| + return true;
|
| + }
|
| +
|
| + /**
|
| * Sends the download request to Android download manager.
|
| *
|
| * @param info Download information about the download.
|
| */
|
| protected void enqueueDownloadManagerRequest(final DownloadInfo info) {
|
| + if (!launchInfoBarIfFileExists(info)) {
|
| + enqueueDownloadManagerRequestInternal(info);
|
| + }
|
| + }
|
| +
|
| + /**
|
| + * Enqueue download manager request, only from native side.
|
| + *
|
| + * @param overwrite Whether or not we will overwrite the file.
|
| + * @param fileName The file name.
|
| + * @param url The URL.
|
| + * @param userAgent The user agent.
|
| + * @param contentDisposition The content disposition.
|
| + * @param mimeType The mime type.
|
| + * @param cookie The cookie
|
| + * @param referer The referer.
|
| + * @param hasUserGesture Whether download was initiated by user gesture.
|
| + * @param contentLength The content length.
|
| + * @param isGETRequest Whether download is a GET request.
|
| + */
|
| + @CalledByNative
|
| + private void enqueueDownloadManagerRequestFromNative(boolean overwrite, String fileName,
|
| + String url, String userAgent, String contentDisposition, String mimeType, String cookie,
|
| + String referer, boolean hasUserGesture, long contentLength, boolean isGETRequest) {
|
| + DownloadInfo downloadInfo = new DownloadInfo.Builder()
|
| + .setUrl(url)
|
| + .setMimeType(mimeType)
|
| + .setDescription(url)
|
| + .setUserAgent(userAgent)
|
| + .setCookie(cookie)
|
| + .setContentDisposition(contentDisposition)
|
| + .setContentLength(contentLength)
|
| + .setReferer(referer)
|
| + .setHasUserGesture(hasUserGesture)
|
| + .setFileName(fileName)
|
| + .setIsGETRequest(isGETRequest)
|
| + .build();
|
| + // Android DownloadManager does not have an overwriting option.
|
| + // We remove the file here instead.
|
| + if (overwrite) deleteFileForOverwrite(downloadInfo);
|
| + enqueueDownloadManagerRequestInternal(downloadInfo);
|
| + }
|
| +
|
| + private void deleteFileForOverwrite(DownloadInfo info) {
|
| + File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
| + if (!dir.isDirectory()) return;
|
| + final File file = new File(dir, info.getFileName());
|
| + if (!file.delete()) {
|
| + Log.e(LOGTAG, "Failed to delete a file." + info.getFileName());
|
| + }
|
| + }
|
| +
|
| + private void enqueueDownloadManagerRequestInternal(final DownloadInfo info) {
|
| DownloadManagerService.getDownloadManagerService(
|
| mContext.getApplicationContext()).enqueueDownloadManagerRequest(info, true);
|
| closeBlankTab();
|
| @@ -495,4 +575,8 @@ public class ChromeDownloadDelegate
|
| private static native boolean nativeIsDownloadDangerous(String filename);
|
| private static native void nativeDangerousDownloadValidated(
|
| Object tab, int downloadId, boolean accept);
|
| + private static native void nativeLaunchDownloadOverwriteInfoBar(ChromeDownloadDelegate delegate,
|
| + Tab tab, String fileName, String dirName, String dirFullPath, String url,
|
| + String userAgent, String contentDisposition, String mimeType, String cookie,
|
| + String referer, boolean hasUserGesture, long contentLength, boolean isGETRequest);
|
| }
|
|
|