Kanika Bug 1465950 - Parsed policies stored by EnterprisePolicies.js to display on about:policies page r?felipe draft
authorKanika Saini
Wed, 20 Jun 2018 22:48:12 +0530
changeset 811208 59d7e841143bd21d4c0328d6d3e5a0e7171722c2
parent 810123 4f6e597104dabedfecfafa2ab63dc79fd7f8bc7a
push id114235
push userbmo:ksaini@mozilla.com
push dateWed, 27 Jun 2018 08:05:36 +0000
reviewersfelipe
bugs1465950
milestone62.0a1
Kanika Bug 1465950 - Parsed policies stored by EnterprisePolicies.js to display on about:policies page r?felipe MozReview-Commit-ID: Ller1U0PY3r
browser/components/enterprisepolicies/EnterprisePolicies.js
browser/components/enterprisepolicies/EnterprisePoliciesContent.js
browser/components/enterprisepolicies/tests/browser/browser.ini
browser/components/enterprisepolicies/tests/browser/browser_policies_getActivePolicies.js
toolkit/components/enterprisepolicies/nsIEnterprisePolicies.idl
--- a/browser/components/enterprisepolicies/EnterprisePolicies.js
+++ b/browser/components/enterprisepolicies/EnterprisePolicies.js
@@ -85,16 +85,17 @@ EnterprisePoliciesManager.prototype = {
     }
 
     if (provider.failed) {
       this.status = Ci.nsIEnterprisePolicies.FAILED;
       return;
     }
 
     this.status = Ci.nsIEnterprisePolicies.ACTIVE;
+    this._parsedPolicies = {};
     this._activatePolicies(provider.policies);
   },
 
   _chooseProvider() {
     if (AppConstants.platform == "win") {
       let gpoProvider = new GPOPoliciesProvider();
       if (gpoProvider.hasPolicies) {
         return gpoProvider;
@@ -129,16 +130,17 @@ EnterprisePoliciesManager.prototype = {
       let [parametersAreValid, parsedParameters] =
         JsonSchemaValidator.validateAndParseParameters(policyParameters, policySchema);
 
       if (!parametersAreValid) {
         log.error(`Invalid parameters specified for ${policyName}.`);
         continue;
       }
 
+      this._parsedPolicies[policyName] = parsedParameters;
       let policyImpl = Policies[policyName];
 
       for (let timing of Object.keys(this._callbacks)) {
         let policyCallback = policyImpl[timing];
         if (policyCallback) {
           this._schedulePolicyCallback(
             timing,
             policyCallback.bind(policyImpl,
@@ -294,16 +296,20 @@ EnterprisePoliciesManager.prototype = {
 
   get status() {
     return this._status;
   },
 
   isAllowed: function BG_sanitize(feature) {
     return !(feature in DisallowedFeatures);
   },
+
+  getActivePolicies() {
+    return this._parsedPolicies;
+  },
 };
 
 let DisallowedFeatures = {};
 
 /**
  * areEnterpriseOnlyPoliciesAllowed
  *
  * Checks whether the policies marked as enterprise_only in the
--- a/browser/components/enterprisepolicies/EnterprisePoliciesContent.js
+++ b/browser/components/enterprisepolicies/EnterprisePoliciesContent.js
@@ -73,13 +73,17 @@ EnterprisePoliciesManagerContent.prototy
   },
 
   get status() {
     return this._status;
   },
 
   isAllowed(feature) {
     return !this._disallowedFeatures.includes(feature);
+  },
+
+  getActivePolicies() {
+    throw Cr.NS_ERROR_NOT_AVAILABLE;
   }
 };
 
 var components = [EnterprisePoliciesManagerContent];
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
--- a/browser/components/enterprisepolicies/tests/browser/browser.ini
+++ b/browser/components/enterprisepolicies/tests/browser/browser.ini
@@ -7,16 +7,17 @@ support-files =
   opensearchEngine.xml
   policytest.xpi
   policy_websitefilter_block.html
   policy_websitefilter_exception.html
 
 [browser_policies_basic_tests.js]
 [browser_policies_broken_json.js]
 [browser_policies_enterprise_only.js]
+[browser_policies_getActivePolicies.js]
 [browser_policies_notice_in_aboutpreferences.js]
 [browser_policies_popups_cookies_addons_flash.js]
 [browser_policies_runOnce_helper.js]
 [browser_policies_setAndLockPref_API.js]
 [browser_policies_simple_pref_policies.js]
 [browser_policies_sorted_alphabetically.js]
 [browser_policy_app_update.js]
 [browser_policy_block_about_addons.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_getActivePolicies.js
@@ -0,0 +1,40 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+add_task(async function test_active_policies() {
+  await setupPolicyEngineWithJson({
+    "policies": {
+      "DisablePrivateBrowsing": true
+    }
+  });
+
+  let expected = {
+    "DisablePrivateBrowsing": true
+  };
+
+  Assert.deepEqual(await Services.policies.getActivePolicies(), expected, "Active policies parsed correctly");
+});
+
+add_task(async function test_wrong_policies() {
+  await setupPolicyEngineWithJson({
+    "policies": {
+      "BlockAboutSupport": [true]
+    }
+  });
+
+  let expected = {};
+
+  Assert.deepEqual(await Services.policies.getActivePolicies(), expected, "Wrong policies ignored");
+});
+
+add_task(async function test_content_process() {
+  await ContentTask.spawn(gBrowser.selectedBrowser, null, function() {
+    try {
+      Services.policies.getActivePolicies();
+    } catch (ex) {
+      is(ex.result, Cr.NS_ERROR_NOT_AVAILABLE, "Function getActivePolicies() doesn't have a valid definition in the content process");
+    }
+  });
+});
--- a/toolkit/components/enterprisepolicies/nsIEnterprisePolicies.idl
+++ b/toolkit/components/enterprisepolicies/nsIEnterprisePolicies.idl
@@ -10,9 +10,15 @@ interface nsIEnterprisePolicies : nsISup
   const short UNINITIALIZED = -1;
   const short INACTIVE      = 0;
   const short ACTIVE        = 1;
   const short FAILED        = 2;
 
   readonly attribute short status;
 
   bool isAllowed(in ACString feature);
+
+  /**
+   * Get the active policies that have been successfully configured
+   * @return an object that contains policy names and their coressponding parameters
+   */
+  jsval getActivePolicies();
 };