From 47427950b9d53895a2867566c44ed5d33a6fa565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 8 Aug 2023 11:32:03 +0000 Subject: [PATCH] Bug 1846500 - Inherit color scheme override from opener. r=pbz Differential Revision: https://phabricator.services.mozilla.com/D185166 --- browser/base/content/pageinfo/pageInfo.js | 14 ----------- docshell/base/BrowsingContext.cpp | 23 +++++++++++++++++++ docshell/base/BrowsingContext.h | 8 +------ .../prompts/content/commonDialog.js | 14 ----------- .../profile/content/createProfileWizard.js | 12 ---------- 5 files changed, 24 insertions(+), 47 deletions(-) diff --git a/browser/base/content/pageinfo/pageInfo.js b/browser/base/content/pageinfo/pageInfo.js index 7d082f8ef2318..732aca9e99d65 100644 --- a/browser/base/content/pageinfo/pageInfo.js +++ b/browser/base/content/pageinfo/pageInfo.js @@ -13,20 +13,6 @@ ChromeUtils.defineESModuleGetters(this, { E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs", }); -// Inherit color scheme overrides from parent window. This is to inherit the -// color scheme of dark themed PBM windows. -{ - let openerColorSchemeOverride = - window.opener?.browsingContext?.top.prefersColorSchemeOverride; - if ( - openerColorSchemeOverride && - window.browsingContext == window.browsingContext.top - ) { - window.browsingContext.prefersColorSchemeOverride = - openerColorSchemeOverride; - } -} - // define a js object to implement nsITreeView function pageInfoTreeView(treeid, copycol) { // copycol is the index number for the column that we want to add to diff --git a/docshell/base/BrowsingContext.cpp b/docshell/base/BrowsingContext.cpp index 3b0a320ab1eb5..0d3e064f81643 100644 --- a/docshell/base/BrowsingContext.cpp +++ b/docshell/base/BrowsingContext.cpp @@ -365,6 +365,12 @@ already_AddRefed BrowsingContext::CreateDetached( MOZ_DIAGNOSTIC_ASSERT(aOpener->mType == aType); fields.Get() = aOpener->Id(); fields.Get() = true; + + if (aType == Type::Chrome && !aParent) { + // See SetOpener for why we do this inheritance. + fields.Get() = + aOpener->Top()->GetPrefersColorSchemeOverride(); + } } if (aParent) { @@ -1076,6 +1082,23 @@ bool BrowsingContext::IsTargetable() const { return !GetClosed() && AncestorsAreCurrent(); } +void BrowsingContext::SetOpener(BrowsingContext* aOpener) { + MOZ_DIAGNOSTIC_ASSERT(!aOpener || aOpener->Group() == Group()); + MOZ_DIAGNOSTIC_ASSERT(!aOpener || aOpener->mType == mType); + + MOZ_ALWAYS_SUCCEEDS(SetOpenerId(aOpener ? aOpener->Id() : 0)); + + if (IsChrome() && IsTop() && aOpener) { + // Inherit color scheme overrides from parent window. This is to inherit the + // color scheme of dark themed PBM windows in dialogs opened by such + // windows. + auto openerOverride = aOpener->Top()->PrefersColorSchemeOverride(); + if (openerOverride != PrefersColorSchemeOverride()) { + MOZ_ALWAYS_SUCCEEDS(SetPrefersColorSchemeOverride(openerOverride)); + } + } +} + bool BrowsingContext::HasOpener() const { return sBrowsingContexts->Contains(GetOpenerId()); } diff --git a/docshell/base/BrowsingContext.h b/docshell/base/BrowsingContext.h index 286677944fa24..3c8a11e5dcbb3 100644 --- a/docshell/base/BrowsingContext.h +++ b/docshell/base/BrowsingContext.h @@ -486,13 +486,7 @@ class BrowsingContext : public nsILoadContext, public nsWrapperCache { } return nullptr; } - void SetOpener(BrowsingContext* aOpener) { - MOZ_DIAGNOSTIC_ASSERT(!aOpener || aOpener->Group() == Group()); - MOZ_DIAGNOSTIC_ASSERT(!aOpener || aOpener->mType == mType); - - MOZ_ALWAYS_SUCCEEDS(SetOpenerId(aOpener ? aOpener->Id() : 0)); - } - + void SetOpener(BrowsingContext* aOpener); bool HasOpener() const; bool HadOriginalOpener() const { return GetHadOriginalOpener(); } diff --git a/toolkit/components/prompts/content/commonDialog.js b/toolkit/components/prompts/content/commonDialog.js index a18b7cbd6cf57..e2edd35679bd7 100644 --- a/toolkit/components/prompts/content/commonDialog.js +++ b/toolkit/components/prompts/content/commonDialog.js @@ -11,20 +11,6 @@ const { CommonDialog } = ChromeUtils.importESModule( var propBag, args, Dialog; -// Inherit color scheme overrides from parent window. This is to inherit the -// color scheme of dark themed PBM windows. -{ - let openerColorSchemeOverride = - window.opener?.browsingContext?.top.prefersColorSchemeOverride; - if ( - openerColorSchemeOverride && - window.browsingContext == window.browsingContext.top - ) { - window.browsingContext.prefersColorSchemeOverride = - openerColorSchemeOverride; - } -} - function commonDialogOnLoad() { propBag = window.arguments[0] .QueryInterface(Ci.nsIWritablePropertyBag2) diff --git a/toolkit/profile/content/createProfileWizard.js b/toolkit/profile/content/createProfileWizard.js index 57f8f3338af27..9e87fb42207d6 100644 --- a/toolkit/profile/content/createProfileWizard.js +++ b/toolkit/profile/content/createProfileWizard.js @@ -24,18 +24,6 @@ var gProfileDisplay; // Called once when the wizard is opened. function initWizard() { - // Inherit color scheme overrides from parent window. This is to inherit the - // color scheme of dark themed PBM windows. - let openerColorSchemeOverride = - window.opener?.browsingContext?.top.prefersColorSchemeOverride; - if ( - openerColorSchemeOverride && - window.browsingContext == window.browsingContext.top - ) { - window.browsingContext.prefersColorSchemeOverride = - openerColorSchemeOverride; - } - try { gProfileService = C[ToolkitProfileService].getService( I.nsIToolkitProfileService