1 // Copyright 2015 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 #include "chrome/browser/android/download/chrome_download_delegate.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/android/download/android_download_manager_overwrite_infobar_delegate.h"
17 #include "chrome/browser/android/tab_android.h"
18 #include "chrome/browser/download/download_extensions.h"
19 #include "chrome/browser/infobars/infobar_service.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "content/public/browser/android/download_controller_android.h"
22 #include "jni/ChromeDownloadDelegate_jni.h"
23 #include "ui/base/l10n/l10n_util.h"
25 // Gets the download warning text for the given file name.
26 static ScopedJavaLocalRef
<jstring
> GetDownloadWarningText(
28 const JavaParamRef
<jclass
>& clazz
,
29 const JavaParamRef
<jstring
>& filename
) {
30 return base::android::ConvertUTF8ToJavaString(
31 env
, l10n_util::GetStringFUTF8(
32 IDS_PROMPT_DANGEROUS_DOWNLOAD
,
33 base::android::ConvertJavaStringToUTF16(env
, filename
)));
36 // Returns true if a file name is dangerous, or false otherwise.
37 static jboolean
IsDownloadDangerous(JNIEnv
* env
,
38 const JavaParamRef
<jclass
>& clazz
,
39 const JavaParamRef
<jstring
>& filename
) {
40 base::FilePath
path(base::android::ConvertJavaStringToUTF8(env
, filename
));
41 return download_util::GetFileDangerLevel(path
) !=
42 download_util::NOT_DANGEROUS
;
45 // Called when a dangerous download is validated.
46 static void DangerousDownloadValidated(JNIEnv
* env
,
47 const JavaParamRef
<jclass
>& clazz
,
48 const JavaParamRef
<jobject
>& tab
,
51 TabAndroid
* tab_android
= TabAndroid::GetNativeTab(env
, tab
);
52 content::DownloadControllerAndroid::Get()->DangerousDownloadValidated(
53 tab_android
->web_contents(), download_id
, accept
);
57 bool ChromeDownloadDelegate::EnqueueDownloadManagerRequest(
58 jobject chrome_download_delegate
,
60 jobject download_info
) {
61 JNIEnv
* env
= base::android::AttachCurrentThread();
63 return Java_ChromeDownloadDelegate_enqueueDownloadManagerRequestFromNative(
64 env
, chrome_download_delegate
, overwrite
, download_info
);
67 // Called when we need to interrupt download and ask users whether to overwrite
69 static void LaunchDownloadOverwriteInfoBar(
71 const JavaParamRef
<jclass
>& clazz
,
72 const JavaParamRef
<jobject
>& delegate
,
73 const JavaParamRef
<jobject
>& tab
,
74 const JavaParamRef
<jobject
>& download_info
,
75 const JavaParamRef
<jstring
>& jfile_name
,
76 const JavaParamRef
<jstring
>& jdir_name
,
77 const JavaParamRef
<jstring
>& jdir_full_path
) {
78 TabAndroid
* tab_android
= TabAndroid::GetNativeTab(env
, tab
);
80 std::string file_name
=
81 base::android::ConvertJavaStringToUTF8(env
, jfile_name
);
82 std::string dir_name
= base::android::ConvertJavaStringToUTF8(env
, jdir_name
);
83 std::string dir_full_path
=
84 base::android::ConvertJavaStringToUTF8(env
, jdir_full_path
);
86 chrome::android::AndroidDownloadManagerOverwriteInfoBarDelegate::Create(
87 InfoBarService::FromWebContents(tab_android
->web_contents()), file_name
,
88 dir_name
, dir_full_path
, delegate
, download_info
);
91 bool RegisterChromeDownloadDelegate(JNIEnv
* env
) {
92 return RegisterNativesImpl(env
);