fix: about panel crash (#37373)

* fix: about panel is a base::Value::Dict

* nix this test for a diff PR
This commit is contained in:
Calvin 2023-02-28 15:26:00 -07:00 committed by GitHub
parent 3a5ae28c95
commit 2e03bdb9b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 29 deletions

View file

@ -363,7 +363,7 @@ class Browser : public WindowListObserver {
base::Time last_dock_show_; base::Time last_dock_show_;
#endif #endif
base::Value about_panel_options_; base::Value::Dict about_panel_options_;
#if BUILDFLAG(IS_WIN) #if BUILDFLAG(IS_WIN)
void UpdateBadgeContents(HWND hwnd, void UpdateBadgeContents(HWND hwnd,

View file

@ -162,31 +162,25 @@ bool Browser::IsEmojiPanelSupported() {
void Browser::ShowAboutPanel() { void Browser::ShowAboutPanel() {
const auto& opts = about_panel_options_; const auto& opts = about_panel_options_;
if (!opts.is_dict()) {
LOG(WARNING) << "Called showAboutPanel(), but didn't use "
"setAboutPanelSettings() first";
return;
}
GtkWidget* dialogWidget = gtk_about_dialog_new(); GtkWidget* dialogWidget = gtk_about_dialog_new();
GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget); GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget);
const std::string* str; const std::string* str;
const base::Value* val; const base::Value::List* list;
if ((str = opts.FindStringKey("applicationName"))) { if ((str = opts.FindString("applicationName"))) {
gtk_about_dialog_set_program_name(dialog, str->c_str()); gtk_about_dialog_set_program_name(dialog, str->c_str());
} }
if ((str = opts.FindStringKey("applicationVersion"))) { if ((str = opts.FindString("applicationVersion"))) {
gtk_about_dialog_set_version(dialog, str->c_str()); gtk_about_dialog_set_version(dialog, str->c_str());
} }
if ((str = opts.FindStringKey("copyright"))) { if ((str = opts.FindString("copyright"))) {
gtk_about_dialog_set_copyright(dialog, str->c_str()); gtk_about_dialog_set_copyright(dialog, str->c_str());
} }
if ((str = opts.FindStringKey("website"))) { if ((str = opts.FindString("website"))) {
gtk_about_dialog_set_website(dialog, str->c_str()); gtk_about_dialog_set_website(dialog, str->c_str());
} }
if ((str = opts.FindStringKey("iconPath"))) { if ((str = opts.FindString("iconPath"))) {
GError* error = nullptr; GError* error = nullptr;
constexpr int width = 64; // width of about panel icon in pixels constexpr int width = 64; // width of about panel icon in pixels
constexpr int height = 64; // height of about panel icon in pixels constexpr int height = 64; // height of about panel icon in pixels
@ -203,9 +197,9 @@ void Browser::ShowAboutPanel() {
} }
} }
if ((val = opts.FindListKey("authors"))) { if ((list = opts.FindList("authors"))) {
std::vector<const char*> cstrs; std::vector<const char*> cstrs;
for (const auto& authorVal : val->GetList()) { for (const auto& authorVal : *list) {
if (authorVal.is_string()) { if (authorVal.is_string()) {
cstrs.push_back(authorVal.GetString().c_str()); cstrs.push_back(authorVal.GetString().c_str());
} }
@ -223,7 +217,7 @@ void Browser::ShowAboutPanel() {
} }
void Browser::SetAboutPanelOptions(base::Value::Dict options) { void Browser::SetAboutPanelOptions(base::Value::Dict options) {
about_panel_options_ = base::Value(std::move(options)); about_panel_options_ = std::move(options);
} }
} // namespace electron } // namespace electron

View file

@ -513,8 +513,7 @@ void Browser::DockSetIcon(v8::Isolate* isolate, v8::Local<v8::Value> icon) {
} }
void Browser::ShowAboutPanel() { void Browser::ShowAboutPanel() {
NSDictionary* options = NSDictionary* options = DictionaryValueToNSDictionary(about_panel_options_);
DictionaryValueToNSDictionary(about_panel_options_.GetDict());
// Credits must be a NSAttributedString instead of NSString // Credits must be a NSAttributedString instead of NSString
NSString* credits = (NSString*)options[@"Credits"]; NSString* credits = (NSString*)options[@"Credits"];
@ -537,13 +536,13 @@ void Browser::ShowAboutPanel() {
} }
void Browser::SetAboutPanelOptions(base::Value::Dict options) { void Browser::SetAboutPanelOptions(base::Value::Dict options) {
about_panel_options_.GetDict().clear(); about_panel_options_.clear();
for (const auto pair : options) { for (const auto pair : options) {
std::string key = pair.first; std::string key = pair.first;
if (!key.empty() && pair.second.is_string()) { if (!key.empty() && pair.second.is_string()) {
key[0] = base::ToUpperASCII(key[0]); key[0] = base::ToUpperASCII(key[0]);
about_panel_options_.GetDict().Set(key, pair.second.Clone()); about_panel_options_.Set(key, pair.second.Clone());
} }
} }
} }

View file

@ -734,30 +734,29 @@ void Browser::ShowEmojiPanel() {
} }
void Browser::ShowAboutPanel() { void Browser::ShowAboutPanel() {
base::Value dict(base::Value::Type::DICTIONARY); base::Value::Dict dict;
std::string aboutMessage = ""; std::string aboutMessage = "";
gfx::ImageSkia image; gfx::ImageSkia image;
// grab defaults from Windows .EXE file // grab defaults from Windows .EXE file
std::unique_ptr<FileVersionInfo> exe_info = FetchFileVersionInfo(); std::unique_ptr<FileVersionInfo> exe_info = FetchFileVersionInfo();
dict.SetStringKey("applicationName", exe_info->file_description()); dict.Set("applicationName", exe_info->file_description());
dict.SetStringKey("applicationVersion", exe_info->product_version()); dict.Set("applicationVersion", exe_info->product_version());
if (about_panel_options_.is_dict()) { // Merge user-provided options, overwriting any of the above
dict.MergeDictionary(&about_panel_options_); dict.Merge(about_panel_options_.Clone());
}
std::vector<std::string> stringOptions = { std::vector<std::string> stringOptions = {
"applicationName", "applicationVersion", "copyright", "credits"}; "applicationName", "applicationVersion", "copyright", "credits"};
const std::string* str; const std::string* str;
for (std::string opt : stringOptions) { for (std::string opt : stringOptions) {
if ((str = dict.FindStringKey(opt))) { if ((str = dict.FindString(opt))) {
aboutMessage.append(*str).append("\r\n"); aboutMessage.append(*str).append("\r\n");
} }
} }
if ((str = dict.FindStringKey("iconPath"))) { if ((str = dict.FindString("iconPath"))) {
base::FilePath path = base::FilePath::FromUTF8Unsafe(*str); base::FilePath path = base::FilePath::FromUTF8Unsafe(*str);
electron::util::PopulateImageSkiaRepsFromPath(&image, path); electron::util::PopulateImageSkiaRepsFromPath(&image, path);
} }
@ -770,7 +769,7 @@ void Browser::ShowAboutPanel() {
} }
void Browser::SetAboutPanelOptions(base::Value::Dict options) { void Browser::SetAboutPanelOptions(base::Value::Dict options) {
about_panel_options_ = base::Value(std::move(options)); about_panel_options_ = std::move(options);
} }
} // namespace electron } // namespace electron

View file

@ -1859,6 +1859,15 @@ describe('app module', () => {
})).to.eventually.be.rejectedWith(/ERR_NAME_NOT_RESOLVED/); })).to.eventually.be.rejectedWith(/ERR_NAME_NOT_RESOLVED/);
}); });
}); });
describe('about panel', () => {
it('app.setAboutPanelOptions() does not crash', () => {
app.setAboutPanelOptions({
applicationName: 'electron!!',
version: '1.2.3'
});
});
});
}); });
describe('default behavior', () => { describe('default behavior', () => {