DownloadController.java revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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
5package org.chromium.content.browser;
6
7import android.content.Context;
8
9import org.chromium.base.CalledByNative;
10import 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")
18public class DownloadController {
19    private static final String LOGTAG = "DownloadController";
20    private static DownloadController sInstance;
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         */
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         */
36        void onDownloadUpdated(final DownloadInfo downloadInfo);
37    }
38
39    private static DownloadNotificationService sDownloadNotificationService;
40
41    @CalledByNative
42    public static DownloadController getInstance() {
43        if (sInstance == null) {
44            sInstance = new DownloadController();
45        }
46        return sInstance;
47    }
48
49    private DownloadController() {
50        nativeInit();
51    }
52
53    private static ContentViewDownloadDelegate downloadDelegateFromView(ContentViewCore view) {
54        return view.getDownloadDelegate();
55    }
56
57    public static void setDownloadNotificationService(DownloadNotificationService service) {
58        sDownloadNotificationService = service;
59    }
60
61    /**
62     * Notifies the download delegate of a new GET download and passes all the information
63     * needed to download the file.
64     *
65     * The download delegate is expected to handle the download.
66     */
67    @CalledByNative
68    public void newHttpGetDownload(ContentViewCore view, String url,
69            String userAgent, String contentDisposition, String mimeType,
70            String cookie, String referer, long contentLength) {
71        ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view);
72
73        if (downloadDelegate != null) {
74            DownloadInfo downloadInfo = new DownloadInfo.Builder()
75                    .setUrl(url)
76                    .setUserAgent(userAgent)
77                    .setContentDisposition(contentDisposition)
78                    .setMimeType(mimeType)
79                    .setCookie(cookie)
80                    .setReferer(referer)
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
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
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
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
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
166    private native void nativeInit();
167}
168