1 // Copyright 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.content.browser; 6 7 import android.content.Context; 8 9 import org.chromium.base.CalledByNative; 10 import org.chromium.base.JNINamespace; 11 12 /** 13 * Java counterpart of android DownloadController. 14 * 15 * Its a singleton class instantiated by the C++ DownloadController. 16 */ 17 @JNINamespace("content") 18 public class DownloadController { 19 private static final String LOGTAG = "DownloadController"; 20 private static final DownloadController sInstance = new DownloadController(); 21 22 /** 23 * Class for notifying the application that download has completed. 24 */ 25 public interface DownloadNotificationService { 26 /** 27 * Notify the host application that a download is finished. 28 * @param downloadInfo Information about the completed download. 29 */ onDownloadCompleted(final DownloadInfo downloadInfo)30 void onDownloadCompleted(final DownloadInfo downloadInfo); 31 32 /** 33 * Notify the host application that a download is in progress. 34 * @param downloadInfo Information about the in-progress download. 35 */ onDownloadUpdated(final DownloadInfo downloadInfo)36 void onDownloadUpdated(final DownloadInfo downloadInfo); 37 } 38 39 private static DownloadNotificationService sDownloadNotificationService; 40 41 @CalledByNative getInstance()42 public static DownloadController getInstance() { 43 return sInstance; 44 } 45 DownloadController()46 private DownloadController() { 47 nativeInit(); 48 } 49 downloadDelegateFromView(ContentViewCore view)50 private static ContentViewDownloadDelegate downloadDelegateFromView(ContentViewCore view) { 51 return view.getDownloadDelegate(); 52 } 53 setDownloadNotificationService(DownloadNotificationService service)54 public static void setDownloadNotificationService(DownloadNotificationService service) { 55 sDownloadNotificationService = service; 56 } 57 58 /** 59 * Notifies the download delegate of a new GET download and passes all the information 60 * needed to download the file. 61 * 62 * The download delegate is expected to handle the download. 63 */ 64 @CalledByNative newHttpGetDownload(ContentViewCore view, String url, String userAgent, String contentDisposition, String mimeType, String cookie, String referer, boolean hasUserGesture, String filename, long contentLength)65 public void newHttpGetDownload(ContentViewCore view, String url, 66 String userAgent, String contentDisposition, String mimeType, 67 String cookie, String referer, boolean hasUserGesture, 68 String filename, long contentLength) { 69 ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view); 70 71 if (downloadDelegate != null) { 72 DownloadInfo downloadInfo = new DownloadInfo.Builder() 73 .setUrl(url) 74 .setUserAgent(userAgent) 75 .setContentDisposition(contentDisposition) 76 .setMimeType(mimeType) 77 .setCookie(cookie) 78 .setReferer(referer) 79 .setHasUserGesture(hasUserGesture) 80 .setFileName(filename) 81 .setContentLength(contentLength) 82 .setIsGETRequest(true) 83 .build(); 84 downloadDelegate.requestHttpGetDownload(downloadInfo); 85 } 86 } 87 88 /** 89 * Notifies the download delegate that a new download has started. This can 90 * be either a POST download or a GET download with authentication. 91 * @param view ContentViewCore associated with the download item. 92 * @param filename File name of the downloaded file. 93 * @param mimeType Mime of the downloaded item. 94 */ 95 @CalledByNative onDownloadStarted(ContentViewCore view, String filename, String mimeType)96 public void onDownloadStarted(ContentViewCore view, String filename, String mimeType) { 97 ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view); 98 99 if (downloadDelegate != null) { 100 downloadDelegate.onDownloadStarted(filename, mimeType); 101 } 102 } 103 104 /** 105 * Notifies the download delegate that a download completed and passes along info about the 106 * download. This can be either a POST download or a GET download with authentication. 107 */ 108 @CalledByNative onDownloadCompleted(Context context, String url, String mimeType, String filename, String path, long contentLength, boolean successful, int downloadId)109 public void onDownloadCompleted(Context context, String url, String mimeType, 110 String filename, String path, long contentLength, boolean successful, int downloadId) { 111 if (sDownloadNotificationService != null) { 112 DownloadInfo downloadInfo = new DownloadInfo.Builder() 113 .setUrl(url) 114 .setMimeType(mimeType) 115 .setFileName(filename) 116 .setFilePath(path) 117 .setContentLength(contentLength) 118 .setIsSuccessful(successful) 119 .setDescription(filename) 120 .setDownloadId(downloadId) 121 .setHasDownloadId(true) 122 .build(); 123 sDownloadNotificationService.onDownloadCompleted(downloadInfo); 124 } 125 } 126 127 /** 128 * Notifies the download delegate about progress of a download. Downloads that use Chrome 129 * network stack use custom notification to display the progress of downloads. 130 */ 131 @CalledByNative onDownloadUpdated(Context context, String url, String mimeType, String filename, String path, long contentLength, boolean successful, int downloadId, int percentCompleted, long timeRemainingInMs)132 public void onDownloadUpdated(Context context, String url, String mimeType, 133 String filename, String path, long contentLength, boolean successful, int downloadId, 134 int percentCompleted, long timeRemainingInMs) { 135 if (sDownloadNotificationService != null) { 136 DownloadInfo downloadInfo = new DownloadInfo.Builder() 137 .setUrl(url) 138 .setMimeType(mimeType) 139 .setFileName(filename) 140 .setFilePath(path) 141 .setContentLength(contentLength) 142 .setIsSuccessful(successful) 143 .setDescription(filename) 144 .setDownloadId(downloadId) 145 .setHasDownloadId(true) 146 .setPercentCompleted(percentCompleted) 147 .setTimeRemainingInMillis(timeRemainingInMs) 148 .build(); 149 sDownloadNotificationService.onDownloadUpdated(downloadInfo); 150 } 151 } 152 153 /** 154 * Notifies the download delegate that a dangerous download started. 155 */ 156 @CalledByNative onDangerousDownload(ContentViewCore view, String filename, int downloadId)157 public void onDangerousDownload(ContentViewCore view, String filename, 158 int downloadId) { 159 ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view); 160 if (downloadDelegate != null) { 161 downloadDelegate.onDangerousDownload(filename, downloadId); 162 } 163 } 164 165 // native methods nativeInit()166 private native void nativeInit(); 167 } 168