DownloadController.java revision 868fa2fe829687343ffae624259930155e16dbd8
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.
4
5package org.chromium.content.browser;
6
7import org.chromium.base.CalledByNative;
8import org.chromium.base.JNINamespace;
9
10import android.content.Context;
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 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.
35         */
36        void onDownloadCompleted(Context context, String url, String mimetype, String path,
37                String description, long contentLength, boolean successful);
38    }
39
40    private static DownloadNotificationService sDownloadNotificationService;
41
42    @CalledByNative
43    public static DownloadController getInstance() {
44        if (sInstance == null) {
45            sInstance = new DownloadController();
46        }
47        return sInstance;
48    }
49
50    private DownloadController() {
51        nativeInit();
52    }
53
54    private static ContentViewDownloadDelegate downloadDelegateFromView(ContentViewCore view) {
55        return view.getDownloadDelegate();
56    }
57
58    public static void setDownloadNotificationService(DownloadNotificationService service) {
59        sDownloadNotificationService = service;
60    }
61
62    /**
63     * Notifies the download delegate of a new GET download and passes all the information
64     * needed to download the file.
65     *
66     * The download delegate is expected to handle the download.
67     */
68    @CalledByNative
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);
73
74        if (downloadDelagate != null) {
75            downloadDelagate.requestHttpGetDownload(url, userAgent, contentDisposition,
76                    mimetype, cookie, referer, contentLength);
77        }
78    }
79
80    /**
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.
86     */
87    @CalledByNative
88    public void onDownloadStarted(ContentViewCore view, String filename, String mimeType) {
89        ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
90
91        if (downloadDelagate != null) {
92            downloadDelagate.onDownloadStarted(filename, mimeType);
93        }
94    }
95
96    /**
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.
99     */
100    @CalledByNative
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);
106        }
107    }
108
109    /**
110     * Notifies the download delegate that a dangerous download started.
111     */
112    @CalledByNative
113    public void onDangerousDownload(ContentViewCore view, String filename,
114            int downloadId) {
115        ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
116        if (downloadDelagate != null) {
117            downloadDelagate.onDangerousDownload(filename, downloadId);
118        }
119    }
120
121    // native methods
122    private native void nativeInit();
123}
124