Bug 1417810 - Added API to override document colors r?ntim draft
authorSatish <psatish@live.in>
Tue, 27 Mar 2018 12:01:44 +0530
changeset 774625 dec291fcab76188abe96116475eab171d4878b14
parent 774444 6aa3b57955fed5e137d0306478e1a4b424a6d392
push id104454
push userbmo:psatish@live.in
push dateThu, 29 Mar 2018 07:53:19 +0000
reviewersntim
bugs1417810
milestone61.0a1
Bug 1417810 - Added API to override document colors r?ntim Changed setting name to 'overrideDocumentColors' MozReview-Commit-ID: LfT56MLlQo9
toolkit/components/extensions/ext-browserSettings.js
toolkit/components/extensions/schemas/browser_settings.json
toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
--- a/toolkit/components/extensions/ext-browserSettings.js
+++ b/toolkit/components/extensions/ext-browserSettings.js
@@ -224,16 +224,26 @@ ExtensionPreferencesManager.addSetting("
     "permissions.default.desktop-notification",
   ],
 
   setCallback(value) {
     return {[this.prefNames[0]]: value ? PERM_DENY_ACTION : undefined};
   },
 });
 
+ExtensionPreferencesManager.addSetting("overrideDocumentColors", {
+  prefNames: [
+    "browser.display.document_color_use",
+  ],
+
+  setCallback(value) {
+    return {[this.prefNames[0]]: value};
+  },
+});
+
 this.browserSettings = class extends ExtensionAPI {
   getAPI(context) {
     let {extension} = context;
     return {
       browserSettings: {
         allowPopupsForUserEvents: getSettingsAPI(
           extension, "allowPopupsForUserEvents",
           () => {
@@ -403,12 +413,42 @@ this.browserSettings = class extends Ext
         webNotificationsDisabled: getSettingsAPI(
           extension, "webNotificationsDisabled",
           () => {
             let prefValue =
               Services.prefs.getIntPref(
                 "permissions.default.desktop-notification", null);
             return prefValue === PERM_DENY_ACTION;
           }),
+        overrideDocumentColors: Object.assign(
+          getSettingsAPI(
+            extension, "overrideDocumentColors",
+            () => {
+              let prefValue = Services.prefs.getIntPref("browser.display.document_color_use");
+              if (prefValue === 1) {
+                return "never";
+              } else if (prefValue === 2) {
+                return "always";
+              }
+              return "high-contrast-only";
+            }
+          ),
+          {
+            set: details => {
+              if (!["never", "always", "high-contrast-only"].includes(details.value)) {
+                throw new ExtensionError(
+                  `${details.value} is not a valid value for overrideDocumentColors.`);
+              }
+              let prefValue = 0; // initialize to 0 - auto/high-contrast-only
+              if (details.value === "never") {
+                prefValue = 1;
+              } else if (details.value === "always") {
+                prefValue = 2;
+              }
+              return ExtensionPreferencesManager.setSetting(
+                extension.id, "overrideDocumentColors", prefValue);
+            },
+          }
+        ),
       },
     };
   }
 };
--- a/toolkit/components/extensions/schemas/browser_settings.json
+++ b/toolkit/components/extensions/schemas/browser_settings.json
@@ -151,12 +151,16 @@
       },
       "proxyConfig": {
         "$ref": "types.Setting",
         "description": "Configures proxy settings. This setting's value is an object of type ProxyConfig."
       },
       "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."
       }
     }
   }
 ]
--- a/toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
@@ -45,16 +45,17 @@ add_task(async function test_browser_set
     "network.proxy.socks_port": 0,
     "network.proxy.socks_version": 5,
     "network.proxy.socks_remote_dns": false,
     "network.proxy.no_proxies_on": "localhost, 127.0.0.1",
     "network.proxy.autoconfig_url": "",
     "signon.autologin.proxy": false,
     "browser.tabs.insertRelatedAfterCurrent": true,
     "browser.tabs.insertAfterCurrent": false,
+    "browser.display.document_color_use": 1,
   };
 
   async function background() {
     browser.test.onMessage.addListener(async (msg, apiName, value) => {
       let apiObj = browser.browserSettings[apiName];
       let result = await apiObj.set({value});
       if (msg === "set") {
         browser.test.assertTrue(result, "set returns true.");
@@ -204,16 +205,26 @@ add_task(async function test_browser_set
 
   await testSetting(
     "openSearchResultsInNewTabs", true,
     {"browser.search.openintab": true});
   await testSetting(
     "openSearchResultsInNewTabs", false,
     {"browser.search.openintab": false});
 
+  await testSetting(
+    "overrideDocumentColors", "high-contrast-only",
+    {"browser.display.document_color_use": 0});
+  await testSetting(
+    "overrideDocumentColors", "never",
+    {"browser.display.document_color_use": 1});
+  await testSetting(
+    "overrideDocumentColors", "always",
+    {"browser.display.document_color_use": 2});
+
   async function testProxy(config, expectedPrefs) {
     // proxyConfig is not supported on Android.
     if (AppConstants.platform === "android") {
       return Promise.resolve();
     }
 
     let proxyConfig = {
       proxyType: "system",
@@ -362,16 +373,26 @@ add_task(async function test_browser_set
 
 add_task(async function test_bad_value() {
   async function background() {
     await browser.test.assertRejects(
       browser.browserSettings.contextMenuShowEvent.set({value: "bad"}),
       /bad is not a valid value for contextMenuShowEvent/,
       "contextMenuShowEvent.set rejects with an invalid value.");
 
+    await browser.test.assertRejects(
+      browser.browserSettings.overrideDocumentColors.set({value: 2}),
+      /2 is not a valid value for overrideDocumentColors/,
+      "overrideDocumentColors.set rejects with an invalid 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.");
+
     browser.test.sendMessage("done");
   }
 
   let extension = ExtensionTestUtils.loadExtension({
     background,
     manifest: {
       permissions: ["browserSettings"],
     },