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(JNIEnv
* env
,
29 return base::android::ConvertUTF8ToJavaString(
30 env
, l10n_util::GetStringFUTF8(
31 IDS_PROMPT_DANGEROUS_DOWNLOAD
,
32 base::android::ConvertJavaStringToUTF16(env
, filename
)));
35 // Returns true if a file name is dangerous, or false otherwise.
36 static jboolean
IsDownloadDangerous(
37 JNIEnv
* env
, jclass clazz
, jstring filename
) {
38 base::FilePath
path(base::android::ConvertJavaStringToUTF8(env
, filename
));
39 return download_util::GetFileDangerLevel(path
) !=
40 download_util::NOT_DANGEROUS
;
43 // Called when a dangerous download is validated.
44 static void DangerousDownloadValidated(
45 JNIEnv
* env
, jclass clazz
, jobject tab
, jint download_id
, jboolean accept
) {
46 TabAndroid
* tab_android
= TabAndroid::GetNativeTab(env
, tab
);
47 content::DownloadControllerAndroid::Get()->DangerousDownloadValidated(
48 tab_android
->web_contents(), download_id
, accept
);
52 void ChromeDownloadDelegate::EnqueueDownloadManagerRequest(
53 jobject chrome_download_delegate
,
55 jobject download_info
) {
56 JNIEnv
* env
= base::android::AttachCurrentThread();
58 Java_ChromeDownloadDelegate_enqueueDownloadManagerRequestFromNative(
59 env
, chrome_download_delegate
, overwrite
, download_info
);
62 // Called when we need to interrupt download and ask users whether to overwrite
64 static void LaunchDownloadOverwriteInfoBar(JNIEnv
* env
,
68 jobject download_info
,
71 jstring jdir_full_path
) {
72 TabAndroid
* tab_android
= TabAndroid::GetNativeTab(env
, tab
);
74 std::string file_name
=
75 base::android::ConvertJavaStringToUTF8(env
, jfile_name
);
76 std::string dir_name
= base::android::ConvertJavaStringToUTF8(env
, jdir_name
);
77 std::string dir_full_path
=
78 base::android::ConvertJavaStringToUTF8(env
, jdir_full_path
);
80 chrome::android::AndroidDownloadManagerOverwriteInfoBarDelegate::Create(
81 InfoBarService::FromWebContents(tab_android
->web_contents()), file_name
,
82 dir_name
, dir_full_path
, delegate
, download_info
);
85 bool RegisterChromeDownloadDelegate(JNIEnv
* env
) {
86 return RegisterNativesImpl(env
);