DownloadController.java revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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
10/**
11 * Java counterpart of android DownloadController.
12 *
13 * Its a singleton class instantiated by the C++ DownloadController.
14 */
15@JNINamespace("content")
16class DownloadController {
17    private static final String LOGTAG = "DownloadController";
18    private static DownloadController sInstance;
19
20    @CalledByNative
21    public static DownloadController getInstance() {
22        if (sInstance == null) {
23            sInstance = new DownloadController();
24        }
25        return sInstance;
26    }
27
28    private DownloadController() {
29        nativeInit();
30    }
31
32    private static ContentViewDownloadDelegate downloadDelegateFromView(ContentViewCore view) {
33        return view.getDownloadDelegate();
34    }
35
36    /**
37     * Notifies the download delegate of a new GET download and passes all the information
38     * needed to download the file.
39     *
40     * The download delegate is expected to handle the download.
41     */
42    @CalledByNative
43    public void newHttpGetDownload(ContentViewCore view, String url,
44            String userAgent, String contentDisposition, String mimetype,
45            String cookie, String referer, long contentLength) {
46        ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
47
48        if (downloadDelagate != null) {
49            downloadDelagate.requestHttpGetDownload(url, userAgent, contentDisposition,
50                    mimetype, cookie, referer, contentLength);
51        }
52    }
53
54    /**
55     * Notifies the download delegate that a new download has started. This can
56     * be either a POST download or a GET download with authentication.
57     */
58    @CalledByNative
59    public void onDownloadStarted(ContentViewCore view) {
60        ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
61
62        if (downloadDelagate != null) {
63            downloadDelagate.onDownloadStarted();
64        }
65    }
66
67    /**
68     * Notifies the download delegate that a download completed and passes along info about the
69     * download. This can be either a POST download or a GET download with authentication.
70     */
71    @CalledByNative
72    public void onDownloadCompleted(ContentViewCore view, String url,
73            String contentDisposition, String mimetype, String path,
74            long contentLength, boolean successful) {
75        ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
76
77        if (downloadDelagate != null) {
78            downloadDelagate.onDownloadCompleted(
79                    url, mimetype, path, contentLength, successful);
80        }
81    }
82
83    /**
84     * Notifies the download delegate that a dangerous download started.
85     */
86    @CalledByNative
87    public void onDangerousDownload(ContentViewCore view, String filename,
88            int downloadId) {
89        ContentViewDownloadDelegate downloadDelagate = downloadDelegateFromView(view);
90        if (downloadDelagate != null) {
91            downloadDelagate.onDangerousDownload(filename, downloadId);
92        }
93    }
94
95    // native methods
96    private native void nativeInit();
97}
98