From 8dfe4abd14746d2a3278f2a65f6deaf81c6d636d Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Sun, 14 Mar 2021 17:26:47 -0700 Subject: [PATCH] fix: handle a nil backgroundColor in win.getBackgroundColor() (#28120) * fix: handle a nil backgroundColor in win.getBackgroundColor() * spec: add crash case * fix: update to fix native_views transparent color * chore: fix lint --- shell/browser/native_window_mac.mm | 6 ++++-- shell/browser/native_window_views.cc | 5 ++++- .../index.js | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 spec-main/fixtures/crash-cases/transparent-window-get-background-color/index.js diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index ea13ec7978a..21fff3e7247 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -996,8 +996,10 @@ void NativeWindowMac::SetBackgroundColor(SkColor color) { } SkColor NativeWindowMac::GetBackgroundColor() { - return skia::CGColorRefToSkColor( - [[[window_ contentView] layer] backgroundColor]); + CGColorRef color = [[[window_ contentView] layer] backgroundColor]; + if (!color) + return SK_ColorTRANSPARENT; + return skia::CGColorRefToSkColor(color); } void NativeWindowMac::SetHasShadow(bool has_shadow) { diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index 646a0368ed5..480445142b3 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -956,7 +956,10 @@ bool NativeWindowViews::IsTabletMode() const { } SkColor NativeWindowViews::GetBackgroundColor() { - return root_view_->background()->get_color(); + auto* background = root_view_->background(); + if (!background) + return SK_ColorTRANSPARENT; + return background->get_color(); } void NativeWindowViews::SetBackgroundColor(SkColor background_color) { diff --git a/spec-main/fixtures/crash-cases/transparent-window-get-background-color/index.js b/spec-main/fixtures/crash-cases/transparent-window-get-background-color/index.js new file mode 100644 index 00000000000..5742ffa171e --- /dev/null +++ b/spec-main/fixtures/crash-cases/transparent-window-get-background-color/index.js @@ -0,0 +1,14 @@ +const { app, BrowserWindow } = require('electron'); + +function createWindow () { + // Create the browser window. + const mainWindow = new BrowserWindow({ + transparent: true + }); + mainWindow.getBackgroundColor(); +} + +app.on('ready', () => { + createWindow(); + setTimeout(() => app.quit()); +});