From d8890abc92028486dbdd72d4566cf1f7789c9435 Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Wed, 22 Jun 2022 12:53:54 -0700 Subject: [PATCH 01/12] fix: modify file extension generation on Windows --- .../electron_download_manager_delegate.cc | 133 ++++++++++++++++++ .../electron_download_manager_delegate.h | 11 ++ 2 files changed, 144 insertions(+) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index 917a3a466e9cd..6098337f2871e 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -30,6 +30,12 @@ #include "shell/common/gin_converters/file_path_converter.h" #include "shell/common/options_switches.h" +#include "base/win/registry.h" +#define IDS_APP_SAVEAS_ALL_FILES 35936 +#include "ui/base/l10n/l10n_util.h" +#define IDS_APP_SAVEAS_EXTENSION_FORMAT 35937 +#include "base/i18n/case_conversion.h" + namespace electron { namespace { @@ -93,6 +99,112 @@ void ElectronDownloadManagerDelegate::GetItemSaveDialogOptions( *options = download->GetSaveDialogOptions(); } +// Get the file type description from the registry. This will be "Text Document" +// for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't +// have an entry for the file type, we return false, true if the description was +// found. 'file_ext' must be in form ".txt". +// Copied from ui/shell_dialogs/select_file_dialog_win.cc +bool ElectronDownloadManagerDelegate::GetRegistryDescriptionFromExtension( + const std::u16string& file_ext, + std::u16string* reg_description) { + DCHECK(reg_description); + base::win::RegKey reg_ext(HKEY_CLASSES_ROOT, base::as_wcstr(file_ext), + KEY_READ); + std::wstring reg_app; + if (reg_ext.ReadValue(nullptr, ®_app) == ERROR_SUCCESS && + !reg_app.empty()) { + base::win::RegKey reg_link(HKEY_CLASSES_ROOT, reg_app.c_str(), KEY_READ); + std::wstring description; + if (reg_link.ReadValue(nullptr, &description) == ERROR_SUCCESS) { + *reg_description = base::WideToUTF16(description); + return true; + } + } + return false; +} + +// Set up a filter for a Save/Open dialog, |ext_desc| as the text descriptions +// of the |file_ext| types (optional), and (optionally) the default 'All Files' +// view. The purpose of the filter is to show only files of a particular type in +// a Windows Save/Open dialog box. The resulting filter is returned. The filter +// created here are: +// 1. only files that have 'file_ext' as their extension +// 2. all files (only added if 'include_all_files' is true) +// If a description is not provided for a file extension, it will be retrieved +// from the registry. If the file extension does not exist in the registry, a +// default description will be created (e.g. "qqq" yields "QQQ File"). +// Copied from ui/shell_dialogs/select_file_dialog_win.cc +std::vector +ElectronDownloadManagerDelegate::FormatFilterForExtensions( + const std::vector& file_ext, + const std::vector& ext_desc, + bool include_all_files, + bool keep_extension_visible) { + const std::u16string all_ext = u"*.*"; + const std::u16string all_desc = + l10n_util::GetStringUTF16(IDS_APP_SAVEAS_ALL_FILES); + + DCHECK(file_ext.size() >= ext_desc.size()); + + if (file_ext.empty()) + include_all_files = true; + + std::vector result; + result.reserve(file_ext.size() + 1); + + for (size_t i = 0; i < file_ext.size(); ++i) { + std::u16string ext = file_ext[i]; + std::u16string desc; + if (i < ext_desc.size()) + desc = ext_desc[i]; + + if (ext.empty()) { + // Force something reasonable to appear in the dialog box if there is no + // extension provided. + include_all_files = true; + continue; + } + + if (desc.empty()) { + DCHECK(ext.find(u'.') != std::u16string::npos); + std::u16string first_extension = ext.substr(ext.find(u'.')); + size_t first_separator_index = first_extension.find(u';'); + if (first_separator_index != std::u16string::npos) + first_extension = first_extension.substr(0, first_separator_index); + + // Find the extension name without the preceeding '.' character. + std::u16string ext_name = first_extension; + size_t ext_index = ext_name.find_first_not_of(u'.'); + if (ext_index != std::u16string::npos) + ext_name = ext_name.substr(ext_index); + + if (!ElectronDownloadManagerDelegate::GetRegistryDescriptionFromExtension( + first_extension, &desc)) { + // The extension doesn't exist in the registry. Create a description + // based on the unknown extension type (i.e. if the extension is .qqq, + // then we create a description "QQQ File"). + desc = l10n_util::GetStringFUTF16(IDS_APP_SAVEAS_EXTENSION_FORMAT, + base::i18n::ToUpper(ext_name)); + include_all_files = true; + } + if (desc.empty()) + desc = u"*." + ext_name; + } else if (keep_extension_visible) { + // Having '*' in the description could cause the windows file dialog to + // not include the file extension in the file dialog. So strip out any '*' + // characters if `keep_extension_visible` is set. + base::ReplaceChars(desc, u"*", base::StringPiece16(), &desc); + } + + result.push_back({desc, ext}); + } + + if (include_all_files) + result.push_back({all_desc, all_ext}); + + return result; +} + void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( uint32_t download_id, content::DownloadTargetCallback callback, @@ -130,6 +242,27 @@ void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( const bool offscreen = !web_preferences || web_preferences->IsOffscreen(); settings.force_detached = offscreen; + auto extension = settings.default_path.FinalExtension(); + if (!extension.empty() && settings.filters.empty()) { + extension.erase(extension.begin()); + + std::u16string ext(extension.begin(), extension.end()); + const std::vector file_ext{u"." + ext}; + std::vector filter_spec = + FormatFilterForExtensions(file_ext, {u""}, true, true); + + std::string extension_spec_conv(filter_spec[0].extension_spec.begin(), + filter_spec[0].extension_spec.end()); + const std::vector filter{extension_spec_conv}; + std::string description_conv(filter_spec[0].description.begin(), + filter_spec[0].description.end()); + + settings.filters.emplace_back(std::make_pair(description_conv, filter)); + + std::vector all_files{"*.*"}; + settings.filters.emplace_back(std::make_pair("All Files", all_files)); + } + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::HandleScope scope(isolate); gin_helper::Promise dialog_promise(isolate); diff --git a/shell/browser/electron_download_manager_delegate.h b/shell/browser/electron_download_manager_delegate.h index e3100e54e5f49..01400e4f5f218 100644 --- a/shell/browser/electron_download_manager_delegate.h +++ b/shell/browser/electron_download_manager_delegate.h @@ -10,6 +10,8 @@ #include "shell/browser/ui/file_dialog.h" #include "shell/common/gin_helper/dictionary.h" +#include "ui/shell_dialogs/execute_select_file_win.h" + namespace content { class DownloadManager; } @@ -60,6 +62,15 @@ class ElectronDownloadManagerDelegate content::DownloadManager* download_manager_; base::WeakPtrFactory weak_ptr_factory_{this}; + + // Copied from ui/shell_dialogs/select_file_dialog_win.h + bool GetRegistryDescriptionFromExtension(const std::u16string& file_ext, + std::u16string* reg_description); + std::vector FormatFilterForExtensions( + const std::vector& file_ext, + const std::vector& ext_desc, + bool include_all_files, + bool keep_extension_visible); }; } // namespace electron From 5c2af2df21c008ac22f182e5f32abd722b9f42a5 Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Wed, 22 Jun 2022 15:05:49 -0700 Subject: [PATCH 02/12] modify includes --- shell/browser/electron_download_manager_delegate.cc | 6 +++--- shell/browser/electron_download_manager_delegate.h | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index 6098337f2871e..0798fa4f310df 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -10,8 +10,10 @@ #include "base/bind.h" #include "base/files/file_util.h" +#include "base/i18n/case_conversion.h" #include "base/task/thread_pool.h" #include "base/threading/thread_restrictions.h" +#include "base/win/registry.h" #include "chrome/common/pref_names.h" #include "components/download/public/common/download_danger_type.h" #include "components/prefs/pref_service.h" @@ -29,12 +31,10 @@ #include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/file_path_converter.h" #include "shell/common/options_switches.h" +#include "ui/base/l10n/l10n_util.h" -#include "base/win/registry.h" #define IDS_APP_SAVEAS_ALL_FILES 35936 -#include "ui/base/l10n/l10n_util.h" #define IDS_APP_SAVEAS_EXTENSION_FORMAT 35937 -#include "base/i18n/case_conversion.h" namespace electron { diff --git a/shell/browser/electron_download_manager_delegate.h b/shell/browser/electron_download_manager_delegate.h index 01400e4f5f218..bc06bde577dfe 100644 --- a/shell/browser/electron_download_manager_delegate.h +++ b/shell/browser/electron_download_manager_delegate.h @@ -9,7 +9,6 @@ #include "content/public/browser/download_manager_delegate.h" #include "shell/browser/ui/file_dialog.h" #include "shell/common/gin_helper/dictionary.h" - #include "ui/shell_dialogs/execute_select_file_win.h" namespace content { From d87fbf54c6e52835c4ba7ae2573e429e7c2b85f2 Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Thu, 23 Jun 2022 14:57:19 -0700 Subject: [PATCH 03/12] include vector in header --- shell/browser/electron_download_manager_delegate.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/browser/electron_download_manager_delegate.h b/shell/browser/electron_download_manager_delegate.h index bc06bde577dfe..a250012f53818 100644 --- a/shell/browser/electron_download_manager_delegate.h +++ b/shell/browser/electron_download_manager_delegate.h @@ -5,6 +5,8 @@ #ifndef ELECTRON_SHELL_BROWSER_ELECTRON_DOWNLOAD_MANAGER_DELEGATE_H_ #define ELECTRON_SHELL_BROWSER_ELECTRON_DOWNLOAD_MANAGER_DELEGATE_H_ +#include + #include "base/memory/weak_ptr.h" #include "content/public/browser/download_manager_delegate.h" #include "shell/browser/ui/file_dialog.h" From 2c9eddcbe662f22cf960af01663a483784e5cd9d Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Thu, 23 Jun 2022 16:00:07 -0700 Subject: [PATCH 04/12] add win build flags --- shell/browser/electron_download_manager_delegate.cc | 11 +++++++++-- shell/browser/electron_download_manager_delegate.h | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index 0798fa4f310df..8e01e604035f6 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -10,10 +10,8 @@ #include "base/bind.h" #include "base/files/file_util.h" -#include "base/i18n/case_conversion.h" #include "base/task/thread_pool.h" #include "base/threading/thread_restrictions.h" -#include "base/win/registry.h" #include "chrome/common/pref_names.h" #include "components/download/public/common/download_danger_type.h" #include "components/prefs/pref_service.h" @@ -31,10 +29,15 @@ #include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/file_path_converter.h" #include "shell/common/options_switches.h" + +#if BUILDFLAG(IS_WIN) +#include "base/i18n/case_conversion.h" +#include "base/win/registry.h" #include "ui/base/l10n/l10n_util.h" #define IDS_APP_SAVEAS_ALL_FILES 35936 #define IDS_APP_SAVEAS_EXTENSION_FORMAT 35937 +#endif // BUILDFLAG(IS_WIN) namespace electron { @@ -99,6 +102,7 @@ void ElectronDownloadManagerDelegate::GetItemSaveDialogOptions( *options = download->GetSaveDialogOptions(); } +#if BUILDFLAG(IS_WIN) // Get the file type description from the registry. This will be "Text Document" // for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't // have an entry for the file type, we return false, true if the description was @@ -204,6 +208,7 @@ ElectronDownloadManagerDelegate::FormatFilterForExtensions( return result; } +#endif // BUILDFLAG(IS_WIN) void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( uint32_t download_id, @@ -242,6 +247,7 @@ void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( const bool offscreen = !web_preferences || web_preferences->IsOffscreen(); settings.force_detached = offscreen; +#if BUILDFLAG(IS_WIN) auto extension = settings.default_path.FinalExtension(); if (!extension.empty() && settings.filters.empty()) { extension.erase(extension.begin()); @@ -262,6 +268,7 @@ void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( std::vector all_files{"*.*"}; settings.filters.emplace_back(std::make_pair("All Files", all_files)); } +#endif // BUILDFLAG(IS_WIN) v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::HandleScope scope(isolate); diff --git a/shell/browser/electron_download_manager_delegate.h b/shell/browser/electron_download_manager_delegate.h index a250012f53818..dd98739740342 100644 --- a/shell/browser/electron_download_manager_delegate.h +++ b/shell/browser/electron_download_manager_delegate.h @@ -11,7 +11,9 @@ #include "content/public/browser/download_manager_delegate.h" #include "shell/browser/ui/file_dialog.h" #include "shell/common/gin_helper/dictionary.h" +#if BUILDFLAG(IS_WIN) #include "ui/shell_dialogs/execute_select_file_win.h" +#endif // BUILDFLAG(IS_WIN) namespace content { class DownloadManager; @@ -64,6 +66,7 @@ class ElectronDownloadManagerDelegate content::DownloadManager* download_manager_; base::WeakPtrFactory weak_ptr_factory_{this}; +#if BUILDFLAG(IS_WIN) // Copied from ui/shell_dialogs/select_file_dialog_win.h bool GetRegistryDescriptionFromExtension(const std::u16string& file_ext, std::u16string* reg_description); @@ -72,6 +75,7 @@ class ElectronDownloadManagerDelegate const std::vector& ext_desc, bool include_all_files, bool keep_extension_visible); +#endif // BUILDFLAG(IS_WIN) }; } // namespace electron From 071c0d8055d572ad4c08369bd08f48c9f137ff65 Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Fri, 24 Jun 2022 14:55:55 -0700 Subject: [PATCH 05/12] remove hardcoded strings --- shell/browser/electron_download_manager_delegate.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index 8e01e604035f6..bb0133f513dff 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -34,9 +34,7 @@ #include "base/i18n/case_conversion.h" #include "base/win/registry.h" #include "ui/base/l10n/l10n_util.h" - -#define IDS_APP_SAVEAS_ALL_FILES 35936 -#define IDS_APP_SAVEAS_EXTENSION_FORMAT 35937 +#include "ui/strings/grit/ui_strings.h" #endif // BUILDFLAG(IS_WIN) namespace electron { From ab0ea4547c3e1a9c338353b388b29dbb324b6b79 Mon Sep 17 00:00:00 2001 From: Michaela Laurencin <35157522+mlaurencin@users.noreply.github.com> Date: Mon, 11 Jul 2022 14:49:26 -0700 Subject: [PATCH 06/12] Update shell/browser/electron_download_manager_delegate.h Co-authored-by: Charles Kerr --- shell/browser/electron_download_manager_delegate.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/browser/electron_download_manager_delegate.h b/shell/browser/electron_download_manager_delegate.h index dd98739740342..5fd2f6d8a094f 100644 --- a/shell/browser/electron_download_manager_delegate.h +++ b/shell/browser/electron_download_manager_delegate.h @@ -5,7 +5,9 @@ #ifndef ELECTRON_SHELL_BROWSER_ELECTRON_DOWNLOAD_MANAGER_DELEGATE_H_ #define ELECTRON_SHELL_BROWSER_ELECTRON_DOWNLOAD_MANAGER_DELEGATE_H_ +#if BUILDFLAG(IS_WIN) #include +#endif // BUILDFLAG(IS_WIN) #include "base/memory/weak_ptr.h" #include "content/public/browser/download_manager_delegate.h" From 6cf38d888020949beaa28e3303991c21f7c5cafa Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Mon, 18 Jul 2022 16:45:43 -0700 Subject: [PATCH 07/12] fix string manipulation and function definitions --- .../electron_download_manager_delegate.cc | 92 +++++++++---------- .../electron_download_manager_delegate.h | 14 --- 2 files changed, 43 insertions(+), 63 deletions(-) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index bb0133f513dff..129640414aca7 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -34,6 +34,7 @@ #include "base/i18n/case_conversion.h" #include "base/win/registry.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/shell_dialogs/execute_select_file_win.h" #include "ui/strings/grit/ui_strings.h" #endif // BUILDFLAG(IS_WIN) @@ -69,46 +70,14 @@ base::FilePath CreateDownloadPath(const GURL& url, return download_path.Append(generated_name); } -} // namespace - -ElectronDownloadManagerDelegate::ElectronDownloadManagerDelegate( - content::DownloadManager* manager) - : download_manager_(manager) {} - -ElectronDownloadManagerDelegate::~ElectronDownloadManagerDelegate() { - if (download_manager_) { - DCHECK_EQ(static_cast(this), - download_manager_->GetDelegate()); - download_manager_->SetDelegate(nullptr); - download_manager_ = nullptr; - } -} - -void ElectronDownloadManagerDelegate::GetItemSavePath( - download::DownloadItem* item, - base::FilePath* path) { - api::DownloadItem* download = api::DownloadItem::FromDownloadItem(item); - if (download) - *path = download->GetSavePath(); -} - -void ElectronDownloadManagerDelegate::GetItemSaveDialogOptions( - download::DownloadItem* item, - file_dialog::DialogSettings* options) { - api::DownloadItem* download = api::DownloadItem::FromDownloadItem(item); - if (download) - *options = download->GetSaveDialogOptions(); -} - #if BUILDFLAG(IS_WIN) // Get the file type description from the registry. This will be "Text Document" // for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't // have an entry for the file type, we return false, true if the description was // found. 'file_ext' must be in form ".txt". // Copied from ui/shell_dialogs/select_file_dialog_win.cc -bool ElectronDownloadManagerDelegate::GetRegistryDescriptionFromExtension( - const std::u16string& file_ext, - std::u16string* reg_description) { +bool GetRegistryDescriptionFromExtension(const std::u16string& file_ext, + std::u16string* reg_description) { DCHECK(reg_description); base::win::RegKey reg_ext(HKEY_CLASSES_ROOT, base::as_wcstr(file_ext), KEY_READ); @@ -136,8 +105,7 @@ bool ElectronDownloadManagerDelegate::GetRegistryDescriptionFromExtension( // from the registry. If the file extension does not exist in the registry, a // default description will be created (e.g. "qqq" yields "QQQ File"). // Copied from ui/shell_dialogs/select_file_dialog_win.cc -std::vector -ElectronDownloadManagerDelegate::FormatFilterForExtensions( +std::vector FormatFilterForExtensions( const std::vector& file_ext, const std::vector& ext_desc, bool include_all_files, @@ -180,8 +148,7 @@ ElectronDownloadManagerDelegate::FormatFilterForExtensions( if (ext_index != std::u16string::npos) ext_name = ext_name.substr(ext_index); - if (!ElectronDownloadManagerDelegate::GetRegistryDescriptionFromExtension( - first_extension, &desc)) { + if (!GetRegistryDescriptionFromExtension(first_extension, &desc)) { // The extension doesn't exist in the registry. Create a description // based on the unknown extension type (i.e. if the extension is .qqq, // then we create a description "QQQ File"). @@ -208,6 +175,37 @@ ElectronDownloadManagerDelegate::FormatFilterForExtensions( } #endif // BUILDFLAG(IS_WIN) +} // namespace + +ElectronDownloadManagerDelegate::ElectronDownloadManagerDelegate( + content::DownloadManager* manager) + : download_manager_(manager) {} + +ElectronDownloadManagerDelegate::~ElectronDownloadManagerDelegate() { + if (download_manager_) { + DCHECK_EQ(static_cast(this), + download_manager_->GetDelegate()); + download_manager_->SetDelegate(nullptr); + download_manager_ = nullptr; + } +} + +void ElectronDownloadManagerDelegate::GetItemSavePath( + download::DownloadItem* item, + base::FilePath* path) { + api::DownloadItem* download = api::DownloadItem::FromDownloadItem(item); + if (download) + *path = download->GetSavePath(); +} + +void ElectronDownloadManagerDelegate::GetItemSaveDialogOptions( + download::DownloadItem* item, + file_dialog::DialogSettings* options) { + api::DownloadItem* download = api::DownloadItem::FromDownloadItem(item); + if (download) + *options = download->GetSaveDialogOptions(); +} + void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( uint32_t download_id, content::DownloadTargetCallback callback, @@ -246,20 +244,16 @@ void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( settings.force_detached = offscreen; #if BUILDFLAG(IS_WIN) - auto extension = settings.default_path.FinalExtension(); + std::wstring extension = settings.default_path.FinalExtension(); if (!extension.empty() && settings.filters.empty()) { - extension.erase(extension.begin()); - - std::u16string ext(extension.begin(), extension.end()); - const std::vector file_ext{u"." + ext}; - std::vector filter_spec = - FormatFilterForExtensions(file_ext, {u""}, true, true); + std::vector filter_spec = FormatFilterForExtensions( + {base::WideToUTF16(extension)}, {u""}, true, true); - std::string extension_spec_conv(filter_spec[0].extension_spec.begin(), - filter_spec[0].extension_spec.end()); + std::string extension_spec_conv( + base::UTF16ToUTF8(filter_spec[0].extension_spec)); const std::vector filter{extension_spec_conv}; - std::string description_conv(filter_spec[0].description.begin(), - filter_spec[0].description.end()); + std::string description_conv( + base::UTF16ToUTF8(filter_spec[0].description)); settings.filters.emplace_back(std::make_pair(description_conv, filter)); diff --git a/shell/browser/electron_download_manager_delegate.h b/shell/browser/electron_download_manager_delegate.h index 5fd2f6d8a094f..73be88da75403 100644 --- a/shell/browser/electron_download_manager_delegate.h +++ b/shell/browser/electron_download_manager_delegate.h @@ -13,9 +13,6 @@ #include "content/public/browser/download_manager_delegate.h" #include "shell/browser/ui/file_dialog.h" #include "shell/common/gin_helper/dictionary.h" -#if BUILDFLAG(IS_WIN) -#include "ui/shell_dialogs/execute_select_file_win.h" -#endif // BUILDFLAG(IS_WIN) namespace content { class DownloadManager; @@ -67,17 +64,6 @@ class ElectronDownloadManagerDelegate content::DownloadManager* download_manager_; base::WeakPtrFactory weak_ptr_factory_{this}; - -#if BUILDFLAG(IS_WIN) - // Copied from ui/shell_dialogs/select_file_dialog_win.h - bool GetRegistryDescriptionFromExtension(const std::u16string& file_ext, - std::u16string* reg_description); - std::vector FormatFilterForExtensions( - const std::vector& file_ext, - const std::vector& ext_desc, - bool include_all_files, - bool keep_extension_visible); -#endif // BUILDFLAG(IS_WIN) }; } // namespace electron From a3e7f9563562106261be0b201d6bde713608da16 Mon Sep 17 00:00:00 2001 From: Michaela Laurencin <35157522+mlaurencin@users.noreply.github.com> Date: Mon, 18 Jul 2022 17:41:29 -0700 Subject: [PATCH 08/12] Update electron_download_manager_delegate.h --- shell/browser/electron_download_manager_delegate.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/shell/browser/electron_download_manager_delegate.h b/shell/browser/electron_download_manager_delegate.h index 73be88da75403..c3b12d8326136 100644 --- a/shell/browser/electron_download_manager_delegate.h +++ b/shell/browser/electron_download_manager_delegate.h @@ -5,9 +5,7 @@ #ifndef ELECTRON_SHELL_BROWSER_ELECTRON_DOWNLOAD_MANAGER_DELEGATE_H_ #define ELECTRON_SHELL_BROWSER_ELECTRON_DOWNLOAD_MANAGER_DELEGATE_H_ -#if BUILDFLAG(IS_WIN) #include -#endif // BUILDFLAG(IS_WIN) #include "base/memory/weak_ptr.h" #include "content/public/browser/download_manager_delegate.h" From f1cd3ac3e978b4fa8fd30b03a7b74b3b3952d249 Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Mon, 25 Jul 2022 16:02:48 -0700 Subject: [PATCH 09/12] convert to std::string and modify for electron --- .../electron_download_manager_delegate.cc | 74 +++++++++---------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index 129640414aca7..215aeda957961 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -75,19 +75,19 @@ base::FilePath CreateDownloadPath(const GURL& url, // for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't // have an entry for the file type, we return false, true if the description was // found. 'file_ext' must be in form ".txt". -// Copied from ui/shell_dialogs/select_file_dialog_win.cc -bool GetRegistryDescriptionFromExtension(const std::u16string& file_ext, - std::u16string* reg_description) { +// Modified from ui/shell_dialogs/select_file_dialog_win.cc +bool GetRegistryDescriptionFromExtension(const std::string& file_ext, + std::string* reg_description) { DCHECK(reg_description); - base::win::RegKey reg_ext(HKEY_CLASSES_ROOT, base::as_wcstr(file_ext), - KEY_READ); + base::win::RegKey reg_ext( + HKEY_CLASSES_ROOT, base::as_wcstr(base::UTF8ToUTF16(file_ext)), KEY_READ); std::wstring reg_app; if (reg_ext.ReadValue(nullptr, ®_app) == ERROR_SUCCESS && !reg_app.empty()) { base::win::RegKey reg_link(HKEY_CLASSES_ROOT, reg_app.c_str(), KEY_READ); std::wstring description; if (reg_link.ReadValue(nullptr, &description) == ERROR_SUCCESS) { - *reg_description = base::WideToUTF16(description); + *reg_description = base::WideToUTF8(description); return true; } } @@ -105,26 +105,26 @@ bool GetRegistryDescriptionFromExtension(const std::u16string& file_ext, // from the registry. If the file extension does not exist in the registry, a // default description will be created (e.g. "qqq" yields "QQQ File"). // Copied from ui/shell_dialogs/select_file_dialog_win.cc -std::vector FormatFilterForExtensions( - const std::vector& file_ext, - const std::vector& ext_desc, +file_dialog::Filters FormatFilterForExtensions( + const std::vector& file_ext, + const std::vector& ext_desc, bool include_all_files, bool keep_extension_visible) { - const std::u16string all_ext = u"*.*"; - const std::u16string all_desc = - l10n_util::GetStringUTF16(IDS_APP_SAVEAS_ALL_FILES); + const std::string all_ext = "*"; + const std::string all_desc = + l10n_util::GetStringUTF8(IDS_APP_SAVEAS_ALL_FILES); DCHECK(file_ext.size() >= ext_desc.size()); if (file_ext.empty()) include_all_files = true; - std::vector result; + file_dialog::Filters result; result.reserve(file_ext.size() + 1); for (size_t i = 0; i < file_ext.size(); ++i) { - std::u16string ext = file_ext[i]; - std::u16string desc; + std::string ext = file_ext[i]; + std::string desc; if (i < ext_desc.size()) desc = ext_desc[i]; @@ -136,40 +136,41 @@ std::vector FormatFilterForExtensions( } if (desc.empty()) { - DCHECK(ext.find(u'.') != std::u16string::npos); - std::u16string first_extension = ext.substr(ext.find(u'.')); - size_t first_separator_index = first_extension.find(u';'); - if (first_separator_index != std::u16string::npos) + DCHECK(ext.find('.') != std::string::npos); + std::string first_extension = ext.substr(ext.find('.')); + size_t first_separator_index = first_extension.find(';'); + if (first_separator_index != std::string::npos) first_extension = first_extension.substr(0, first_separator_index); // Find the extension name without the preceeding '.' character. - std::u16string ext_name = first_extension; - size_t ext_index = ext_name.find_first_not_of(u'.'); - if (ext_index != std::u16string::npos) + std::string ext_name = first_extension; + size_t ext_index = ext_name.find_first_not_of('.'); + if (ext_index != std::string::npos) ext_name = ext_name.substr(ext_index); if (!GetRegistryDescriptionFromExtension(first_extension, &desc)) { // The extension doesn't exist in the registry. Create a description // based on the unknown extension type (i.e. if the extension is .qqq, // then we create a description "QQQ File"). - desc = l10n_util::GetStringFUTF16(IDS_APP_SAVEAS_EXTENSION_FORMAT, - base::i18n::ToUpper(ext_name)); + desc = l10n_util::GetStringFUTF8( + IDS_APP_SAVEAS_EXTENSION_FORMAT, + base::i18n::ToUpper(base::UTF8ToUTF16(ext_name))); include_all_files = true; } if (desc.empty()) - desc = u"*." + ext_name; + desc = "*." + ext_name; } else if (keep_extension_visible) { // Having '*' in the description could cause the windows file dialog to // not include the file extension in the file dialog. So strip out any '*' // characters if `keep_extension_visible` is set. - base::ReplaceChars(desc, u"*", base::StringPiece16(), &desc); + base::ReplaceChars(desc, "*", base::StringPiece(), &desc); } - result.push_back({desc, ext}); + result.push_back({desc, {ext}}); } if (include_all_files) - result.push_back({all_desc, all_ext}); + result.push_back({all_desc, {all_ext}}); return result; } @@ -246,19 +247,12 @@ void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( #if BUILDFLAG(IS_WIN) std::wstring extension = settings.default_path.FinalExtension(); if (!extension.empty() && settings.filters.empty()) { - std::vector filter_spec = FormatFilterForExtensions( - {base::WideToUTF16(extension)}, {u""}, true, true); - - std::string extension_spec_conv( - base::UTF16ToUTF8(filter_spec[0].extension_spec)); - const std::vector filter{extension_spec_conv}; - std::string description_conv( - base::UTF16ToUTF8(filter_spec[0].description)); + file_dialog::Filters filter_spec = FormatFilterForExtensions( + {base::WideToUTF8(extension)}, {""}, true, true); - settings.filters.emplace_back(std::make_pair(description_conv, filter)); - - std::vector all_files{"*.*"}; - settings.filters.emplace_back(std::make_pair("All Files", all_files)); + for (file_dialog::Filter filter : filter_spec) { + settings.filters.emplace_back(filter); + } } #endif // BUILDFLAG(IS_WIN) From a5c299b345846a1d836895e4817999c15fdb782c Mon Sep 17 00:00:00 2001 From: Michaela Laurencin <35157522+mlaurencin@users.noreply.github.com> Date: Tue, 26 Jul 2022 17:57:40 -0700 Subject: [PATCH 10/12] Update shell/browser/electron_download_manager_delegate.cc Co-authored-by: Charles Kerr --- shell/browser/electron_download_manager_delegate.cc | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index 215aeda957961..8f9ee71b0b189 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -245,13 +245,11 @@ void ElectronDownloadManagerDelegate::OnDownloadPathGenerated( settings.force_detached = offscreen; #if BUILDFLAG(IS_WIN) - std::wstring extension = settings.default_path.FinalExtension(); - if (!extension.empty() && settings.filters.empty()) { - file_dialog::Filters filter_spec = FormatFilterForExtensions( - {base::WideToUTF8(extension)}, {""}, true, true); - - for (file_dialog::Filter filter : filter_spec) { - settings.filters.emplace_back(filter); + if (settings.filters.empty()) { + const std::wstring extension = settings.default_path.FinalExtension(); + if (!extension.empty()) { + settings.filters = FormatFilterForExtensions( + {base::WideToUTF8(extension)}, {""}, true, true); } } #endif // BUILDFLAG(IS_WIN) From d736cc348cf40ad62650401de7ccc13fb3a854d7 Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Tue, 26 Jul 2022 18:01:05 -0700 Subject: [PATCH 11/12] remove vector include and update conversion --- shell/browser/electron_download_manager_delegate.cc | 4 ++-- shell/browser/electron_download_manager_delegate.h | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index 8f9ee71b0b189..668bc9de3672a 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -79,8 +79,8 @@ base::FilePath CreateDownloadPath(const GURL& url, bool GetRegistryDescriptionFromExtension(const std::string& file_ext, std::string* reg_description) { DCHECK(reg_description); - base::win::RegKey reg_ext( - HKEY_CLASSES_ROOT, base::as_wcstr(base::UTF8ToUTF16(file_ext)), KEY_READ); + base::win::RegKey reg_ext(HKEY_CLASSES_ROOT, + base::UTF8ToWide(file_ext).c_str(), KEY_READ); std::wstring reg_app; if (reg_ext.ReadValue(nullptr, ®_app) == ERROR_SUCCESS && !reg_app.empty()) { diff --git a/shell/browser/electron_download_manager_delegate.h b/shell/browser/electron_download_manager_delegate.h index c3b12d8326136..e3100e54e5f49 100644 --- a/shell/browser/electron_download_manager_delegate.h +++ b/shell/browser/electron_download_manager_delegate.h @@ -5,8 +5,6 @@ #ifndef ELECTRON_SHELL_BROWSER_ELECTRON_DOWNLOAD_MANAGER_DELEGATE_H_ #define ELECTRON_SHELL_BROWSER_ELECTRON_DOWNLOAD_MANAGER_DELEGATE_H_ -#include - #include "base/memory/weak_ptr.h" #include "content/public/browser/download_manager_delegate.h" #include "shell/browser/ui/file_dialog.h" From b93536709d8ceec4dfcd746d4d929fafe774ce7e Mon Sep 17 00:00:00 2001 From: mlaurencin Date: Tue, 26 Jul 2022 19:00:01 -0700 Subject: [PATCH 12/12] add vectr include for lint --- shell/browser/electron_download_manager_delegate.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/browser/electron_download_manager_delegate.cc b/shell/browser/electron_download_manager_delegate.cc index 668bc9de3672a..c8a26676a3646 100644 --- a/shell/browser/electron_download_manager_delegate.cc +++ b/shell/browser/electron_download_manager_delegate.cc @@ -31,6 +31,8 @@ #include "shell/common/options_switches.h" #if BUILDFLAG(IS_WIN) +#include + #include "base/i18n/case_conversion.h" #include "base/win/registry.h" #include "ui/base/l10n/l10n_util.h"