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.VisibleForTesting; | 21 import org.chromium.base.VisibleForTesting; |
22 import org.chromium.chrome.R; | 22 import org.chromium.chrome.R; |
23 import org.chromium.chrome.browser.Tab; | 23 import org.chromium.chrome.browser.Tab; |
24 import org.chromium.chrome.browser.infobar.ConfirmInfoBar; | 24 import org.chromium.chrome.browser.infobar.ConfirmInfoBar; |
25 import org.chromium.chrome.browser.infobar.DownloadOverwriteInfoBar; | |
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; |
31 | 32 |
32 import java.io.File; | 33 import java.io.File; |
33 import java.util.List; | 34 import java.util.List; |
34 | 35 |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 @Override | 300 @Override |
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 |
310 private boolean launchInfoBarIfFileExists(final DownloadInfo info) { | |
qinmin
2015/02/20 01:00:40
java doc
Changwan Ryu
2015/02/23 06:50:37
Done.
| |
311 String fileName = info.getFileName(); | |
312 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIR ECTORY_DOWNLOADS); | |
313 if (!dir.mkdir() && !dir.isDirectory()) return false; | |
314 String dirName = dir.getName(); | |
315 final File file = new File(dir, info.getFileName()); | |
316 String fullDirPath = file.getParent(); | |
317 if (!file.exists()) return false; | |
318 if (TextUtils.isEmpty(fileName) || TextUtils.isEmpty(dirName) | |
319 || TextUtils.isEmpty(fullDirPath)) { | |
320 return false; | |
321 } | |
322 | |
323 InfoBar infoBar = new DownloadOverwriteInfoBar(0, new DownloadOverwriteI nfoBar.Callback() { | |
Ted C
2015/02/20 01:48:08
We are really trying to avoid any java only infoba
Changwan Ryu
2015/02/23 06:50:37
Hmm... I don't have a clear idea how this should b
Ted C
2015/02/23 18:48:15
Couldn't you just have a different native Download
| |
324 @Override | |
325 public void onCloseButtonClicked(DownloadOverwriteInfoBar infoBar) { | |
326 infoBar.dismissJavaOnlyInfoBar(); | |
327 } | |
328 | |
329 @Override | |
330 public void onAction(DownloadOverwriteInfoBar infoBar, int action) { | |
331 if (action == InfoBar.ACTION_TYPE_OVERWRITE) { | |
332 // Android DownloadManager does not have an overwriting opti on. | |
333 // We remove the file here instead. | |
334 file.delete(); | |
335 } | |
336 if (action == InfoBar.ACTION_TYPE_OVERWRITE | |
337 || action == InfoBar.ACTION_TYPE_CREATE_NEW_FILE) { | |
338 enqueueDownloadManagerRequestInternal(info); | |
339 } | |
340 infoBar.dismissJavaOnlyInfoBar(); | |
341 } | |
342 }, fileName, dirName, fullDirPath); | |
343 infoBar.setExpireOnNavigation(false); | |
344 mTab.getInfoBarContainer().addInfoBar(infoBar); | |
345 return true; | |
346 } | |
347 | |
309 /** | 348 /** |
310 * Sends the download request to Android download manager. | 349 * Sends the download request to Android download manager. |
311 * | 350 * |
312 * @param info Download information about the download. | 351 * @param info Download information about the download. |
313 */ | 352 */ |
314 protected void enqueueDownloadManagerRequest(final DownloadInfo info) { | 353 protected void enqueueDownloadManagerRequest(final DownloadInfo info) { |
354 if (!launchInfoBarIfFileExists(info)) { | |
355 enqueueDownloadManagerRequestInternal(info); | |
356 } | |
357 } | |
358 | |
359 private void enqueueDownloadManagerRequestInternal(final DownloadInfo info) { | |
315 DownloadManagerService.getDownloadManagerService( | 360 DownloadManagerService.getDownloadManagerService( |
316 mContext.getApplicationContext()).enqueueDownloadManagerRequest( info, true); | 361 mContext.getApplicationContext()).enqueueDownloadManagerRequest( info, true); |
317 closeBlankTab(); | 362 closeBlankTab(); |
318 } | 363 } |
319 | 364 |
320 /** | 365 /** |
321 * Check the external storage and notify user on error. | 366 * Check the external storage and notify user on error. |
322 * | 367 * |
323 * @param fileName Name of the download file. | 368 * @param fileName Name of the download file. |
324 */ | 369 */ |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
489 | 534 |
490 protected Context getContext() { | 535 protected Context getContext() { |
491 return mContext; | 536 return mContext; |
492 } | 537 } |
493 | 538 |
494 private static native String nativeGetDownloadWarningText(String filename); | 539 private static native String nativeGetDownloadWarningText(String filename); |
495 private static native boolean nativeIsDownloadDangerous(String filename); | 540 private static native boolean nativeIsDownloadDangerous(String filename); |
496 private static native void nativeDangerousDownloadValidated( | 541 private static native void nativeDangerousDownloadValidated( |
497 Object tab, int downloadId, boolean accept); | 542 Object tab, int downloadId, boolean accept); |
498 } | 543 } |
OLD | NEW |