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 DownloadController sInstance;
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() {
43 if (sInstance == null) {
44 sInstance = new DownloadController();
49 private DownloadController() {
53 private static ContentViewDownloadDelegate downloadDelegateFromView(ContentViewCore view) {
54 return view.getDownloadDelegate();
57 public static void setDownloadNotificationService(DownloadNotificationService service) {
58 sDownloadNotificationService = service;
62 * Notifies the download delegate of a new GET download and passes all the information
63 * needed to download the file.
65 * The download delegate is expected to handle the download.
68 public void newHttpGetDownload(ContentViewCore view, String url,
69 String userAgent, String contentDisposition, String mimeType,
70 String cookie, String referer, String filename, long contentLength) {
71 ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view);
73 if (downloadDelegate != null) {
74 DownloadInfo downloadInfo = new DownloadInfo.Builder()
76 .setUserAgent(userAgent)
77 .setContentDisposition(contentDisposition)
78 .setMimeType(mimeType)
81 .setFileName(filename)
82 .setContentLength(contentLength)
83 .setIsGETRequest(true)
85 downloadDelegate.requestHttpGetDownload(downloadInfo);
90 * Notifies the download delegate that a new download has started. This can
91 * be either a POST download or a GET download with authentication.
92 * @param view ContentViewCore associated with the download item.
93 * @param filename File name of the downloaded file.
94 * @param mimeType Mime of the downloaded item.
97 public void onDownloadStarted(ContentViewCore view, String filename, String mimeType) {
98 ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view);
100 if (downloadDelegate != null) {
101 downloadDelegate.onDownloadStarted(filename, mimeType);
106 * Notifies the download delegate that a download completed and passes along info about the
107 * download. This can be either a POST download or a GET download with authentication.
110 public void onDownloadCompleted(Context context, String url, String mimeType,
111 String filename, String path, long contentLength, boolean successful, int downloadId) {
112 if (sDownloadNotificationService != null) {
113 DownloadInfo downloadInfo = new DownloadInfo.Builder()
115 .setMimeType(mimeType)
116 .setFileName(filename)
118 .setContentLength(contentLength)
119 .setIsSuccessful(successful)
120 .setDescription(filename)
121 .setDownloadId(downloadId)
122 .setHasDownloadId(true)
124 sDownloadNotificationService.onDownloadCompleted(downloadInfo);
129 * Notifies the download delegate about progress of a download. Downloads that use Chrome
130 * network stack use custom notification to display the progress of downloads.
133 public void onDownloadUpdated(Context context, String url, String mimeType,
134 String filename, String path, long contentLength, boolean successful, int downloadId,
135 int percentCompleted, long timeRemainingInMs) {
136 if (sDownloadNotificationService != null) {
137 DownloadInfo downloadInfo = new DownloadInfo.Builder()
139 .setMimeType(mimeType)
140 .setFileName(filename)
142 .setContentLength(contentLength)
143 .setIsSuccessful(successful)
144 .setDescription(filename)
145 .setDownloadId(downloadId)
146 .setHasDownloadId(true)
147 .setPercentCompleted(percentCompleted)
148 .setTimeRemainingInMillis(timeRemainingInMs)
150 sDownloadNotificationService.onDownloadUpdated(downloadInfo);
155 * Notifies the download delegate that a dangerous download started.
158 public void onDangerousDownload(ContentViewCore view, String filename,
160 ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view);
161 if (downloadDelegate != null) {
162 downloadDelegate.onDangerousDownload(filename, downloadId);
167 private native void nativeInit();