Android Download: Fix an issue that Chrome removes external renamed file.

When user downloads a file in Chrome and remove the file in other
application, we check if the file is externally removed and call
Android API DownloadManager.remove(id) to avoid a crash on Android.

However, if the user rename the file, currently Chrome still deletes it
via the id provided by Android DownloadManager.

In this CL, we only let Android remove the file if it's deleted in
Chrome.


TBR=xingliu@chromium.org

(cherry picked from commit 88276e189cc40e61a2f60fffd2b7a49b63af6695)

Bug: 774870
Change-Id: I5448a155c1e05e44ec3a812e93d3035d7061ecf7
Reviewed-on: https://chromium-review.googlesource.com/899770
Reviewed-by: Min Qin <qinmin@chromium.org>
Reviewed-by: David Trainor <dtrainor@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#534738}
Reviewed-on: https://chromium-review.googlesource.com/930037
Reviewed-by: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#544}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerDelegate.java
index 3aa4da0a..f623f65 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerDelegate.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerDelegate.java
@@ -132,10 +132,14 @@
     /**
      * Removes a download from Android DownloadManager.
      * @param downloadGuid The GUID of the download.
+     * @param externallyRemoved If download is externally removed in other application.
      */
-    void removeCompletedDownload(String downloadGuid) {
+    void removeCompletedDownload(String downloadGuid, boolean externallyRemoved) {
         long downloadId = removeDownloadIdMapping(downloadGuid);
-        if (downloadId != INVALID_SYSTEM_DOWNLOAD_ID) {
+
+        // Let Android DownloadManager to remove download only if the user removed the file in
+        // Chrome. If the user renamed or moved the file, Chrome should keep it intact.
+        if (downloadId != INVALID_SYSTEM_DOWNLOAD_ID && !externallyRemoved) {
             DownloadManager manager =
                     (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
             manager.remove(downloadId);
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerService.java b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerService.java
index f44d9d9..0ba7796 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerService.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerService.java
@@ -982,9 +982,11 @@
      * Removes a download from the list.
      * @param downloadGuid GUID of the download.
      * @param isOffTheRecord Whether the download is off the record.
+     * @param externallyRemoved If the file is externally removed by other applications.
      */
     @Override
-    public void removeDownload(final String downloadGuid, boolean isOffTheRecord) {
+    public void removeDownload(
+            final String downloadGuid, boolean isOffTheRecord, boolean externallyRemoved) {
         mHandler.post(() -> {
             nativeRemoveDownload(getNativeDownloadManagerService(), downloadGuid, isOffTheRecord);
             removeDownloadProgress(downloadGuid);
@@ -993,7 +995,7 @@
         new AsyncTask<Void, Void, Void>() {
             @Override
             public Void doInBackground(Void... params) {
-                mDownloadManagerDelegate.removeCompletedDownload(downloadGuid);
+                mDownloadManagerDelegate.removeCompletedDownload(downloadGuid, externallyRemoved);
                 return null;
             }
         }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/BackendProvider.java b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/BackendProvider.java
index 88a3dd5..e69265b1 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/BackendProvider.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/BackendProvider.java
@@ -33,7 +33,7 @@
         void checkForExternallyRemovedDownloads(boolean isOffTheRecord);
 
         /** See {@link DownloadManagerService#removeDownload}. */
-        void removeDownload(String guid, boolean isOffTheRecord);
+        void removeDownload(String guid, boolean isOffTheRecord, boolean externallyRemoved);
 
         /** See {@link DownloadManagerService#isDownloadOpenableInBrowser}. */
         boolean isDownloadOpenableInBrowser(boolean isOffTheRecord, String mimeType);
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java
index 0ec3d19..c20c892 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java
@@ -384,7 +384,8 @@
         @Override
         public boolean removePermanently() {
             // Tell the DownloadManager to remove the file from history.
-            mBackendProvider.getDownloadDelegate().removeDownload(getId(), isOffTheRecord());
+            mBackendProvider.getDownloadDelegate().removeDownload(
+                    getId(), isOffTheRecord(), hasBeenExternallyRemoved());
             mBackendProvider.getThumbnailProvider().removeThumbnailsFromDisk(getId());
             return true;
         }
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/download/ui/StubbedProvider.java b/chrome/android/javatests/src/org/chromium/chrome/browser/download/ui/StubbedProvider.java
index 0f9d559..354d6a4 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/download/ui/StubbedProvider.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/download/ui/StubbedProvider.java
@@ -80,7 +80,8 @@
         }
 
         @Override
-        public void removeDownload(final String guid, final boolean isOffTheRecord) {
+        public void removeDownload(
+                final String guid, final boolean isOffTheRecord, boolean externallyRemoved) {
             mHandler.post(new Runnable() {
                 @Override
                 public void run() {