| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.download; | 5 package org.chromium.chrome.browser.download; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.Intent; | 8 import android.content.Intent; |
| 9 import android.net.Uri; | 9 import android.net.Uri; |
| 10 import android.os.AsyncTask; | 10 import android.os.AsyncTask; |
| 11 import android.os.Environment; | 11 import android.os.Environment; |
| 12 import android.text.TextUtils; | 12 import android.text.TextUtils; |
| 13 import android.util.Log; | 13 import android.util.Log; |
| 14 import android.webkit.MimeTypeMap; | 14 import android.webkit.MimeTypeMap; |
| 15 import android.webkit.URLUtil; | 15 import android.webkit.URLUtil; |
| 16 import android.widget.Toast; | 16 import android.widget.Toast; |
| 17 | 17 |
| 18 import org.chromium.base.VisibleForTesting; | 18 import org.chromium.base.VisibleForTesting; |
| 19 import org.chromium.base.annotations.CalledByNative; | 19 import org.chromium.base.annotations.CalledByNative; |
| 20 import org.chromium.chrome.R; | 20 import org.chromium.chrome.R; |
| 21 import org.chromium.chrome.browser.infobar.ConfirmInfoBar; | 21 //import org.chromium.chrome.browser.infobar.ConfirmInfoBar; |
| 22 import org.chromium.chrome.browser.infobar.InfoBar; | 22 //import org.chromium.chrome.browser.infobar.InfoBar; |
| 23 import org.chromium.chrome.browser.infobar.InfoBarListeners; | 23 //import org.chromium.chrome.browser.infobar.InfoBarListeners; |
| 24 import org.chromium.chrome.browser.tab.Tab; | 24 //import org.chromium.chrome.browser.tab.Tab; |
| 25 import org.chromium.chrome.browser.tabmodel.TabModelSelector; | 25 //import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| 26 import org.chromium.content.browser.ContentViewDownloadDelegate; | 26 import org.chromium.content.browser.ContentViewDownloadDelegate; |
| 27 import org.chromium.content.browser.DownloadInfo; | 27 import org.chromium.content.browser.DownloadInfo; |
| 28 import org.chromium.content_public.browser.WebContents; | 28 import org.chromium.content_public.browser.WebContents; |
| 29 | 29 |
| 30 import java.io.File; | 30 import java.io.File; |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Chrome implementation of the ContentViewDownloadDelegate interface. | 33 * Chrome implementation of the ContentViewDownloadDelegate interface. |
| 34 * | 34 * |
| 35 * Listens to POST and GET download events. GET download requests are passed alo
ng to the | 35 * Listens to POST and GET download events. GET download requests are passed alo
ng to the |
| 36 * Android Download Manager. POST downloads are expected to be handled natively
and listener | 36 * Android Download Manager. POST downloads are expected to be handled natively
and listener |
| 37 * is responsible for adding the completed download to the download manager. | 37 * is responsible for adding the completed download to the download manager. |
| 38 * | 38 * |
| 39 * Prompts the user when a dangerous file is downloaded. Auto-opens PDFs after d
ownloading. | 39 * Prompts the user when a dangerous file is downloaded. Auto-opens PDFs after d
ownloading. |
| 40 */ | 40 */ |
| 41 public class ChromeDownloadDelegate | 41 public class ChromeDownloadDelegate |
| 42 implements ContentViewDownloadDelegate, InfoBarListeners.Confirm { | 42 implements ContentViewDownloadDelegate { |
| 43 // The application context. | 43 // The application context. |
| 44 private final Context mContext; | 44 private final Context mContext; |
| 45 private final Tab mTab; | 45 private final Tab mTab; |
| 46 private final TabModelSelector mTabModelSelector; | 46 private final TabModelSelector mTabModelSelector; |
| 47 private static final String LOGTAG = "ChromeDownloadDelegate"; | 47 private static final String LOGTAG = "ChromeDownloadDelegate"; |
| 48 | 48 |
| 49 // Pending download request for a dangerous file. | 49 // Pending download request for a dangerous file. |
| 50 private DownloadInfo mPendingRequest; | 50 private DownloadInfo mPendingRequest; |
| 51 | 51 |
| 52 @Override | 52 // @Override |
| 53 public void onConfirmInfoBarButtonClicked(ConfirmInfoBar infoBar, boolean co
nfirm) { | 53 // public void onConfirmInfoBarButtonClicked(ConfirmInfoBar infoBar, boolean
confirm) { |
| 54 if (mPendingRequest.hasDownloadId()) { | 54 // if (mPendingRequest.hasDownloadId()) { |
| 55 nativeDangerousDownloadValidated(mTab, mPendingRequest.getDownloadId
(), confirm); | 55 // nativeDangerousDownloadValidated(mTab, mPendingRequest.getDownload
Id(), confirm); |
| 56 if (confirm) { | 56 // if (confirm) { |
| 57 showDownloadStartNotification(); | 57 // showDownloadStartNotification(); |
| 58 } | 58 // } |
| 59 closeBlankTab(); | 59 // closeBlankTab(); |
| 60 } else if (confirm) { | 60 // } else if (confirm) { |
| 61 // User confirmed the download. | 61 // // User confirmed the download. |
| 62 if (mPendingRequest.isGETRequest()) { | 62 // if (mPendingRequest.isGETRequest()) { |
| 63 enqueueDownloadManagerRequest(mPendingRequest); | 63 // enqueueDownloadManagerRequest(mPendingRequest); |
| 64 } else { | 64 // } else { |
| 65 DownloadInfo newDownloadInfo = DownloadInfo.Builder.fromDownload
Info( | 65 // DownloadInfo newDownloadInfo = DownloadInfo.Builder.fromDownlo
adInfo( |
| 66 mPendingRequest).setIsSuccessful(true).build(); | 66 // mPendingRequest).setIsSuccessful(true).build(); |
| 67 DownloadManagerService.getDownloadManagerService(mContext).onDow
nloadCompleted( | 67 // DownloadManagerService.getDownloadManagerService(mContext).onD
ownloadCompleted( |
| 68 newDownloadInfo); | 68 // newDownloadInfo); |
| 69 } | 69 // } |
| 70 } else { | 70 // } else { |
| 71 // User did not accept the download, discard the file if it is a POS
T download. | 71 // // User did not accept the download, discard the file if it is a P
OST download. |
| 72 if (!mPendingRequest.isGETRequest()) { | 72 // if (!mPendingRequest.isGETRequest()) { |
| 73 discardFile(mPendingRequest.getFilePath()); | 73 // discardFile(mPendingRequest.getFilePath()); |
| 74 } | 74 // } |
| 75 } | 75 // } |
| 76 mPendingRequest = null; | 76 // mPendingRequest = null; |
| 77 infoBar.dismissJavaOnlyInfoBar(); | 77 // infoBar.dismissJavaOnlyInfoBar(); |
| 78 } | 78 // } |
| 79 | 79 |
| 80 @Override | 80 // @Override |
| 81 public void onInfoBarDismissed(InfoBar infoBar) { | 81 // public void onInfoBarDismissed(InfoBar infoBar) { |
| 82 if (mPendingRequest != null) { | 82 // if (mPendingRequest != null) { |
| 83 if (mPendingRequest.hasDownloadId()) { | 83 // if (mPendingRequest.hasDownloadId()) { |
| 84 nativeDangerousDownloadValidated(mTab, mPendingRequest.getDownlo
adId(), false); | 84 // nativeDangerousDownloadValidated(mTab, mPendingRequest.getDown
loadId(), false); |
| 85 } else if (!mPendingRequest.isGETRequest()) { | 85 // } else if (!mPendingRequest.isGETRequest()) { |
| 86 // Infobar was dismissed, discard the file if a POST download is
pending. | 86 // // Infobar was dismissed, discard the file if a POST download
is pending. |
| 87 discardFile(mPendingRequest.getFilePath()); | 87 // discardFile(mPendingRequest.getFilePath()); |
| 88 } | 88 // } |
| 89 } | 89 // } |
| 90 // Forget the pending request. | 90 // // Forget the pending request. |
| 91 mPendingRequest = null; | 91 // mPendingRequest = null; |
| 92 } | 92 // } |
| 93 | 93 |
| 94 /** | 94 /** |
| 95 * Creates ChromeDownloadDelegate. | 95 * Creates ChromeDownloadDelegate. |
| 96 * @param context The application context. | 96 * @param context The application context. |
| 97 * @param tabModelSelector The TabModelSelector responsible for {@code mTab}
. | 97 * @param tabModelSelector The TabModelSelector responsible for {@code mTab}
. |
| 98 * @param tab The corresponding tab instance. | 98 * @param tab The corresponding tab instance. |
| 99 */ | 99 */ |
| 100 public ChromeDownloadDelegate( | 100 // public ChromeDownloadDelegate( |
| 101 Context context, TabModelSelector tabModelSelector, Tab tab) { | 101 // Context context, TabModelSelector tabModelSelector, Tab tab) { |
| 102 mContext = context; | 102 // mContext = context; |
| 103 mTab = tab; | 103 // mTab = tab; |
| 104 mTabModelSelector = tabModelSelector; | 104 // mTabModelSelector = tabModelSelector; |
| 105 mPendingRequest = null; | 105 // mPendingRequest = null; |
| 106 } | 106 // } |
| 107 | 107 |
| 108 /** | 108 /** |
| 109 * Return the download path of a file. | 109 * Return the download path of a file. |
| 110 * @param fileName Name of the file. | 110 * @param fileName Name of the file. |
| 111 * @return path of the saved file. | 111 * @return path of the saved file. |
| 112 */ | 112 */ |
| 113 protected String downloadPath(String fileName) { | 113 protected String downloadPath(String fileName) { |
| 114 return mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "
/" + fileName; | 114 return mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "
/" + fileName; |
| 115 } | 115 } |
| 116 | 116 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 @Override | 261 @Override |
| 262 public void onDangerousDownload(String filename, int downloadId) { | 262 public void onDangerousDownload(String filename, int downloadId) { |
| 263 DownloadInfo downloadInfo = new DownloadInfo.Builder() | 263 DownloadInfo downloadInfo = new DownloadInfo.Builder() |
| 264 .setFileName(filename) | 264 .setFileName(filename) |
| 265 .setDescription(filename) | 265 .setDescription(filename) |
| 266 .setHasDownloadId(true) | 266 .setHasDownloadId(true) |
| 267 .setDownloadId(downloadId).build(); | 267 .setDownloadId(downloadId).build(); |
| 268 confirmDangerousDownload(downloadInfo); | 268 confirmDangerousDownload(downloadInfo); |
| 269 } | 269 } |
| 270 | 270 |
| 271 /** | 271 // /** |
| 272 * Launch an info bar if the file name already exists for the download. | 272 // * Launch an info bar if the file name already exists for the download. |
| 273 * @param info The information of the file we are about to download. | 273 // * @param info The information of the file we are about to download. |
| 274 * @return Whether an info bar has been launched or not. | 274 // * @return Whether an info bar has been launched or not. |
| 275 */ | 275 // */ |
| 276 private boolean launchInfoBarIfFileExists(final DownloadInfo info) { | 276 // private boolean launchInfoBarIfFileExists(final DownloadInfo info) { |
| 277 // Checks if file exists. | 277 // // Checks if file exists. |
| 278 final String fileName = info.getFileName(); | 278 // final String fileName = info.getFileName(); |
| 279 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIR
ECTORY_DOWNLOADS); | 279 // File dir = Environment.getExternalStoragePublicDirectory(Environment.D
IRECTORY_DOWNLOADS); |
| 280 if (!dir.mkdir() && !dir.isDirectory()) return false; | 280 // if (!dir.mkdir() && !dir.isDirectory()) return false; |
| 281 String dirName = dir.getName(); | 281 // String dirName = dir.getName(); |
| 282 final File file = new File(dir, info.getFileName()); | 282 // final File file = new File(dir, info.getFileName()); |
| 283 String fullDirPath = file.getParent(); | 283 // String fullDirPath = file.getParent(); |
| 284 if (!file.exists()) return false; | 284 // if (!file.exists()) return false; |
| 285 if (TextUtils.isEmpty(fileName) || TextUtils.isEmpty(dirName) | 285 // if (TextUtils.isEmpty(fileName) || TextUtils.isEmpty(dirName) |
| 286 || TextUtils.isEmpty(fullDirPath)) { | 286 // || TextUtils.isEmpty(fullDirPath)) { |
| 287 return false; | 287 // return false; |
| 288 } | 288 // } |
| 289 | 289 // |
| 290 nativeLaunchDownloadOverwriteInfoBar( | 290 // nativeLaunchDownloadOverwriteInfoBar( |
| 291 this, mTab, info, info.getFileName(), dirName, fullDirPath); | 291 // this, mTab, info, info.getFileName(), dirName, fullDirPath); |
| 292 return true; | 292 // return true; |
| 293 } | 293 // } |
| 294 | 294 |
| 295 /** | 295 /** |
| 296 * Sends the download request to Android download manager. | 296 * Sends the download request to Android download manager. |
| 297 * | 297 * |
| 298 * @param info Download information about the download. | 298 * @param info Download information about the download. |
| 299 */ | 299 */ |
| 300 protected void enqueueDownloadManagerRequest(final DownloadInfo info) { | 300 protected void enqueueDownloadManagerRequest(final DownloadInfo info) { |
| 301 if (!launchInfoBarIfFileExists(info)) { | 301 if (!launchInfoBarIfFileExists(info)) { |
| 302 enqueueDownloadManagerRequestInternal(info); | 302 enqueueDownloadManagerRequestInternal(info); |
| 303 } | 303 } |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 } | 512 } |
| 513 | 513 |
| 514 private static native String nativeGetDownloadWarningText(String filename); | 514 private static native String nativeGetDownloadWarningText(String filename); |
| 515 private static native boolean nativeIsDownloadDangerous(String filename); | 515 private static native boolean nativeIsDownloadDangerous(String filename); |
| 516 private static native void nativeDangerousDownloadValidated( | 516 private static native void nativeDangerousDownloadValidated( |
| 517 Object tab, int downloadId, boolean accept); | 517 Object tab, int downloadId, boolean accept); |
| 518 private static native void nativeLaunchDownloadOverwriteInfoBar(ChromeDownlo
adDelegate delegate, | 518 private static native void nativeLaunchDownloadOverwriteInfoBar(ChromeDownlo
adDelegate delegate, |
| 519 Tab tab, DownloadInfo downloadInfo, String fileName, String dirName, | 519 Tab tab, DownloadInfo downloadInfo, String fileName, String dirName, |
| 520 String dirFullPath); | 520 String dirFullPath); |
| 521 } | 521 } |
| OLD | NEW |