Bug 1733461 - Add a webextension preference to override light or dark color scheme. r=mixedpuppy
authorCimbali <me@cimba.li>
Thu, 07 Oct 2021 19:04:10 +0000
changeset 595145 48501517af5e2f706db61827cf639c7731bd62e0
parent 595144 8cb1af57958919354f5ecfc8082a10fad17cb301
child 595146 fe6f7dd7fa1e3d64c561032531ad601599ab6c4e
push id151078
push userscaraveo@mozilla.com
push dateThu, 07 Oct 2021 19:06:35 +0000
treeherderautoland@48501517af5e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1733461
milestone95.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1733461 - Add a webextension preference to override light or dark color scheme. r=mixedpuppy Differential Revision: https://phabricator.services.mozilla.com/D127743
toolkit/components/extensions/parent/ext-browserSettings.js
toolkit/components/extensions/schemas/browser_settings.json
toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
--- a/toolkit/components/extensions/parent/ext-browserSettings.js
+++ b/toolkit/components/extensions/parent/ext-browserSettings.js
@@ -171,16 +171,25 @@ ExtensionPreferencesManager.addSetting("
   permission: "browserSettings",
   prefNames: ["browser.display.document_color_use"],
 
   setCallback(value) {
     return { [this.prefNames[0]]: value };
   },
 });
 
+ExtensionPreferencesManager.addSetting("overrideContentColorScheme", {
+  permission: "browserSettings",
+  prefNames: ["layout.css.prefers-color-scheme.content-override"],
+
+  setCallback(value) {
+    return { [this.prefNames[0]]: value };
+  },
+});
+
 ExtensionPreferencesManager.addSetting("useDocumentFonts", {
   permission: "browserSettings",
   prefNames: ["browser.display.use_document_fonts"],
 
   setCallback(value) {
     return { [this.prefNames[0]]: value };
   },
 });
@@ -430,16 +439,53 @@ this.browserSettings = class extends Ext
               return ExtensionPreferencesManager.setSetting(
                 extension.id,
                 "overrideDocumentColors",
                 prefValue
               );
             },
           }
         ),
+        overrideContentColorScheme: Object.assign(
+          getSettingsAPI({
+            context,
+            name: "overrideContentColorScheme",
+            callback() {
+              let prefValue = Services.prefs.getIntPref(
+                "layout.css.prefers-color-scheme.content-override"
+              );
+              if (prefValue === 0) {
+                return "dark";
+              } else if (prefValue === 1) {
+                return "light";
+              }
+              return "system";
+            },
+          }),
+          {
+            set: details => {
+              if (!["light", "dark", "system"].includes(details.value)) {
+                throw new ExtensionError(
+                  `${details.value} is not a valid value for overrideContentColorScheme.`
+                );
+              }
+              let prefValue = 2; // initialize to 2 - system colors
+              if (details.value === "light") {
+                prefValue = 1;
+              } else if (details.value === "dark") {
+                prefValue = 0;
+              }
+              return ExtensionPreferencesManager.setSetting(
+                extension.id,
+                "overrideContentColorScheme",
+                prefValue
+              );
+            },
+          }
+        ),
         useDocumentFonts: Object.assign(
           getSettingsAPI({
             context,
             name: "useDocumentFonts",
             callback() {
               return (
                 Services.prefs.getIntPref(
                   "browser.display.use_document_fonts"
--- a/toolkit/components/extensions/schemas/browser_settings.json
+++ b/toolkit/components/extensions/schemas/browser_settings.json
@@ -96,16 +96,20 @@
       "webNotificationsDisabled": {
         "$ref": "types.Setting",
         "description": "Disables webAPI notifications."
       },
       "overrideDocumentColors": {
         "$ref": "types.Setting",
         "description": "This setting controls whether the user-chosen colors override the page's colors."
       },
+      "overrideContentColorScheme": {
+        "$ref": "types.Setting",
+        "description": "This setting controls whether a light or dark color scheme overrides the page's preferred color scheme."
+      },
       "useDocumentFonts": {
         "$ref": "types.Setting",
         "description": "This setting controls whether the document's fonts are used."
       },
       "zoomFullPage": {
         "$ref": "types.Setting",
         "description": "This boolean setting controls whether zoom is applied to the full page or to text only."
       },
--- a/toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
@@ -43,16 +43,17 @@ add_task(async function test_browser_set
     "permissions.default.desktop-notification": PERM_UNKNOWN_ACTION,
     "ui.context_menus.after_mouseup": false,
     "browser.tabs.closeTabByDblclick": false,
     "browser.tabs.loadBookmarksInTabs": false,
     "browser.search.openintab": false,
     "browser.tabs.insertRelatedAfterCurrent": true,
     "browser.tabs.insertAfterCurrent": false,
     "browser.display.document_color_use": 1,
+    "layout.css.prefers-color-scheme.content-override": 2,
     "browser.display.use_document_fonts": 1,
     "browser.zoom.full": true,
     "browser.zoom.siteSpecific": true,
   };
 
   async function background() {
     let listeners = new Set([]);
     browser.test.onMessage.addListener(async (msg, apiName, value) => {
@@ -266,16 +267,26 @@ add_task(async function test_browser_set
   });
   await testSetting("overrideDocumentColors", "never", {
     "browser.display.document_color_use": 1,
   });
   await testSetting("overrideDocumentColors", "always", {
     "browser.display.document_color_use": 2,
   });
 
+  await testSetting("overrideContentColorScheme", "dark", {
+    "layout.css.prefers-color-scheme.content-override": 0,
+  });
+  await testSetting("overrideContentColorScheme", "light", {
+    "layout.css.prefers-color-scheme.content-override": 1,
+  });
+  await testSetting("overrideContentColorScheme", "system", {
+    "layout.css.prefers-color-scheme.content-override": 2,
+  });
+
   await testSetting("useDocumentFonts", false, {
     "browser.display.use_document_fonts": 0,
   });
   await testSetting("useDocumentFonts", true, {
     "browser.display.use_document_fonts": 1,
   });
 
   await testSetting("zoomFullPage", true, {
@@ -335,16 +346,28 @@ add_task(async function test_bad_value()
     );
 
     await browser.test.assertRejects(
       browser.browserSettings.overrideDocumentColors.set({ value: "bad" }),
       /bad is not a valid value for overrideDocumentColors/,
       "overrideDocumentColors.set rejects with an invalid value."
     );
 
+    await browser.test.assertRejects(
+      browser.browserSettings.overrideContentColorScheme.set({ value: 0 }),
+      /0 is not a valid value for overrideContentColorScheme/,
+      "overrideContentColorScheme.set rejects with an invalid value."
+    );
+
+    await browser.test.assertRejects(
+      browser.browserSettings.overrideContentColorScheme.set({ value: "bad" }),
+      /bad is not a valid value for overrideContentColorScheme/,
+      "overrideContentColorScheme.set rejects with an invalid value."
+    );
+
     browser.test.sendMessage("done");
   }
 
   let extension = ExtensionTestUtils.loadExtension({
     background,
     manifest: {
       permissions: ["browserSettings"],
     },