Skip to content

Commit

Permalink
browser(firefox): allow setting colorScheme on the context level
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Apr 6, 2020
1 parent 19019c9 commit ff36704
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 6 deletions.
13 changes: 13 additions & 0 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ nsDocShell::nsDocShell(BrowsingContext* aBrowsingContext,
mFileInputInterceptionEnabled(false),
mBypassCSPEnabled(false),
mOnlineOverride(nsIDocShell::ONLINE_OVERRIDE_NONE),
mColorSchemeOverride(COLOR_SCHEME_OVERRIDE_NONE),
mAllowAuth(mItemType == typeContent),
mAllowKeywordFixup(false),
mIsOffScreenBrowser(false),
Expand Down Expand Up @@ -3501,6 +3502,18 @@ nsDocShell::SetOnlineOverride(OnlineOverride aOnlineOverride) {
return NS_OK;
}

NS_IMETHODIMP
nsDocShell::GetColorSchemeOverride(ColorSchemeOverride* aColorSchemeOverride) {
*aColorSchemeOverride = GetRootDocShell()->mColorSchemeOverride;
return NS_OK;
}

NS_IMETHODIMP
nsDocShell::SetColorSchemeOverride(ColorSchemeOverride aColorSchemeOverride) {
mColorSchemeOverride = aColorSchemeOverride;
return NS_OK;
}

// =============== Juggler End =======================

NS_IMETHODIMP
Expand Down
1 change: 1 addition & 0 deletions docshell/base/nsDocShell.h
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,7 @@ class nsDocShell final : public nsDocLoader,
nsString mLanguageOverride;
RefPtr<nsGeolocationService> mGeolocationServiceOverride;
OnlineOverride mOnlineOverride;
ColorSchemeOverride mColorSchemeOverride;

bool mAllowAuth : 1;
bool mAllowKeywordFixup : 1;
Expand Down
8 changes: 8 additions & 0 deletions docshell/base/nsIDocShell.idl
Original file line number Diff line number Diff line change
Expand Up @@ -1146,5 +1146,13 @@ interface nsIDocShell : nsIDocShellTreeItem
};
[infallible] attribute nsIDocShell_OnlineOverride onlineOverride;

cenum ColorSchemeOverride : 8 {
COLOR_SCHEME_OVERRIDE_LIGHT,
COLOR_SCHEME_OVERRIDE_DARK,
COLOR_SCHEME_OVERRIDE_NO_PREFERENCE,
COLOR_SCHEME_OVERRIDE_NONE, /* This clears the override. */
};
[infallible] attribute nsIDocShell_ColorSchemeOverride colorSchemeOverride;

void setGeolocationOverride(in nsIDOMGeoPosition position);
};
14 changes: 14 additions & 0 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16122,6 +16122,20 @@ void Document::RemoveToplevelLoadingDocument(Document* aDoc) {
}

