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.ActivityNotFoundException; | 7 import android.content.ActivityNotFoundException; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
11 import android.content.pm.ResolveInfo; | 11 import android.content.pm.ResolveInfo; |
12 import android.net.Uri; | 12 import android.net.Uri; |
13 import android.os.AsyncTask; | 13 import android.os.AsyncTask; |
14 import android.os.Environment; | 14 import android.os.Environment; |
15 import android.text.TextUtils; | 15 import android.text.TextUtils; |
16 import android.util.Log; | 16 import android.util.Log; |
17 import android.webkit.MimeTypeMap; | 17 import android.webkit.MimeTypeMap; |
18 import android.webkit.URLUtil; | 18 import android.webkit.URLUtil; |
19 import android.widget.Toast; | 19 import android.widget.Toast; |
20 | 20 |
| 21 import org.chromium.base.CalledByNative; |
21 import org.chromium.base.VisibleForTesting; | 22 import org.chromium.base.VisibleForTesting; |
22 import org.chromium.chrome.R; | 23 import org.chromium.chrome.R; |
23 import org.chromium.chrome.browser.Tab; | 24 import org.chromium.chrome.browser.Tab; |
24 import org.chromium.chrome.browser.infobar.ConfirmInfoBar; | 25 import org.chromium.chrome.browser.infobar.ConfirmInfoBar; |
25 import org.chromium.chrome.browser.infobar.InfoBar; | 26 import org.chromium.chrome.browser.infobar.InfoBar; |
26 import org.chromium.chrome.browser.infobar.InfoBarListeners; | 27 import org.chromium.chrome.browser.infobar.InfoBarListeners; |
27 import org.chromium.chrome.browser.tabmodel.TabModelSelector; | 28 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
28 import org.chromium.content.browser.ContentViewDownloadDelegate; | 29 import org.chromium.content.browser.ContentViewDownloadDelegate; |
29 import org.chromium.content.browser.DownloadInfo; | 30 import org.chromium.content.browser.DownloadInfo; |
30 import org.chromium.content_public.browser.WebContents; | 31 import org.chromium.content_public.browser.WebContents; |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 public void onDangerousDownload(String filename, int downloadId) { | 301 public void onDangerousDownload(String filename, int downloadId) { |
301 DownloadInfo downloadInfo = new DownloadInfo.Builder() | 302 DownloadInfo downloadInfo = new DownloadInfo.Builder() |
302 .setFileName(filename) | 303 .setFileName(filename) |
303 .setDescription(filename) | 304 .setDescription(filename) |
304 .setHasDownloadId(true) | 305 .setHasDownloadId(true) |
305 .setDownloadId(downloadId).build(); | 306 .setDownloadId(downloadId).build(); |
306 confirmDangerousDownload(downloadInfo); | 307 confirmDangerousDownload(downloadInfo); |
307 } | 308 } |
308 | 309 |
309 /** | 310 /** |
| 311 * Launch an info bar if the file name already exists for the download. |
| 312 * @param info The information of the file we are about to download. |
| 313 * @return Whether an info bar has been launched or not. |
| 314 */ |
| 315 private boolean launchInfoBarIfFileExists(final DownloadInfo info) { |
| 316 // Checks if file exists. |
| 317 final String fileName = info.getFileName(); |
| 318 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIR
ECTORY_DOWNLOADS); |
| 319 if (!dir.mkdir() && !dir.isDirectory()) return false; |
| 320 String dirName = dir.getName(); |
| 321 final File file = new File(dir, info.getFileName()); |
| 322 String fullDirPath = file.getParent(); |
| 323 if (!file.exists()) return false; |
| 324 if (TextUtils.isEmpty(fileName) || TextUtils.isEmpty(dirName) |
| 325 || TextUtils.isEmpty(fullDirPath)) { |
| 326 return false; |
| 327 } |
| 328 |
| 329 nativeLaunchDownloadOverwriteInfoBar(this, mTab, info.getFileName(), dir
Name, fullDirPath, |
| 330 info.getUrl(), info.getUserAgent(), info.getContentDisposition()
, |
| 331 info.getMimeType(), info.getCookie(), info.getReferer(), info.ha
sUserGesture(), |
| 332 info.getContentLength(), info.isGETRequest()); |
| 333 return true; |
| 334 } |
| 335 |
| 336 /** |
310 * Sends the download request to Android download manager. | 337 * Sends the download request to Android download manager. |
311 * | 338 * |
312 * @param info Download information about the download. | 339 * @param info Download information about the download. |
313 */ | 340 */ |
314 protected void enqueueDownloadManagerRequest(final DownloadInfo info) { | 341 protected void enqueueDownloadManagerRequest(final DownloadInfo info) { |
| 342 if (!launchInfoBarIfFileExists(info)) { |
| 343 enqueueDownloadManagerRequestInternal(info); |
| 344 } |
| 345 } |
| 346 |
| 347 /** |
| 348 * Enqueue download manager request, only from native side. |
| 349 * |
| 350 * @param overwrite Whether or not we will overwrite the file. |
| 351 * @param fileName The file name. |
| 352 * @param url The URL. |
| 353 * @param userAgent The user agent. |
| 354 * @param contentDisposition The content disposition. |
| 355 * @param mimeType The mime type. |
| 356 * @param cookie The cookie |
| 357 * @param referer The referer. |
| 358 * @param hasUserGesture Whether download was initiated by user gesture. |
| 359 * @param contentLength The content length. |
| 360 * @param isGETRequest Whether download is a GET request. |
| 361 */ |
| 362 @CalledByNative |
| 363 private void enqueueDownloadManagerRequestFromNative(boolean overwrite, Stri
ng fileName, |
| 364 String url, String userAgent, String contentDisposition, String mime
Type, String cookie, |
| 365 String referer, boolean hasUserGesture, long contentLength, boolean
isGETRequest) { |
| 366 DownloadInfo downloadInfo = new DownloadInfo.Builder() |
| 367 .setUrl(url) |
| 368 .setMimeType(mimeType) |
| 369 .setDescription(url) |
| 370 .setUserAgent(userAgent) |
| 371 .setCookie(cookie) |
| 372 .setContentDisposition(contentDisposition) |
| 373 .setContentLength(contentLength) |
| 374 .setReferer(referer) |
| 375 .setHasUserGesture(hasUserGesture) |
| 376 .setFileName(fileName) |
| 377 .setIsGETRequest(isGETRequest) |
| 378 .build(); |
| 379 // Android DownloadManager does not have an overwriting option. |
| 380 // We remove the file here instead. |
| 381 if (overwrite) deleteFileForOverwrite(downloadInfo); |
| 382 enqueueDownloadManagerRequestInternal(downloadInfo); |
| 383 } |
| 384 |
| 385 private void deleteFileForOverwrite(DownloadInfo info) { |
| 386 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIR
ECTORY_DOWNLOADS); |
| 387 if (!dir.isDirectory()) return; |
| 388 final File file = new File(dir, info.getFileName()); |
| 389 if (!file.delete()) { |
| 390 Log.e(LOGTAG, "Failed to delete a file." + info.getFileName()); |
| 391 } |
| 392 } |
| 393 |
| 394 private void enqueueDownloadManagerRequestInternal(final DownloadInfo info)
{ |
315 DownloadManagerService.getDownloadManagerService( | 395 DownloadManagerService.getDownloadManagerService( |
316 mContext.getApplicationContext()).enqueueDownloadManagerRequest(
info, true); | 396 mContext.getApplicationContext()).enqueueDownloadManagerRequest(
info, true); |
317 closeBlankTab(); | 397 closeBlankTab(); |
318 } | 398 } |
319 | 399 |
320 /** | 400 /** |
321 * Check the external storage and notify user on error. | 401 * Check the external storage and notify user on error. |
322 * | 402 * |
323 * @param fileName Name of the download file. | 403 * @param fileName Name of the download file. |
324 */ | 404 */ |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 } | 568 } |
489 | 569 |
490 protected Context getContext() { | 570 protected Context getContext() { |
491 return mContext; | 571 return mContext; |
492 } | 572 } |
493 | 573 |
494 private static native String nativeGetDownloadWarningText(String filename); | 574 private static native String nativeGetDownloadWarningText(String filename); |
495 private static native boolean nativeIsDownloadDangerous(String filename); | 575 private static native boolean nativeIsDownloadDangerous(String filename); |
496 private static native void nativeDangerousDownloadValidated( | 576 private static native void nativeDangerousDownloadValidated( |
497 Object tab, int downloadId, boolean accept); | 577 Object tab, int downloadId, boolean accept); |
| 578 private static native void nativeLaunchDownloadOverwriteInfoBar(ChromeDownlo
adDelegate delegate, |
| 579 Tab tab, String fileName, String dirName, String dirFullPath, String
url, |
| 580 String userAgent, String contentDisposition, String mimeType, String
cookie, |
| 581 String referer, boolean hasUserGesture, long contentLength, boolean
isGETRequest); |
498 } | 582 } |
OLD | NEW |