Skip to content

Commit 11ad172

Browse files
authored
browser(firefox): allow setting colorScheme on the context level (#1672)
1 parent 5673fd7 commit 11ad172

File tree

2 files changed

+104
-30
lines changed

2 files changed

+104
-30
lines changed

browser_patches/firefox/BUILD_NUMBER

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1071
1+
1072

browser_patches/firefox/patches/bootstrap.diff

Lines changed: 103 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ index 040c7b124dec6bb254563bbe74fe50012cb077a3..b4e6b8132786af70e8ad0dce88b67c28
138138
const transportProvider = {
139139
setListener(upgradeListener) {
140140
diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp
141-
index 514a4f2890a20558afe0d9c1aec697612fc8e873..a013080177ee2c49342db2cda869b10a698482f4 100644
141+
index 514a4f2890a20558afe0d9c1aec697612fc8e873..8d16217833c15ee3611be913646c77fc440473e5 100644
142142
--- a/docshell/base/nsDocShell.cpp
143143
+++ b/docshell/base/nsDocShell.cpp
144144
@@ -15,6 +15,12 @@
@@ -186,25 +186,26 @@ index 514a4f2890a20558afe0d9c1aec697612fc8e873..a013080177ee2c49342db2cda869b10a
186186
#include "nsNetCID.h"
187187
#include "nsNetUtil.h"
188188
#include "nsObjectLoadingContent.h"
189-
@@ -350,6 +360,9 @@ nsDocShell::nsDocShell(BrowsingContext* aBrowsingContext,
189+
@@ -350,6 +360,10 @@ nsDocShell::nsDocShell(BrowsingContext* aBrowsingContext,
190190
mUseErrorPages(false),
191191
mObserveErrorPages(true),
192192
mCSSErrorReportingEnabled(false),
193193
+ mFileInputInterceptionEnabled(false),
194194
+ mBypassCSPEnabled(false),
195195
+ mOnlineOverride(nsIDocShell::ONLINE_OVERRIDE_NONE),
196+
+ mColorSchemeOverride(COLOR_SCHEME_OVERRIDE_NONE),
196197
mAllowAuth(mItemType == typeContent),
197198
mAllowKeywordFixup(false),
198199
mIsOffScreenBrowser(false),
199-
@@ -1219,6 +1232,7 @@ bool nsDocShell::SetCurrentURI(nsIURI* aURI, nsIRequest* aRequest,
200+
@@ -1219,6 +1233,7 @@ bool nsDocShell::SetCurrentURI(nsIURI* aURI, nsIRequest* aRequest,
200201
isSubFrame = mLSHE->GetIsSubFrame();
201202
}
202203

203204
+ FireOnFrameLocationChange(this, aRequest, aURI, aLocationFlags);
204205
if (!isSubFrame && !isRoot) {
205206
/*
206207
* We don't want to send OnLocationChange notifications when
207-
@@ -3340,6 +3354,155 @@ nsDocShell::GetMessageManager(ContentFrameMessageManager** aMessageManager) {
208+
@@ -3340,6 +3355,167 @@ nsDocShell::GetMessageManager(ContentFrameMessageManager** aMessageManager) {
208209
return NS_OK;
209210
}
210211

@@ -355,12 +356,24 @@ index 514a4f2890a20558afe0d9c1aec697612fc8e873..a013080177ee2c49342db2cda869b10a
355356
+ return NS_OK;
356357
+}
357358
+
359+
+NS_IMETHODIMP
360+
+nsDocShell::GetColorSchemeOverride(ColorSchemeOverride* aColorSchemeOverride) {
361+
+ *aColorSchemeOverride = GetRootDocShell()->mColorSchemeOverride;
362+
+ return NS_OK;
363+
+}
364+
+
365+
+NS_IMETHODIMP
366+
+nsDocShell::SetColorSchemeOverride(ColorSchemeOverride aColorSchemeOverride) {
367+
+ mColorSchemeOverride = aColorSchemeOverride;
368+
+ return NS_OK;
369+
+}
370+
+
358371
+// =============== Juggler End =======================
359372
+
360373
NS_IMETHODIMP
361374
nsDocShell::GetIsNavigating(bool* aOut) {
362375
*aOut = mIsNavigating;
363-
@@ -12137,6 +12300,9 @@ class OnLinkClickEvent : public Runnable {
376+
@@ -12137,6 +12313,9 @@ class OnLinkClickEvent : public Runnable {
364377
mNoOpenerImplied, nullptr, nullptr,
365378
mIsUserTriggered, mTriggeringPrincipal, mCsp);
366379
}
@@ -370,7 +383,7 @@ index 514a4f2890a20558afe0d9c1aec697612fc8e873..a013080177ee2c49342db2cda869b10a
370383
return NS_OK;
371384
}
372385

373-
@@ -12226,6 +12392,9 @@ nsresult nsDocShell::OnLinkClick(
386+
@@ -12226,6 +12405,9 @@ nsresult nsDocShell::OnLinkClick(
374387
this, aContent, aURI, target, aFileName, aPostDataStream,
375388
aHeadersDataStream, noOpenerImplied, aIsUserTriggered, aIsTrusted,
376389
aTriggeringPrincipal, aCsp);
@@ -381,7 +394,7 @@ index 514a4f2890a20558afe0d9c1aec697612fc8e873..a013080177ee2c49342db2cda869b10a
381394
}
382395

383396
diff --git a/docshell/base/nsDocShell.h b/docshell/base/nsDocShell.h
384-
index cc88045201371eb2195a28c60fcd3b6d940e8b72..7fad3529cc7a22b0b2aa8d8cb5ebbb5814aa2490 100644
397+
index cc88045201371eb2195a28c60fcd3b6d940e8b72..2ea1e4825849c3bf42d0ab2c06a73ce79363922a 100644
385398
--- a/docshell/base/nsDocShell.h
386399
+++ b/docshell/base/nsDocShell.h
387400
@@ -13,6 +13,7 @@
@@ -423,7 +436,7 @@ index cc88045201371eb2195a28c60fcd3b6d940e8b72..7fad3529cc7a22b0b2aa8d8cb5ebbb58
423436
// Handles retrieval of subframe session history for nsDocShell::LoadURI. If a
424437
// load is requested in a subframe of the current DocShell, the subframe
425438
// loadType may need to reflect the loadType of the parent document, or in
426-
@@ -1292,6 +1303,12 @@ class nsDocShell final : public nsDocLoader,
439+
@@ -1292,6 +1303,13 @@ class nsDocShell final : public nsDocLoader,
427440
bool mUseErrorPages : 1;
428441
bool mObserveErrorPages : 1;
429442
bool mCSSErrorReportingEnabled : 1;
@@ -432,12 +445,13 @@ index cc88045201371eb2195a28c60fcd3b6d940e8b72..7fad3529cc7a22b0b2aa8d8cb5ebbb58
432445
+ nsString mLanguageOverride;
433446
+ RefPtr<nsGeolocationService> mGeolocationServiceOverride;
434447
+ OnlineOverride mOnlineOverride;
448+
+ ColorSchemeOverride mColorSchemeOverride;
435449
+
436450
bool mAllowAuth : 1;
437451
bool mAllowKeywordFixup : 1;
438452
bool mIsOffScreenBrowser : 1;
439453
diff --git a/docshell/base/nsIDocShell.idl b/docshell/base/nsIDocShell.idl
440-
index ee89208c3ada6da09ecda6147e1a372ee0562810..ce8d31365b5190ac2c974100a6ec6408c53681d5 100644
454+
index ee89208c3ada6da09ecda6147e1a372ee0562810..1d3156f00f193bd9c1173a1326a1cb140efcf7b5 100644
441455
--- a/docshell/base/nsIDocShell.idl
442456
+++ b/docshell/base/nsIDocShell.idl
443457
@@ -44,6 +44,7 @@ interface nsIURI;
@@ -448,7 +462,7 @@ index ee89208c3ada6da09ecda6147e1a372ee0562810..ce8d31365b5190ac2c974100a6ec6408
448462
interface nsIDocShellLoadInfo;
449463
interface nsIEditor;
450464
interface nsIEditingSession;
451-
@@ -1129,4 +1130,21 @@ interface nsIDocShell : nsIDocShellTreeItem
465+
@@ -1129,4 +1130,29 @@ interface nsIDocShell : nsIDocShellTreeItem
452466
* @see nsISHEntry synchronizeLayoutHistoryState().
453467
*/
454468
void synchronizeLayoutHistoryState();
@@ -468,10 +482,18 @@ index ee89208c3ada6da09ecda6147e1a372ee0562810..ce8d31365b5190ac2c974100a6ec6408
468482
+ };
469483
+ [infallible] attribute nsIDocShell_OnlineOverride onlineOverride;
470484
+
485+
+ cenum ColorSchemeOverride : 8 {
486+
+ COLOR_SCHEME_OVERRIDE_LIGHT,
487+
+ COLOR_SCHEME_OVERRIDE_DARK,
488+
+ COLOR_SCHEME_OVERRIDE_NO_PREFERENCE,
489+
+ COLOR_SCHEME_OVERRIDE_NONE, /* This clears the override. */
490+
+ };
491+
+ [infallible] attribute nsIDocShell_ColorSchemeOverride colorSchemeOverride;
492+
+
471493
+ void setGeolocationOverride(in nsIDOMGeoPosition position);
472494
};
473495
diff --git a/dom/base/Document.cpp b/dom/base/Document.cpp
474-
index bc0aeaefb9ca6d7cd15fc5ad189d2d260d519997..e948a5c20308ad8a9f43b3a53c532ee0cf62950c 100644
496+
index bc0aeaefb9ca6d7cd15fc5ad189d2d260d519997..c5f649f2b0508028955be4fa9562cfc1370b0a00 100644
475497
--- a/dom/base/Document.cpp
476498
+++ b/dom/base/Document.cpp
477499
@@ -3269,6 +3269,9 @@ void Document::SendToConsole(nsCOMArray<nsISecurityConsoleMessage>& aMessages) {
@@ -496,6 +518,27 @@ index bc0aeaefb9ca6d7cd15fc5ad189d2d260d519997..e948a5c20308ad8a9f43b3a53c532ee0
496518
// If this is a data document - no need to set CSP.
497519
if (mLoadedAsData) {
498520
return NS_OK;
521+
@@ -16114,6 +16122,20 @@ void Document::RemoveToplevelLoadingDocument(Document* aDoc) {
522+
}
523+
524+
StylePrefersColorScheme Document::PrefersColorScheme() const {
525+
+ auto* docShell = static_cast<nsDocShell*>(GetDocShell());
526+
+ nsIDocShell::ColorSchemeOverride colorScheme;
527+
+ if (docShell->GetColorSchemeOverride(&colorScheme) == NS_OK &&
528+
+ colorScheme != nsIDocShell::COLOR_SCHEME_OVERRIDE_NONE) {
529+
+ switch (colorScheme) {
530+
+ case nsIDocShell::COLOR_SCHEME_OVERRIDE_LIGHT:
531+
+ return StylePrefersColorScheme::Light;
532+
+ case nsIDocShell::COLOR_SCHEME_OVERRIDE_DARK:
533+
+ return StylePrefersColorScheme::Dark;
534+
+ case nsIDocShell::COLOR_SCHEME_OVERRIDE_NO_PREFERENCE:
535+
+ return StylePrefersColorScheme::NoPreference;
536+
+ };
537+
+ }
538+
+
539+
if (nsContentUtils::ShouldResistFingerprinting(this)) {
540+
return StylePrefersColorScheme::Light;
541+
}
499542
diff --git a/dom/base/Navigator.cpp b/dom/base/Navigator.cpp
500543
index 1388a20edd594c6799e47ed567edb1f7d9cc9224..549026512c86e1e7e81a200d1c45f2cb58b3fc67 100644
501544
--- a/dom/base/Navigator.cpp
@@ -2123,10 +2166,10 @@ index 0000000000000000000000000000000000000000..ba34976ad05e7f5f1a99777f76ac08b1
21232166
+this.SimpleChannel = SimpleChannel;
21242167
diff --git a/juggler/TargetRegistry.js b/juggler/TargetRegistry.js
21252168
new file mode 100644
2126-
index 0000000000000000000000000000000000000000..836b5c537aa322663de78cd35b64f215c4c91abc
2169+
index 0000000000000000000000000000000000000000..345aa8b0ebbd9d1e8c9c79913bd7ec3fbb1cc768
21272170
--- /dev/null
21282171
+++ b/juggler/TargetRegistry.js
2129-
@@ -0,0 +1,552 @@
2172+
@@ -0,0 +1,561 @@
21302173
+const {EventEmitter} = ChromeUtils.import('resource://gre/modules/EventEmitter.jsm');
21312174
+const {Helper} = ChromeUtils.import('chrome://juggler/content/Helper.js');
21322175
+const {SimpleChannel} = ChromeUtils.import('chrome://juggler/content/SimpleChannel.js');
@@ -2456,6 +2499,10 @@ index 0000000000000000000000000000000000000000..836b5c537aa322663de78cd35b64f215
24562499
+ await this._channel.connect('').send('setOnlineOverride', override).catch(e => void e);
24572500
+ }
24582501
+
2502+
+ async setColorScheme(colorScheme) {
2503+
+ await this._channel.connect('').send('setColorScheme', colorScheme).catch(e => void e);
2504+
+ }
2505+
+
24592506
+ async hasFailedToOverrideTimezone() {
24602507
+ return await this._channel.connect('').send('hasFailedToOverrideTimezone').catch(e => true);
24612508
+ }
@@ -2542,6 +2589,11 @@ index 0000000000000000000000000000000000000000..836b5c537aa322663de78cd35b64f215
25422589
+ await Promise.all(Array.from(this.pages).map(page => page.setOnlineOverride(override)));
25432590
+ }
25442591
+
2592+
+ async setColorScheme(colorScheme) {
2593+
+ this.options.colorScheme = colorScheme;
2594+
+ await Promise.all(Array.from(this.pages).map(page => page.setColorScheme(colorScheme)));
2595+
+ }
2596+
+
25452597
+ async grantPermissions(origin, permissions) {
25462598
+ this._permissions.set(origin, permissions);
25472599
+ const promises = [];
@@ -2796,10 +2848,10 @@ index 0000000000000000000000000000000000000000..268fbc361d8053182bb6c27f626e853d
27962848
+
27972849
diff --git a/juggler/content/FrameTree.js b/juggler/content/FrameTree.js
27982850
new file mode 100644
2799-
index 0000000000000000000000000000000000000000..255d1a842e9646eccc7c7bf8902baf94ef094c0e
2851+
index 0000000000000000000000000000000000000000..5a1df2837d70531a670163b7c860108895bc9106
28002852
--- /dev/null
28012853
+++ b/juggler/content/FrameTree.js
2802-
@@ -0,0 +1,452 @@
2854+
@@ -0,0 +1,462 @@
28032855
+"use strict";
28042856
+const Ci = Components.interfaces;
28052857
+const Cr = Components.results;
@@ -2936,6 +2988,16 @@ index 0000000000000000000000000000000000000000..255d1a842e9646eccc7c7bf8902baf94
29362988
+ frame._addBinding(name, script);
29372989
+ }
29382990
+
2991+
+ setColorScheme(colorScheme) {
2992+
+ const docShell = this._mainFrame._docShell;
2993+
+ switch (colorScheme) {
2994+
+ case 'light': docShell.colorSchemeOverride = Ci.nsIDocShell.COLOR_SCHEME_OVERRIDE_LIGHT; break;
2995+
+ case 'dark': docShell.colorSchemeOverride = Ci.nsIDocShell.COLOR_SCHEME_OVERRIDE_DARK; break;
2996+
+ case 'no-preference': docShell.colorSchemeOverride = Ci.nsIDocShell.COLOR_SCHEME_OVERRIDE_NO_PREFERENCE; break;
2997+
+ default: docShell.colorSchemeOverride = Ci.nsIDocShell.COLOR_SCHEME_OVERRIDE_NONE; break;
2998+
+ }
2999+
+ }
3000+
+
29393001
+ frameForDocShell(docShell) {
29403002
+ return this._docShellToFrame.get(docShell) || null;
29413003
+ }
@@ -3322,10 +3384,10 @@ index 0000000000000000000000000000000000000000..be70ea364f9534bb3b344f64970366c3
33223384
+
33233385
diff --git a/juggler/content/PageAgent.js b/juggler/content/PageAgent.js
33243386
new file mode 100644
3325-
index 0000000000000000000000000000000000000000..643fcfeb7d1084c2a5eb99031d0f626ebee725f9
3387+
index 0000000000000000000000000000000000000000..7f1958121f24c5f5360f24d7580ebf1132f91e83
33263388
--- /dev/null
33273389
+++ b/juggler/content/PageAgent.js
3328-
@@ -0,0 +1,936 @@
3390+
@@ -0,0 +1,932 @@
33293391
+"use strict";
33303392
+const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
33313393
+const Ci = Components.interfaces;
@@ -3512,11 +3574,7 @@ index 0000000000000000000000000000000000000000..643fcfeb7d1084c2a5eb99031d0f626e
35123574
+ cv.stopEmulatingMedium();
35133575
+ else if (type)
35143576
+ cv.emulateMedium(type);
3515-
+ switch (colorScheme) {
3516-
+ case 'light': cv.emulatePrefersColorScheme(cv.PREFERS_COLOR_SCHEME_LIGHT); break;
3517-
+ case 'dark': cv.emulatePrefersColorScheme(cv.PREFERS_COLOR_SCHEME_DARK); break;
3518-
+ case 'no-preference': cv.emulatePrefersColorScheme(cv.PREFERS_COLOR_SCHEME_NO_PREFERENCE); break;
3519-
+ }
3577+
+ this._frameTree.setColorScheme(colorScheme);
35203578
+ }
35213579
+
35223580
+ _addScriptToEvaluateOnNewDocument({script, worldName}) {
@@ -5059,10 +5117,10 @@ index 0000000000000000000000000000000000000000..3a386425d3796d0a6786dea193b3402d
50595117
+
50605118
diff --git a/juggler/content/main.js b/juggler/content/main.js
50615119
new file mode 100644
5062-
index 0000000000000000000000000000000000000000..0060f625a8ad10d7f0df121bdc5fcfa8d5d7b336
5120+
index 0000000000000000000000000000000000000000..40eb01dfb672fdb3e31d03fef83ae3210698b17c
50635121
--- /dev/null
50645122
+++ b/juggler/content/main.js
5065-
@@ -0,0 +1,156 @@
5123+
@@ -0,0 +1,162 @@
50665124
+const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
50675125
+const {Helper} = ChromeUtils.import('chrome://juggler/content/Helper.js');
50685126
+const {FrameTree} = ChromeUtils.import('chrome://juggler/content/content/FrameTree.js');
@@ -5130,7 +5188,7 @@ index 0000000000000000000000000000000000000000..0060f625a8ad10d7f0df121bdc5fcfa8
51305188
+ response = { sessionIds: [], browserContextOptions: {} };
51315189
+
51325190
+ const { sessionIds, browserContextOptions } = response;
5133-
+ const { userAgent, bypassCSP, javaScriptDisabled, viewport, scriptsToEvaluateOnNewDocument, bindings, locale, timezoneId, geolocation, onlineOverride } = browserContextOptions;
5191+
+ const { userAgent, bypassCSP, javaScriptDisabled, viewport, scriptsToEvaluateOnNewDocument, bindings, locale, timezoneId, geolocation, onlineOverride, colorScheme } = browserContextOptions;
51345192
+
51355193
+ let failedToOverrideTimezone = false;
51365194
+ if (timezoneId)
@@ -5155,6 +5213,8 @@ index 0000000000000000000000000000000000000000..0060f625a8ad10d7f0df121bdc5fcfa8
51555213
+ }
51565214
+
51575215
+ frameTree = new FrameTree(docShell);
5216+
+ if (colorScheme !== undefined)
5217+
+ frameTree.setColorScheme(colorScheme);
51585218
+ for (const script of scriptsToEvaluateOnNewDocument || [])
51595219
+ frameTree.addScriptToEvaluateOnNewDocument(script);
51605220
+ for (const { name, script } of bindings || [])
@@ -5191,6 +5251,10 @@ index 0000000000000000000000000000000000000000..0060f625a8ad10d7f0df121bdc5fcfa8
51915251
+ setOnlineOverrideInDocShell(override);
51925252
+ },
51935253
+
5254+
+ setColorScheme(colorScheme) {
5255+
+ frameTree.setColorScheme(colorScheme);
5256+
+ },
5257+
+
51945258
+ ensurePermissions() {
51955259
+ // noop, just a rountrip.
51965260
+ },
@@ -5298,10 +5362,10 @@ index 0000000000000000000000000000000000000000..bf37558bccc48f4d90eadc971c1eb3e4
52985362
+this.AccessibilityHandler = AccessibilityHandler;
52995363
diff --git a/juggler/protocol/BrowserHandler.js b/juggler/protocol/BrowserHandler.js
53005364
new file mode 100644
5301-
index 0000000000000000000000000000000000000000..da90d080ac091afa6c75cfc993d8850231e0d41a
5365+
index 0000000000000000000000000000000000000000..97e88dd582090971d122064b8a131096a317b6be
53025366
--- /dev/null
53035367
+++ b/juggler/protocol/BrowserHandler.js
5304-
@@ -0,0 +1,185 @@
5368+
@@ -0,0 +1,189 @@
53055369
+"use strict";
53065370
+
53075371
+const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
@@ -5453,6 +5517,10 @@ index 0000000000000000000000000000000000000000..da90d080ac091afa6c75cfc993d88502
54535517
+ await this._targetRegistry.browserContextForId(browserContextId).setOnlineOverride(override);
54545518
+ }
54555519
+
5520+
+ async setColorScheme({browserContextId, colorScheme}) {
5521+
+ await this._targetRegistry.browserContextForId(browserContextId).setColorScheme(colorScheme);
5522+
+ }
5523+
+
54565524
+ async addScriptToEvaluateOnNewDocument({browserContextId, script}) {
54575525
+ await this._targetRegistry.browserContextForId(browserContextId).addScriptToEvaluateOnNewDocument(script);
54585526
+ }
@@ -6293,10 +6361,10 @@ index 0000000000000000000000000000000000000000..78b6601b91d0b7fcda61114e6846aa07
62936361
+this.EXPORTED_SYMBOLS = ['t', 'checkScheme'];
62946362
diff --git a/juggler/protocol/Protocol.js b/juggler/protocol/Protocol.js
62956363
new file mode 100644
6296-
index 0000000000000000000000000000000000000000..e5482da00da11c88b278164b148f44c724cb0c89
6364+
index 0000000000000000000000000000000000000000..6e187212618130bc716a0fd0121ed0dd23d35770
62976365
--- /dev/null
62986366
+++ b/juggler/protocol/Protocol.js
6299-
@@ -0,0 +1,775 @@
6367+
@@ -0,0 +1,781 @@
63006368
+const {t, checkScheme} = ChromeUtils.import('chrome://juggler/content/protocol/PrimitiveTypes.js');
63016369
+
63026370
+// Protocol-specific types.
@@ -6613,6 +6681,12 @@ index 0000000000000000000000000000000000000000..e5482da00da11c88b278164b148f44c7
66136681
+ override: t.Optional(t.Enum(['online', 'offline'])),
66146682
+ }
66156683
+ },
6684+
+ 'setColorScheme': {
6685+
+ params: {
6686+
+ browserContextId: t.Optional(t.String),
6687+
+ colorScheme: t.Optional(t.Enum(['dark', 'light', 'no-preference'])),
6688+
+ },
6689+
+ },
66166690
+ },
66176691
+};
66186692
+

0 commit comments

Comments
 (0)