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.
5 package org.chromium.content.browser;
7 import android.content.Context;
9 import org.chromium.base.CalledByNative;
10 import org.chromium.base.JNINamespace;
13 * Java counterpart of android DownloadController.
15 * Its a singleton class instantiated by the C++ DownloadController.
17 @JNINamespace("content")
18 public class DownloadController {
19 private static final String LOGTAG = "DownloadController";
20 private static final DownloadController sInstance = new DownloadController();
23 * Class for notifying the application that download has completed.
25 public interface DownloadNotificationService {
27 * Notify the host application that a download is finished.
28 * @param downloadInfo Information about the completed download.
30 void onDownloadCompleted(final DownloadInfo downloadInfo);
33 * Notify the host application that a download is in progress.
34 * @param downloadInfo Information about the in-progress download.
36 void onDownloadUpdated(final DownloadInfo downloadInfo);
39 private static DownloadNotificationService sDownloadNotificationService;
42 public static DownloadController getInstance() {
46 private DownloadController() {
50 private static ContentViewDownloadDelegate downloadDelegateFromView(ContentViewCore view) {
51 return view.getDownloadDelegate();
54 public static void setDownloadNotificationService(DownloadNotificationService service) {
55 sDownloadNotificationService = service;
59 * Notifies the download delegate of a new GET download and passes all the information
60 * needed to download the file.
62 * The download delegate is expected to handle the download.
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);
71 if (downloadDelegate != null) {
72 DownloadInfo downloadInfo = new DownloadInfo.Builder()
74 .setUserAgent(userAgent)
75 .setContentDisposition(contentDisposition)
76 .setMimeType(mimeType)
79 .setHasUserGesture(hasUserGesture)
80 .setFileName(filename)
81 .setContentLength(contentLength)
82 .setIsGETRequest(true)
84 downloadDelegate.requestHttpGetDownload(downloadInfo);
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.
96 public void onDownloadStarted(ContentViewCore view, String filename, String mimeType) {
97 ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view);
99 if (downloadDelegate != null) {
100 downloadDelegate.onDownloadStarted(filename, mimeType);
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.
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()
114 .setMimeType(mimeType)
115 .setFileName(filename)
117 .setContentLength(contentLength)
118 .setIsSuccessful(successful)
119 .setDescription(filename)
120 .setDownloadId(downloadId)
121 .setHasDownloadId(true)
123 sDownloadNotificationService.onDownloadCompleted(downloadInfo);
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.
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()
138 .setMimeType(mimeType)
139 .setFileName(filename)
141 .setContentLength(contentLength)
142 .setIsSuccessful(successful)
143 .setDescription(filename)
144 .setDownloadId(downloadId)
145 .setHasDownloadId(true)
146 .setPercentCompleted(percentCompleted)
147 .setTimeRemainingInMillis(timeRemainingInMs)
149 sDownloadNotificationService.onDownloadUpdated(downloadInfo);
154 * Notifies the download delegate that a dangerous download started.
157 public void onDangerousDownload(ContentViewCore view, String filename,
159 ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view);
160 if (downloadDelegate != null) {
161 downloadDelegate.onDangerousDownload(filename, downloadId);
166 private native void nativeInit();