| // Copyright 2018 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "chrome/browser/download/android/download_dialog_bridge.h" |
| |
| #include "base/android/jni_android.h" |
| #include "base/android/jni_string.h" |
| #include "base/metrics/field_trial_params.h" |
| #include "base/metrics/histogram_macros.h" |
| #include "chrome/browser/download/android/download_controller.h" |
| #include "chrome/browser/download/android/jni_headers/DownloadDialogBridge_jni.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/profiles/profile_manager.h" |
| #include "chrome/common/pref_names.h" |
| #include "components/download/public/common/download_features.h" |
| #include "components/prefs/android/pref_service_android.h" |
| #include "components/prefs/pref_service.h" |
| #include "ui/android/window_android.h" |
| |
| // ----------------------------------------------------------------------------- |
| // DownloadDialogResult |
| DownloadDialogResult::DownloadDialogResult() = default; |
| |
| DownloadDialogResult::DownloadDialogResult(const DownloadDialogResult&) = |
| default; |
| |
| DownloadDialogResult::~DownloadDialogResult() = default; |
| |
| // ----------------------------------------------------------------------------- |
| // DownloadDialogBridge. |
| DownloadDialogBridge::DownloadDialogBridge() : is_dialog_showing_(false) { |
| JNIEnv* env = base::android::AttachCurrentThread(); |
| java_obj_.Reset(env, Java_DownloadDialogBridge_create( |
| env, reinterpret_cast<intptr_t>(this))); |
| DCHECK(!java_obj_.is_null()); |
| } |
| |
| DownloadDialogBridge::~DownloadDialogBridge() { |
| JNIEnv* env = base::android::AttachCurrentThread(); |
| Java_DownloadDialogBridge_destroy(env, java_obj_); |
| } |
| |
| void DownloadDialogBridge::ShowDialog( |
| gfx::NativeWindow native_window, |
| int64_t total_bytes, |
| net::NetworkChangeNotifier::ConnectionType connection_type, |
| DownloadLocationDialogType dialog_type, |
| const base::FilePath& suggested_path, |
| Profile* profile, |
| DialogCallback dialog_callback) { |
| if (!native_window) |
| return; |
| |
| UMA_HISTOGRAM_ENUMERATION("MobileDownload.Location.Dialog.Type", dialog_type); |
| |
| dialog_callback_ = std::move(dialog_callback); |
| |
| // This shouldn't happen. |
| if (dialog_type == DownloadLocationDialogType::NO_DIALOG) { |
| NOTREACHED(); |
| } |
| |
| // If dialog is showing, run the callback to continue without confirmation. |
| if (is_dialog_showing_) { |
| DownloadDialogResult dialog_result; |
| dialog_result.location_result = |
| DownloadLocationDialogResult::DUPLICATE_DIALOG; |
| dialog_result.file_path = suggested_path; |
| CompleteSelection(std::move(dialog_result)); |
| return; |
| } |
| |
| is_dialog_showing_ = true; |
| |
| JNIEnv* env = base::android::AttachCurrentThread(); |
| Java_DownloadDialogBridge_showDialog( |
| env, java_obj_, native_window->GetJavaObject(), |
| static_cast<long>(total_bytes), static_cast<int>(connection_type), |
| static_cast<int>(dialog_type), suggested_path.AsUTF8Unsafe(), |
| profile->GetJavaObject()); |
| } |
| |
| void DownloadDialogBridge::OnComplete( |
| JNIEnv* env, |
| std::string& returned_path, |
| bool did_user_confirm) { |
| DownloadDialogResult dialog_result; |
| dialog_result.location_result = |
| did_user_confirm |
| ? DownloadLocationDialogResult::USER_CONFIRMED |
| : DownloadLocationDialogResult::CONFIRMED_WITHOUT_USER_INPUT; |
| dialog_result.file_path = base::FilePath(returned_path); |
| |
| CompleteSelection(std::move(dialog_result)); |
| is_dialog_showing_ = false; |
| } |
| |
| void DownloadDialogBridge::OnCanceled(JNIEnv* env) { |
| if (dialog_callback_) { |
| DownloadDialogResult dialog_result; |
| dialog_result.location_result = DownloadLocationDialogResult::USER_CANCELED; |
| CompleteSelection(std::move(dialog_result)); |
| } |
| |
| is_dialog_showing_ = false; |
| } |
| |
| void DownloadDialogBridge::CompleteSelection(DownloadDialogResult result) { |
| if (!dialog_callback_) |
| return; |
| |
| UMA_HISTOGRAM_ENUMERATION("MobileDownload.Location.Dialog.Result", |
| result.location_result); |
| std::move(dialog_callback_).Run(std::move(result)); |
| } |
| |
| // static |
| void JNI_DownloadDialogBridge_SetDownloadAndSaveFileDefaultDirectory( |
| JNIEnv* env, |
| const base::android::JavaParamRef<jobject>& jpref_service, |
| std::string& directory) { |
| PrefService* pref_service = |
| PrefServiceAndroid::FromPrefServiceAndroid(jpref_service); |
| |
| base::FilePath path(directory); |
| pref_service->SetFilePath(prefs::kDownloadDefaultDirectory, path); |
| pref_service->SetFilePath(prefs::kSaveFileDefaultDirectory, path); |
| } |