diff --git a/lib/browser/api/menu-item.js b/lib/browser/api/menu-item.js index 2579e6cfd4df..171f2ead068e 100644 --- a/lib/browser/api/menu-item.js +++ b/lib/browser/api/menu-item.js @@ -16,7 +16,7 @@ const MenuItem = function (options) { } this.submenu = this.submenu || roles.getDefaultSubmenu(this.role); if (this.submenu != null && this.submenu.constructor !== Menu) { - this.submenu = Menu.buildFromTemplate(this.submenu, true); + this.submenu = Menu.buildFromTemplate(this.submenu); } if (this.type == null && this.submenu != null) { this.type = 'submenu'; diff --git a/lib/browser/api/menu.js b/lib/browser/api/menu.js index 1ddc02d80d86..c0db47e77b6b 100644 --- a/lib/browser/api/menu.js +++ b/lib/browser/api/menu.js @@ -173,11 +173,9 @@ Menu.setApplicationMenu = function (menu) { } }; -Menu.buildFromTemplate = function (template, isSubmenu = false) { +Menu.buildFromTemplate = function (template) { if (!Array.isArray(template)) { throw new TypeError('Invalid template for Menu: Menu template must be an array'); - } else if (!isSubmenu && template.length === 0) { - throw new TypeError('Invalid template for Menu: Menu template must be an non-empty array'); } if (!areValidTemplateItems(template)) { diff --git a/shell/browser/api/electron_api_top_level_window.cc b/shell/browser/api/electron_api_top_level_window.cc index 42bd20991162..9046c4ebea16 100644 --- a/shell/browser/api/electron_api_top_level_window.cc +++ b/shell/browser/api/electron_api_top_level_window.cc @@ -689,10 +689,16 @@ void TopLevelWindow::SetMenu(v8::Isolate* isolate, v8::Local value) { if (value->IsObject() && value->ToObject(context).ToLocal(&object) && gin::ConvertFromV8(isolate, value, &menu) && !menu.IsEmpty()) { menu_.Reset(isolate, menu.ToV8()); - window_->SetMenu(menu->model()); + + // We only want to update the menu if the menu has a non-zero item count, + // or we risk crashes. + if (menu->model()->GetItemCount() == 0) { + RemoveMenu(); + } else { + window_->SetMenu(menu->model()); + } } else if (value->IsNull()) { - menu_.Reset(); - window_->SetMenu(nullptr); + RemoveMenu(); } else { isolate->ThrowException( v8::Exception::TypeError(gin::StringToV8(isolate, "Invalid Menu"))); diff --git a/spec-main/api-menu-spec.ts b/spec-main/api-menu-spec.ts index e6e1a0ea5530..fcdc99b084d3 100644 --- a/spec-main/api-menu-spec.ts +++ b/spec-main/api-menu-spec.ts @@ -93,12 +93,6 @@ describe('Menu module', function () { }).to.throw(/Invalid template for MenuItem: must have at least one of label, role or type/); }); - it('throws when an empty array is passed as a template', () => { - expect(() => { - Menu.buildFromTemplate([]); - }).to.throw(/Invalid template for Menu: Menu template must be an non-empty array/); - }); - it('throws when an non-array is passed as a template', () => { expect(() => { Menu.buildFromTemplate('hello' as any);