1 // Copyright (c) 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 org.chromium.base.CalledByNative;
8 import org.chromium.base.JNINamespace;
10 import android.content.Context;
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 context Application context.
29 * @param url The full url to the content that was downloaded.
30 * @param mimetype The mimetype of downloaded file.
31 * @param path Path of the downloaded file.
32 * @param description Description of the downloaded file.
33 * @param contentLength The file size of the downloaded file (in bytes).
34 * @param successful Whether the download succeeded.
36 void onDownloadCompleted(Context context, String url, String mimetype, String path,
37 String description, long contentLength, boolean successful);
40 private static DownloadNotificationService sDownloadNotificationService;
43 public static DownloadController getInstance() {
44 if (sInstance == null) {
45 sInstance = new DownloadController();
50 private DownloadController() {
54 private static ContentViewDownloadDelegate downloadDelegateFromView(ContentViewCore view) {
55 return view.getDownloadDelegate();
58 public static void setDownloadNotificationService(DownloadNotificationService service) {
59 sDownloadNotificationService = service;
63 * Notifies the download delegate of a new GET download and passes all the information
64 * needed to download the file.
66 * The download delegate is expected to handle the download.
69 public void newHttpGetDownload(ContentViewCore view, String url,
70 String userAgent, String contentDisposition, String mimetype,
71 String cookie, String referer, long contentLength) {
72 ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
74 if (downloadDelagate != null) {
75 downloadDelagate.requestHttpGetDownload(url, userAgent, contentDisposition,
76 mimetype, cookie, referer, contentLength);
81 * Notifies the download delegate that a new download has started. This can
82 * be either a POST download or a GET download with authentication.
83 * @param view ContentViewCore associated with the download item.
84 * @param filename File name of the downloaded file.
85 * @param mimeType Mime of the downloaded item.
88 public void onDownloadStarted(ContentViewCore view, String filename, String mimeType) {
89 ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
91 if (downloadDelagate != null) {
92 downloadDelagate.onDownloadStarted(filename, mimeType);
97 * Notifies the download delegate that a download completed and passes along info about the
98 * download. This can be either a POST download or a GET download with authentication.
101 public void onDownloadCompleted(Context context, String url, String mimetype,
102 String filename, String path, long contentLength, boolean successful) {
103 if (sDownloadNotificationService != null) {
104 sDownloadNotificationService.onDownloadCompleted(context, url, mimetype, path,
105 filename, contentLength, successful);
110 * Notifies the download delegate that a dangerous download started.
113 public void onDangerousDownload(ContentViewCore view, String filename,
115 ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
116 if (downloadDelagate != null) {
117 downloadDelagate.onDangerousDownload(filename, downloadId);
122 private native void nativeInit();