StylePrefersColorScheme Document::PrefersColorScheme() const {
auto* docShell = static_cast<nsDocShell*>(GetDocShell());
nsIDocShell::ColorSchemeOverride colorScheme;
if (docShell->GetColorSchemeOverride(&colorScheme) == NS_OK &&
colorScheme != nsIDocShell::COLOR_SCHEME_OVERRIDE_NONE) {
switch (colorScheme) {
case nsIDocShell::COLOR_SCHEME_OVERRIDE_LIGHT:
return StylePrefersColorScheme::Light;
case nsIDocShell::COLOR_SCHEME_OVERRIDE_DARK:
return StylePrefersColorScheme::Dark;
case nsIDocShell::COLOR_SCHEME_OVERRIDE_NO_PREFERENCE:
return StylePrefersColorScheme::NoPreference;
};
}

if (nsContentUtils::ShouldResistFingerprinting(this)) {
return StylePrefersColorScheme::Light;
}
Expand Down
9 changes: 9 additions & 0 deletions juggler/TargetRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ class PageTarget {
await this._channel.connect('').send('setOnlineOverride', override).catch(e => void e);
}

async setColorScheme(colorScheme) {
await this._channel.connect('').send('setColorScheme', colorScheme).catch(e => void e);
}

async hasFailedToOverrideTimezone() {
return await this._channel.connect('').send('hasFailedToOverrideTimezone').catch(e => true);
}
Expand Down Expand Up @@ -413,6 +417,11 @@ class BrowserContext {
await Promise.all(Array.from(this.pages).map(page => page.setOnlineOverride(override)));
}

async setColorScheme(colorScheme) {
this.options.colorScheme = colorScheme;
await Promise.all(Array.from(this.pages).map(page => page.setColorScheme(colorScheme)));
}

async grantPermissions(origin, permissions) {
this._permissions.set(origin, permissions);
const promises = [];
Expand Down
10 changes: 10 additions & 0 deletions juggler/content/FrameTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ class FrameTree {
frame._addBinding(name, script);
}

setColorScheme(colorScheme) {
const docShell = this._mainFrame._docShell;
switch (colorScheme) {
case 'light': docShell.colorSchemeOverride = Ci.nsIDocShell.COLOR_SCHEME_OVERRIDE_LIGHT; break;
case 'dark': docShell.colorSchemeOverride = Ci.nsIDocShell.COLOR_SCHEME_OVERRIDE_DARK; break;
case 'no-preference': docShell.colorSchemeOverride = Ci.nsIDocShell.COLOR_SCHEME_OVERRIDE_NO_PREFERENCE; break;
default: docShell.colorSchemeOverride = Ci.nsIDocShell.COLOR_SCHEME_OVERRIDE_NONE; break;
}
}

frameForDocShell(docShell) {
return this._docShellToFrame.get(docShell) || null;
}
Expand Down
6 changes: 1 addition & 5 deletions juggler/content/PageAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,7 @@ class PageAgent {
cv.stopEmulatingMedium();
else if (type)
cv.emulateMedium(type);
switch (colorScheme) {
case 'light': cv.emulatePrefersColorScheme(cv.PREFERS_COLOR_SCHEME_LIGHT); break;
case 'dark': cv.emulatePrefersColorScheme(cv.PREFERS_COLOR_SCHEME_DARK); break;
case 'no-preference': cv.emulatePrefersColorScheme(cv.PREFERS_COLOR_SCHEME_NO_PREFERENCE); break;
}
this._frameTree.setColorScheme(colorScheme);
}

_addScriptToEvaluateOnNewDocument({script, worldName}) {
Expand Down
8 changes: 7 additions & 1 deletion juggler/content/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function initialize() {
response = { sessionIds: [], browserContextOptions: {} };

const { sessionIds, browserContextOptions } = response;
const { userAgent, bypassCSP, javaScriptDisabled, viewport, scriptsToEvaluateOnNewDocument, bindings, locale, timezoneId, geolocation, onlineOverride } = browserContextOptions;
const { userAgent, bypassCSP, javaScriptDisabled, viewport, scriptsToEvaluateOnNewDocument, bindings, locale, timezoneId, geolocation, onlineOverride, colorScheme } = browserContextOptions;

let failedToOverrideTimezone = false;
if (timezoneId)
Expand All @@ -90,6 +90,8 @@ function initialize() {
}

frameTree = new FrameTree(docShell);
if (colorScheme !== undefined)
frameTree.setColorScheme(colorScheme);
for (const script of scriptsToEvaluateOnNewDocument || [])
frameTree.addScriptToEvaluateOnNewDocument(script);
for (const { name, script } of bindings || [])
Expand Down Expand Up @@ -126,6 +128,10 @@ function initialize() {
setOnlineOverrideInDocShell(override);
},

setColorScheme(colorScheme) {
frameTree.setColorScheme(colorScheme);
},

ensurePermissions() {
// noop, just a rountrip.
},
Expand Down
4 changes: 4 additions & 0 deletions juggler/protocol/BrowserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class BrowserHandler {
await this._targetRegistry.browserContextForId(browserContextId).setOnlineOverride(override);
}

async setColorScheme({browserContextId, colorScheme}) {
await this._targetRegistry.browserContextForId(browserContextId).setColorScheme(colorScheme);
}

async addScriptToEvaluateOnNewDocument({browserContextId, script}) {
await this._targetRegistry.browserContextForId(browserContextId).addScriptToEvaluateOnNewDocument(script);
}
Expand Down
6 changes: 6 additions & 0 deletions juggler/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ const Browser = {
override: t.Optional(t.Enum(['online', 'offline'])),
}
},
'setColorScheme': {
params: {
browserContextId: t.Optional(t.String),
colorScheme: t.Optional(t.Enum(['dark', 'light', 'no-preference'])),
},
},
},
};

Expand Down

0 comments on commit ff36704

Please sign in to comment.