• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 package org.chromium.content.browser;
6 
7 import android.content.Context;
8 
9 import org.chromium.base.CalledByNative;
10 import 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")
18 public 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          */
onDownloadCompleted(final DownloadInfo downloadInfo)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          */
onDownloadUpdated(final DownloadInfo downloadInfo)36         void onDownloadUpdated(final DownloadInfo downloadInfo);
37     }
38 
39     private static DownloadNotificationService sDownloadNotificationService;
40 
41     @CalledByNative
getInstance()42     public static DownloadController getInstance() {
43         if (sInstance == null) {
44             sInstance = new DownloadController();
45         }
46         return sInstance;
47     }
48 
DownloadController()49     private DownloadController() {
50         nativeInit();
51     }
52 
downloadDelegateFromView(ContentViewCore view)53     private static ContentViewDownloadDelegate downloadDelegateFromView(ContentViewCore view) {
54         return view.getDownloadDelegate();
55     }
56 
setDownloadNotificationService(DownloadNotificationService service)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
newHttpGetDownload(ContentViewCore view, String url, String userAgent, String contentDisposition, String mimeType, String cookie, String referer, String filename, long contentLength)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);
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                     .setFileName(filename)
82                     .setContentLength(contentLength)
83                     .setIsGETRequest(true)
84                     .build();
85             downloadDelegate.requestHttpGetDownload(downloadInfo);
86         }
87     }
88 
89     /**
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.
95      */
96     @CalledByNative
onDownloadStarted(ContentViewCore view, String filename, String mimeType)97     public void onDownloadStarted(ContentViewCore view, String filename, String mimeType) {
98         ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view);
99 
100         if (downloadDelegate != null) {
101             downloadDelegate.onDownloadStarted(filename, mimeType);
102         }
103     }
104 
105     /**
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.
108      */
109     @CalledByNative
onDownloadCompleted(Context context, String url, String mimeType, String filename, String path, long contentLength, boolean successful, int downloadId)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()
114                     .setUrl(url)
115                     .setMimeType(mimeType)
116                     .setFileName(filename)
117                     .setFilePath(path)
118                     .setContentLength(contentLength)
119                     .setIsSuccessful(successful)
120                     .setDescription(filename)
121                     .setDownloadId(downloadId)
122                     .setHasDownloadId(true)
123                     .build();
124             sDownloadNotificationService.onDownloadCompleted(downloadInfo);
125         }
126     }
127 
128     /**
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.
131      */
132     @CalledByNative
onDownloadUpdated(Context context, String url, String mimeType, String filename, String path, long contentLength, boolean successful, int downloadId, int percentCompleted, long timeRemainingInMs)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()
138             .setUrl(url)
139             .setMimeType(mimeType)
140             .setFileName(filename)
141             .setFilePath(path)
142             .setContentLength(contentLength)
143             .setIsSuccessful(successful)
144             .setDescription(filename)
145             .setDownloadId(downloadId)
146             .setHasDownloadId(true)
147             .setPercentCompleted(percentCompleted)
148             .setTimeRemainingInMillis(timeRemainingInMs)
149             .build();
150             sDownloadNotificationService.onDownloadUpdated(downloadInfo);
151         }
152     }
153 
154     /**
155      * Notifies the download delegate that a dangerous download started.
156      */
157     @CalledByNative
onDangerousDownload(ContentViewCore view, String filename, int downloadId)158     public void onDangerousDownload(ContentViewCore view, String filename,
159             int downloadId) {
160         ContentViewDownloadDelegate downloadDelegate = downloadDelegateFromView(view);
161         if (downloadDelegate != null) {
162             downloadDelegate.onDangerousDownload(filename, downloadId);
163         }
164     }
165 
166     // native methods
nativeInit()167     private native void nativeInit();
168 }
169