| 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.Manifest.permission; | 7 import android.Manifest.permission; |
| 8 import android.app.Activity; | 8 import android.app.Activity; |
| 9 import android.app.DownloadManager; | 9 import android.app.DownloadManager; |
| 10 import android.content.Context; | 10 import android.content.Context; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 import org.chromium.base.Log; | 25 import org.chromium.base.Log; |
| 26 import org.chromium.base.ThreadUtils; | 26 import org.chromium.base.ThreadUtils; |
| 27 import org.chromium.base.annotations.CalledByNative; | 27 import org.chromium.base.annotations.CalledByNative; |
| 28 import org.chromium.chrome.R; | 28 import org.chromium.chrome.R; |
| 29 import org.chromium.chrome.browser.ChromeActivity; | 29 import org.chromium.chrome.browser.ChromeActivity; |
| 30 import org.chromium.chrome.browser.infobar.InfoBarIdentifier; | 30 import org.chromium.chrome.browser.infobar.InfoBarIdentifier; |
| 31 import org.chromium.chrome.browser.infobar.SimpleConfirmInfoBarBuilder; | 31 import org.chromium.chrome.browser.infobar.SimpleConfirmInfoBarBuilder; |
| 32 import org.chromium.chrome.browser.tab.EmptyTabObserver; | 32 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
| 33 import org.chromium.chrome.browser.tab.Tab; | 33 import org.chromium.chrome.browser.tab.Tab; |
| 34 import org.chromium.chrome.browser.tabmodel.TabModelSelector; | 34 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| 35 import org.chromium.content.browser.ContentViewDownloadDelegate; | |
| 36 import org.chromium.content.browser.DownloadController; | |
| 37 import org.chromium.content.browser.DownloadInfo; | |
| 38 import org.chromium.content_public.browser.WebContents; | 35 import org.chromium.content_public.browser.WebContents; |
| 39 import org.chromium.ui.base.WindowAndroid; | 36 import org.chromium.ui.base.WindowAndroid; |
| 40 import org.chromium.ui.base.WindowAndroid.PermissionCallback; | 37 import org.chromium.ui.base.WindowAndroid.PermissionCallback; |
| 41 import org.chromium.ui.widget.Toast; | 38 import org.chromium.ui.widget.Toast; |
| 42 | 39 |
| 43 import java.io.File; | 40 import java.io.File; |
| 44 | 41 |
| 45 /** | 42 /** |
| 46 * Chrome implementation of the ContentViewDownloadDelegate interface. | 43 * Chrome implementation of the ContentViewDownloadDelegate interface. |
| 47 * | 44 * |
| 48 * Listens to POST and GET download events. GET download requests are passed alo
ng to the | 45 * Listens to POST and GET download events. GET download requests are passed alo
ng to the |
| 49 * Android Download Manager. POST downloads are expected to be handled natively
and listener | 46 * Android Download Manager. POST downloads are expected to be handled natively
and listener |
| 50 * is responsible for adding the completed download to the download manager. | 47 * is responsible for adding the completed download to the download manager. |
| 51 * | 48 * |
| 52 * Prompts the user when a dangerous file is downloaded. Auto-opens PDFs after d
ownloading. | 49 * Prompts the user when a dangerous file is downloaded. Auto-opens PDFs after d
ownloading. |
| 53 */ | 50 */ |
| 54 public class ChromeDownloadDelegate implements ContentViewDownloadDelegate { | 51 public class ChromeDownloadDelegate { |
| 55 private static final String TAG = "Download"; | 52 private static final String TAG = "Download"; |
| 56 | 53 |
| 57 private class DangerousDownloadListener implements SimpleConfirmInfoBarBuild
er.Listener { | 54 private class DangerousDownloadListener implements SimpleConfirmInfoBarBuild
er.Listener { |
| 58 @Override | 55 @Override |
| 59 public boolean onInfoBarButtonClicked(boolean confirm) { | 56 public boolean onInfoBarButtonClicked(boolean confirm) { |
| 60 assert mTab != null; | 57 assert mTab != null; |
| 61 if (mPendingRequest == null) return false; | 58 if (mPendingRequest == null) return false; |
| 62 if (mPendingRequest.getDownloadGuid() != null) { | 59 if (mPendingRequest.getDownloadGuid() != null) { |
| 63 nativeDangerousDownloadValidated(mTab, mPendingRequest.getDownlo
adGuid(), confirm); | 60 nativeDangerousDownloadValidated(mTab, mPendingRequest.getDownlo
adGuid(), confirm); |
| 64 if (confirm) { | 61 if (confirm) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 mTab = tab; | 142 mTab = tab; |
| 146 mTab.addObserver(new EmptyTabObserver() { | 143 mTab.addObserver(new EmptyTabObserver() { |
| 147 @Override | 144 @Override |
| 148 public void onDestroyed(Tab tab) { | 145 public void onDestroyed(Tab tab) { |
| 149 mTab = null; | 146 mTab = null; |
| 150 } | 147 } |
| 151 }); | 148 }); |
| 152 | 149 |
| 153 mPendingRequest = null; | 150 mPendingRequest = null; |
| 154 mDangerousDownloadListener = new DangerousDownloadListener(); | 151 mDangerousDownloadListener = new DangerousDownloadListener(); |
| 152 nativeInit(tab.getWebContents()); |
| 155 } | 153 } |
| 156 | 154 |
| 157 @Override | 155 @CalledByNative |
| 158 public void requestHttpGetDownload(DownloadInfo downloadInfo, boolean mustDo
wnload) { | 156 private void requestHttpGetDownload(String url, String userAgent, String con
tentDisposition, |
| 157 String mimeType, String cookie, String referer, boolean hasUserGestu
re, |
| 158 String filename, long contentLength, boolean mustDownload) { |
| 159 // If we're dealing with A/V content that's not explicitly marked for do
wnload, check if it | 159 // If we're dealing with A/V content that's not explicitly marked for do
wnload, check if it |
| 160 // is streamable. | 160 // is streamable. |
| 161 if (!mustDownload) { | 161 if (!mustDownload) { |
| 162 // Query the package manager to see if there's a registered handler
that matches. | 162 // Query the package manager to see if there's a registered handler
that matches. |
| 163 Intent intent = new Intent(Intent.ACTION_VIEW); | 163 Intent intent = new Intent(Intent.ACTION_VIEW); |
| 164 intent.setDataAndType(Uri.parse(downloadInfo.getUrl()), downloadInfo
.getMimeType()); | 164 intent.setDataAndType(Uri.parse(url), mimeType); |
| 165 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 165 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 166 // If the intent is resolved to ourselves, we don't want to attempt
to load the url | 166 // If the intent is resolved to ourselves, we don't want to attempt
to load the url |
| 167 // only to try and download it again. | 167 // only to try and download it again. |
| 168 if (DownloadManagerService.openIntent(mContext, intent, false)) { | 168 if (DownloadManagerService.openIntent(mContext, intent, false)) { |
| 169 return; | 169 return; |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 DownloadInfo downloadInfo = new DownloadInfo.Builder() |
| 173 .setUrl(url) |
| 174 .setUserAgent(userAgent) |
| 175 .setContentDisposition(contentDisposition) |
| 176 .setMimeType(mimeType) |
| 177 .setCookie(cookie) |
| 178 .setReferer(referer) |
| 179 .setHasUserGesture(hasUserGesture) |
| 180 .setFileName(filename) |
| 181 .setContentLength(contentLength) |
| 182 .setIsGETRequest(true) |
| 183 .build(); |
| 172 onDownloadStartNoStream(downloadInfo); | 184 onDownloadStartNoStream(downloadInfo); |
| 173 } | 185 } |
| 174 | 186 |
| 175 /** | 187 /** |
| 176 * Notify the host application a download should be done, even if there is a | 188 * Notify the host application a download should be done, even if there is a |
| 177 * streaming viewer available for this type. | 189 * streaming viewer available for this type. |
| 178 * | 190 * |
| 179 * @param downloadInfo Information about the download. | 191 * @param downloadInfo Information about the download. |
| 180 */ | 192 */ |
| 181 protected void onDownloadStartNoStream(final DownloadInfo downloadInfo) { | 193 protected void onDownloadStartNoStream(final DownloadInfo downloadInfo) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 InfoBarIdentifier.CONFIRM_DANGEROUS_DOWNLOAD, drawableId, titleT
ext, okButtonText, | 277 InfoBarIdentifier.CONFIRM_DANGEROUS_DOWNLOAD, drawableId, titleT
ext, okButtonText, |
| 266 cancelButtonText, true); | 278 cancelButtonText, true); |
| 267 } | 279 } |
| 268 | 280 |
| 269 /** | 281 /** |
| 270 * Called when a danagerous download is about to start. | 282 * Called when a danagerous download is about to start. |
| 271 * | 283 * |
| 272 * @param filename File name of the download item. | 284 * @param filename File name of the download item. |
| 273 * @param downloadGuid GUID of the download. | 285 * @param downloadGuid GUID of the download. |
| 274 */ | 286 */ |
| 275 @Override | 287 @CalledByNative |
| 276 public void onDangerousDownload(String filename, String downloadGuid) { | 288 private void onDangerousDownload(String filename, String downloadGuid) { |
| 277 DownloadInfo downloadInfo = new DownloadInfo.Builder() | 289 DownloadInfo downloadInfo = new DownloadInfo.Builder() |
| 278 .setFileName(filename) | 290 .setFileName(filename) |
| 279 .setDescription(filename) | 291 .setDescription(filename) |
| 280 .setDownloadGuid(downloadGuid).build(); | 292 .setDownloadGuid(downloadGuid).build(); |
| 281 confirmDangerousDownload(downloadInfo); | 293 confirmDangerousDownload(downloadInfo); |
| 282 } | 294 } |
| 283 | 295 |
| 284 @Override | 296 @CalledByNative |
| 285 public void requestFileAccess(final long callbackId) { | 297 private void requestFileAccess(final long callbackId) { |
| 286 if (mTab == null) { | 298 if (mTab == null) { |
| 287 // TODO(tedchoc): Show toast (only when activity is alive). | 299 // TODO(tedchoc): Show toast (only when activity is alive). |
| 288 DownloadController.getInstance().onRequestFileAccessResult(callbackI
d, false); | 300 DownloadController.getInstance().onRequestFileAccessResult(callbackI
d, false); |
| 289 return; | 301 return; |
| 290 } | 302 } |
| 291 final String storagePermission = android.Manifest.permission.WRITE_EXTER
NAL_STORAGE; | 303 final String storagePermission = android.Manifest.permission.WRITE_EXTER
NAL_STORAGE; |
| 292 final Activity activity = mTab.getWindowAndroid().getActivity().get(); | 304 final Activity activity = mTab.getWindowAndroid().getActivity().get(); |
| 293 | 305 |
| 294 if (activity == null) { | 306 if (activity == null) { |
| 295 DownloadController.getInstance().onRequestFileAccessResult(callbackI
d, false); | 307 DownloadController.getInstance().onRequestFileAccessResult(callbackI
d, false); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 DownloadManagerService.getDownloadManagerService( | 473 DownloadManagerService.getDownloadManagerService( |
| 462 mContext.getApplicationContext()).onDownloadFailed(fileName, rea
son); | 474 mContext.getApplicationContext()).onDownloadFailed(fileName, rea
son); |
| 463 } | 475 } |
| 464 | 476 |
| 465 /** | 477 /** |
| 466 * Called when download starts. | 478 * Called when download starts. |
| 467 * | 479 * |
| 468 * @param filename Name of the file. | 480 * @param filename Name of the file. |
| 469 * @param mimeType MIME type of the content. | 481 * @param mimeType MIME type of the content. |
| 470 */ | 482 */ |
| 471 @Override | 483 @CalledByNative |
| 472 public void onDownloadStarted(String filename, String mimeType) { | 484 private void onDownloadStarted(String filename, String mimeType) { |
| 473 if (!isDangerousFile(filename, mimeType)) { | 485 if (!isDangerousFile(filename, mimeType)) { |
| 474 showDownloadStartNotification(); | 486 showDownloadStartNotification(); |
| 475 closeBlankTab(); | 487 closeBlankTab(); |
| 476 } | 488 } |
| 477 } | 489 } |
| 478 | 490 |
| 479 /** | 491 /** |
| 480 * Shows the download started notification. | 492 * Shows the download started notification. |
| 481 */ | 493 */ |
| 482 private void showDownloadStartNotification() { | 494 private void showDownloadStartNotification() { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 } | 636 } |
| 625 return true; | 637 return true; |
| 626 } | 638 } |
| 627 return false; | 639 return false; |
| 628 } | 640 } |
| 629 | 641 |
| 630 protected Context getContext() { | 642 protected Context getContext() { |
| 631 return mContext; | 643 return mContext; |
| 632 } | 644 } |
| 633 | 645 |
| 646 private native void nativeInit(WebContents webContents); |
| 634 private static native String nativeGetDownloadWarningText(String filename); | 647 private static native String nativeGetDownloadWarningText(String filename); |
| 635 private static native boolean nativeIsDownloadDangerous(String filename); | 648 private static native boolean nativeIsDownloadDangerous(String filename); |
| 636 private static native void nativeDangerousDownloadValidated( | 649 private static native void nativeDangerousDownloadValidated( |
| 637 Object tab, String downloadGuid, boolean accept); | 650 Object tab, String downloadGuid, boolean accept); |
| 638 private static native void nativeLaunchDownloadOverwriteInfoBar(ChromeDownlo
adDelegate delegate, | 651 private static native void nativeLaunchDownloadOverwriteInfoBar(ChromeDownlo
adDelegate delegate, |
| 639 Tab tab, DownloadInfo downloadInfo, String fileName, String dirName, | 652 Tab tab, DownloadInfo downloadInfo, String fileName, String dirName, |
| 640 String dirFullPath); | 653 String dirFullPath); |
| 641 private static native void nativeLaunchPermissionUpdateInfoBar( | 654 private static native void nativeLaunchPermissionUpdateInfoBar( |
| 642 Tab tab, String permission, long callbackId); | 655 Tab tab, String permission, long callbackId); |
| 643 } | 656 } |
| OLD | NEW |