| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/download/chrome_download_delegate.h" | 5 #include "chrome/browser/android/download/chrome_download_delegate.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <type_traits> | 10 #include <type_traits> |
| 11 | 11 |
| 12 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 14 #include "base/android/scoped_java_ref.h" | 14 #include "base/android/scoped_java_ref.h" |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "chrome/browser/android/download/android_download_manager_duplicate_inf
obar_delegate.h" | 18 #include "chrome/browser/android/download/android_download_manager_duplicate_inf
obar_delegate.h" |
| 19 #include "chrome/browser/android/download/download_controller_base.h" | 19 #include "chrome/browser/android/download/download_controller_base.h" |
| 20 #include "chrome/browser/android/tab_android.h" | 20 #include "chrome/browser/android/tab_android.h" |
| 21 #include "chrome/browser/infobars/infobar_service.h" | 21 #include "chrome/browser/infobars/infobar_service.h" |
| 22 #include "chrome/browser/permissions/permission_update_infobar_delegate_android.
h" | |
| 23 #include "chrome/common/safe_browsing/file_type_policies.h" | 22 #include "chrome/common/safe_browsing/file_type_policies.h" |
| 24 #include "chrome/grit/chromium_strings.h" | |
| 25 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
| 26 #include "jni/ChromeDownloadDelegate_jni.h" | 24 #include "jni/ChromeDownloadDelegate_jni.h" |
| 27 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 28 | 26 |
| 29 using base::android::ConvertUTF8ToJavaString; | 27 using base::android::ConvertUTF8ToJavaString; |
| 30 using base::android::JavaParamRef; | 28 using base::android::JavaParamRef; |
| 31 using base::android::ScopedJavaLocalRef; | 29 using base::android::ScopedJavaLocalRef; |
| 32 using content::WebContents; | 30 using content::WebContents; |
| 33 | 31 |
| 34 // Gets the download warning text for the given file name. | 32 // Gets the download warning text for the given file name. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab); | 64 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab); |
| 67 | 65 |
| 68 std::string file_path = | 66 std::string file_path = |
| 69 base::android::ConvertJavaStringToUTF8(env, jfile_path); | 67 base::android::ConvertJavaStringToUTF8(env, jfile_path); |
| 70 | 68 |
| 71 chrome::android::AndroidDownloadManagerDuplicateInfoBarDelegate::Create( | 69 chrome::android::AndroidDownloadManagerDuplicateInfoBarDelegate::Create( |
| 72 InfoBarService::FromWebContents(tab_android->web_contents()), file_path, | 70 InfoBarService::FromWebContents(tab_android->web_contents()), file_path, |
| 73 delegate, download_info, is_incognito); | 71 delegate, download_info, is_incognito); |
| 74 } | 72 } |
| 75 | 73 |
| 76 static void LaunchPermissionUpdateInfoBar( | |
| 77 JNIEnv* env, | |
| 78 const JavaParamRef<jclass>& clazz, | |
| 79 const JavaParamRef<jobject>& tab, | |
| 80 const JavaParamRef<jstring>& jpermission, | |
| 81 jlong callback_id) { | |
| 82 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab); | |
| 83 | |
| 84 std::string permission = | |
| 85 base::android::ConvertJavaStringToUTF8(env, jpermission); | |
| 86 | |
| 87 // Convert java long long int to c++ pointer, take ownership. | |
| 88 static_assert( | |
| 89 std::is_same< | |
| 90 DownloadControllerBase::AcquireFileAccessPermissionCallback, | |
| 91 base::Callback<void(bool)>>::value, | |
| 92 "Callback types don't match!"); | |
| 93 std::unique_ptr<base::Callback<void(bool)>> cb( | |
| 94 reinterpret_cast<base::Callback<void(bool)>*>(callback_id)); | |
| 95 | |
| 96 std::vector<std::string> permissions; | |
| 97 permissions.push_back(permission); | |
| 98 | |
| 99 PermissionUpdateInfoBarDelegate::Create( | |
| 100 tab_android->web_contents(), | |
| 101 permissions, | |
| 102 IDS_MISSING_STORAGE_PERMISSION_DOWNLOAD_EDUCATION_TEXT, | |
| 103 *cb); | |
| 104 } | |
| 105 | |
| 106 ChromeDownloadDelegate::ChromeDownloadDelegate( | 74 ChromeDownloadDelegate::ChromeDownloadDelegate( |
| 107 WebContents* web_contents) {} | 75 WebContents* web_contents) {} |
| 108 | 76 |
| 109 ChromeDownloadDelegate::~ChromeDownloadDelegate() { | 77 ChromeDownloadDelegate::~ChromeDownloadDelegate() { |
| 110 JNIEnv* env = base::android::AttachCurrentThread(); | 78 JNIEnv* env = base::android::AttachCurrentThread(); |
| 111 env->DeleteGlobalRef(java_ref_); | 79 env->DeleteGlobalRef(java_ref_); |
| 112 } | 80 } |
| 113 | 81 |
| 114 void ChromeDownloadDelegate::SetJavaRef(JNIEnv* env, jobject jobj) { | 82 void ChromeDownloadDelegate::SetJavaRef(JNIEnv* env, jobject jobj) { |
| 115 java_ref_ = env->NewGlobalRef(jobj); | 83 java_ref_ = env->NewGlobalRef(jobj); |
| 116 } | 84 } |
| 117 | 85 |
| 118 void ChromeDownloadDelegate::OnDownloadStarted(const std::string& filename) { | 86 void ChromeDownloadDelegate::OnDownloadStarted(const std::string& filename) { |
| 119 JNIEnv* env = base::android::AttachCurrentThread(); | 87 JNIEnv* env = base::android::AttachCurrentThread(); |
| 120 ScopedJavaLocalRef<jstring> jfilename = ConvertUTF8ToJavaString( | 88 ScopedJavaLocalRef<jstring> jfilename = ConvertUTF8ToJavaString( |
| 121 env, filename); | 89 env, filename); |
| 122 Java_ChromeDownloadDelegate_onDownloadStarted(env, java_ref_, jfilename); | 90 Java_ChromeDownloadDelegate_onDownloadStarted(env, java_ref_, jfilename); |
| 123 } | 91 } |
| 124 | 92 |
| 125 void ChromeDownloadDelegate::RequestFileAccess(intptr_t callback_id) { | |
| 126 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 127 Java_ChromeDownloadDelegate_requestFileAccess( | |
| 128 env, java_ref_, callback_id); | |
| 129 } | |
| 130 | |
| 131 void ChromeDownloadDelegate::EnqueueDownloadManagerRequest( | 93 void ChromeDownloadDelegate::EnqueueDownloadManagerRequest( |
| 132 const std::string& url, | 94 const std::string& url, |
| 133 const std::string& user_agent, | 95 const std::string& user_agent, |
| 134 const base::string16& file_name, | 96 const base::string16& file_name, |
| 135 const std::string& mime_type, | 97 const std::string& mime_type, |
| 136 const std::string& cookie, | 98 const std::string& cookie, |
| 137 const std::string& referer) { | 99 const std::string& referer) { |
| 138 JNIEnv* env = base::android::AttachCurrentThread(); | 100 JNIEnv* env = base::android::AttachCurrentThread(); |
| 139 ScopedJavaLocalRef<jstring> jurl = | 101 ScopedJavaLocalRef<jstring> jurl = |
| 140 ConvertUTF8ToJavaString(env, url); | 102 ConvertUTF8ToJavaString(env, url); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 159 auto* web_contents = WebContents::FromJavaWebContents(jweb_contents); | 121 auto* web_contents = WebContents::FromJavaWebContents(jweb_contents); |
| 160 ChromeDownloadDelegate::CreateForWebContents(web_contents); | 122 ChromeDownloadDelegate::CreateForWebContents(web_contents); |
| 161 ChromeDownloadDelegate::FromWebContents(web_contents)->SetJavaRef(env, obj); | 123 ChromeDownloadDelegate::FromWebContents(web_contents)->SetJavaRef(env, obj); |
| 162 } | 124 } |
| 163 | 125 |
| 164 bool RegisterChromeDownloadDelegate(JNIEnv* env) { | 126 bool RegisterChromeDownloadDelegate(JNIEnv* env) { |
| 165 return RegisterNativesImpl(env); | 127 return RegisterNativesImpl(env); |
| 166 } | 128 } |
| 167 | 129 |
| 168 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeDownloadDelegate); | 130 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeDownloadDelegate); |
| OLD | NEW |